Set Up Ubuntu Server to Host VMsDownload and install Ubuntu Server 18.04 LTS. After installation, update package indexes and packages: > sudo apt update
> sudo apt upgrade
and also set up auto updates. Install packages related to VM hosting and management: > sudo apt install qemu qemu-kvm libvirt-bin virtinst cpu-checker Download the script qemu, place it under /etc/libvirt/hooks, and set the permissions so it's executable. This script updates an iptables rule to allow port forwarding to services running on the VMs. 1. Create a New Virtual MachineDownload an ISO image, e.g. > wget http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso Create a config file like ubuntu-server-18.04.cfg which specifies the parameters of the VM. Run the script createvm.sh to create the VM. For example: > createvm.sh ubuntu-server-18.04.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 ubuntu-server-1804 --name ubuntu-server-1804-1 --file /mnt/data/vm/ubuntu-server-1804-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='no' 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='ubuntu-server-1804-2' ip='192.168.122.101' />" --live --config 3.3 Port ForwardingNow create a <name>.vm file, e.g. ubuntu-server-1804-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 ubuntu-server-1804-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 /etc/rc.local (create the file if it doesn't exist) 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.4 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:
|