TQ
dev.com

Blog about software development

Subscribe

Serial console access from the KVM CLI

13 Jan 2020 - by 'Maurits van der Schee'

In a previous post I have installed KVM (on Ubuntu 18.04) from the CLI on my Dell R720xd. In this post I will show how to adjust the GRUB config to enable the serial console in KVM and how to access it on the KVM CLI. This is great for fixing SSH connectivity issues and to do easy LUKS password entry during boot. In this post we will also explore how to allow access to the grub menu from the serial console.

Enable the console in GRUB

In order for the "console" command of "virsh" to work you need to modify the guest's grub config. In the guest run the following command:

sudo nano /etc/default/grub

Look for the line:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

and replace it with the following, or add this line if absent (on CentOS):

GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"

On CentOS remove "rhgb quiet" from the line "GRUB_CMDLINE_LINUX" to ensure messages are shown.

On Ubunutu this is only effective after running "update-grub" using:

sudo update-grub

On CentOS you need to use the "grub2-mkconfig" command to update the grub config:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

This should write the grub config to disk.

Connect to the console

To test that it works, we reboot the machine using:

sudo reboot

After rebooting the VM you can connect to the console using "virsh" by running:

virsh console maurits-cloud

Make sure to disconnect any graphical client (virt-manager) if you see this error message:

error: operation failed: Active console session exists for this domain

Console access is very good for emergency SSH connectivity fixes and LUKS password entry.

Grub menu via the console

You can also enable access to the grub menu using the console, but I feel this is not really necessary. The grub menu is already accessible from the VNC console. But if you do want this, then you should have the following entries in your "/etc/default/grub" file (change or add them):

GRUB_TIMEOUT=10
GRUB_TIMEOUT_STYLE=countdown
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"

Again apply and reboot with:

sudo update-grub
sudo reboot

You should now see the grub menu when you access the console using the "virsh console" command after pressing the escape (Esc) key during the countdown.

Next: Assigning a static IP address in KVM

In the next post I will walk you through setting up static IP addresses and hostnames for your KVM virtual machines. Hostnames allow you to connect to machines using their name instead of their (DHCP assigned) IP address. Static IP addresses are a requirement for doing port forwarding, a topic we will touch in another post in this blog series.

Click here to read the next article (on static ip addresses in KVM).


PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.