reset password

Set Up Ubuntu Server to Host VMs

Download and install Ubuntu Server 16.04.4 LTS. During installation, select the following options:

  • Mail Server
  • Standard System Utilities
  • Virtual Machine Host
  • OpenSSH Server

After installation, use apt-get to install the following packages:

  • virtinst
  • libosinfo-bin

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:

  • Auto apply non-security updates
  • Email notification after upgrade
  • Auto remove unused dependencies
  • Auto reboot if a reboot is required after upgrade (you can set an auto-reboot time as well)

and restart the unattended-upgrades service:

> sudo service unattended-upgrades restart

1. Create a New Virtual Machine

Download 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. 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 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 Machine

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='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:

> 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='sd2018-ubuntu-server-1' ip='192.168.122.100'/>
    </dhcp>

Now create a <name>.vm file, e.g. sd2018-ubuntu-server-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 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).

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 help List all virsh commands
virsh help <command> Display the information of a virsh command

 

 

This page has been viewed 14820 times.