- Invocation — QEMU documentation
- Hosts/Linux - QEMU
- Download (spice-space.org)
- spice / linux / vd_agent · GitLab
- virt-manager/virt-manager: Desktop tool for managing virtual machines via libvirt (github.com)
- MobaXterm Xserver and tabbed SSH client - resetmasterpassword (mobatek.net)
关键字:
QEMU、qemu-kvm(Kernel-based Virtual Machine)、frp、Ubuntu 24.04、screen、enable-kvm、qemu-kvm、w/o VNC、QCOW2、 GTK 窗口、VNC display、Spice、virt-viewer、virt-manager、MobaXterm、make install、mobaxterm、X server
Tips:
QEMU
是一个开源的机器模拟器和虚拟器;virt-manager
是一个用于管理虚拟化环境的图形化工具;virt-manager
利用QEMU
来创建、启动、停止、暂停和管理虚拟机。它为用户提供了一个直观的图形界面,方便用户操作虚拟机,而无需直接使用QEMU
的命令行参数。virt-Manager 是一个通过 libvirt 管理虚拟机的图形工具。大多数使用是与 QEMU/KVM 虚拟机一起使用,但 Xen 和 libvirt LXC 容器得到很好的支持。- 由于是使用反向代理 frp 内网穿透在无图形界面的Ubuntu 24.04 LTS主机,通过ssh安装QEMU,频繁出现掉线问题,所以使用Screen
apt-get install screen
root@atc:~# screen -v
Screen version 4.09.01 (GNU) 20-Aug-23
- 运行
screen
命令来创建新会话,想给会话起一个名字,可以在创建会话时使用-S
参数:screen -S my_session - 在已有的
screen
会话中创建一个新的窗口(可以理解为类似一个新的子会话),可以使用快捷键Ctrl + A
,然后按c
。这将在当前screen
会话中创建一个新的窗口,可以在其中运行不同的命令或程序。 - 分离会话(即关闭终端但不中断命令):按下 Ctrl + A,然后再按下 D。重新连接到会话:可以使用 screen -r 命令重新连接到之前分离的会话,继续查看命令的运行情况或进行交互
- 强制退出当前的 screen 会话,然后恢复工作。使用 Ctrl + A,然后按 Ctrl + \,这将发送 SIGQUIT 信号强制退出 screen 会话。注意,这可能会导致正在运行的程序非正常退出,可能会有数据丢失的风险。通常情况下不能直接重新连接到该会话,因为强制退出会导致会话非正常终止。
- 重新连接到指定的会话:screen -r <session_id>,其中 <session_id> 是通过 screen -ls 查看到的会话编号。(-r [session] Reattach to a detached screen process)
- 退出当前会话:
Ctrl + A
:
:进入命令模式,可以输入一些命令,如quit
退出当前会话,screen -ls
列出所有会话等 - 关闭会话:screen -S <session_id> -X quit
- -S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
- -X Execute <cmd> as a screen command in the specified session.
1、在无图形界面的Ubuntu 24.04 LTS主机,按照维基百科Hosts/Linux - QEMU安装QEMU。
ssh连接反向代理 frp 内网穿透到Ubuntu 24.04 LTS主机进行QEMU安装
1.1、内核从6.8.0-41 升级到 6.8.0-45-generic
root@atc:~# uname -a
Linux atc 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
1.2、安装 required additional packages 和 Recommended additional packages
- git-email,用于发送补丁
- libsdl2-dev(libsdl2-devel),SDL 图形用户界面所需
- libgtk-3-dev(gtk3-devel),用于替代 VNC 的简单用户界面
- libvte-dev(vte291-devel),用于通过 GTK 界面访问 QEMU 监视器和串口/控制台设备
- libcapstone-dev(capstone-devel),用于反汇编 CPU 指令
1.3、编译安装qemu(Simple build and test with KVM):
- qemu-kvm是 QEMU 与 KVM 结合的产物。QEMU 用于模拟硬件设备,而 KVM 提供底层的虚拟化支持,利用硬件虚拟化技术提高虚拟机的性能。qemu-kvm借助硬件虚拟化技术,性能更高。
git clone git://git.qemu-project.org/qemu.git
build with KVM
# Switch to the QEMU root directory
cd qemu
# Configure QEMU for x86_64 only - faster build
./configure --target-list=x86_64-softmmu --enable-debug
# Build in parallel - my system has 4 CPUs
make -j4 //生成的命令位于:/home/qemu/qemu/build
# ./qemu-system-x86_64 -M help
Supported machines are:
pc Standard PC (i440FX + PIIX, 1996) (alias of pc-i440fx-9.2)
pc-i440fx-9.2 Standard PC (i440FX + PIIX, 1996) (default)
......
- 原文没有 make install,make主要负责编译和构建项目,而make install负责将构建好的项目安装到系统中特定的位置;如将库文件复制到系统的库路径中(如 /usr/local/lib),将可执行文件复制到系统的可执行路径:/usr/local/bin/目录下,以便用户可以方便地使用该项目(这里复制了11个可执行程序:elf2dmp qemu-edid qemu-ga qemu-img qemu-io qemu-keymap qemu-nbd qemu-pr-helper qemu-storage-daemon qemu-system-x86_64 qemu-vmsr-helper)。
- “Run QEMU with KVM enabled (w/o VNC)”(无 VNC)和 “Run QEMU with KVM enabled (with VNC)”(有 VNC)的区别
- 图形显示方式
- 没有 VNC(Virtual Network Computing)时,QEMU 可能以文本模式或者在没有图形界面的情况下运行。这意味着用户无法通过图形界面直接观察和交互虚拟机的运行状态。可能更适合在服务器环境中运行,或者在不需要图形界面的自动化任务中使用。
- 启用 VNC 后,用户可以通过 VNC 客户端连接到虚拟机的图形界面,就像远程控制一台计算机一样。这使得用户可以直观地看到虚拟机的运行情况,进行各种操作,如安装软件、配置系统等。适用于需要图形界面进行交互的场景,如开发、测试或者需要可视化操作的任务。
- 远程访问能力
- 无 VNC:远程访问虚拟机可能比较困难,通常需要通过其他方式,如 SSH 连接到虚拟机并执行命令行操作,但无法直接看到图形界面。
- 有 VNC:可以通过 VNC 客户端从任何支持 VNC 的设备远程访问虚拟机的图形界面,提供了更大的灵活性和便利性。
- 复杂性和配置要求
- 无 VNC:配置相对简单,不需要设置 VNC 相关的参数,对于只需要命令行操作或者自动化任务的用户来说更加方便。
- 有 VNC:需要额外配置 VNC 服务器和客户端,包括设置 VNC 密码、端口号等参数。这增加了一定的复杂性,但也提供了更丰富的功能和交互性。
1.4、install a guest OS in a VM之创建QCOW2 格式的虚拟磁盘文件,下载安装ISO镜像。
# Create a disk for the VM
./qemu-img create -f qcow2 test.qcow2 16G
# Download an install ISO
# wget -c https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/38/Server/x86_64/iso/Fedora-Server-dvd-x86_64-38-1.6.iso
- qcow2(QEMU Copy On Write version 2)是一种虚拟机磁盘映像格式,主要用于 QEMU/KVM 虚拟化平台。它具有多个高级功能,使其成为管理虚拟机磁盘映像的理想选择。文件扩展名为.qcow2。qcow2 支持写时拷贝、快照、动态扩展。
- Win解压缩是一款功能强大的文件处理软件,支持71种压缩格式和文件系统格式,包括常见的zip、7z、rar、xz、bz2、gz、qcow2等格式。
1.5、(w/o VNC:without VNC)没有 VNC(Virtual Network Computing)时,QEMU 可能以文本模式或者在没有图形界面的情况下运行。这意味着用户无法通过图形界面直接观察和交互虚拟机的运行状态。
在命令中添加-nographic参数来以非图形化模式运行 QEMU,-enable-kvm启用 KVM
./qemu-system-x86_64 -m 1024 -enable-kvm \
-drive if=virtio,file=test.qcow2,cache=none \
-cdrom Fedora-Server-dvd-x86_64-38-1.6.iso \
-nographic
-nographic
:-nographic 不仅影响对视频输出的处理,还会更改串行和并行端口数据的目的地。
Normally, if QEMU is compiled with graphical window support, it displays output such as guest graphics, guest console, and the QEMU monitor in a window. With this option, you can totally disable graphical output so that QEMU is a simple command line application. The emulated serial port is redirected on the console and muxed with the monitor (unless redirected elsewhere explicitly). Therefore, you can still use QEMU to debug a Linux kernel with a serial console. Use C-a h for help on switching between the console and monitor.
点击“Install Fedora 38”,然后屏幕空白,没反应,应该是默认弹出图形安装界面,但是ssh -X (启用 X11 转发)通过反代理连接到主机不支持图形显示;这里没有字符安装界面。——本地和远程都需要安装 X 服务器和相应的软件:本地机器上需要有一个能够接收和显示图形界面的 X 服务器软件,例如在 Linux 系统中通常默认安装了X.Org服务器。远程服务器上也需要安装相应的图形程序和支持 X11 的库。这里与远程服务器的 X11库没关系,是ssh -X 的问题,见2.2,MobaXterm运行X servver执行gedit图形应用是OK的(gedit在局域网内和内网穿透均OK)。
- 假如提示“Booting from DVD/CD... Boot failed: Could not read from CDROM (code 0003)”,原因是Fedora-Server-dvd-x86_64-38-1.6.iso未下载成功。
- 未加-nographic参数时,提示“gtk initialization failed”;如使用graphic,需安装 GTK 开发包:libgtk-3-dev (gtk3-devel), for a simple UI instead of VNC
- Invocation — QEMU documentation中gtk部分,在 GTK 窗口中显示视频输出。此界面提供下拉菜单和其他用户界面元素,以便在运行时配置和控制虚拟机——Display video output in a GTK window;Start QEMU as a Spice server and launch the default Spice client application;Display video output via curses;Export the display over D-Bus interfaces;Display video output via SDL;Display video output in a Cocoa window
-vnc display[,option[,option[,...]]]
让 QEMU 监听 VNC 显示并在 VNC 会话中重定向 VGA 显示;VNC 客户端中连接进行图形化操作。
- Normally, if QEMU is compiled with graphical window support, it displays output such as guest graphics, guest console, and the QEMU monitor in a window. With this option, you can have QEMU listen on VNC display display and redirect the VGA display over the VNC session. It is very useful to enable the usb tablet device when using this option (option
-device usb-tablet
). When using the VNC display, you must use the-k
parameter to set the keyboard layout if you are not using en-us. Valid syntax for the display is to=L
With this option, QEMU will try next available VNC displays, until the number L, if the originally defined “-vnc display” is not available, e.g. port 5900+display is already used by another application. By default, to=0.host:d
TCP connections will only be allowed from host on display d. By convention the TCP port is 5900+d. Optionally, host can be omitted in which case the server will accept connections from any host.
- Start QEMU as a Spice server and launch the default Spice client application:启动 QEMU 作为 Spice 服务器,使用Spice客户端应用程序连接到QEMU Spice 服务器,例如在 Linux 系统上,connect to a virtual machine using SPICE:apt install virt-viewer。在Windows系统的Spice客户端应用是remote-viewer.exe:https://releases.pagure.org/virt-viewer/virt-viewer-x64-11.0-1.0.msi
- Display video output via curses:
curses
是一个在类 Unix 操作系统上用于创建文本用户界面(TUI)的编程库,许多命令行工具使用curses
来提供更友好的用户界面,例如文本编辑器nano
、终端多路复用器tmux
等 - Display video output via SDL: QEMU 通过 SDL(Simple DirectMedia Layer)显示视频输出,SDL 是一个跨平台的多媒体开发库 apt install libsdl2-dev, SDL 在一个单独的图形窗口中显示视频输出。
- Display video output in a GTK window:使用 QEMU 将视频输出显示在 GTK(GIMP Toolkit)窗口中,例如在 Ubuntu 上可以使用
sudo apt install libgtk-3-dev
安装 GTK 开发库 - Display video output in a Cocoa window:Cocoa 是苹果公司为 macOS 和 iOS 操作系统开发提供的应用程序开发框架。
- -display none 与-nographic的差异:-nographic完全禁用图形输出,以便 QEMU 成为一个简单的命令行应用程序,模拟的串行端口被重定向到控制台上,并与监视器复用(除非明确重定向到其他地方),因此,仍然可以使用 QEMU 通过串行控制台调试 Linux 内核。-display none不显示视频输出。客户机仍会看到一个模拟的图形卡,但它的输出不会显示给 QEMU 用户。此选项与 -nographic 选项的不同之处在于,它仅影响视频输出的处理方式;-nographic 还会更改串行和并行端口数据的目的地。
2、上线1.5节推测,运行 QEMU安装时出现“Install Fedora 38”界面,但是点击“Install Fedora 38”,然后屏幕空白,没反应,这里验证是不是通过反代理连接到主机不支持图形显示导致的。
2.1、不通过反代的话,在局域网内测试,使用支持X server的MobaXterm,但是打不开,忘记了密码,在MobaXterm Xserver and tabbed SSH client - resetmasterpassword (mobatek.net) 下载ResetMasterPassword.exe工具,使用此工具重置主密码,之前保存在 MobaXterm 中的所有连接密码都将丢失。
2.2、使用MobaXterm先运行gedit图形应用是OK的(gedit在局域网内和内网穿透均OK),然后测试运行QEMU安装Fedora-Server,一样屏幕空白,没反应。去掉-nographic参数后验证测试:
- MobaXterm上stop X server,提示“gtk initialization failed”
#qemu-system-x86_64 -m 1024 -enable-kvm -drive if=virtio,file=test.qcow2,cache=none -cdrom Fedora-Server-dvd-x86_64-38-1.6.iso
MoTTY X11 proxy: unable to connect to forwarded X server: Network error: Software caused connection abort
gtk initialization failed
- MobaXterm上running X server,正常(在外网穿透至内网也OK,一是要去掉-nographic参数,二是使用MobaXterm且running X server),弹出图形安装界面:
- 继续选择安装,下图选择 2)Use text mode,正常!
- 安装完Fedora-Server-dvd-x86_64-38-1.6.iso,启动 QEMU 的 x86_64 架构虚拟机的命令:
- enable-kvm:如果宿主机支持 KVM(Kernel-based Virtual Machine)硬件加速,则启用它以提高虚拟机性能。
-drive if=virtio,file=test.qcow2,cache=none
:定义虚拟机的磁盘驱动器,使用 virtio 接口,磁盘映像文件为test.qcow2
,并且不使用缓存(cache=none
)
root@atc:/home/qemu# qemu-system-x86_64 -m 1024 -enable-kvm -drive if=virtio,file=test.qcow2,cache=none