qemu usage
qemu实操
背景
由于之前惯于使用HyperV/VirtualBox/VMWare界面去做本地虚拟机管理,也使用过ESXi这种专业的服务器软件进行网页远程管理.目前在家办公,配有一台老式笔记本和PC台式机各种系统共存,搞个ESXi太复杂,现场景远程也没有一个好的界面方式去操控,偶尔用vboxmanage命令行操作过。也简单使用过qemu,但是其桥接网络配置有一定复杂度(比如VBOX界面上可以直接配置桥接到局域网路,不会改变原网卡配置,qemu的bridge却要清除原接口配置,转移到bridge网卡接口上来,会导致远程操作中断),现想在本地搞一些嵌入式入门,但是发现常用的qemu不能在wifi接口上做桥接
在多次翻阅资料和尝试下,终于达成初步目的。现编写qemu/linux/busybox简单x86模拟启动bridge/tap通信,以备后期查阅
qemu x86/linux/busybox简单桥接模拟
网上获取linux源码tar包,解压成linux-src目录
**********************!!!PROTECTION POLICY!!!**********************
Waiting For The Next Deployment, Maybe It Would Be Displayed After That.
网上获取busybox源码tar包,解压成busybox-src目录
# busybox-src目录下
make menuconfig
# 启用static binary,在Settings/Build Options下找. 期间处理networking/tc.c报错,移除即可;ncurses头文件找不到,参照https://bbs.archlinux.org/viewtopic.php?id=295859, 修改scripts/kconfig/lxdialog/check-lxdialog.sh check内main返回值(int/void)
# 编译安装到_install目录,以便构建rootfs
make -j$(($(nproc)-1))
make -j$(($(nproc)-1)) install
构建rootfs
cd _install
mkdir etc dev mnt proc sys tmp etc/init.d -p
cat >etc/fstab <<EOF
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
EOF
cat >etc/init.d/rcS <<EOF
echo -e "Welcome to tinyLinux"
/bin/mount -a
echo -e "Remounting the root filesystem"
mount -o remount,rw /
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
EOF
chmod 755 etc/init.d/rcS
cat > etc/inittab <<EOF
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
EOF
chmod 755 etc/inittab
cd dev
sudo mknod console c 5 1
sudo mknod null c 1 3
sudo mknod tty1 c 4 1
cat > etc/resolv.conf <<EOF
nameserver 192.168.0.1
options edns0 trust-ad
search .
EOF
构建rootfs压缩镜像
# busyboxy-src 下
rm -rf rootfs.ext3
rm -rf fs
dd if=/dev/zero of=./rootfs.ext3 bs=1M count=32
mkfs.ext3 rootfs.ext3
mkdir fs
sudo mount -o loop rootfs.ext3 ./fs
sudo cp -rf ./_install/* ./fs
sudo umount ./fs
sudo gzip --best -c rootfs.ext3 > rootfs.img.gz
构建wifi桥接网络
ip link add name br0 type bridge
ip addr add 172.20.0.1/16 dev br0
ip link set br0 up
[[ ! -d /etc/qemu ]] && mkdir /etc/qemu
echo allow br0 > /etc/qemu/bridge.conf
sysctl net.ipv4.ip_forward=1
sysctl net.ipv6.conf.default.forwarding=1
sysctl net.ipv6.conf.all.forwarding=1
iptables -t nat -A POSTROUTING -o wlp3s0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i tap0 -o wlp3s0 -j ACCEPT
启动qemu
sudo qemu-system-x86_64 -kernel ./linux-src/arch/x86/boot/bzImage -initrd ./busybox-src/rootfs.img.gz -net nic -net bridge,br=br0
在qemu console内设置对应网络信息并测试网络
**********************!!!PROTECTION POLICY!!!**********************
Waiting For The Next Deployment, Maybe It Would Be Displayed After That.