24/7/365 Support

Managing virtual machines with virsh in Ubuntu

In the previous recipe, we saw how to start and manage virtual machines with KVM. This recipe covers the use of Virsh and virt-install to create and manage virtual machines. The libvirt Linux library exposes various APIs to manage hypervisors and virtual machines. Virsh is a command-line tool that provides an interface to libvirt APIs.

To create a new machine, Virsh needs the machine definition in XML format. virt-install is a Python script to easily create a new virtual machine without manipulating bits of XML. It provides an easy-to-use interface to define a machine, create an XML definition for it and then load it in Virsh to start it.

In this recipe, we will create a new virtual machine with virt-install and see how it can be managed with various Virsh commands.

Getting ready

You will need access to the root account or an account with sudo privileges.

Install the required packages, as follows:

$ sudo apt-get update

$ sudo apt-get install -y qemu-kvm libvirt-bin virtinst

Install packages to create the cloud init disk:

$ sudo apt-get install genisoimage

Add your user to the libvirtd group and update group membership for the current session:

$ sudo adduser ubuntu libvirtd

$ newgrp libvirtd

How to do it…

We need to create a new virtual machine. This can be done either with an XML definition of the machine or with a tool called virt-install. We will again use the prebuilt Ubuntu Cloud images and initialize them with a secondary disk:

First, download the Ubuntu Cloud image and prepare it for use:

$ mkdir ubuntuvm && cd ubuntuvm

$ wget -O trusty.img.dist \

http://cloud-images.ubuntu.com/releases/trusty/release/ubuntu- 14.04-server-cloudimg-amd64-disk1.img

$ qemu-img convert -O qcow2 trusty.img.dist trusty.img.orig

$ qemu-img create -f qcow2 -b trusty.img.orig trusty.img

Create the initialization disk to initialize your cloud image:

$ sudo vi user-data

#cloud-config

password: password

chpasswd: { expire: False }

ssh_pwauth: True

$ sudo vi meta-data

instance-id: ubuntu01;

local-hostname: ubuntu

$ genisoimage -output cidata.iso -volid cidata -joliet \

-rock user-data meta-data

Now that we have all the necessary data, let's create a new machine, as follows:

$ virt-install --import --name ubuntu01 \

--ram 256 --vcpus 1 --disk trusty.img \

--disk cidata.iso,device=cdrom \

--network bridge=virbr0 \

--graphics vnc,listen=0.0.0.0 --noautoconsole -v

This should create a virtual machine and start it. A display should be opened on the local VNC port 5900. You can access the VNC through other systems available on the local network with a GUI.

You can set up local port forwarding and access VNC from your local system as follows:

$ ssh kvm_hostname_or_ip -L 5900:127.0.0.1:5900

$ vncviewer localhost:5900

Once the cloud-init process completes, you can log in with the default user, ubuntu, and the password set in user-data.

Now that the machine is created and running, we can use the virsh command to manage this machine. You may need to connect virsh and qemu before using them:

$ virsh connect qemu:///system

Get a list of running machines with virsh list. The --all parameter will show all available machines, whether they are running or stopped:

$ virsh list --all # or virsh --connect qemu:///system list

You can open a console to a running machine with virsh as follows. This should give you a login prompt inside the virtual machine:

$ virsh console ubuntu01

To close the console, use the Ctrl + ] key combination.

Once you are done with the machine, you can shut it down with virsh shutdown. This will call a shutdown process inside the virtual machine:

$ virsh shutdown ubuntu01

You can also stop the machine without a proper shutdown, as follows:

$ virsh destroy ubuntu01

To completely remove the machine, use virsh undefine. With this command, the machine will be deleted and cannot be used again:

$ virsh destroy ubuntu01

How it works…

Both the virt-install and virsh commands collectively give you an easy-to-use virtualization environment. Additionally, the system does not need to support hardware virtualization. When it's available, the virtual machines will use KVM and hardware acceleration, and when KVM is not supported, Qemu will be used to emulate virtual hardware.

With virt-install, we have easily created a KVM virtual machine. This command abstracts the XML definition required by libvirt. With a list of various parameters, we can easily define all the components with their respective configurations. You can get a full list of virt-install parameters with the --help flag.

The virtinst package, which installs virt-install, also contains some

more commands, such as virt-clone, virt-admin, and virt-xml. Use tab

completion in your bash shell to get a list of all virt-* commands.

Once the machine is defined and running, it can be managed with virsh subcommands. Virsh provides tons of subcommands to manage virtual machines, or domains as they are called by libvirt. You can start or stop machines, pause and resume them, or stop them entirely. You can even modify the machine configuration to add or remove devices as needed, or create a clone of an existing machine. To get a list of all machine (domain) management commands, use virsh help domain.

Once you have your first virtual machine, it becomes easier to create new machines using the XML definition from it. You can dump the XML definition with virsh dumpxml machine, edit it as required, and then create a new machine using XML configuration with virsh create configuration.xml.

There are a lot more options available for the virsh and virt-install commands; check their respective manual pages for more details.

There's more…

In the previous example, we used cloud images to quickly start a virtual machine. You do not need to use cloud machines, and you can install the operating system on your own using the respective installation media.

See also

Check out the manual pages for virt-install using $ man virt-install

Check out the manual pages for virsh using $ man virsh

The official Libvirt site: http://libvirt.org/

The Libvirt documentation on Ubuntu Server guide: https://help.ubuntu.com/lts/serverguide/libvirt.html

Help Category:

What Our Clients Say