Direct terminal access via Serial Console

It is possible to access a Guest directly using the Serial Console interface, in which case setting up bridged networking, SHH, and similar is not necessary.

This is done using the virsh utility which a shell interface for libvirt.

Configure a Serial Console in the Guest

First, we need to configure a serial console (see SerialConsoleHowto) in the guest, in order that it will accept a connection:

sudo editor /etc/init/ttyS0.conf

Add the configuration:

# ttyS0 - getty
#
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]

respawn
exec /sbin/getty -L 115200 ttyS0 xterm

Run the following command to initiate the Serial Console (or restart the VM):

sudo start ttyS0

Here we are using "xterm" as the terminal type, which works well if we are connecting using a gnome-terminal (or xterm) instance. It is likely preferable to set this to match the terminal type which will be used to connect. This can be found using this command in the host/client terminal you will be using to connect:

echo $TERM

For example, if you are running screen it is "screen", for byobu "screen-bce", etc.

You can also set the variable temporarily when you are already connected to the console:

export TERM=screen

Connect kernel & GRUB to Serial Console

If you want everything, including boot selection & default console messages, to the serial console, modify/add the following lines to /etc/default/grub:

GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
 
# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"

Then, update the configuration with update-grub After reboot you will have full access to the VM through the serial console. Removing the console=tty from the CMDLINE_LINUX will completely disable the graphical console as well.

Connect using virsh

First we connect to the (local) qemu session:

virsh -c qemu:///session

Now in the virsh shell, start the virtual machine:

start my_vm

When it has started connect to the console:

console my_vm

You're in!

Misc notes

Use Ctrl + ] to exit the console.

If the console acts strangely or displays incorrectly, check that the TERM variable is set correctly (see above).

If the terminal seems to cut off the top of the output, change it to have 80x24 of viewing area, e.g. in byobu this means you will need to account for the statusbar and have the window itself at 80x26

DraftKVMSerialConsole (last edited 2011-02-09 20:20:12 by the-loeki)