Ubuntu KVM 2: Creating the Guest

Posted by Jared Kipe on | 1 Comments

Tags: , , ,

Time to create the Guest, or virtualized, machine.  Ubuntu gives us a very handy script that will take a lot of the guest install and configuration out of our hands... as long as you know all of the flags you need to setup a working install.

Note* For these articles I'm going to assume a clean Ubuntu 14.04 Trusty server install with minimal extras (nano, htop, openssh-server etc.) and/or the outcome of previous articles.

KVM Guest Introduction

KVM uses libvirt, and thus virsh to manage the Guest OS's.  Virsh lets you do all of the things that a physical computer operator (thats you) might do to real hardware.

Virsh Command Effect On Guest
create <name> Creates new Virtual Machine (abstractly)
start <name> boots virtual machine
shutdown <name> Attempts graceful power off
destroy <name> Hart power off
autostart <name> Starts VM on Host boot
list See state of running or crashed machines

As well as things you wish you could do to physical hardware.

Virsh Command Effect on Guest
suspend <name> Like a forced sleep (Virtual RAM still in Physical RAM)
resume <name> Un-suspend
save <name> <file> Save Virtual RAM to file, effectively deep sleep
restore <file> Resume from, above saved, file

These are not the only virsh commands, but are a lot of the frequently used ones.

Creating guest with `ubuntu-vm-builder`

Guests are effectively just XML files, that encapsulate the hardware details of the guest machine. This includes, number of CPU's, amount of RAM, location and type of hard drive (guest's hard drive that is) and any associated network hardware.  The definition XML files do not include, state of machine, contents of HDD (OS and programs), and specific configuration of the guest's network hardware (e.g. IP address and DNS servers).

Getting all of that setup can be a big confusing, especially for the first time (or even 10'th) and if you want to exercise control over the whole process.  A good intermediate step is to use Ubuntu's handy script/package `ubuntu-vm-builder`.

`ubuntu-vm-builder` encapsulates the setup of hardware, download and install of software and can even download and setup packages (e.g. openssh-server or nginx) that you would end up installing right after first boot anyway.

Install KVM, QEMU, Virsh, and ubuntu-vm-builder

Make sure apt-get is up to date and install the KVM and needed tools.

sudo apt-get update
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder

My `ubuntu-vm-builder` stub command

This next part is obviously dependent on your setup, but I will describe every flag I use.

You can just change values as you go, and copy/paste the whole thing into a running shell when you are ready to build a VM.  This will download, install, and configure your new VM for you. Also, you can add yourself to the libvirtd group to be able to add a guest, and manage them through virsh without using sudo.

sudo ubuntu-vm-builder kvm trusty \
    --domain mydomain.com \
    --dest demo1 \
    --hostname demo1 \
    --arch amd64 \
    --mem 1024 \
    --cpus 4 \
    --user localadmin \
    --pass Temp2014 \
    --bridge br0 \
    --ip \
    --mask \
    --net \
    --bcast \
    --gw \
    --dns \
    --components main,universe \
    --addpkg acpid \
    --addpkg openssh-server \
    --addpkg linux-image-generic \
    --libvirt qemu:///system ;

Let me go through most of these:

  • kvm type of guest
  • trusty Ubuntu release to download install (it is `ubuntu-vm-builder` afer all)
  • --domain base domain to be added to host name in /etc/hosts
  • --dest name of virtual machine in virsh
  • --hostname name of your host in /etc/hosts
  • --arch CPU architecture of guest, notable choices are i386 and amd64
  • --mem guest memory/RAM in megabytes
  • --cpus number of guest CPU cores.
  • --user & --pass auto created non-root admin users -- remember to immediately change the password --
  • --bridge specifically defined network interface (because I left virsh's default network)
  • --ip --mask --net --bcast --gw --dns the guest specific network setup. You could skip all of these to use DHCP by default
  • --components distribution componengs, defaults to just main
  • --addpkg used to add a package after the base install, you will find linux-image-generic is necessary for KVM virtual machines, and acpid is needed to have proper guest power management. openssh-server is generally a safe bet to be able to interact with your virtual machine after creation
  • --libvirt the destination hypervisor or dom0, I believe you can use this to target a different physical host, but I've only installed on the locally running one.

Start the new Guest Virtual Machine

At this point you can user virsh to start/boot your new guest.

sudo virsh start demo1
#Domain demo1 started

Congratulations, you can now SSH into this machine from any computer that has network access, and log in with that username and password!

Post your comment


  • Gravatar for Manoj Kumar

    Dear jared,

    Thank you for this valulable information. I followed your instruction the same way you mentioned in blog and installed kvm on the top of ubuntu server and then created a guest running ubuntu server. I also installed the package ubuntu-desktop on the guest. But now I am not getting any way to start GUI on my newly created guest. I am able to connect to guest through ssh. I tried using startx but i am getting error: (WW) CIRRUS: More than one matching Device section found: Autoconfigured Video Device cirrus
    and my screen is getting freezed. Could you please help me on this as how to start GUI ?

    Posted by Manoj Kumar, 13/11/2014 12:04am (6 years ago)

RSS feed for comments on this page | RSS feed for all comments