Usage of KVM

This article will describe usage of KVM for experimental work of Linux.

1 Configuration on host machine

2 Configuration on virtual machine

3 Clone virtual machine

Cloning is good for trying Linux. You can run auto scripts with using virt-clone.

$ sudo virt-clone -o ubuntu-16.04 -n ubuntu-16.04-clone \
-f /var/lib/libvirt/images/ubuntu-16.04-clone.img

3.1 Example for clone management

I manage virtual machines with clone as below.

  • Prepare base virtual machine which has been installed OS and basic packages like openssh-server. This machine has 80GByte volumes which is formatted with LVM.
  • Clone new virtual machine from base virtual machine.
  • Register new virtual machine to bind server and dhcp server.
  • If I need more size like android build, I append new volumes to new virtual machine's LVM.
  • Do experimental work on new virtual machine.
  • Remove virtual machine after work.
  • Unregister new virtual machine from bind server and dhcp server.

Cloning does not copy all of 80GByte. It copies about 4GByte which is used by installed OS.

My machine which is used HDD have 3 minutes for clone. If using SSD and RAID0, it might take less than 1 minute.

I use snapshot only when updating base virtual machine.

4 Remove virtual machine

Remove virtual machine with virsh undefine and rm.

$ virsh undefine ubuntu-16.04-clone
$ rm ubuntu-16.04-clone.img # /var/lib/libvirt/images/ubuntu-16.04-clone.img

5 Connect to virtual machine console

If prepared serial console on virtual machine, you can operate virtual machine via console with virsh console.

$ virsh console <vmname>

The virsh can be run via SSH.

$ ssh -q -t <server> virsh console <vmanme>

6 Connect to virtual machine display

Use VGA because VNC server cannot use QXL as display.

0001_VGA.png

Use VNC server.

0002_VNC.png

You can find display number of VNC with virsh vncdisplay.

$ sudo virsh vncdisplay <vmname>
:1

$

Running below scripts on OSX will connect to VNC server. Please change SERVER to yours.

#!/bin/sh

SERVER=acer.hiroom2.com
VIRSH="ssh -q -t ${SERVER} sudo virsh"

display=`${VIRSH} vncdisplay ${1} | sed -e 's/://g' -e 's/[ \r\n]//g'`
port=`expr 5900 + ${display}`
open vnc://${SERVER}:${port}

When cloning virtual machine with virt-clone, VNC password is not taken over to new virtual machine. You need to append passwd='password' to virtual machines XML file. Please change 'password' to yours.

$ sudo virt-clone -o <src_vmname> -n <dst_vmname> -f <imgname>
$ sudo sed -i "s/keymap='ja'/keymap='ja' passwd='password'/g" \
/etc/libvirt/qemu/<dst_vmname>.xml
$ sudo systemctl restart libvirtd

7 Get list of virtual machine

virsh list provides list of virtual machine. –name option is useful for scripts.

$ sudo virsh list --name            # Print active domain
$ sudo virsh list --name --inactive # Print inactive domain
$ sudo virsh list --name --all      # Print all domain

For example, all virtual machines are suspended as below.

$ for vm in `sudo virsh list --name --all`; do
  sleep 30;                # wait VM
  sudo virsh suspend ${vm} # Suspend VM
done

8 Get virtual machine MAC address

Extract MAC address from virsh dumpxml.

#!/bin/sh

VIRSH="sudo virsh"

mac=`${VIRSH} dumpxml ${1} | grep "mac address"`
mac=`echo ${mac} | sed s/\ *\<mac\ address=\'//g | sed s:\'/\>::g`
echo "${mac}"

9 suspend/resume virtual machine when rebooing host machine

You might need to reboot host machine for security update or system backup. The libvirt-guests service provides suspend/resume virtual machine when rebooting host machine.

$ sudo systemctl enable libvirt-guests
$ sudo systemctl restart libvirt-guests

In case of CentOS 7 is as below.

$ diff -uprN /etc/sysconfig/libvirt-guests{.org,}
--- /etc/sysconfig/libvirt-guests.org   2016-07-12 21:39:20.801212101 +0900
+++ /etc/sysconfig/libvirt-guests       2016-07-12 21:39:45.574005615 +0900
@@ -9,6 +9,7 @@
 #           guests marked as autostart will still be automatically started by
 #           libvirtd
 #ON_BOOT=start
+ON_BOOT=start

 # Number of seconds to wait between each guest start. Set to 0 to allow
 # parallel startup.
@@ -23,6 +24,7 @@
 #             ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a
 #             value suitable for your guests.
 #ON_SHUTDOWN=suspend
+ON_SHUTDOWN=suspend

 # If set to non-zero, shutdown will suspend guests concurrently. Number of
 # guests on shutdown at any time will not exceed number set in this variable.

In case of Ubuntu 16.04 is as below.

$ diff -uprN /etc/default/libvirt-guests{.org,}
--- /etc/default/libvirt-guests.org     2016-07-12 13:23:54.793891291 +0900
+++ /etc/default/libvirt-guests 2016-07-12 13:27:00.748614276 +0900
@@ -9,6 +9,7 @@
 #           guests marked as autostart will still be automatically started by
 #           libvirtd
 #ON_BOOT=ignore
+ON_BOOT=start

 # Number of seconds to wait between each guest start. Set to 0 to allow
 # parallel startup.
@@ -23,6 +24,7 @@
 #             ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a
 #             value suitable for your guests.
 #ON_SHUTDOWN=shutdown
+ON_SHUTDOWN=suspend

 # If set to non-zero, shutdown will suspend guests concurrently. Number of
 # guests on shutdown at any time will not exceed number set in this variable.