Examine an existing VM
Before looking into how to create and startup a guest VM – let’s examine one that’s already up and running to see where we are trying to get to.
From the command line, issue “virsh list –all” to see what VM’s are installed.:
[root@arches current]# virsh list --all Id Name State ---------------------------------------------------- - vm1-ubuntu shut off
Which file is being used as the virtual disk for the VM? The configuration file that describes how the Virtual machine is put together is contained in an XML file. One part of that XML file describes which linux file is pretending to be the hard-disk of our VM.
[gary@arches current]$ sudo virsh dumpxml vm1-ubuntu | grep 'source file' <source file='/var/lib/libvirt/images/vDisk0.img'/>
If this is just a regular file, what does linux knoq about this file? It recognizes it as a disk image owned by root…
[gary@arches current]$ sudo file /var/lib/libvirt/images/vDisk0.img /var/lib/libvirt/images/vDisk0.img: x86 boot sector; partition 1: ID=0x83, active, starthead 32, startsector 2048, 497664 sectors; partition 2: ID=0x5, starthead 59, startsector 501758, 20467714 sectors, code offset 0x63 [gary@arches current]$ sudo ls -l /var/lib/libvirt/images/vDisk0.img -rw-------. 1 root root 10737418240 Dec 13 11:47 /var/lib/libvirt/images/vDisk0.img [gary@arches current]$
Now we have seen how the VM is described (in XML) and seen that the VM “Hard Disk” is jsut a file – let’s start the VM.
[gary@arches current]$ sudo virsh start vm1-ubuntu Domain vm1-ubuntu started [gary@arches current]$ sudo virsh list --all Id Name State ---------------------------------------------------- 2 vm1-ubuntu running
We will see that our VM is just a process in Linux that happens to be owned by the user qemu.
[gary@arches Downloads]$ ps -ef | grep ubuntu qemu 18619 1 1 21:08 ? 00:00:22 /usr/libexec/qemu-kvm -name vm1-ubuntu -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off
Now we can connect to console:
[gary@arches current]$ sudo virsh console vm1-ubuntu Connected to domain vm1-ubuntu Escape character is ^] Ubuntu 14.04.1 LTS ubuntu-vm1 ttyS0 ubuntu-vm1 login: gary
What does the filesystem look like within the guest?
gary@ubuntu-vm1:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/ubuntu--vm1--vg-root 7.6G 1.3G 5.9G 18% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 991M 4.0K 991M 1% /dev tmpfs 201M 980K 200M 1% /run none 5.0M 0 5.0M 0% /run/lock none 1002M 0 1002M 0% /run/shm none 100M 0 100M 0% /run/user /dev/sda1 236M 37M 187M 17% /boot
Notice that the underlying image file (from ls output is 10G, but the root fs is only 7.6G)
fdisk shows us what’s going on.
gary@ubuntu-vm1:~$ sudo fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000a03fd Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 20969471 10233857 5 Extended /dev/sda5 501760 20969471 10233856 8e Linux LVM Disk /dev/mapper/ubuntu--vm1--vg-root: 8329 MB, 8329887744 bytes 255 heads, 63 sectors/track, 1012 cylinders, total 16269312 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/ubuntu--vm1--vg-root doesn't contain a valid partition table Disk /dev/mapper/ubuntu--vm1--vg-swap_1: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/ubuntu--vm1--vg-swap_1 doesn't contain a valid partition table
One last thing
The disk image file reports 10G from the “ls” output, but is it really 10G in size? Let’s use “du” to tell us if this is the case.
[root@arches current]# du -h /var/lib/libvirt/images/*
1.9G /var/lib/libvirt/images/vDisk0.img
No! It’s only 2G – how can this be? It must be a sparse file that has only 2G of actual data. That figure lines up with the “usage” column of “df”. In unix land we call these “sparse files” in Virtualization and storage realm this is called “thin provisioning”.
Reproducing this image
OK, so we would like to reproduce that same image from scratch. An Ubuntu guest VM with 10G of disk space using “thin provisioning”.
1) Start by creating a sparse file (as root)
[root@arches current]# truncate –size=10G /var/lib/libvirt/images/ubuntu2.img
2) Start the VM and attach the installation CDROM to the VM.
[root@arches current]# virt-install -r 1024 –accelerate -n vm2-ubuntu -f /var/lib/libvirt/images/ubuntu2.img –cdrom /var/lib/libvirt/images/ubuntu-14.04.1-server-amd64.iso
3) Install the guest OS as usual.
From a GNOME desktop, the virt-install command will throw up a window representing the console in the “Virt Viewer” application. Use this console to go through the installation process of ubuntu.
Finally
4) See that the new VM is running.
[root@arches current]# virsh list –all
Id Name State
—————————————————-
2 vm1-ubuntu running
4 vm2-ubuntu running
5) Connect to the VGA console
[gary@arches current]$ sudo virt-viewer vm2-ubuntu
6) Connect to the serial console
You will need to create/edit /etc/init/ttyS0.conf (note the capitalization of the “S”) and reboot the guest in order to get the console to work correctly with “virsh console ….” otherwise virt-viewer works just fine.
( The /etc/init/ttys0.conf file does not exist by default, so copy it from /etc/init/tty1.conf and made changes to the last line to reflect the working copy as below)
gary@ubuntu-vm1:~$ sudo cat /etc/init/ttyS0.conf # tty1 - getty # # This service maintains a getty on tty1 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[2345] and ( not-container or container CONTAINER=lxc or container CONTAINER=lxc-libvirt) stop on runlevel [!2345] respawn exec /sbin/getty -8 115200 ttyS0 xterm
[gary@arches Downloads]$ sudo virsh console vm2-ubuntu Connected to domain vm2-ubuntu Escape character is ^] Ubuntu 14.04.1 LTS ubuntu-vm2 ttyS0 ubuntu-vm2 login: