Working in pairs, you will build a Linux-based virtualization host server on hardware provided in the class.
virt-manager GUI and the virsh command-line interface to install and manage a VM.NOTE: get into the habit of logging into the server as a normal user, and try to avoid logging in as “root”. You will see a prompt which ends with $. It’s advisable to disable logging in as root directly over SSH (not covered here).
If there is a single command you wish to run as root, prefix it with sudo. If you have a series of commands to run as root, start a root shell with sudo -s, and the prompt will change to #. When you have finished, type exit to leave the root shell.
libvirt# apt-get install qemu-kvm libvirt-clients libvirt-daemon-system
NOTE: To make sure that your user nsrc has the rights to call the virsh tool, it has automatically been added to the libvirt UNIX group.
Note: If you are logged into the graphical environment, log out completely, and log in again.
At a command line prompt, type the following:
# virt-host-validate
The output should look like:
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking for device /dev/kvm : PASS
QEMU: Checking for device /dev/vhost-net : PASS
QEMU: Checking for device /dev/net/tun : PASS
LXC: Checking for Linux >= 2.6.26 : PASS
Another way to check:
To verify, do:
$ ls -l /dev/kvm
If you see:
crw-rw---- 1 root kvm 10, 232 Jan 17 21:24 /dev/kvm
Then it’s OK 1
If KVM acceleration is not available, then you may need to reboot into the BIOS settings and enable VT-x (Intel) or AMD-V (AMD).
Install an X11 desktop (which we want just so that we can run virt-manager)
# apt-get install tasksel
# tasksel install lubuntu-core
# apt-get install ssh-askpass chromium-browser --no-install-recommends
This will take a while - be patient!
Once the install is finished, start the graphical environment:
# service lightdm start
As we are doing this via an SSH session and not on the machine’s console you will not see any graphical environment yet. In a bit we will install another tool that will let you connect to your host and view it using a graphical environment.
Be sure you are the root user:
$ sudo -s
Then do:
# apt-get install virt-manager
This installs a of extra packages because it depends on many graphics libraries.
Unfortunately, virt-manager can only run under Linux. However it is possible to create and access a remote Linux desktop so you can use virt-manager remotely.
Firstly, install the required package:
# apt-get install vnc4server
(If you have not installed a desktop environment like LXDE, then you should also install a simple window manager like “openbox” or “fluxbox”)
Now as the “nsrc” user - not as root! - type “vncserver” to start a server. Please use the class password when you are prompted to enter a password below.
nsrc@hostX:~$ vncserver
You will require a password to access your desktops.
Password: <choose a password>
Verify: <enter the same password>
New 'hostX.ws.nsrc.org:1 (nsrc)' desktop is hostX.ws.nsrc.org:1
Creating default startup script /home/nsrc/.vnc/xstartup
Starting applications specified in /home/nsrc/.vnc/xstartup
Log file is /home/nsrc/.vnc/hostX.ws.nsrc.org:1.log
If you want the VNC desktop to have the full LXDE desktop environment, you need to edit a config file on your hostX.ws.nsrc.org machine, comment out a couple of lines and add a new line.
$ cd
$ editor .vnc/xstartup
...
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
x-session-manager &
Now restart your VNC server:
$ vncserver -kill :1
$ vncserver
Note the VNC screen number, in the above example it is :1. (The actual TCP port number is this value plus 5900, or 5901 in this case).
Now connect using a VNC client on your laptop to <yourhost>:<screen>, which would be hostX:1 in the above example. You should be prompted for the password you used when you first typed vncserver. Hopefully you used the class password :-)
You can find VNC clients to install by going to:
http://www.ws.nsrc.org/downloads/
For Windows you can download the file VNC-Viewer-6.21.920-Windows.exe and install this.
If you are given a warning about the connection not being encrypted you can click “Continue” and/or the checkbox to not be warned for future connections.
If you just get a blank screen, then you are probably in the ‘openbox’ window manager. Right-click, select Terminal from the menu, and in the terminal type ‘virt-manager’.
You should see a menu at the bottom of your screen. You can click on the lower left of the screen and select to open an LXTerminal and the Virtual Machine Manager if you wish. Here is a sample image below:
If you see a pop-up window prompting you to upgrade the operating system be sure to click the “Don’t Upgrade” option.
You can also switch between text and graphical consoles using Ctrl-Alt-F1 and Ctrl-Alt-F7 respectively.
Note that you can disconnect and reconnect from the desktop with VNC, and it will be just as you left it.
Note 1: If you reboot your VM server, then you will have to login over ssh and type “vncserver” again to restart the VNC server. However it will remember the password you used before.
Note 2: This is not the way we recommend to manage Linux systems, for many reasons. However it does give you a usable way to run virt-manager over the network.
If you had a cluster of machines, you can run virt-manager on one machine, and then use File > Add Connection to get it to communicate with libvirt on other machines. Then only the manager machine needs to have a graphical desktop. 2
On the virtualization host you have just built, you are going to install Ubuntu Server in a VM, using the virt-manager GUI which is similar to the VirtualBox GUI.
From our in-class webserver:
# cd /var/lib/libvirt/images
# wget http://www.ws.nsrc.org/downloads/ubuntu-18.04.3-server-amd64.isoUse Start > System Tools > Virtual Machine Manager, or just type virt-manager at a terminal prompt while in the GUI.
NOTE: to release the keyboard and mouse, press left-CTRL and left-ALT together.
In the Virtual Machine Manager window:
ubuntu-18.04.3-server-amd64.iso (if it’s not there, click “Browse Local”, choose File System on the left, and browse to where you copied/downloaded the Ubuntu ISO image), then click on Choose VolumeAt this point the VM will start and the console should appear. Continue to install your Ubuntu virtual machine however you like. When asked for a server name we suggest using “ubuntu1” as this is what we will use to refer to this VM going forward.
Follow the instructions from the “Ubuntu initial installation” exercise if you wish. We recommend using “Guided - use entire disk” for partitioning instead of following the LVM details, and please enter http://apt.ws.nsrc.org:3142/ for the HTTP proxy. Install the “OpenSSH Server” package.
The second-to-last screen will prompt you about whether you wish to “Install the GRUB boot loader to the master boot record?” At this screen respond “Yes”.
When the installer has finished and rebooted, your VM should come back up. Login, type “ifconfig” to find what IP address it has come up on. This should be an address on 100.64.0.X which has been picked dynamically via DHCP.
SSH into this address from your laptop (e.g. using Putty) and login. If you didn’t select OpenSSH Server during the installation, then you can add it using apt-get install openssh-server
Congratulations: you have a created a working VM, it is on the network, and you have remote access to it!
Back on your host, you should be able to find the running kvm (or qemu-system) process like this:
$ ps auxwww | egrep '(kvm|qemu-system)'
Note the very large number of command line parameters given to kvm. Now you can see why we need libvirt to manage this for us :-)
The disk image file is in the directory /var/lib/libvirt/images and you can find it like this:
# cd /var/lib/libvirt/images
# ls -slh
It’s probably called ubuntu1.qcow2, or whatever name you gave it when you created it.
Note that the total size of the file is 4.0GB, but the disk space used (the left-hand column on the line) is less than this. This is because the default format used by virt-manager, QCOW2, only allocates space as required [^2].
[^2] This is not to be confused with a sparse file. Newer versions of the QCOW2 format allow preallocation, where all free space is written out to the file.
To view how much space is really used, you can use the du (disk use) command, like so:
# du -m *
-m = megabytes
When you are logged into the VM host platform remotely (we mean hostX.ws.nsrc.org, not the VM you just created), e.g. over ssh, it’s often far easier and quicker to interact using the command line rather than trying to pull back a graphical desktop and GUI.
When logged in as user “nsrc”, do the following to create a config file .config/libvirt/libvirt.conf in your home directory.
$ cd
$ newgrp libvirt
$ mkdir -p .config/libvirt # it may already exist
$ editor .config/libvirt/libvirt.conf
In the file, add the following line:
uri_default = "qemu:///system"
Save and exit from the file.
You should be able to run virsh as a non-root user (remember, it’s always a good idea to run as few commands as root as possible).
On the host server (either a terminal window or logged in using ssh), try the following commands:
$ virsh list
$ virsh list --all
The first shows only running VMs, the second shows all defined VMs (including halted ones).
You can send a shutdown signal like this:
$ virsh shutdown ubuntu1
After a few seconds, try virsh list again to see if it has shut down. (If it hasn’t - this means the guest isn’t processing ACPI signals).
To restart the VM:
$ virsh start ubuntu1
You can find all the parameters of the VM, such as how much memory and CPU cores it has, and what disk images are attached, by looking at the XML:
$ virsh dumpxml ubuntu1 | less
See if you can find the path to the disk image file in the XML.
(Hit space to advance to next page, ‘b’ to go back, and ‘q’ to quit)
The XML files are actually stored under /etc/libvirt/qemu/, but it is safer to manipulate them using the virsh commands, which test the XML file for errors before saving it.