Create a KVM guest

From LSDevLinux
Revision as of 10:36, 24 November 2010 by Mayhewn (talk | contribs) (Reverted edits by Okopacare (Talk); changed back to last version by Mstrobert)

Jump to: navigation, search

These steps will create a KVM virtual guest machine for Ubuntu Server JeOS 9.10 Karmic or Ubuntu Desktop 9.10.

"host" is the machine hosting the guest. MACHINE is what you want the hostname of the guest to be.

Prepare host

  • Install packages:
host:$ sudo aptitude install kvm virt-manager
  • Download the Ubuntu Server 9.10 ISO image from
  • Set up bridging on your workstation:

Assuming you have an "auto eth0" in /etc/network/interfaces, append to that file

auto br0
iface br0 inet dhcp
  bridge_ports eth0

This creates a bridge that your guests can join to access the external network through eth0. (I assume this is what is happening.)

host:$ sudo /etc/init.d/networking restart

Create guest

Instructions are currently written for an Ubuntu 10.04 Lucid host and guest.

  • Choose Applications > System Tools > Virtual Machine Manager.
  • In Virtual Machine Manager, right-click localhost, and choose New.
  • In the Name box, type the name of the virtual machine ($MACHINE). Click Local install media and click Forward.
  • Click Use ISO image, Browse, navigate to and click the downloaded ISO file and click Open. In OS Type, choose Linux. In Version, choose Ubuntu 10.04 (Lucid Lynx), and click Forward.
  • In the Memory (RAM) box, enter 1024, and click Forward.
  • Click Select managed or other existing storage, and click Browse. The Locate or create storage volume dialog appears.
  • Click New Volume. In the Name box, delete the trailing .img. In the Format box, choose qcow2. In the Max Capacity box, enter as much as you can (many gigabytes), and click Finish.
  • In the Locate or create storage volume dialog, click the new image and click Choose Volume.
  • In the New VM dialog, click Forward.
  • Click to expand Advanced options. In the drop-down box, choose Host device eth0 (Bridge br0). Verify that the Architecture box matches the architecture used by the ISO you are installing from, and click Finish. The ISO will be booted from and display in a window.

Ubuntu Desktop

Ubuntu 10.04 Lucid

  • In the guest, on the Install dialog, choose a language and click Install Ubuntu 10.04 LTS. Choose time zone and keyboard layout.
  • Click Specify partitions manually (advanced), Forward. Create one partition for slash (/), at maximal size. Don't make a swap partition. Click Forward.
  • Specify name (such as "boss"), password, and hostname ($MACHINE). Optionally click Log in automatically. Click Forward.
  • On the Ready to install screen, click Advanced, enter your proxy in the form, click OK, click Install.
  • Reboot the guest after installation and log in.

Ubuntu 10.10 Maverick

  • In the guest, on the Install dialog, choose a language and click Install Ubuntu. Click Forward.
  • Click Specify partitions manually (advanced) and click Forward.
  • Create one partition for slash (/), at maximal size. Don't make a swap partition. Click Install Now.
  • Choose time zone and keyboard layout.
  • Specify name (such as "boss"), hostname ($MACHINE), and password. Optionally click Log in automatically. Click Forward.

Configure guest

  • In an Ubuntu 10.10 Maverick guest, choose System > Preferences > Network Proxy. Click Manual proxy configuration. In the HTTP proxy and Port boxes, enter 4128. Click Apply System-Wide and click Close.
  • Choose Applications > Ubuntu Software Centre. Choose Edit > Software Sources. In the Download from box, choose Other, select a fast server (such as United States -, and click Choose Server. On the Updates tab, click Install security updates without confirmation. Click Close. Wait for Ubuntu Software Centre to finish updating the cache, and choose File > Close.
  • Apply all security and bug-fix updates to date, and install openssh-server:
guest:$ sudo apt-get install aptitude
guest:$ sudo aptitude safe-upgrade
guest:$ sudo aptitude install vim openssh-server
  • Choose System > Preferences > Screensaver. Clear Activate screensaver when computer is idle and Lock screen when screensaver is active. Click Power Management. In the Put display to sleep when inactive for box, choose Never, and click Close. Click Close
  • Optionally set guest to start at host boot: In the Virtual Machine window, choose View > Details. Click Boot Options, select Start virtual machine on host boot up, and click Apply. Choose View > Console.
  • If your office supports this, configure dhclient to register itself by name with your dhcp server to enable looking up this guest's IP by name.

Determine MAC address using ifconfig (look for HWaddr). Edit /etc/dhcp3/dhclient.conf and put $MACHINE into the line:

send host-name "<hostname>";

Then insert 2 lines such as:

send dhcp-client-identifier <GUEST MAC ADDR>;
append domain-name "";
guest:$ sudo invoke-rc.d networking restart
  • Enable authentication from the host's user, and start manipulating the system from ssh:
host:$ ssh-copy-id boss@$MACHINE   # or boss@$MACHINE.local
host:$ ssh boss@$MACHINE   # or boss@$MACHINE.local
  • Optionally set up swap:
guest:$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 # 1GiB
guest:$ sudo chmod 600 /swapfile # Protect memory from being read
guest:$ sudo mkswap /swapfile
guest:$ sudo swapon /swapfile

Append to /etc/fstab :

/swapfile none swap sw 0 0
  • Optionally enable passwordless sudo:
guest:$ sudo visudo

Append the 2 lines:

# Enable passwordless sudo for all admins
  • Set up others to be able to authenticate:
theiruser@their-workstation:$ ssh-copy-id boss@$MACHINE
  • Reboot guest to use new kernel and verify system returns to a good state after rebooting.

Use higher display resolution in guest

When running an Ubuntu 10.04 Lucid or Debian 6.0 squeeze host and Ubuntu 10.04 Lucid or Ubuntu 10.10 Maverick guest, you can enable a higher resolution in the guest by doing the following:

  1. Shut down the guest.
  2. In the guest Virtual Machine window, choose View > Details.
  3. Click Video. In the Model box, change cirrus to vmvga. Click Apply.
  4. Choose View > Console, Virtual Machine > Run.
  5. Choose System > Preferences > Monitors to select a higher resolution.

Windows XP guests seem to support higher resolution without needing to do this. The guest may say that it can't find a driver for your hardware, but then that doesn't seem to matter and you can pick a high resolution.

Guest blocking host sound

You can stop the guest from blocking your host sound output by doing this:

  1. In the guest's Virtual Machine window, choose View > Details.
  2. Click Sound: es1370.
  3. Click Remove.

To restore it:

  1. Click Add Hardware.
  2. In the Hardware type box, select Sound and click Forward.
  3. In the Model box, select es1370 and click Forward. Click Finish.


To take a snapshot of a guest image:

# Shut down virtual machine first.
host:$ cd /var/lib/libvirt/images
host:$ sudo qemu-img snapshot -l machine.img # list snapshots in an image
host:$ sudo qemu-img snapshot -c snapshotName machine.img # create a new snapshot for a machine
# Start virtual machine. Modify data. Break something. Shut down virtual machine.
host:$ sudo qemu-img snapshot -a snapshotName machine.img # apply a snapshot

To do this, the guest image needs to be qcow2, not raw. To check what type of image it is, run:

host:$ sudo file /var/lib/libvirt/images/$MACHINE.img

It should say:

MACHINE.img:  Qemu Image, Format: Qcow , Version: 2

If your guest image is raw, you can convert it to qcow2 by doing (and being careful with an image that contains important data or configuration):

# Show down virtual guest first
host:$ sudo qemu-img convert -O qcow2 /var/lib/libvirt/images/$MACHINE.img \
       /var/lib/libvirt/images/$MACHINE.img.qcow2 && \
       sudo mv /var/lib/libvirt/images/$MACHINE.img.qcow2 /var/lib/libvirt/images/$MACHINE.img
host:$ sudo chmod 600 /var/lib/libvirt/images/$MACHINE.img