ARM KVM虚拟机启动console串口方法

ARM KVM虚拟机启用console串口方法

【说明】

​ 要使用kvm虚拟机串口,要求串口类型是pty(Psuedo TTY,伪tty)。arm架构的虚拟机串口类型是file,因此arm虚拟机需先修改为console pty,再配置虚机grub参数。

【步骤】

1.通过云平台或者virt-manager新建一台arm虚拟机,在xml文件中看到console类型是file

(nova-libvirt)[root@desktop /]# virsh list
 Id   Name                State
-----------------------------------
 31   instance-00000001   running
(nova-libvirt)[root@desktop /]# virsh dumpxml 31 |grep graphics
    <graphics type='spice' port='5900' autoport='yes' listen='172.20.41.163' keymap='en-us'>
    </graphics>
(nova-libvirt)[root@kydesktop /]# virsh dumpxml 31 |egrep "serial|console"
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
    <serial type='file'>
      <source path='/var/lib/nova/instances/53f6ff57-9b5b-4f0e-ba77-b675147c0438/console.log'/>
      <target type='system-serial' port='0'>
      <alias name='serial0'/>
    </serial>
    <console type='file'>
      <source path='/var/lib/nova/instances/53f6ff57-9b5b-4f0e-ba77-b675147c0438/console.log'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
(nova-libvirt)[root@desktop /]#

2.我们通过virt-manager,将串口由file类型改成pty

​ 注意,由于虚拟机是nova起的,这里我们先关闭nova_compute服务,直接使用virt-manager管理虚拟机。

通过virt-Manager将虚拟机关机,再将当前串口删除,新建一个串口类型为pty,开机即可。

3.检查虚拟机,发现类型已经为pty

(nova-libvirt)[root@desktop /]# virsh list
 Id   Name                State
-----------------------------------
 33   instance-00000001   running
(nova-libvirt)[root@desktop /]# virsh dumpxml 33 |egrep "serial|console"
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
    <serial type='pty'>
      <target type='system-serial' port='0'>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/10'>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
(nova-libvirt)[root@desktop /]#

4.进入虚拟机,在grub中新增console=ttyAMA0参数

root@kn-PC:~# cat /boot/grub/grub.cfg |grep console=ttyAMA0
        linux   /uImage-5.4.18-19-generic root=UUID=5d7dd920-af61-4a73-a681-d4e88cf727c1 ro  quiet splash loglevel=0 resume=UUID=39f41e97-3a2f-4d74-a1bd-4155408fb751 audit=0 security=none console=ttyAMA0
root@kn-PC:~#
root@kn-PC:~#
root@kn-PC:~# systemctl start serial-getty@ttyAMA0.service
root@kn-PC:~# systemctl enable serial-getty@ttyAMA0.service
root@kn-PC:~# systemctl status serial-getty@ttyAMA0.service
● serial-getty@ttyAMA0.service - Serial Getty on ttyAMA0
     Loaded: loaded (/lib/systemd/system/serial-getty@.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-08-24 15:12:33 CST; 12min ago
       Docs: man:agetty(8)
             man:systemd-getty-generator(8)
             http://0pointer.de/blog/projects/serial-console.html
   Main PID: 1078 (agetty)
      Tasks: 1 (limit: 4595)
     Memory: 304.0K
     CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyAMA0.service
             └─1078 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyAMA0 vt220

8月 24 15:12:33 kylin-PC systemd[1]: Started Serial Getty on ttyAMA0.
root@kn-PC:~#
root@kn-PC:~# ps -ef |grep ttyAMA0
root        1078       1  0 15:14 ttyAMA0  00:00:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyAMA0 vt220
root        1752    1525  0 15:24 pts/0    00:00:00 grep --color=auto ttyAMA0

5.通过Virt-Manager重启虚拟机

6.在virsh页面通过virsh console <domain_id>命令,用串口连接虚拟机

(nova-libvirt)[root@desktop /]# virsh list
 Id   Name                State
-----------------------------------
 33   instance-00000001   running
(nova-libvirt)[root@desktop /]# virsh console 33
Connected to domain instance-00000001
Escape character is ^]
Kylin V10 Professional kylin-PC ttyAMA0
kylin-PC login: root
密码:
Welcome to Kylin V10 Professional (GNU/Linux 5.4.18-19-generic aarch64)

 * Management:     http://www.kylinos.cn/ * Support:        http://www.kylinos.cn/service.aspx
上一次登录:二 8月 24 15:21:08 CST 2021从 172.20.40.1pts/0 上
root@kn-PC:~#
root@kn-PC:~# hostname
kylin-PC

【排错】

1.虚拟机找不到/boot/grub/grub.cfg文件

处理:若以上目录没有grub.cfg文件,那在/boot/efi/EFI/kylin/grub.cfg文件中新增console参数

2.arm的虚拟机,刚开机又自动关机

处理:kvm虚拟机被nova_compute关闭,可先关闭nova_compute服务,再使用virt-manger操作。

3.提示报错: operation failed: Active console session exists for this domain

处理:因为同一个domain打开了多个console会话,通过命令关闭:

#ps -ef |grep virsh
root 1457681 1457267 0 15:28 pts/18 00:00:00 virsh console 33
root 1458083 1151292 0 15:29 pts/15 00:00:00 grep virsh
#kill -9 1457681
#ps -ef |grep virsh

4.提示报错:error: internal error: character device console0 is not using a PTY

原因:虚拟机serial/console类型不是pty,查看xml当前应该是file类型,可按照virt-manager手动删除虚拟机当前串口,再新增一个pty类型串口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值