How to redirect Linux(guest) kernel log to windows(host) in virtualbox via serial port?

This article is mainly helpful for kernel developers who would like to access Linux (guest) kernel logs using some tool from windows(host) PC. This is one way to see what is happening to the guest OS when the kernel is booting. Usually this information is passed to and from guest and host via a RS232 serial port. This is most commonly seen in linux embedded systems. So, In real world, guest could be an embedded platform like Beaglebone or Raspberry Pi.

Configuring both guest and host require mainly four steps.

  1. Make a virtual serial port available to guest from VirtualBox settings page
  2. Configure linux kernel to redirect the kernel log to the above virtual serial port
  3. Configure VirtualBox to read back the kernel log into named windows pipe
  4. Finally read the redirected kernel log using putty

Make a virtual serial port available to Linux guest from VirtualBox settings page

This is the simplest step of all. Just make sure you started VirtualBox with administrator permissions. Then all we need to do is enable the following checkbox in the VirutalBox settings as shown below.

Enable virtual serial port to guest in virtualbox settings
Enable virtual serial port to guest in virtualbox settings

Configure linux kernel to redirect the kernel log to the above virtual serial port

This step requires couple of things to be configured in the guest OS. So we boot the guest linux operating system.

  1. Open sudo gedit /etc/init/ttyS0.conf
  2. Add following content in to the file created above
  3. # 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 or RUNLEVEL=[2345]
    stop on runlevel [!2345]
    respawn
    exec /sbin/getty -L 115200 ttyS0 vt102
  4. Save the above file and run the command sudo start ttyS0
  5. The above step creates a service to make the incoming serial connection interactive by running getty.
  6. Next major step is configure the kernel to make available the kernel log from the system boot
  7. Run sudo gedit /etc/default/grub
  8. In that file change GRUB_CMDLINE_LINUX=”” to GRUB_CMDLINE_LINUX=”console=ttyS0,115200n8 ignore_loglevel”
  9. Also uncomment the line #GRUB_TERMINAL=console to GRUB_TERMINAL=console and add a line below it GRUB_SERIAL_COMMAND=”serial –speed=115200 –unit=0 –word=8 –parity=no –stop=1″
  10. Save the file and exit and run sudo update-grub2 to update grub
  11. Now from the next boot the kernel log is made available to any serial program which tries to connect to /dev/ttyS0

Configure VirtualBox to read back the kernel log into named windows pipe

This step configures the windows host to read back the kernel log via windows pipe. You can think of windows pipe as an equivalent mechanism to linux virtual files present in sysfs. Following screenshot will show the required settings to be done in VirutalBox(to make any changes to virutalbox settings the guest should be shutdown first or else the settings will be greyed out)

Enable virtual connection between guest /dev/ttyS0 to windows virtual file \.pipemyserial
Enable virtual connection between guest /dev/ttyS0 to windows virtual file \.pipemyserial

Finally read the redirected kernel log using putty

Download and Run putty with administrator permissions. And configure it as shown below.

Configure putty to read via kernel logs via \.pipemyserial
Configure putty to read via kernel logs via \.pipemyserial

Make sure before clicking open in putty your VM is just started.  Once connected to Guest OS, you can get the logs as shown below.

Linux kernel logs in putty
Linux kernel logs in putty

Happy Kernel Hacking!

Leave a comment

Leave a Reply