6
5

More than 3 years have passed since last update.

Ubuntu 20.04でシリアルコンソールを使う

Last updated at Posted at 2020-04-26

serial console

English

sudo cp -nv /etc/default/grub   /etc/default/grub.orig
sudo cp -nv /boot/grub/grub.cfg /boot/grub/grub.cfg.orig
sudo vim /etc/default/grub

/etc/default/grub:

## remove (optional):
# GRUB_TIMEOUT_STYLE=hidden

## change:
# (optional)
# GRUB_TIMEOUT=0
GRUB_TIMEOUT=2

## add:
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200"

## change:
# GRUB_CMDLINE_LINUX=""
GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200"

説明

GRUB_TIMEOUT_STYLE=hidden を消して GRUB_TIMEOUT=2 にするとブート時にGRUBメニューが表示されるようになります。

GRUB menu

GRUB_TIMEOUT が1以上(あるいは-1)であれば、GRUB_TIMEOUT_STYLE=hidden であっても Escape キーを押すことでメニューは出せます (ref.)。

GRUB_TERMINAL="console serial" をつけることで、VGAコンソールとserialターミナルの両方にGRUBメニューが表示されます。

GRUB menu in both VGA console and serial terminal

シリアルコンソールの出力が表示されない場合、マニュアルを参照して、ハードウェアに合わせて GRUB_SERIAL_COMMAND="serial --speed=115200" を変えてみて下さい。

GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,115200"Linux kernelのcommand-line parameterです。GRUB_TERMINAL="console serial" と同様、このオプションによりVGAコンソール (tty1) とシリアルコンソール (ttyS0) の両方でtty入力/出力が可能になります。

tty output on both tty1 and ttyS0

これはboot時にGRUB menuで変更することも可能です。

Modify command-line parameter in GRUB menu


/etc/default/grub を編集したら sudo update-grub を実行して、boot時に実際に読み込まれる /boot/grub/grub.cfg を生成します。これは内部的に grub-mkconfig -o /boot/grub/grub.cfg を実行しています。


/etc/default/grub のオリジナル、修正後、diff

オリジナル(環境によって変わります。これはQEMUのもの):

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

修正後:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
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
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console
#GRUB_TERMINAL="console serial"
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200"

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

diff:

sudo apt install python3-pip
pip3 install icdiff
~/.local/bin/icdiff /etc/default/grub.orig   /etc/default/grub
~/.local/bin/icdiff /boot/grub/grub.cfg.orig /boot/grub/grub.cfg

diff /etc/default/grub

/boot/grub/grub.cfg のdiffは大きいので自分の目で確かめてみて下さい。


再起動するとシリアルコンソールにdmesgが流れ、ログインプロンプトが表示されるようになります。

クリエイティブ・コモンズ・ライセンス
この 作品 は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5