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
Now add the 'nsrc' user into the 'libvirt' group.
# usermod -G libvirt -a nsrc
This is needed so you can talk to the libvirt daemon. If you are currently logged in as 'nsrc' you will need to logout and login again to pick up this group. The command "id" will show you what groups you are a member of.
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 xorg lxde lightdm ssh-askpass chromium --no-install-recommends
This will take a while - be patient!
Once the install is finished, start the graphical environment:
# service lightdm start
You should now be able to login to the graphical environment as the "nsrc" user. You can get a shell window using Start > System Tools > LXTerminal.
Do so now.
You can also switch between text and graphical consoles using Ctrl-Alt-F1 and Ctrl-Alt-F7 respectively.
# apt-get install virt-manager
This installs a lot of extra packages because it depends on a lot of graphics libraries.
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.
First, copy the file ubuntu-14.04.5-server-i386.iso into the directory /var/lib/libvirt/images on your host.
How to do this depends on where you are copying the image from.
/media/XXX), and open a window.(XXX will be a random number, look at the title of the window)
You can then copy the ISO:
~~~
# cp /media/XXX/iso/ubuntu-14.04.5-server-i386.iso /var/lib/libvirt/images/
~~~
If it doesn't work, you may need to mount it by hand:
~~~
# dmesg | tail # find the ID of the device just inserted, e.g. sdb
# mount /dev/sdb1 /mnt
# ls /mnt # find the file
# ls /mnt/iso # find the file
# cp /mnt/iso/ubuntu-14.04.5-server-i386.iso /var/lib/libvirt/images/
# umount /mnt
~~~
If you are copying from a web server:
# cd /var/lib/libvirt/images
# wget http://www.ws.nsrc.org/downloads/ubuntu-14.04.5-server-i386.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-14.04.5-server-i386.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 VolumeUbuntu 13.10 (Saucy Salamander) (or later)At this point the VM will start and the console should appear. Continue to install your Ubuntu virtual machine however you like. Follow the instructions from the VirtualBox exercise if you wish. We recommend using "Guided - use entire disk" for partitioning, and please enter http://apt.ws.nsrc.org:3142/ for the HTTP proxy. Install the "OpenSSH Server" package.
When it 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 10.10.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, 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.
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). Under Debian you need to create a config file to make this work.
When logged in as user "nsrc", do the following to create a config file .config/libvirt/libvirt.conf in your home directory, containing one line:
$ cd
$ mkdir -p .config/libvirt # it may already exist
$ editor .config/libvirt/libvirt.conf
uri_default = "qemu:///system"
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 acpid is not running inside the guest)
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.
On Ubuntu, The kvm-ok utility, included in the package cpu-checker, will tell you.↩