当前位置: 首页 > news >正文

kvm console 的使用

  1. 使用介绍:

    [root@kvm1 ~]# virsh console --helpNAME控制台 - 连接到客户机控制台SYNOPSISconsole <domain> [--devname <string>] [--force] [--safe]DESCRIPTION为虚拟机连接虚拟串行控制台OPTIONS[--domain] <string>  domain name, id or uuid--devname <string>  字符设备名称--force          	强制连接控制台(断开已连接的会话)--safe           	只有在具备安全操作控制台的相关支持的情况下,才可进行连接。
    
  2. 使用一台新安装的虚拟机测试

    无法进入到虚拟机的控制台;原因是:虚拟机没有正确配置串口设备

    [root@kvm1 ~]# virsh listId   Name        State
    ---------------------------3    centos7.9   running
    。/
    [root@kvm1 ~]# virsh console centos7.9
    Connected to domain centos7.9
    Escape character is ^]
    
  3. 为已经安装好的虚拟机配置串口

    我的虚拟机是centos7.9 ,也测试过ubuntu22.04步骤也是一样的;这里必须能够进入到虚拟机,使用ip连接到虚拟机或者使用virt-manger

    在内核启动参数中添加 console=ttyS0 参数,添加完重启系统

    grubby --update-kernel=ALL --args="console=ttyS0"
    

    启动getty 程序

    # systemd 会在 ttyS0 端口上启动 getty 程序。getty 程序会监听该端口,等待用户输入用户名和密码。
    [root@localhost ~]# systemctl start serial-getty@ttyS0
    [root@localhost ~]# systemctl enbale serial-getty@ttyS0[root@localhost ~]# systemctl status serial-getty@ttyS0
    ● serial-getty@ttyS0.service - Serial Getty on ttyS0Loaded: loaded (/usr/lib/systemd/system/serial-getty@.service; enabled; vendor preset: disabled)Active: active (running) since Tue 2025-06-24 05:03:42 EDT; 1min 12s agoDocs: man:agetty(8)man:systemd-getty-generator(8)http://0pointer.de/blog/projects/serial-console.htmlMain PID: 8627 (agetty)CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyS0.service└─8627 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220Jun 24 05:03:42 localhost.localdomain systemd[1]: serial-getty@ttyS0.service holdoff time over, scheduling restart.
    Jun 24 05:03:42 localhost.localdomain systemd[1]: Stopped Serial Getty on ttyS0.
    Jun 24 05:03:42 localhost.localdomain systemd[1]: Started Serial Getty on ttyS0.
    
  4. 使用宿主机来连接虚拟机

    virsh console centos7.9 敲一下回车进入输入账号密码界面,使用 ctrl + ] 退出虚拟机

    [root@kvm1 ~]# virsh listId   Name        State
    ---------------------------3    centos7.9   running[root@kvm1 ~]# virsh console centos7.9
    Connected to domain centos7.9
    Escape character is ^]CentOS Linux 7 (Core)
    Kernel 3.10.0-1160.el7.x86_64 on an x86_64localhost login: root
    Password:
    Last login: Tue Jun 24 05:03:30 on ttyS0
    [root@localhost ~]#
    [root@kvm1 ~]#
    
  5. 在创建虚拟机时添加 console=ttyS0 内核参数

    virt-install \--name centos7.9 \--os-type linux \--os-variant centos7.0 \--memory 2048 \--vcpus 1 \--disk size=20 \--location /var/lib/libvirt/images/CentOS-7-x86_64-DVD-2009.iso \--network bridge=br0,model=virtio \--initrd-inject /root/centos79.cfs \--extra-args="ks=file:/centos79.cfs console=ttyS0" \--graphics vnc \--noautoconsole
    
    • --extra-args="ks=file:/centos79.cfs console=ttyS0" : 添加内核参数,不但指定了ks文件,而且添加了串口

      ks文件内容如下:

      #version=DEVEL
      # System authorization information
      auth --enableshadow --passalgo=sha512
      # Use CDROM installation media
      cdrom
      # Use graphical install
      text
      # Run the Setup Agent on first boot
      firstboot --enable
      ignoredisk --only-use=vda
      # Keyboard layouts
      keyboard --vckeymap=us --xlayouts='us'
      # System language
      lang en_US.UTF-8# Network information
      network  --bootproto=dhcp --device=eth0 --ipv6=auto --no-activate
      network  --hostname=localhost.localdomain# Root password
      rootpw --iscrypted $6$6RNXJjFR0En6Sxwj$21fidwn6Mt1BPl9rMzJCg34DxtrEUHbjeWrirBAkyt45BC8HJsR6Cw0BJ5roT0U2QBAvucc23MEjA.NvTsues.
      # System services
      services --enabled="chronyd"
      # System timezone
      timezone America/New_York --isUtc
      # System bootloader configuration
      bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
      # Partition clearing information
      clearpart --all --initlabel
      # Disk partitioning information
      part /boot --fstype="xfs" --ondisk=vda --size=300
      part / --fstype="xfs" --ondisk=vda --grow%packages
      @^minimal
      @core
      chrony
      kexec-tools%end%addon com_redhat_kdump --enable --reserve-mb='auto'%end%anaconda
      pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
      pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
      pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
      %end
      reboot
      
    1. 在宿主机上使用virsh consle 来连接虚拟机
      可以看出是正常连接的.

      [root@kvm1 ~]# virsh console centos7.9
      Connected to domain centos7.9
      Escape character is ^]CentOS Linux 7 (Core)
      Kernel 3.10.0-1160.el7.x86_64 on an x86_64192 login: root
      Password:
      Last login: Tue Jun 24 09:57:02 from 192.168.25.1
      
    2. 查看getty 程序 是否启动,ttyS0 端口是否被监听

      如下可以看出,在使用virsh-install 安装 虚拟机时在内核 console=ttyS0,会自动启动getty 程序

      [root@192 ~]# systemctl status serial-getty@ttyS0
      ● serial-getty@ttyS0.service - Serial Getty on ttyS0Loaded: loaded (/usr/lib/systemd/system/serial-getty@.service; disabled; vendor preset: disabled)Active: active (running) since Tue 2025-06-24 11:42:34 EDT; 1min 38s agoDocs: man:agetty(8)man:systemd-getty-generator(8)http://0pointer.de/blog/projects/serial-console.htmlMain PID: 565 (agetty)CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyS0.service└─565 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220Jun 24 11:42:34 localhost.localdomain systemd[1]: Started Serial Getty on ttyS0.
    3. 查看内核参数

      总结: 启动串口需要 在内核启动参数种添加 console=ttyS0,需要启动 serial-getty@ttyS0 ttyS0串口编号,补充:串口端口号必须在 /etc/securetty 里面。

      [root@192 ~]#  cat /etc/grub2.cfg | grep "console=ttyS0"linux16 /vmlinuz-3.10.0-1160.el7.x86_64 root=UUID=237e423d-356b-4352-9bc0-19e1321b4d79 ro crashkernel=auto spectre_v2=retpoline console=ttyS0 LANG=en_US.UTF-8linux16 /vmlinuz-0-rescue-1d166deb1edb4402b9b9267d1f2fee03 root=UUID=237e423d-356b-4352-9bc0-19e1321b4d79 ro crashkernel=auto spectre_v2=retpoline console=ttyS0
      
http://www.lryc.cn/news/574598.html

相关文章:

  • linux网 络
  • 计算机基础和Java编程的练习题
  • 储能系统的离网,并网,构网,跟网 简介
  • 【HTTP】取消已发送的请求
  • Linux线程概念及常用接口(1)
  • OSS客户端签名直传实践:Web端安全上传TB级文件方案(含STS临时授权)
  • 从牛顿流体到弹性固体:旋转流变仪的高精度流变特性测定与工业应用
  • Unity_UI_NGUI_UI组件
  • 对人工智能的厌倦感是真实存在的,而且它给品牌带来的损失远不止是参与度的下降
  • 【Linux网络编程】多路转接I/O(一)select,poll
  • 51c嵌入式~电路~合集8
  • 一、永磁同步电机矢量控制——电机数学模型
  • 第十节 新特性与趋势-CSS层叠规则升级
  • AI智能化高效办公:WPS AI全场景深度应用指南
  • 设计模式系列(09):结构型模式 - 适配器模式
  • ffmpeg环境配置
  • HTTP响应头Content-Disposition解析
  • 【MV】编排8:基于时间线数据多层分段避免过度拟合特定歌曲
  • Verilog基础:编译指令`default_nettype
  • Windows搭建opencv cuda开发环境并验证是否成功
  • VO,DTO,BO,PO,DO
  • 7.5.3_2处理冲突的方法-开放定址法
  • Redis 分布式锁原理与实战-学习篇
  • 快速搭建系统原型,UI界面,有哪些高效的AI工具和方法
  • PLA/PHA生物降解化妆品包装材料的稳定性与货架期契合性研究
  • 3D人物建模与WebGL渲染实战
  • bmc TrueSight 监控 Oracle 11g 配置
  • Selenium 4.0 + AI:重构自动化测试的智能革命
  • 【大模型问题】ms-swift微调时,显存持续增长原因分析与解决方案
  • Python Bug 修复案例分析:编码问题导致程序出现bug 两种修复方法