Once you’ve got your virtual machine installed, you’ll need to know the various commands for everyday administration of KVM virtual machines. In these examples, change the name of the VM from ‘vm’ to whatever yours is called.
To show general info about virtual machines, including names and current state:
#virsh list –all
To see a top-style monitor window for all VMs:
#virt-top
To show info about a specific virtual machine:
#virsh dominfo vm
To start a virtual machine:
#virsh start vm
To pause a virtual machine:
#virsh suspend vm
To resume a virtual machine:
#virsh resume vm
To shut down a virtual machine (the ‘acpid’ service must be running on the guest for this to work):
#virsh shutdown vm
To force a hard shutdown of a virtual machine:
#virsh destroy vm
To remove a domain (don’t do this unless you’re sure you really don’t want this virtual machine any more):
#virsh undefine vm
Cloning virtual machines
To clone a guest VM, firstly it’s necessary to create new disk volumes for the clone, then we use the virt-clone command to clone the existing VM:
#lvcreate -L 10G -n vm1 vg_www
#virt-clone -o vm -n vm1 -f /dev/mapper/vg_www-vm1
Then dump the XML for the new VM:
#virsh dumpxml vm1 > /tmp/vm1.xml
Edit /tmp/vm1.xml. Look for the ‘vcpu’ line and change the ‘cpuset’ number to the CPU core you want to dedicate to this VM. Then make this change effective:
#virsh define /tmp/vm1.xml
You’ll also need to grab the MAC address from the XML. Keep this available as we’ll need it in a minute:
#grep “mac address” /tmp/vm1.xml | awk -F: ‘{print $2}’
Start up the new VM and connect to it via VNC as per the instructions in the Installation section above. Edit /etc/sysconfig/network and change the hostname to whatever you want to use for this new machine. Then edit /etc/sysconfig/network-scripts/ifcfg-eth0 and change the ‘HOSTNAME’ and ‘IPADDR’ to the settings you want for this new machine. Change the ‘HWADDR’ to the MAC address you obtained a moment ago, making sure that the letters are capitalised.
Then reboot and the new VM should be ready.
Backing up and migrating virtual machines
In order to take backups and to be able to move disk volumes from virtual machines to other hosts, we basically need to create disk image files from the LVM volumes. We’ll first snapshot the LVM volume and take the disk image from the snapshot, as this significantly reduces the amount of time that the VM needs to remain paused (i.e. effectively offline) for. We remove the snapshot at the end of the process so that the VM’s IO is not negatively affected.
This disk image, once created, can then be stored in a separate location as a backup, and/or transferred to another host server in order to copy or move the VM there.
So, make sure that the VM is paused or shut down, then create a LVM snapshot, then resume the VM, then create the image from the snapshot, then remove the snapshot:
#virsh suspend vm
#lvcreate -L 100M -n vm-snapshot -s /dev/vg_www/vm
#virsh resume vm
#dd if=/dev/mapper/vg_www-vm–snapshot of=/tmp/vm.img bs=1M
#lvremove /dev/mapper/vg_www-vm–snapshot
You can then do what you like with /tmp/vm.img – store it as a backup, move it to another server, and so forth.
In order to restore from it or create a VM from it on a new server, firstly use ‘lvcreate’ to create the LVM volume for restore if it isn’t already there, then copy the disk image to the LVM volume:
#dd if=/tmp/vm.img of=/dev/mapper/vg_www-vm bs=1M
You may also need to perform this procedure for the swap partition depending on what you are trying to achieve.
You’ll also want to back up the current domain configuration for the virtual machine:
#virsh dumpxml vm > /tmp/vm.xml
Then just store the XML file alongside the disk image(s) you’ve taken.
If you’re moving the virtual machine to a new server then once you’ve got the root and swap LVM volumes in place, you’ll need to create the domain for the virtual machine on the new server. Firstly edit the XML file and change the locations of disk volumes to the layout on the new server if it’s different to the old server, then define the new domain:
#virsh define /tmp/vm.xml
You should then be able to start up the ‘vm’ virtual machine on the new server.
Resizing partitions on a guest
Let’s say we want to expand the root partition on our VM from 10G to 15G. Firstly make sure the VM is shut down, then use virt-filesystems to get the information we need for the resize procedure:
#virsh shutdown vm
#virt-filesystems -lh -a /dev/mapper/vg_www-vm
This will probably tell you that the available filesystem on that volume is/dev/sda1, which is how these tools see the virtual machine’s /dev/vda1 partition. We’ll proceed on the basis that this is the case, but if the filesystem device name is different then alter the command below accordingly.
Next we create a new volume, then we perform the virt-resize command from the old volume to the new volume, then we set the new volume as the partition for our domain:
#lvcreate -L 15G -n vmnew vg_www
#virt-resize –expand /dev/sda1 /dev/mapper/vg_www-vm /dev/mapper/vg_www-vmnew
#lvrename /dev/vg_www/vm /dev/vg_www/vm-old
#lvrename /dev/vg_www/vmnew /dev/vg_www/vm
#virsh start vm
Then, when you’re sure the guest is running OK with the new resized partition, remove the old root partition volume:
#lvremove /dev/mapper/vg_www-vm-old














