远程服务器QEMU+Ubuntu+GRUB+VNC最佳实践

这段时间参与大量内核切换测试工作,实体机需要硬件自检太过笨重,因此主要通过QEMU验证正确性。有一个很大的问题是QEMU运行起来后终端看不到系统的状态,只能通过VNC连接,这无疑是比较低效的方法。更为重要的是,QEMU搭载在远程服务器上。下面介绍本人的最佳实践。

1. 准备

  • 远程服务器:(ip) a.b.c.d
    • QEMU
    • Ubuntu镜像
  • 本地机器
    • ssh
    • VNCViewer

2. QEMU启动安装Ubuntu

2.1 服务器端

OS_IMG=./ubuntu20.img
qemu-system-x86_64 
	-m 4G\
	-hda ${OS_IMG} \
	-enable-kvm \
	-cpu host \
	-device e1000,netdev=net \
	-netdev user,id=net,hostfwd=tcp::2333-:22 \
	-smp cpus=20 \
	-serial stdio \
	-vnc :2
  • -netdev ... hostfwd=tcp::2333-:22:此时a.b.c.d:2333端口被转发到虚拟Ubuntu的22端口,意味着可以通过ssh a.b.c.d:2333连接虚拟机。不过需要虚拟机上安装openssh
  • -serial stdio:虚拟机的串口使用服务器的标准输出(也就是能够在终端显示)
  • -vnc :2:暴露VNC口a.b.c.d:5902

2.2 本地端

此时只能使用VNC连接安装,如何快速连接远端VNC口呢?参考

总的来说,首先在本地建立起 ssh隧道

ssh -L 5902:localhost:5902 username@a.b.c.d

这代表本地端的5902端口与a.b.c.d:5902建立起了一条隧道,将本地机的5902端口转发到远端机器的5902端口

然后打开VNCReviewer,连接localhost:5902即可

在这里插入图片描述

3. 从服务器终端控制虚拟机GRUB与虚拟机终端

安装完毕后,重新运行QEMU,终端上仍然在等待VNC连接,没有任何输出。参考

远程连接虚拟机

ssh username@a.b.c.d -p 2333

修改虚拟机配置/etc/default/grub如下:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=saved
# GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="maybe-ubiquity"
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL="serial"

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

关键点如下:

  • GRUB_TERMINAL="serial",GRUB的输出通过串口输出
  • GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8" :Ubuntu Linux的输出通过串口输出

sudo reboot后就能够在服务器终端上看到GRUB菜单以及Ubuntu的输出信息,然后就可以彻底抛弃VNC了。


OK,从现在开始就可以起飞了🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 08:54:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 08:54:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 08:54:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 08:54:06       20 阅读

热门阅读

  1. C#WPF控件TextBox应用实例

    2023-12-06 08:54:06       31 阅读
  2. C#WPF控件RepeatButton重复按钮应用实例

    2023-12-06 08:54:06       35 阅读
  3. Linux快速给用户改密码

    2023-12-06 08:54:06       36 阅读
  4. 第18章 C++11标准库(STL)

    2023-12-06 08:54:06       48 阅读
  5. using meta-SQL 使用元SQL

    2023-12-06 08:54:06       26 阅读
  6. springboot定时任务

    2023-12-06 08:54:06       42 阅读