Create a KVM guest

From LSDevLinux
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 ubuntu.com.
  • 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 proxy.example.com:port, click OK, click Install.
  • Reboot the guest after installation and log in.

Ubuntu 12.04 Precise

In the installer window in the guest:

  • Select language.
  • Partition the disk with just a big slash (/) partition. Don't bother making a swap.
  • Choose time zone.
  • Choose keyboard.
  • Choose username, hostname, password.
  • Restart when installation is complete.

Configure guest

For configuring an Ubuntu 12.04 Precise guest.

  • If using a proxy at your location: Launch System Settings. Open Hardware - Network. Click Network proxy. Set a proxy and click Apply system wide. Close the Network window.
  • Launch Ubuntu Software Centre. Choose Edit > Software Sources. On the Updates tab, for When there are security updates: choose Download and install automatically. Click Close. Close Ubuntu Software Centre.
  • Apply all security and bug-fix updates to date, and install openssh-server:
guest:$ sudo apt-get update
guest:$ sudo apt-get dist-upgrade
guest:$ sudo apt-get install vim openssh-server
  • Launch System Settings. Open Personal - Brightness and Lock. For Turn screen off when inactive for: choose Never. Turn off Lock. Close the Brightness and Lock window.
  • 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 " cal.example.com example.com";
guest:$ sudo invoke-rc.d networking restart
  • Enable authentication from the host's user, and start manipulating the system from ssh (replace "boss" with your username on guest):
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
%sudo ALL=NOPASSWD: ALL
  • Optionally 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.

Snapshots

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

Reclaim deleted space

Unlinking a file on a virtual guest's disk may not free the space used by the image file on the host. (This is in the case of a growing image file, rather than a fixed size image file, and assumes use of qcow2 format disk image.)

To reclaim the deleted space, make a large file full of zeros on the guest, delete it, and recreate the image file. This will lose your qcow2 snapshots. When doing any operation like this, make sure to have a backup of your data or configuration. Here are some commands to reclaim the space.

guest$ sudo dd if=/dev/zero of=/zeros bs=1024 count=largeAmount  # where largeAmount will fill (or almost fill) the disk
guest$ sudo rm /zeros
guest$ sudo halt now  # shut down guest
# Shrink guest disk image file by recreating it without zeroed content. Also loses snapshots.
host$ sudo qemu-img convert -O qcow2 guestDiskImageFile.img guestDiskImageFile-smaller.img
host$ sudo mv guestDiskImageFile-smaller.img guestDiskImageFile.img

Further reading: [1]