kvm in linux (2)

history of terminal

in history, computer is so huge and hosted at a big room, while the operator stay in another office room with another small machine as terminal to the host computer. as the host computer can run multi-users, each of which needs a terminal.

when times goes to personal computer(PC), there is no need for multi-users login, so each PC has integrated with I/O device(monitor and keybord)

nowadays, we have plenty of end-user terminals, e.g. smart phone, smart watch, Ipad e.t.c, all of these are terminal, as the real host computer is in cloud now.

in a word, any device that can accept input and display output is a terminal, which play the role as the human-machine interface.

three kinds of terminal

ssh is TCP/IP protocol, what’s inside is the remote terminal streaming flow. the TCP/IP plays as the tunnel. of course, any communication protocol can play the role as the tunnel.

  • local terminal, with usb to keyboard and monitor.

  • serial terminal, the guest machine connect to the host machine, who has keyboard and monitor. basically the guest machine borrow the host machine’s IO, which needs the host machine to run a terminal elmulator.

  • tcp/ip tunnel terminal, e.g. ssh

both local terminal and serial terminal, directly connect to physical device, e.g. VGA interface, usb, serial e.t.c, so both are called physical terminal. ssh has nothing to do with physical device.

tty

in Linux, /dev/ttyX represents a physical-terminal. from tty1 to tty63 are all local terminal. during Linux kernal init, 63 local terminals are generated, which can be switched by Fn-Alt-Fx, (x can be 1, 2, 3…). each current terminal is called focus terminal.

focus terminal is taken as global variable, so any input will transfer to current focus terminal. for serial, there is no focus terminal.

in Linux, /dev/console represents current focus terminal, consider as a pointer to current focus terminal, wherever write sth to /dev/console will present at current focus terminal.

/dev/tty is the global variable itself. whichever terminal you are working with, when write to /dev/tty, it will be present.

/dev/ttyS#num# represents serial terminal.

getty & login

in multi-terminal time, each terminal must bind to one user, user must first login the terminal, getty is the login process, which is called in init. after login successfully, the terminal tty#num# can be owned by the user.

there are a few differenet version of getty, e.g. agetty e.t.c.

pty & pts

pty stands for pseudo-tty, pty is a (master-slave) pair terminal device, including: pts pseudo-terminal slave, and ptmx pseudo-terminal master.

a few concepts as following:

  • serial terminal /dev/ttySn

  • pseudo-termianl /dev/pty/

  • controlling terminal /dev/tty

  • console terminal /dev/console

Linux Serial Console

serial communication

in old-style PC, serial is COM interface, also called DB9 interface, with RS-232 standard.

each user can connect to host machine through a terminal. console is same as terminal to connect user to host machine, but console is higher priority than terminal. nowadays less difference between terminal and console.

Teletype is the earliest terminal device, tty is physical or pseudo terminal connect to the host machine, nowadays tty also used for serial device.

serial is the connection from terminal/keyboard to dev-board.

1
ls /dev/tty*

configuration

Linux kernel must be configured to use the serial port as its console, which is done by passing the kernel the console parameter when the kernel is started by the boot loader.

the init system should keep a process runnign to monitor the serial console for logins, the monitoring process is traditionally named getty

a number of system utilities need to be configured tomake them aware of the console, or configured to prevent them from disrupting the console.

serial port

Linux names as the first serial port has the file name /dev/ttys0, the second serial port has the file name /dev/ttyS1 and so on. most boot loaders have yet another naming scheme, the first serial port is numbered 0, the second serial port is numbered 1

configure GRUB boot loader

configure GRUB to use the serial console

1
2
3
4
info grub
/boot/grub/grub.cfg
serial --unit=0 --speed=9600 --word=8 --parity=no --stop=1
terminal serial

init system

getty is started by init:

1
co:2345:respawn:/sbin/getty ttyS0 CON9600 vt102
  • co is an arbitrary entry, representing console

  • 2345, run levels where this entry gets started.

  • respawn, re-run the program if it dies

  • /sbin/getty ttyS0 CON9600 vt102, getty connecting to /dev/ttyS0 with the settings for CON9600bps, and the terminal is VT100 model

virsh console

how to connect ubuntu kvm virtual machine through serial console. in earlier version and distributions, it need to configure serial console in grub file, but in Ubuntu it’s very easy adn reliable as most of configurations and settings are already configured in OS.

setup

runs ubuntu14.04 guest mahcine on ubuntu 16.04 host machine. how to setup serial console, we have to connect guest machine and login on as root user

login through SSH

  • connect on KVM guest machine through ssh from host machine
1
2
ssh 192.168.122.1
hostname

connect through VNC

conenct guest machine through VNC viewer and setup serial console. There are times when we need to troubleshoot Virtual Machines with unknown status like Hang in between, IP address issues, password problems, Serial console Hang etc. In case scenarios, we could relay on VNC configuration of KVM Guest Machines.

vnc viewer is a graphic viewer, so only need add graphics component in config.xml:

1
<graphics type='vnc' port='-1' autoport='yes' passwd='mypassword'/>

run virsh vncdisplay #vm_name# we can get our vnc (server) IP, which then can be accessed by vnc guest viewer. here, kvm virtual machine has implemented a vnc server, and any vnc viewer in the same physical machine, can access this vnc server, even without external networking.

configure serial console in ubuntu guest

after getting login console, we can start serial console and enable it with:

1
2
3
# systemctl start serial-getty@ttyS0
# systemctl enable serial-getty@ttyS0
Created symlink /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service → /lib/systemd/system/serial-getty@.service.

now we can connect serial console with virsh console:

1
virsh console vm_name

after installnation, reboot first, then the physical machine has dual-OS: GuestOS and HostOS, which can exit GuestOS by Ctrl + ], or login GuestOS by virsh consoel #guest_vm#

in summary, virsh console implement a serial console for kvm guest machine, which connect the guest machine to host machine through serial, which is not a ssh, need new knowledges about serial.

virsh console hangs

virsh console vm hangs at: Escape character is ^], which can exit by ctrl + ]

sol1:

go to guest machine/terminal, and edit /etc/default/grub, appending;

1
2
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"

then execute:

1
2
update-grub
reboot

the problem here, as the KVM vm shares the kernel of the host machine, if update grub, the host machine will reboot with serial connection(?)

Centos virsh console hangs

  • go to /etc/securetty and append ttyS0

  • append S0:12345:respawn:/sbin/agetty ttyS0 115200 to /etc/init/ttyS0.conf

  • /etc/grub.conf(Centos)

I only found /boot/grub/grub.cfg in ubuntu. In the kernel line, appending console=ttyS0. but there is no kernel line in ubuntu grub.cfg.

Ubuntu virsh console hangs

1
2
3
systemctl disable systemd-networkd-wait-online
systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service

which gives:

1
2
Mar 27 11:17:18 ubuntu systemd[1]: Started Serial Getty on ttyS0.
Mar 27 11:17:18 ubuntu agetty[445120]: /dev/ttyS0: not a tty

check in /dev/ttyS* :

1
2
3
crw-rw---- 1 root dialout 4, 73 Mar 24 09:20 ttyS9
crw--w---- 1 root tty 4, 64 Mar 24 09:20 ttyS0
crw-rw---- 1 root dialout 4, 65 Mar 24 09:20 ttyS1

interesting here, ttyS0 belongs to tty group, all otehr ttyS#num# belongs to dialout group.

tty and dialout

change /dev/ttyS0 to tty group

can’t access /dev/ttyS

add USER to tty/dialout group

1
2
sudo usermod -a -G tty $USER
sudo usermod -a -G dialout $USER

reboot and go on

refer

remote serial console HOWTO

remote serial console HOWTO in Chinese

understand Linux terminal history in Chinese

Linux terminal introduction in Chinese

serial communication in Chinese

arhLinux: working with the serial console

gnu org: grub

geekpills: start vnc remote access for guest operating systems