环境
Ubuntu 22.04.1 LTS
kernel version: 5.15.0-60-generic
qemu version: v7.0.0-rc0
安装步骤
git clone https://github.com/qemu/qemu.git
# 我用的版本是v7.0.0-rc0
git checkout v7.0.0-rc0
# 开启virtfs方便后续切换vm的kernel版本
./configure --enable-kvm --enable-virtfs --prefix=/想安装的路径
# 安装依赖
apt install pkg-config
apt install libglib2.0-dev
apt install libpixman-1-dev
# 编译安装
make -j $(nproc)
make install
创建镜像并安装系统
# 创建镜像
qemu-img create -f qcow2 ubuntu22.04.img 16G
# 安装系统
qemu-system-x86_64 -m 1024 -enable-kvm \
-drive if=virtio,file=ubuntu22.04.img,cache=none \
-cdrom ubuntu-22.04-live-server-amd64.iso \
-vnc :1
接下来打开vnc,ip是host的ip,端口号为1,然后跟正常安装linux系统一样,vnc可以随便下一个,我用的是vnc viewer
File->New Connection,或者直接在红框的位置填上
hostIP地址: 1
注意:
- 冒号是英文的,不要少了空格,否则会登不上
- 安装ubuntu系统记得安装openssh,默认是没有勾选的。
登录VM
qemu-system-x86_64 -m 2048 \
-drive file=ubuntu22.04.img,if=virtio \
-nic user,hostfwd=tcp::5557-:22 \
-vnc :1 -monitor stdio
# vnc可以查看开机情况,ssh可以登录VM与host主机共享网络
ssh -p 5557 username@localhost
# 往VM主机发送文件
scp -P 5557 filename username@localhost:/directory
# sharefolder方案,需要qemu安装的时候打开--enable-virtfs
# host端启动qemu
qemu-system-x86_64 -m 2048 \
-drive file=ubuntu22.04.img,if=virtio \
-nic user,hostfwd=tcp::5557-:22 \
-fsdev local,security_model=passthrough,id=fsdev0,path=/host_directory \
-device virtio-9p-pci,fsdev=fsdev0,mount_tag=kernelcode \
-vnc :1 -monitor stdio
# 共享文件夹靠id和tag标识,可以加载多个
# VM启动后加载共享文件夹
mount -t 9p -o trans=virtio kernelcode /vm_directory