Manage KVM Using Shell

If you have kvm configured & willing to manage it using commands rather using virt-manager. Following are few useful commands.

Here linux is used to present guest / domain
1326210859 is snapshot name

To List All Guests

virsh list --all

To Start/stop suspend/resume reboot guests

virsh start linux
virsh shutdown linux
 
virsh suspend linux
virsh resume linux
 
virsh reboot linux

To immediately stop running guest

virsh destroy linux

To Autostart Guest on HOST boot / reboot

virsh autostart linux

To take snapshot

virsh snapshot-create linux

To list snapshot

virsh snapshot-list linux
 Name                 Creation Time             State
------------------------------------------------------------
 1326210859           2012-01-10 21:24:19 +0530 running

To revert snapshot

virsh snapshot-revert linux 1326210859

To delete snapshot

virsh snapshot-delete linux 1326210859

To save state & revert guest to / from file

virsh save linux linux.dump
virsh restore linux.dump

To migrate kvm guest to another host
Requirement

guest must be running
guest image must be on shared storage, nfs / san / nas etc

virsh migrate --live linux qemu+ssh://DST_HOST/system

For migration, all you need is to setup two or more hosts.
Configure nfs/san/nas
Create guest & store image on nfs
On other hosts all you need is to configure nfs / nas / san storage.
Use above command to migrate running guest to another server

To resize guest disk image

qemu-img resize linux.img +10G

To convert image from raw image to qcow2

#to preallocate storage space
qemu-img convert -f raw -O qcow2 -o preallocation=metadata linux.img linux.qcow2 -p
 
#without preallocation
qemu-img convert -f raw -O qcow2 linux.img linux.qcow2 -p

To clone guest, here we are cloning linux to linux-clone

virt-clone --original linux --name linux-clone --file /var/lib/libvirt/images/nfs_storage/linux.qcow2
Allocating 'linux.qcow2'         1% [-       ]  29 MB/s | 163 MB     04:40 ETA

To check resource utilization

virt-top
virt-top 16:00:17 - x86_64 2/2CPU 2936MHz 3799MB
4 domains, 2 active, 2 running, 0 sleeping, 0 paused, 2 inactive D:0 O:0 X:0
CPU: 40.3%  Mem: 2048 MB (2048 MB by guests)
 
   ID S RDRQ WRRQ RXBY TXBY %CPU %MEM    TIME   NAME
    6 R  500   12 3590    0 40.3 26.0   0:19.61 linux-clone
    2 R    0    0 3590    0  0.0 26.0   0:25.57 linux
    -                                           (linuxonnfs)
    -                                           (xp32)

To connect guest console

virt-viewer linux

To backup guest configuration file

virsh dumpxml linux > linux.xml

Default Path For Guest config

/etc/libvirt/qemu/

Default Path For Guest Images

/var/lib/libvirt/images/


if you find any missing point in here, please let us know in comment section or tweet us at @linuxreaders. To get more articles like this, subscribe to our RSS feeds / Mails.

Article by Dhaval Thakar

Hi, I am Linux Admin. You'll find my articles small & without much descriptions.
Read 218 articles by

Archives

Categories

Page optimized by WP Minify WordPress Plugin