最後更新: 2019-02-15
介紹
KVM 的 console 是透過 serail port 去實現的.
在 Linux 裡, 一般 console 是一個 character device 來
"virtio console device" 中的 device 則是 /dev/hvc[0-7] 其一個 (<target type='virtio' port='0'/>)
目錄
- Console 的 XML
- Serial Port Login Shell
- Linux Kernal 的 console Settings
- VNC Console
Console 的 XML
設定 VM guest 有 console
<console type='pty'></console>
由於 console 一定是 serial port 來, 所以保存後會有
<serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console>
serial and console elements have partially overlapping scopes.
difference between the two is that serial is used for emulated, usually native, serial consoles,
whereas console is used for paravirtualized ones (virsh # console myvm)
可以用以下一句給一個真實(Host 上)的 serial port 給 VM guest
<devices> <!-- Serial port --> <serial type='pty'> <source path='/dev/pts/3'/> # host interface <target port='0'/> </serial> </devices>
進入 console
virsh # console seafile
Connected to domain seafile Escape character is ^]
離開 console (返回 Shell)
# 只要載入 ^] 就可以離開 console
鍵盤: Ctrl+5 OR Ctrl+]
Serial Port Login Shell
除了 VM 要設定好 device 外, Guest OS 亦要有所設定才同到 virsh console
找出 serial port:
dmesg | grep ttyS
serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
00:05: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
OR
yum install setserial
setserial -g /dev/ttyS*
/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4 /dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3 /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4 /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
-g: arguments to setserial are interpreted as a list of devices
Ubuntu-12.04:
用 upstart 實現:
/etc/init/ttyS0.conf start on stopped rc RUNLEVEL=[2345] stop on runlevel [!2345] respawn exec /sbin/getty -L 38400 ttyS0 vt102
Debian6:
用 init 實現:
修改 /etc/inittab
default 是有 "#" 在前面的 !
T0:23:respawn:/sbin/getty -L ttyS0 38400 linux
之後執行
init q
Centos6:
修改 /etc/securetty
........................ # 加入 ttyS0
/etc/init/serial.conf
# Automatically start a configured serial console (port is in /etc/securetty)
# 它 default 只會去找 /dev/console, 所以, 我們要建立以下檔案
/etc/init/ttyS0.conf
# ttyS0 - agetty # # This service maintains a agetty on ttyS0. stop on runlevel [S016] start on runlevel [23] respawn exec agetty -h -L -w /dev/ttyS0 115200 vt102
Centos7:
stty -F /dev/ttyS0 speed 115200
systemctl start serial-getty@ttyS0
systemctl enable serial-getty@ttyS0
Created symlink from /etc/systemd/system/getty.target.wants/[email protected] to /usr/lib/systemd/system/[email protected].
Remark
To make use of a serial console, just use console=ttyS0 on the kernel command line(/proc/cmdline),
and systemd will automatically start a getty on it for you.
By default systemd will instantiate one [email protected] on the main kernel console.
In systemd, two template units are responsible for bringing up a login prompt on text consoles:
[email protected] is responsible for virtual terminal (VGA screen)(/dev/tty1 ...)
* echo $TERM # Result: linux
[email protected] is responsible for all other terminals (/dev/ttyS0 ...)
* echo $TERM # Result: vt102
virtio-serial (Centos7)
Channel is exposed in the guest under /dev/vport*,
and if the optional element name is specified, /dev/virtio-ports/$name
With qemu, if name is "org.qemu.guest_agent.0",
then libvirt can interact with a guest agent installed in the guest,
(for actions such as guest shutdown or file system quiescing)
設定 virtio console
<console type='pty'>
<target type='virtio'/>
</console>
save 後會變成
<console type='pty'> <target type='virtio' port='0'/> </console> <controller type='virtio-serial' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </controller>
Guest 的設定
Guest 啟動後會有 /dev/hvc0, 並且會由 systemd-getty-generator 啟動 getty
(/usr/lib/systemd/system-generators/systemd-getty-generator)
ps a | grep hvc0
3829 hvc0 Ss+ 0:00 /sbin/agetty --keep-baud 115200 38400 9600 hvc0 vt220
Doc
man systemd-getty-generator
Linux Kernal 的 console Settings
在 /etc/default/grub 加入 Kernel Parmeter
GRUB_CMDLINE_LINUX="console=ttyS0,38400n8"
之後行
update-grub
連接 console
virsh # console debian3
output:
Connected to domain debian3
Escape character is ^] <--- ctrl + ]
Debian GNU/Linux 6.0 debian3 ttyS0
debian3 login:
Exit console
# * English keyboard
Keyboard: Ctrl+]
Host pty
每一個 VM Guest 運行時, 那他的 console 都會對應 host 上的一個 pty
virsh # ttyconsole debian1
output:
/dev/pts/8
P.S.
當有 output 時, 亦即 VM有設定了 console port ( XML 設定)
<console type='pty'> <target type='serial' port='0'/> </console>
virsh console DID 亦是連這個 port 的 !!
VNC Console
查看某 VM Guest 的 VNC Port:
virsh # vncdisplay debian1
192.168.123.10:0
XML 設定:
<graphics type='vnc' port='-1' autoport='yes' listen='192.168.123.10' passwd='PW' keymap='en-us'/>
- 當 port='-1' autoport='yes': 在 5900 ~ 5999 之間找可用的 Port
- Default 只 listen 127.0.0.1
指定用那一個 port:
<graphics type='vnc' port='5913' autoport='no' listen='192.168.123.10'/>
保存後會自動變成:
<devices> ... <graphics type='vnc' port='5913' autoport='no' listen='192.168.123.10'> <listen type='address' address='192.168.123.10'/> </graphics> </devices>
Change VPN Port on Guest running:
qemu-monitor-command <domain> --hmp change vnc :65
- --hmp command is in human monitor protocol
Chnage VNC Password:
qemu-monitor-command <domain> --hmp set_password vnc <YOUR_PW>
Tips
1) Screen Size
Mon: 24" (使用 1920 x 1080)
VNC: (VM Guest 1024 x 960)