目的: 台机已经装好Linux ,显示器有问题,平时一般都SSH 控制,当Linux 网络挂的时候,想通过串口来管理。

硬件: 台机自带两个串口( 上面的是COM1)[under Windows] or ttyS0[under Linux)) ,笔记本T61p 没有串口,买了个USB 转串口( 具体见下图) 。同时需要串×××叉线一根(NULL modem Cable), 母对母( 公的是针) ,都是凹口那种。

目标机器: 安装的CentOS 5.2 Linux 内核2.6.18-92.1.22.el5 2.6.18-92.1.22.el5xen
客户端: Windows XP SP3 IBM OEM. 安装USB 转串口驱动。

需要把输出定向到串口,一般有下面几处:
  1. BIOS 中的设置
  2. GRUB 设置
  3. /etc/inittab 设置
  4. /etc/securetty 设置

BIOS 中的设置

BIOS 设置的作用我没尝试过,如果是可以连BIOS 设置都可以定向到串口,那个强大了。也不知道如何去掉机器需要连键盘的限制。没拿出显示器所以没看我台机的BIOS ,不过这个功能一般只有服务器级别的机器有。【注1 】台机一般没有串口重定向功能。

GRUB 设置
这里的配置是为了把grub 菜单定向到串口,这样你就可以在串口操作grub ,选择启动项,使用single mode 等等。
同时也要给kernel 参数加上console 选项。

[root@CentOS5 ~]# cat /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/vg00/lv_root
# initrd /initrd-version.img
#boot=/dev/hdb1
serial --unit=0 --speed=9600 --word=8 --parity=no --stop=1
terminal --timeout=10 serial console
default=2
timeout=20
#splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz => 这些在console 下会工作不正常,注释掉。
#hiddenmenu
title CentOS (2.6.18-92.1.22.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-92.1.22.el5 ro root=/dev/vg00/lv_root rhgb quiet
initrd /initrd-2.6.18-92.1.22.el5.img
title CentOS Serial[ttyS0] - Console (2.6.18-92.1.22.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-92.1.22.el5 ro root=/dev/vg00/lv_root console=ttyS0,9600 console=tty0
initrd /initrd-2.6.18-92.1.22.el5.img
title CentOS Console - serial[ttyS0] (2.6.18-92.1.22.el5) [get more output in serial console]
root (hd0,0)
kernel /vmlinuz-2.6.18-92.1.22.el5 ro root=/dev/vg00/lv_root console=tty0 console=ttyS0,9600
initrd /initrd-2.6.18-92.1.22.el5.img
title CentOS Xen (2.6.18-92.1.22.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-92.1.22.el5
module /vmlinuz-2.6.18-92.1.22.el5xen ro root=/dev/vg00/lv_root rhgb quiet xencons=off
module /initrd-2.6.18-92.1.22.el5xen.img
title CentOS Xen Serial[ttyS0] - Console (2.6.18-92.1.22.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-92.1.22.el5
module /vmlinuz-2.6.18-92.1.22.el5xen ro root=/dev/vg00/lv_root xencons=off console=ttyS0,9600 console=tty0
module /initrd-2.6.18-92.1.22.el5xen.img
title CentOS Xen Console - serial[ttyS0] (2.6.18-92.1.22.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-92.1.22.el5
module /vmlinuz-2.6.18-92.1.22.el5xen ro root=/dev/vg00/lv_root xencons=off console=tty0 console=ttyS0,9600
module /initrd-2.6.18-92.1.22.el5xen.img
title Other
rootnoverify (hd0,0)
chainloader +1

后面的console 有顺序关系,详见下文【注2 】,我常用串口所以console=tty0 console=ttyS0,9600 这个顺序的输出信息比较多。

因为我的机器有xen 的内核,xen 默认情况下会把串口做为xencons ,所以这里设置xencons=off.
When multiple consoles are listed output is sent to all consoles and input is taken from the last listed console. The last console is the one Linux uses as the /dev/console device.
/etc/inittab 设置
系统系统后,需要在串口上监听请求,所以要有个类似服务器的程序(agetty, mgetty etc)
/etc/inittab 中添加
s0:2345:respawn:/sbin/agetty -L -f /etc/issue.serial 9600 ttyS0 vt100
#s1:2345:respawn:/sbin/agetty -L -f /etc/issue.serial 9600 ttyS1 vt100
#S0:2345:respawn:/sbin/mgetty -r -x 9 ttyS0 ==> mgetty 用来调试比较好,log 也丰富。
What is a getty?
A getty is is a program that opens a tty port, prompts for a login name, and runs the /bin/login command. It is normally invoked by init.
所以其他程序就不能再来占用ttyS0 了,串口是独占模式的吧。

vt100:
vt100 is the terminal emulation. You can use others, but VT100 is the most common or "standard". Another widely used termial type is VT102.
OK 都可以了,可以重启server 了。这里如果想测试下串口线,推荐先在Linux 启动下mgetty ,因为他的debug 信息和log 比较丰富,适合排错和测试。
#mgetty –r –x 9 ttyS0
Log /var/log/mgetty* 中。

/etc/securetty 设置
因为一般我们都是使用root 登陆串口来维护,所以需要设置root 可以在COM1 COM2 登陆。
/etc/securetty 中添加:
ttyS0
ttyS1

Windows 客户端连接可以用超级终端或者Putty
File?id=dcqhbwpm_413cbbh4tf6_b

注意启动server 的时候,putty 就应该去连接串口了,这样才可以看到所有信息。
Press any key to continue.
File?id=dcqhbwpm_414dhvxrhc5_b
Grub menu
File?id=dcqhbwpm_415g8x4tjhf_b
Booting.
File?id=dcqhbwpm_416hs3pqgg6_b

本来正常情况下,接下去kernel boot 信息应该也会打到串口的。但是没有,确定是Xen 内核的关系。【注3 】好像有人已经碰到这个问题了。
系统启动起来后,过了init agetty 就起来了,这个时候就可以通过串口登陆了。( /etc/securetty 中需要添加ttyS0)
File?id=dcqhbwpm_417c76jj7g8_b


后记:
在做这次实验的时候开始心里很没底,因为串口从来没搞过。虽然编程时涉及过串口编程,但到我这边已经只是网络编程了。买了一个USB 转串口接口,两个串×××叉线。我想要NULL modem 线,店主不知道( 额…), 开始不工作时一度怀疑线有问题。

还有就是开始不理解串口工作原理,我知道串口是字符设备,有啥数据得当时就去读。曾经我连了串口并开了Putty 去连,界面上没输出。1) 因为当时就是没数据。 2) 是有时候终端需要按键来激活( 比如按几下回车)

还有就是可能终端设置问题。这块我现在还不太清楚。因为终端设置涉及到有些键盘操作,比如删除。我好几次发现Backspace 键没反应而重启过好几次机器。后来发现我的终端设置需要先<= = 左箭头左移然后 delete


我的主板说明书上接口图:
File?id=dcqhbwpm_418gjpcgmch_b
【注1 :

C.2. Configure the BIOS to use the serial port

Many servers allow the BIOS to be configured from the serial port, especially on systems designed for rack mounting. At the moment few machines designed to be used as desktop systems allow the BIOS to be accessed from the serial port.
【注2 :

Chapter 5. Configure Linux kernel

The Linux kernel is configured to select the console by passing it the console parameter. The console parameter can be given repeatedly, but the parameter can only be given once for each console technology. So console=tty0 console=lp0 console=ttyS0 is acceptable but console=ttyS0 console=ttyS1 will not work.
When multiple consoles are listed output is sent to all consoles and input is taken from the last listed console. The last console is the one Linux uses as the /dev/console device.
【注3 :