Serial Connection from a KVM Virtual Machine

Connecting to a machine through a serial line is a basic necessity if you happen to be an administrator of a server farm or lab. On a physical bare metal machine it easy to connect a null modem cable and configure & be done with it. So for a person who is done that would usually  hope to do it on VirtualSetup and if that happens to have no network connectivity initially  to do a ssh it is a very basic requirement.

This post  is geared towards linux hosts and guests with KVM and libvirt library,  with virt-manager.
This post is also geared for Guest Linux OS , more recent with systemd  to be exact. Since systemd has caused major changes in /etc folder we will be going over those few commands too.

Here is a screenshot of an already running VM of CentOS 7.

Screenshot from 2015-08-17 00-11-02  Screenshot from 2015-08-17 00-13-19

 

As the second Image shows the serial is already created by default if you use virt-manager to create the VM. If its not present click Add Hardware to create one serial connection.  Once this is created you can connect to the serial by either:

virsh  console CentOS

or the VM number which is 4 in my case

virsh console 4

or the regular minicom connection

minicom -D /dev/pts/0

Once that is added the rest of the settings is part of CentOS Guest. We want the entire action that happens on the screen of the machine terminal to be present on the serial console too. Lets start with grub. This configuration is for grub 2. So we will edit the file

/etc/default/grub

The content of which will be :

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

So edit the highlighted line and add two more line as shown:

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_INPUT="console serial"
GRUB_TERMINAL_OUTPUT="console serial"
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX="rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

We have added the INPUT and OUTPUT so that we are able to view and also change at the grub prompt or pause the countdown to default OS. We also added the serial terminal baudrate and 8 word parity, which can also be used on baremetal setup too ,though it doesn’t matter in a VM setup.

Moving on we need to see what the kernel does. So lets edit the grub config for the kernel command line parameter.

/boot/grub2/grub.cfg

search for the linux16 line under the CentOS boot menuentry, which should look like this

linux16 /vmlinuz-3.10.0-229.el7.x86_64 root=/dev/sda1 ro rhgb quiet

Edit it and add the serial specific config as follows, you can remove the quiet part if you want the kernel to spew out everything at you .

linux16 /vmlinuz-3.10.0-229.el7.x86_64 root=/dev/sda1 ro rhgb quiet console=ttyS0,115200 console=tty0

Save the grub.cfg and exit. This sets up the kernel to show all its messages on the serial.

The last part is to make sure you are able to login on serial console. Now this is tricky in systemd machines , earlier it was simpler to edit /etc/inittab or /etc/init.d/serial-ttyS0.conf. Not Now.

Now you need to start the getty service on ttyS0

systemctl start serial-getty@ttyS0.service

and to permenantly run it every time

systemctl enable serial-getty@ttyS0.service

And one more thing edit /etc/securetty and add ttyS0 to it so that you have permission from selinux to be able to login

Thats it, now reboot and watch the magic happen and let me know in the comments on any issues you face

 

 

Hibernating in Fedora 22

Hibernation in Fedora 22 is not enabled by default, only the suspend part is enabled .
Even if you give the following command it just gives a fresh system after resume

systemctl hibernate

One needs to enable it in certain scripts for it to function properly. Following are the steps to enable it

– Enable Recovery from a hibernate in grub bootloader settings file

  - /etc/default/grub

The contents will be as follows

 GRUB_TIMEOUT=5
 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
 GRUB_DEFAULT=saved
 GRUB_DISABLE_SUBMENU=true
 GRUB_TERMINAL_OUTPUT="console"
 GRUB_CMDLINE_LINUX="rhgb quiet"
 GRUB_DISABLE_RECOVERY="true" 

The last line is what matters to us : GRUB_DISABLE_RECOVERY should be set to false. So edit it , replace true to false and it should look like this . If the line is not present , dont worry, just add it

 GRUB_TIMEOUT=5
 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
 GRUB_DEFAULT=saved
 GRUB_DISABLE_SUBMENU=true
 GRUB_TERMINAL_OUTPUT="console"
 GRUB_CMDLINE_LINUX="rhgb quiet"
 GRUB_DISABLE_RECOVERY="false"

– Enable kernel parameters for resume

Enable resume in kernel parameters of grub bootloader so that it resumes from image in the swap partition. So in the following file edit the line for your fedora vmlinuz boot line

  /boot/grub2/grub.cfg

Make sure you edit the proper line by checking the uname command for the kernel version. Mine was 64 bit fedora 22 running kernel 4.0.5-300 , so the following line needs to be edited.

linux16 /boot/vmlinuz-4.0.5-300.fc22.x86_64 root=/dev/sda1 ro rhgb quiet

At the end of the above line add resume=/dev/sda3, assuming ofcourse sda3 is your swap partition

linux16 /boot/vmlinuz-4.0.5-300.fc22.x86_64 root=/dev/sda1 ro rhgb quiet resume=/dev/sda3

Thats it, now reboot the system and hibernate away!! 🙂 let me know in comments any issues you face.

Note: FC-22 mutter package gives some issues once after resume, just update it to version 3.6.3-2