reset password

Set Up Ubuntu Server to Host VMs

Download 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

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.

1. Create a New Virtual Machine

Download 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. TightVNCfrom 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 One

Once 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 Machine

3.1 VNC Configuration

After 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

  • autoport is no
  • port is a number other than -1. We want to set the VNC port to a fixed number so the students know which port to use to connect to their VM. The convention for VNC ports are 5900+n where 5900 is the default VNC port.
  • Add a passwd attribute.

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 Configuration

When 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:

    <dhcp>
      <range start='192.168.122.2' end='192.168.122.99'/>
      <host mac='52:54:00:88:3a:28' name='ubuntu-server-1804-1' ip='192.168.122.100'/>
    </dhcp>

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

Now create a <name>.vm file, e.g. ubuntu-server-1804-1.vm, that contains the following properties:

  • name: the name of the VM
  • vnc: the VNC port used by the VM
  • mac: the MAC address of the VM
  • ip: the IP address of the VM
  • port_mappings: because our VMs only have internal IP addresses, to access any service running on a VM, we'll need to map the service port on the VM to a port on the host so that any traffic to the host port will be forwarded to the VM port. The format of port mapping is "<vm port1>;<host port1> <vm port2>;<host port2> ...".

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 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 VM

Set the VM to be auto-start after a system reboot:

> virsh autostart <name>

And finally, start the VM:

> virsh start <name>

4. Mange Virtual Machines

VMs can be managed using virsh. Here are some common virsh commands:

virsh list --all List all VMs
virsh list List the running VMs
virsh start|shutdown|suspend|reboot <name> Start, stop, suspend, or reboot a VM
virsh autostart <name> Automatically starts a VM at boot
virsh dumpxml <name> Display the information of a VM in XML format
virsh undefine <name> Remove a VM. Note that the VM should be stopped first.
virsh help List all virsh commands
virsh help <command> Display the information of a virsh command

 

 

This page has been viewed 14824 times.