Ubuntu 16.04: GRUB2 and Linux with serial console

This article will describe changing GRUB2 and Linux to serial console.

This is not for desktop environment but embedded system and virtual machine environment.

 

1 Why using serial console

The serial console is helpful for CUI environment.

  • An embedded system which has no monitor display like router can be operated by serial console with RS232C.
  • A virtual machine like KVM + libvirt can be operated by serial console with CUI tools like virsh.

2 /etc/default/grub

Change /etc/default/grub as below.

  • Change GRUB terminal to console and ttyS0. This will provide one GRUB to a monitor display and serial console.
  • Remove hidden parameter for avoiding "no video mode activated" error. And change GRUB timeout from 10 seconds to 1 second.
  • Change linux kernel console to tty1 and ttyS0. This setting will be taken over to userland, and there will be two login prompt for tty1 and ttyS0.
$ diff -uprN /etc/default/grub{.org,}
--- /etc/default/grub.org       2016-06-07 08:26:05.869583873 +0900
+++ /etc/default/grub   2016-06-07 22:02:51.288594112 +0900
@@ -4,12 +4,10 @@
 #   info -f grub -n 'Simple configuration'

 GRUB_DEFAULT=0
-GRUB_HIDDEN_TIMEOUT=0
-GRUB_HIDDEN_TIMEOUT_QUIET=true
-GRUB_TIMEOUT=10
+GRUB_TIMEOUT=1
 GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
-GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
-GRUB_CMDLINE_LINUX=""
+GRUB_CMDLINE_LINUX_DEFAULT=""
+GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200"

 # Uncomment to enable BadRAM filtering, modify to suit your needs
 # This works with Linux (no patch required) and with any kernel that obtains
@@ -17,7 +15,8 @@ GRUB_CMDLINE_LINUX=""
 #GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

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

 # The resolution used on graphical terminal
 # note that you can use only modes which your graphic card supports via VBE

3 grub-mkconfig

Update /boot/grub/grub.cfg with grub-mkconfig.

$ sudo grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-22-generic
Found initrd image: /boot/initrd.img-4.4.0-22-generic
Found linux image: /boot/vmlinuz-4.4.0-21-generic
Found initrd image: /boot/initrd.img-4.4.0-21-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done

Reboot Ubuntu 16.04.

$ sudo reboot

4 Execution result

GRUB to monitor display is as below.

0001_GRUB-to-Display.png

GRUB to serial console is as below.

This article used "sudo virsh console <vmname>" for connecting serial console.

0002_GRUB-to-SerialConsole.png

Login prompt to monitor display is as below.

tty1 is displayed with press Ctrl + Alt + F1.

0003_Linux-to-Display.png

Login prompt to serial console is as below.

0004_Linux-to-SerialConsole.png

4.1 Single user mode / Recovery mode

  • When running single user mode, there will be two root prompt for tty1 and ttyS0.
  • When running recovery mode, there will be one root prompt for ttyS0. If you want to switch prompt to tty1, you need to move "console=ttyS0,115200" to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub because recovery mode does not user GRUB_CMDLINE_LINUX_DEFAULT.
  • When running single user mode or recovery mode, root prompt for ttyS0 will be corrupted, e.g. noecho. Please append "stty sane" to root.bashrc as below.
case "$TERM" in
  linux|vt*) stty sane ;;
esac

A root prompt for ttyS0 with using "stty sane" is as below.

0005_RecoveryMode-to-SerialConsole.png