qemu网络
主要参考 https://wiki.qemu.org/Documentation/Networking
qemu网络分为后端和前端。
backends是在qemu里设置,如下面的-netdev user
, 指定了user类型的backends, 并且讲本地的5555端口和guest上22绑定,如此可以使用ssh root@localhost -p 5555
来登录guest.
另外尝试过自编译的qemu-system-aarch64启动,报错qemu-system-aarch64: -netdev user,id=net0,hostfwd=tcp::5555-:22: network backend 'user' is not compiled into this binary
,也就是后端支持需要qemu里源码修改。
sudo qemu-system-aarch64 -m 1024 -cpu cortex-a57 -M virt -nographic -smp 4 -kernel ~/software/linux/arch/arm64/boot/Image -append "noinintrd sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M loglevel=8" -drive if=none,file=linux_rootfs.ext4,id=hd0 -device virtio-blk-device,drive=hd0 -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22
User Networking is implemented using "slirp", which provides a full TCP/IP stack within QEMU and uses that stack to implement a virtual NAT'd network.
user网络类型是通过slirp实现,它在qemu内部提供了完整的TCP/IP栈来实现一个虚拟的网络设备
Note that from inside the guest, connecting to a port on the "gateway" IP address will connect to that port on the host; so for instance "ssh 10.0.2.2" will ssh from the guest to the host.
用户网络默认配置情况如上图,通过对gateway的IP访问,可以访问host机器。
User networking默认配置后,可以访问网页,apt-get都可以工作,但ping不行(即使有ping工具也不行)。因为只仿真了TCP/IP, 没有仿真ICMP.
qemu中的文件系统映射到真实的linux_rootfs.exe4这个磁盘文件,在qemu里面修改文件退出qemu,文件的内容可以固化到磁盘的linux_rootfs.exe4中,这点和docker启动后的tmpfs不一样。
qemu执行安装程序
qemu -cdrom类似在系统中插入了一个安装磁盘,其中的mini.iso可以,-vnc在qemu中启动一个vnc server,client连接之后可以完成系统安装。
2024-06-30$ qemu-system-x86_64 -netdev user,id=net0,hostfwd=tcp::5900-:5900 -device e1000,netdev=net0 -m 1024 -smp 4 -hda ubuntu.img -cdrom mini.iso -serial mon:stdio -vnc 0.0.0.0:0
安装完之后再启动就直接从硬盘读取boot,而不用再指定kernel+rootfs的形式。ubuntu.img中除了我们开机后看到的文件系统,还有kernel的内容。
qemu-system-x86_64 -m 1024 -smp 4 -hda ubuntu.img -serial mon:stdio -vnc 0.0.0.0:0
qemu 9pfs 共享文件
使用9pfs进行mount, 其中linux构建内核默认9p选项已经开启。
#host上
sudo virtfs-proxy-helper -p $PWD/test2 -s /tmp/vhostqemu -u zwl -g zwl
sudo qemu-system-aarch64 -m 1024 -cpu cortex-a57 -M virt -nographic -smp 4 \
-kernel ~/software/linux/arch/arm64/boot/Image -append "noinintrd sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M loglevel=8" -drive if=none,file=linux_rootfs.ext4,id=hd0 -device virtio-blk-device,drive=hd0 \
-virtfs local,path=/home/zwl/work/daily/2024-07-02,mount_tag=myfs,security_model=mapped-xattr
# 启动的guest上
sudo mount -t 9p -o trans=virtio myfs daily/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律