libvirt

The libvirt library is used to interface with different virtualization technologies. Before getting started with libvirt it is best to make sure your hardware supports the necessary virtualization extensions for KVM. Enter the following from a terminal prompt:

 egrep '(vmx|svm)' /proc/cpuinfo

If nothing is printed, it means that your cpu does not support hardware virtualization.

[Note]

On most computer whose processor supports virtualization, it is necessary to activate an option in the bios to enable it. The method described above does not show the status of it's activation.

Virtual Networking

There are a few different ways to allow a virtual machine access to the external network. The default virtual network configuration is usermode networking, which uses the SLIRP protocol and traffic is NATed through the host interface to the outside network.

To enable external hosts to directly access services on virtual machines a bridge needs to be configured. This allows the virtual interfaces to connect to the outside network through the physical interface, making them appear as normal hosts to the rest of the network. For information on setting up a bridge see the section called “Bridging”.

Installation

To install the necessary packages, from a terminal prompt enter:

sudo apt-get install kvm libvirt-bin

After installing libvirt-bin, the user used to manage virtual machines will need to be added to the libvirtd group. Doing so will grant the user access to the advanced networking options.

In a terminal enter:

sudo adduser $USERNAME libvirtd
[Note]

If the user chosen is the current user, you will need to log out and back in for the new group membership to take effect.

You are now ready to install a Guest operating system. Installing a virtual machine follows the same process as installing the operating system directly on the hardware. You either need a way to automate the installation, or a keyboard and monitor will need to be attached to the physical machine.

In the case of virtual machines a Graphical User Interface (GUI) is analogous to using a physical keyboard and mouse. Instead of installing a GUI the virt-viewer application can be used to connect to a virtual machine's console using VNC. See the section called “Virtual Machine Viewer” for more information.

There are several ways to automate the Ubuntu installation process, for example using preseeds, kickstart, etc. Refer to the Ubuntu Installation Guide for details.

Yet another way to install an Ubuntu virtual machine is to use ubuntu-vm-builder. ubuntu-vm-builder allows you to setup advanced partitions, execute custom post-install scripts, etc. For details see the section called “JeOS and vmbuilder”

virt-install

virt-install is part of the python-virtinst package. To install it, from a terminal prompt enter:

sudo apt-get install python-virtinst

There are several options available when using virt-install. For example:

sudo virt-install -n web_devel -r 256 -f web_devel.img -s 4 -c jeos.iso --accelerate --connect=qemu:///system --vnc --noautoconsole
  • -n web_devel: the name of the new virtual machine will be web_devel in this example.

  • -r 256: specifies the amount of memory the virtual machine will use.

  • -f web_devel.img: indicates the path to the virtual disk which can be a file, partition, or logical volume. In this example a file named web_devel.img.

  • -s 4: the size of the virtual disk.

  • -c jeos.iso: file to be used as a virtual CDROM. The file can be either an ISO file or the path to the host's CDROM device.

  • --accelerate: enables the kernel's acceleration technologies.

  • --vnc: exports the guest's virtual console using VNC.

  • --noautoconsole: will not automatically connect to the virtual machine's console.

After launching virt-install you can connect to the virtual machine's console either locally using a GUI or with the virt-viewer utility.

virt-clone

The virt-clone application can be used to copy one virtual machine to another. For example:

sudo virt-clone -o web_devel -n database_devel -f /path/to/database_devel.img --connect=qemu:///system 
  • -o: original virtual machine.

  • -n: name of the new virtual machine.

  • -f: path to the file, logical volume, or partition to be used by the new virtual machine.

  • --connect: specifies which hypervisor to connect to.

Also, use -d or --debug option to help troubleshoot problems with virt-clone.

[Note]

Replace web_devel and database_devel with appropriate virtual machine names.

Virtual Machine Management

virsh

There are several utilities available to manage virtual machines and libvirt. The virsh utility can be used from the command line. Some examples:

  • To list running virtual machines:

    virsh -c qemu:///system list
    
  • To start a virtual machine:

    virsh -c qemu:///system start web_devel
    
  • Similarly, to start a virtual machine at boot:

    virsh -c qemu:///system autostart web_devel
    
  • Reboot a virtual machine with:

    virsh -c qemu:///system reboot web_devel
    
  • The state of virtual machines can be saved to a file in order to be restored later. The following will save the virtual machine state into a file named according to the date:

    virsh -c qemu:///system save web_devel web_devel-022708.state
    

    Once saved the virtual machine will no longer be running.

  • A saved virtual machine can be restored using:

    virsh -c qemu:///system restore web_devel-022708.state
    
  • To shutdown a virtual machine do:

    virsh -c qemu:///system shutdown web_devel
    
  • A CDROM device can be mounted in a virtual machine by entering:

    virsh -c qemu:///system attach-disk web_devel /dev/cdrom /media/cdrom
    
[Note]

In the above examples replace web_devel with the appropriate virtual machine name, and web_devel-022708.state with a descriptive file name.

Virtual Machine Manager

The virt-manager package contains a graphical utility to manage local and remote virtual machines. To install virt-manager enter:

sudo apt-get install virt-manager

Since virt-manager requires a Graphical User Interface (GUI) environment it is recommended to be installed on a workstation or test machine instead of a production server. To connect to the local libvirt service enter:

virt-manager -c qemu:///system

You can connect to the libvirt service running on another host by entering the following in a terminal prompt:

virt-manager -c qemu+ssh://virtnode1.mydomain.com/system
[Note]

The above example assumes that SSH connectivity between the management system and virtnode1.mydomain.com has already been configured, and uses SSH keys for authentication. SSH keys are needed because libvirt sends the password prompt to another process. For details on configuring SSH see the section called “OpenSSH Server”

Virtual Machine Viewer

The virt-viewer application allows you to connect to a virtual machine's console. virt-viewer does require a Graphical User Interface (GUI) to interface with the virtual machine.

To install virt-viewer from a terminal enter:

sudo apt-get install virt-viewer

Once a virtual machine is installed and running you can connect to the virtual machine's console by using:

virt-viewer qemu:///system web_devel

Similar to virt-manager, virt-viewer can connect to a remote host using SSH with key authentication, as well:

virt-viewer -c qemu+ssh://virtnode1.mydomain.com/system web_devel

Be sure to replace web_devel with the appropriate virtual machine name.

If configured to use a bridged network interface you can also setup SSH access to the virtual machine. See the section called “OpenSSH Server” and the section called “Bridging” for more details.

Resources

  • See the KVM home page for more details.

  • For more information on libvirt see the libvirt home page

  • The Virtual Machine Manager site has more information on virt-manager development.

  • Also, stop by the #ubuntu-virt IRC channel on freenode to discuss virtualization technology in Ubuntu.