Redhat KVM Cheat sheet

Title:Redhat KVM Cheat sheet
Author: Douglas O'Leary <dkoleary@olearycomputers.com>
Description:Examples of standard commands used in KVM manipulation
Disclaimer:Standard: Use the information that follows at your own risk. If you screw up a system, don't blame it on me...

Table of Contents

General

Getting tired of having to check out google or man pages whenever I go back to the KVM and, while the GUI is actually usable, I have an issue with GUIs. Everything KVM related can be done through the command line.

Installation

Easiest way is to install the Virtualization groups via yum. I also tend to move the images directory so I'm not filling up /var. Short easy steps:

# yum grouplist | grep -i virt
  Virtualization
  Virtualization Client
  Virtualization Platform
  Virtualization Tools

# yum grouplist | grep -i virt | while read line
  do
    yum -y groupinstall "${line}"
  done
# lvcreate -L 200g -n ignite vg00
# mkfs.ext4 /dev/vg00/ignite
# mkdir -p -m 755 /ignite
# // Edit fstab
  /dev/mapper/vg00-ignite /ignite  ext4    defaults  1 2
# mount /ignite
# mkdir -p -m 755 /ignite/images
# chcon --reference /var/lib/libvirt/images /ignite/images
# rmdir /var/lib/libvirt/images
# ln -s /ignite/images /var/lib/libvirt/images

List, Start, and Stop guests

The main difference is that the --all command will display guests that are stopped whereas it won't if you leave it off.

The destroy argument is badly named. It doesn't eliminate the guest; it just stops it ... hard. There won't be any shutdown command run. It's akin to yanking the power out of a system.

shutdown, as you might expect, does a graceful OS shutdown.

Accessing the guest console

Install a new guest

virt-install --name vm1 --ram 2048 --vcpus=2 \
--disk path=/var/lib/libvirt/images/vm1.img,size=10 \
--noautoconsole --os-type=linux --os-variant=rhel6 \
--location ftp://192.168.122.1/pub/inst
virt-install --name vm1 --ram 2048 --vcpus=2 \
--disk path=/var/lib/libvirt/images/vm1.img,size=10 \
--noautoconsole --os-type=linux --os-variant=rhel6 \
--location ftp://192.168.122.1/pub/inst \
-x "ks=ftp://192.168.122.1/pub/kickstart/vm.cfg"

Starting install...
Retrieving file vmlinuz...             | 7.6 MB     00:00 ...
Retrieving file initrd.img...          |  60 MB     00:00 ...
Allocating 'vm1.img'                   |  10 GB     00:00

Virtual volume manipulation

# virsh vol-list default
Name                 Path
-----------------------------------------
bt5-gnome.img        /var/lib/libvirt/images/bt5-gnome.img
guest-1.img          /var/lib/libvirt/images/guest-1.img
guest-2.img          /var/lib/libvirt/images/guest-2.img
guest.img            /var/lib/libvirt/images/guest.img
guest1.img           /var/lib/libvirt/images/guest1.img
python.img           /var/lib/libvirt/images/python.img
testies.img          /var/lib/libvirt/images/testies.img
vm1.img              /var/lib/libvirt/images/vm1.img
# virsh domblklist vm1
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/vm1.img
# virsh vol-create-as default vm1-1.img 10g
Vol vm1-1.img created

# virsh vol-list default | head -2 ; virsh vol-list default | grep vm
Name                 Path
-----------------------------------------
vm1-1.img            /var/lib/libvirt/images/vm1-1.img
vm1.img              /var/lib/libvirt/images/vm1.img

# ll /var/lib/libvirt/images/vm*
-rw-------. 1 root root 10737418240 Jun 25 11:57 /var/lib/libvirt/images/vm1-1.img
-rw-------. 1 qemu qemu 10737418240 Jun 25 11:58 /var/lib/libvirt/images/vm1.img
# virsh attach-disk vm1 /var/lib/libvirt/images/vm1-1.img vdb --persistent
Disk attached successfully
# virsh detach-disk vm1 vdb --persistent
Disk detached successfully
# virsh vol-delete /var/lib/libvirt/images/vm1-1.img default
Vol /var/lib/libvirt/images/vm1-1.img deleted

Delete a guest

kill_vm()
{  [[ ${#1} -eq 0 ]] && return
   vm=$1
   pv=$(virsh domblklist ${vm} | grep /var | awk '{print $NF}')
   virsh destroy ${vm}
   virsh undefine ${vm}
   virsh vol-delete ${pv}
   [[ -f ${pv} ]] && rm ${pv}
}
kill_vm vm1