Search This Blog

Tuesday, May 19, 2009

KVM

Manage your virtual machines

From the shell

You can manage your VMs from the shell using virsh. You can get a list of the available commands if you type "help". Type "help command" to get additional infos for a particular command.

Define your new VM

Before you can manage your new VM with virsh, you must define it:

$ virsh --connect qemu:///system
Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands
'quit' to quit

virsh # define /etc/libvirt/qemu/newvm.xml
Domain newvm defined from /etc/libvirt/qemu/newvm.xml

Note that to list newvm, you must use 'list --inactive' or 'list --all', since list without any options will only list currently running machines.

List your VMs

Virsh allows you to list the virtual machines available on the current host:

yhamon@paris:/etc/libvirt/qemu$ virsh --connect qemu:///system
Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands
'quit' to quit

virsh # help list
NAME
list - list domains

SYNOPSIS
list [--inactive | --all]

DESCRIPTION
Returns list of domains.

OPTIONS
--inactive list inactive domains
--all list inactive & active domains

virsh # list
Id Name State
----------------------------------
15 mirror running
16 vm2 running

virsh # list --all
Id Name State
----------------------------------
15 mirror running
16 vm2 running
- test5 shut off

Define, undefine, start, shutdown, destroy VMs

The VMs you see with list --all are VMs that have been "defined" from an XML file. Every VM is configured via a XML file in /etc/libvirt/qemu. If you want to remove a VM from the list of VMs, you need to undefine it:

virsh # undefine test5   # WARNING: undefine will delete your XML file!
Domain test5 has been undefined

virsh # list --all
Id Name State
----------------------------------
15 mirror running
16 vm2 running

To be able to undefine a virtual machine, it needs to be shutdown first:

virsh # shutdown mirror
Domain mirror is being shutdown

This command asks for a nice shutdown (like running shutdown in command line) - but you can also use "destroy", the more brutal way of shutting down a VM, equivalent of taking the power cable off:

virsh # destroy mirror
Domain mirror destroyed

If you have made a change to the XML configuration file, you need to tell KVM to reload it before restarting the VM:

virsh # define /etc/libvirt/qemu/mirror.xml
Domain mirror defined from /etc/libvirt/qemu/mirror.xml

Then, to restart the VM:

virsh # start mirror
Domain mirror started

Suspend and resume a Virtual Machine

Virsh allows you to easily suspend and resume a virtual machine.

virsh # suspend mirror
Domain mirror suspended

virsh # resume mirror
Domain mirror resumed