Tuesday, October 23, 2012

Using virtio console with KVM

Yesterday I finally figured out how to use the virtio console on KVM, which is very easy once you know what to do.

A short explanation of what a "virtio console" is:
On paravirtualized environments like Xen the VM (guest) has no direct hardware access, but also no emulated hardware but instead needs special drivers that know it is running on a hypervisor. The advantage of this approach is that you don't need to emulate hardware and that the driver for the guest is often much simpler (it does not need to probe hardware, for example, as it talks to the hypervisor via a special interface). This makes for a very convenient default Xen setup where you can just do "xm console " on the hypervisor and get a functional console without any fiddling.
On fully virtualized environments, usually an emulated serial port is used for that, which is harder to set up. It is not impossible, but definitely not that easy and there is quite some stuff to configure to get it working.
Nowadays even fully virtualized systems often have some paravirtualized drivers available, mostly for performance reasons. One of those paravirtualized drivers available with KVM is the virtio console driver.

So back on topic: how to set up the virtio console driver for a KVM guest
First, edit the configuration of your guest, in my case with "virsh edit factorytest" (factorytest is the name of my guest). Add the following snippet of configuration before the closing "</devices>" tag:

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

Then a few things need to be done on the KVM guest. In my case of an openSUSE 12.2 guest, the virtio_console module was loaded automatically. However, it surely won't hurt to put it into INITRD_MODULES in /etc/sysconfig/kernel.
The second thing is "console=hvc0" needs to go onto the Kernel command line.
The easiest way to accomplish this is to use the YaST bootloader module and add it in the "Boot Loader Options" tab. You need to actually make sure there is "console=tty console=hvc0" there, because without the "console=tty" the system did not boot up cleanly for me. I guess that the kernel is not happy with only a console that gets added later via a module, but I need to investigate that.

All the rest was handled by systemd and friends just fine, after rebooting the VM guest, I was immedately able to do "virsh console factorytest", hit "Enter", get the login prompt and log in.