Set Up Ubuntu Server to Host VMsDownload and install Ubuntu Server 16.04.4 LTS. During installation, select the following options:
After installation, use apt-get to install the following packages:
Download the script qemu and place it under /etc/libvirt/hooks. This script updates an iptables rule to allow port forwarding to services running on the VMs. Edit /etc/apt/apt.conf.d/50unattended-upgrades to enable the following:
and restart the unattended-upgrades service: > sudo service unattended-upgrades restart 1. Create a New Virtual MachineDownload an ISO image, e.g. > wget http://releases.ubuntu.com/16.04.4/ubuntu-16.04.4-server-amd64.iso Create a config file like sd2018-ubuntu-server.cfg which specifies the parameters of the VM. Run the script createvm.sh to create the VM. For example: > createvm.sh sd2018-ubuntu-server.cfg The VM tool will create a VNC session to provide a GUI so we can complete the installation of the guest OS. First, open another terminal and run the following command to find out the port of the VNC session: > virsh dumpxml <name> | grep vnc where <name> is the name of the VM specified in the config file. And then, connect to the VNC session using a VNC viewer (e.g. TightVNC) from a different computer -- we need a GUI to run a VNC viewer, and the Ubuntu Server we use to host VMs does not have one. After the OS installation is completed, the VM will be restarted, and you can connect to it at the same port using VNC. 2. Create a Virtual Machine from an Existing OneOnce a VM is created, it's easy to clone more VMs with the same configuration using virt-clone. For example: > virt-clone --original sd2018-ubuntu-server --name sd2018-ubuntu-server-1 --file /mnt/disk2/vm/sd2018-ubuntu-server-1.qcow2 The first argument is the name of the original VM -- note that this VM must be stopped before it can be cloned. The second argument is the name of new VM, and the third argument is the VM image file to be created (qcow2 is the VM image file format). 3. Configure a Virtual Machine3.1 VNC ConfigurationAfter a VM is created, use the following command to edit the VM settings: > virsh edit <name> Locate the line that looks like "<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>", and change it so that
For example: <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0' passwd="abcd"> And while you are editing the VM settings, note down the MAC address of the VM as we'll use it in the next step. 3.2 Network ConfigurationWhen you configure your first VM: > virsh net-edit default In the <dhcp> section, limit the dynamic range, and add a <host> entry for the VM so it has a fixed IP, which is important for port forwarding. For example:
Restart the host for the changes to take effect. For subsequent VMs, use net-update instead as it does not require reboot and the changes take effect immediately. For example: > virsh net-update default add ip-dhcp-host "<host mac='52:54:00:00:00:01' name='sd2018-ubuntu-server-2' ip='192.168.122.101' />" --live --config Now create a <name>.vm file, e.g. sd2018-ubuntu-server-1.vm, that contains the following properties:
After the vm file is created, run the script iptables.sh to set up port forwarding, e.g. > iptables.sh sd2018-ubuntu-server-1.vm Note that iptables.sh can be run on one vm file, or on a directory that contains a number of vm files, which is useful for recreating the iptables rules after a system restart -- in fact, you should put this script in rc.local so it's run automatically after a system restart. Also note that eno1 in the script is the LAN interface, and on different computers the name of the interface may be different (e.g. eth0 instead of eno1). 3.3 Start VMSet the VM to be auto-start after a system reboot: > virsh autostart <name> And finally, start the VM: > virsh start <name> 4. Mange Virtual MachinesVMs can be managed using virsh. Here are some common virsh commands:
|