在QEMU中运行debian
我在 LFS(Linux from scratch) 的构建目标之一就是 BLFS(Beyond Linux from scratch)虚拟化 ,目标是用最精简的系统来运行虚拟化集群。不借助复杂的包装工具(例如 Libvirt虚拟机管理器 ),仅使用 QEMU 来实现虚拟化:
更为深入理解Linux虚拟化的底层技术
使用有限的组件运行起全功能的Linux虚拟化
为后续构建更为复杂的 BLFS(Beyond Linux from scratch) Kubernetes 提供基础
备注
本文qemu运行没有支持uefi,所以磁盘分区是传统方式。要支持 IOMMU 之后才能支持GPU passthrough,也就是需要实现 在QEMU中运行GPU passthrough的Debian
准备
创建镜像虚拟磁盘:
创建
qcow2
格式磁盘镜像qemu-img create -f qcow2 /sources/images/debian.qcow 6G
# 输出信息:
# Formatting '/sources/images/debian.qcow', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=6442450944 lazy_refcounts=off refcount_bits=16
# 在没有使用时占用磁盘很少,动态扩展
# -rw-r--r-- 1 root root 193K Feb 5 17:24 debian.qcow
启动虚拟机
qemu运行参数
-vga std
,则启动 VNC :
执行标准vga安装,显示VNC
qemu-system-x86_64 \
-nodefaults \
-enable-kvm \
-cpu host,kvm=off \
-m 8G \
-smp cores=4 \
-boot order=c,once=d,menu=on \
-drive file=/sources/images/debian.qcow,if=virtio \
-cdrom /sources/debian-cd_current_amd64_iso-dvd_debian-12.9.0-amd64-DVD-1.iso \
-net nic,model=virtio,macaddr=52:54:00:00:00:01 -net bridge,br=br0 \
-vga std \
-vnc :0 \
-serial mon:stdio \
-name "debian"
# 默认终端提示
# VNC server running on 127.0.0.1:5900
# 如果需要VNC监听所有网络接口,则添加参数 -vnc :0 ,此时终端就看不到提示,但是使用VNC客户端可以连接
我的 BLFS QEMU 编译只采用最基本库完成,但是我发现依然能够支持VNC启动。上述命令会立即将屏幕输出到VNC界面,此时可以使用VNC客户端连接以后,选择图形安装
采用
console
字符模式安装虚拟机
执行安装
qemu-system-x86_64 \
-nodefaults \
-enable-kvm \
-cpu host,kvm=off \
-m 8G \
-smp cores=4 \
-boot order=c,once=d,menu=on \
-drive file=/sources/images/debian.qcow,if=virtio \
-cdrom /sources/debian-cd_current_amd64_iso-dvd_debian-12.9.0-amd64-DVD-1.iso \
-net nic,model=virtio,macaddr=52:54:00:00:00:01 -net bridge,br=br0 \
-nographic \
-vga none \
-serial mon:stdio \
-name "debian"
这里遇到一个问题,在上述运行配置中我取消了图形界面,想从串口控制台安装(也就是选择安装菜单中 Install
),显示如下:

提示我选择 video mode
,但是我回车没有响应: 我在安装完成之后,执行 Debian虚拟机精简系统初始化 串口控制台配置之后,就能够在字符终端正常访问。
完成安装以后,则修订为如下命令运行虚拟机:
qemu运行debian系统
qemu-system-x86_64 \
-nodefaults \
-enable-kvm \
-cpu host,kvm=off \
-m 8G \
-smp cores=4 \
-drive file=/sources/images/debian.qcow,if=virtio \
-net nic,model=virtio,macaddr=52:54:00:00:00:01 -net bridge,br=br0 \
-vga std \
-vnc :0 \
-serial mon:stdio \
-name "debian"
# 默认终端提示
# VNC server running on 127.0.0.1:5900
# 如果需要VNC监听所有网络接口,则添加参数 -vnc :0 ,此时终端就看不到提示,但是使用VNC客户端可以连接
Debian虚拟机精简系统初始化 调整虚拟机