KVM, installing

From Ubuntuwiki.net

Jump to: navigation, search

Contents

Check for CPU Hardware Virtualization support

First, you should check to make sure your CPU supports virtualization:

you@box:~$ egrep '(vmx|svm)' /proc/cpuinfo

This should give you several lines of output. If it gives you nothing whatsoever, your CPU does not support virtualization, and you cannot continue.

Install the necessary packages

To install the packages you'll need for virtualization, and for bridging a network interface for your VMs to share with the host:

you@box:~$ sudo apt-get update && sudo apt-get install kvm libvirt-bin virtinst bridge-utils

Now you'll need to add the user you're currently logged in as to the new libvirt group, so that it is privileged to inspect and maintain VMs.

you@box~$: sudo adduser `id -un` libvirt

(You'll have to log out and back in again to make this change effective.)

Make sure KVM is working properly

Now, check to make sure that KVM is properly installed and working:

you@box:$ sudo virsh -c qemu:///system list

You should get title rows for a list of VMs, which is currently empty:

 Id Name                 State
----------------------------------

If you get an error, something went wrong, and you'll need to fix that before you can move on.

Bridge a network interface

Now you'll need to bridge a network interface to use with your VMs. In this example, we will take an existing network interface eth0, which we will convert to a bridged interface br0. The relevant portion of /etc/network/interfaces looks like this:

auto eth0
iface eth0 inet static
        address 192.168.0.10
        netmask 255.255.255.0
        gateway 192.168.0.1

Now we will change the eth0 portion to look like this:

auto eth0
iface eth0 inet manual

And add a br0 section immediately afterward, which looks like this:

auto br0
iface br0 inet static
        address 192.168.0.10
        netmask 255.255.255.0
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off

Our machine will now use br0 as its primary interface, with the same network information as eth0 had initially. Your VMs will create "eth0"s of their own from this bridged interface, which you will be able to configure with any network information for any subnets which are directly connected to the physical eth0 on the host - typically, more IPs from the same subnet br0 is configured to use. For example, a VM we created on this host might be configured to use 192.168.0.11 as its IP address.

Setting up guests (VMs)

First, we'll create a new LV to act as the storage for the VM we're about to create:

you@box:~$ sudo lvcreate -L20G -n vm01 vg0

Now we'll set up a new guest, named vm01, with 1G of RAM and 2 virtual CPUs, ready to install Debian Lenny from an ISO saved in your home directory, creating its network interface from br0, using the LVM logical volume "vm01" on volume group "vg0" for its disk:

you@box:~$ virt-install --connect qemu:///system -n vm01 -r 1024 --vcpus=2 --disk path=/dev/vg0/vm01 -c /home/you/debian-507-amd64-netinst.iso --vnc --noautoconsole --os-type linux --os-variant debianLenny --accelerate --network=bridge:br0 --hvm

If you have two bridged interfaces instead of just one, you can specify them both when you create a VM - just add an additional --network=bridge: argument to the virt-install command when you create it.

If you already have a VM that you wish to add a second network interface to, see KVM, adding bridges.

If you wish to use an image file instead of an LVM logical volume for the VM's disk, change --disk path=/cev/vg0/vm01 to something like -f /home/you/vm01.qcow2 -s 12 (which would create a 12GB image file named vm01.qcow2 in your home directory). Note that file-based VMs tend to impact the host's hardware more heavily during disk operation, and do not have LVM snapshot features available!

Personal tools