Fedora 28: 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 /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.
  • 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.
$ sudo cp /etc/default/grub /etc/default/grub.orig
$ C="resume=/dev/mapper/fedora-swap rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap"
$ S="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
$ cat <<EOF | sudo tee /etc/default/grub
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="\$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="console serial"
GRUB_CMDLINE_LINUX="${C}"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,115200"
GRUB_SERIAL_COMMAND="${S}"
GRUB_DISABLE_RECOVERY="true"
EOF

2 grub2-mkconfig

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

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.16.7-300.fc28.x86_64
Found initrd image: /boot/initramfs-4.16.7-300.fc28.x86_64.img
Found linux image: /boot/vmlinuz-4.16.3-301.fc28.x86_64
Found initrd image: /boot/initramfs-4.16.3-301.fc28.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-3ba892070dae45849f0ccc5e362387a9
Found initrd image:
/boot/initramfs-0-rescue-3ba892070dae45849f0ccc5e362387a9.img
done

Reboot system.

$ sudo reboot

3 Execution result

GRUB to monitor display is as below.

0001_GRUB-on-tty1.png

GRUB to serial console is as below. This article used "sudo virsh console <vmname>" for connecting serial console.

0002_GRUB-on-ttyS0.png

Login prompt to monitor display is as below. tty1 is displayed with press Ctrl + Alt + F1.

0003_Login-on-tty1.png

Login prompt to serial console is as below.

0004_Login-on-ttyS0.png

4 Change serial console window size

The default value of serial console window size is 80x24. This will effect emacs, tmux and screen.

$ echo $COLUMNS $LINES
80 24

0005_emacs-before-resize.png

There are several ways for changing serial console window size. I think resize command in xterm package is the easiest way.

$ sudo dnf install -y xterm
$ resize
COLUMNS=90;
LINES=36;
export COLUMNS LINES;

0006_emacs-after-resize.png

Make resize command to be run after login.

$ cat <<EOF >> ~/.bashrc
case "\$(tty)" in
  /dev/ttyS*) resize > /dev/null 2>&1;;
esac
EOF

"trap 'resize > /dev/null' DEBUG" will call resize command many times. If you need to change serial console window size after ~/.bashrc, you had better call resize command manually.

$ trap 'resize > /dev/null' DEBUG
$ for i in $(seq 0 9); do echo $i; done # resize will be called 10 times.