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.
You will need access to the root account or an account with sudo
privileges.
$ sudo apt-get update $ sudo apt-get install -y qemu-kvm libvirt-bin virtinst
$ sudo apt-get install genisoimage
libvirtd
group and update group membership for the current session:$ sudo adduser ubuntu libvirtd $ newgrp libvirtd
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:
$ 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
$ 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
$ 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.
ubuntu
, and the password set in user-data
.virsh
command to manage this machine. You may need to connect virsh
and qemu
before using them:$ virsh connect qemu:///system
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
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.
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
virsh undefine
. With this command, the machine will be deleted and cannot be used again:$ virsh destroy ubuntu01
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.
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.
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.
Download the installation media and then use following command to start the installation. Make sure you change the -c
parameter to the downloaded ISO file, along with the location:
$ sudo virt-install -n ubuntu -r 1024 --disk path=/var/lib/libvirt/images/ubuntu01.img,bus=virtio,size=4 -c ubuntu-16.04-server-i386.iso --network network=default,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole -v
The command will wait for the installation to complete. You can access the GUI installation using the VNC client.
Forward your local port to access VNC on a KVM host. Make sure you replace 5900
with the respective port from virsh vncdisplay node0:
$ ssh kvm_hostname_or_ip -L 5900:127.0.0.1:5900
Now you can connect to VNC at localhost:5900
.
Ubuntu provides another super easy tool named uvtool. This tool focuses on the creation of virtual machines out of Ubuntu Cloud images. It synchronizes cloud images from Ubuntu servers to your local machine. Later, these images can be used to launch virtual machines in minutes. You can install and use uvtool with the following commands:
$ sudo apt-get install uvtool
Download the Xenial image from the cloud images:
$ uvt-simplestreams-libvirt sync release=xenial arch=amd64
Start a virtual machine:
$ uvt-kvm create virtsys01
Finally, get the IP of a running system:
$ uvt-kvm ip virtsys01
Check out the manual page with the man uvtool
command and visit the official uvtool page at https://help.ubuntu.com/lts/serverguide/cloud-images-and-uvtool.html for more details.
$ man virt-install
$ man virsh