KVM On Ubuntu - KVM Logo

Installing and Running KVM on Ubuntu 14.04 Part 5

In part 4 in this series on installing and running KVM I installed the first guest machine. In this part I’ll tweak the guest so that it’s more to my liking and easier to recover should something go wrong.

Auto-starting with KVM

Firstly I want to make the guest automatically start once the host machine has finished booting. The host won’t be restarted very often but the last thing I want is a power drop out while I’m away on holiday taking all the guests out for two weeks. To set a guest to auto-start simply start a virsh session and run the following command:

virsh # autostart demo2
Domain demo2 marked as autostarted

If possible I like to do a restart at this point just to check that everything is configured correctly but on a busy host that isn’t always possible.

Creating a Console

The VNC access that KVM provides by default is, I suppose, enough to get you out of a sticky spot if you mess up the network settings on a guest but for me nothing really beats having an actual console to sit at when the wheels come off. To set up a console first start up a virsh shell and then run the command:

virsh # ttyconsole demo2

If you get no output there is no console currently defined for the machine specified. To create a console execute:

virsh # edit demo2

In the guest settings file add a console block inside the devices block:

<console type='pty'>
    <target port='0'/>

Now start or restart the guest machine in order to create the console device, for example:

virsh # shutdown demo2
virsh # start demo2

Now shell into the guest and create a new start up script (if you don’t have nano installed pick your favourite editor):

sudo nano /etc/init/ttyS0.conf

In the file enter the following:

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
exec /sbin/getty -L 115200 ttyS0 xterm

Save the file and then restart the guest or execute

sudo start ttyS0

Now switch back to the host machine and inside a virsh session enter “console demo2”:

virsh # console demo2

Connected to domain demo2
Escape character is ^]

Ubuntu 14.04.1 LTS demo2 ttyS0

demo2 login:

The prompt will initially stop after telling you about the escape character, press enter to get a login prompt. You should now be able to log in to the machine and work on it as if it was local. When you are done exit as normal and then type Ctrl + ] to leave the console.

In part six of this series I’ll show you how to access the guest machine using VNC and an SSH port forward.

Read More