Linux虚拟化之qemu的编译及使用

依赖安装

1
2
3
4
apt install pkg-config
apt install libglib2.0-dev
apt install libpixman-1-dev
apt install bison flex

安装构建工具

1
2
3
4
# 需要更新python>=3.7
apt-get upgrade python3
apt install ninja-build
pip3 install meson==0.63

编译安装util-linux-2.39

1
2
3
4
5
6
7
8
9
10
apt-get install libtool-bin
apt-get install autopoint

wget https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.39/util-linux-2.39.tar.gz
tar -xvzf util-linux-2.39.tar.gz
cd util-linux-2.39
./autogen.sh
./configure --prefix=/usr/local # --disable-blkid
make
make install

编译安装libslirp

1
2
3
4
5
6
7
# 手动编译,直接`apt-get install libslirp-dev`安装没有.a静态文件
# wget https://gitlab.freedesktop.org/slirp/libslirp/-/archive/v4.7.0/libslirp-v4.7.0.tar.gz
# tar -xvzf libslirp-v4.7.0.tar.gz
git clone https://gitlab.freedesktop.org/slirp/libslirp.git
cd libslirp
meson build
ninja -C build install

静态编译qemu

1
2
3
4
5
wget https://download.qemu.org/qemu-8.1.0.tar.xz
tar xvJf qemu-8.1.0.tar.xz
cd qemu-8.1.0
./configure --target-list=x86_64-softmmu --static --enable-slirp
make

打包取出

1
2
3
4
mkdir qemu-bins
find build -name "qemu-*" -type f -perm -111 -exec cp {} ./qemu-bins/ \;
cd qemu-bins
tar -cvzf qemu-8.1-static.tgz ./*

qemu使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 放开防火墙规则
iptables -I INPUT -p tcp --dport 5900 -j ACCEPT
iptables -I INPUT -p udp --dport 5900 -j ACCEPT
iptables -I INPUT -p tcp --dport 22345 -j ACCEPT

# 1. 创建磁盘
qemu-img create -f qcow2 centos.img 10G

# 2. 安装系统
qemu-system-x86_64 -L {path/of/bios-256k.bin} -m 2048 -enable-kvm centos.img -cdrom ./CentOS-7-x86_64-Minimal-2009.iso -vnc {hostip}:0

# 3. ssh端口转发
qemu-system-x86_64 -L {path/of/bios-256k.bin} -m 2048 -enable-kvm centos.img -net nic -net user,hostfwd=::22345-:22 -vnc {hostip}:0

# 4. 远程连接
ssh -p 22345 root@{hostip}

qemu参数

    1. vhost_user:
      1
      2
      3
      4
      5
      6
      7
      8
      ./qemu-system-x86_64 -L /usr/share/kvm/ -m 2048 -enable-kvm centos.img \
      -net nic -net user,hostfwd=::22345-:22 \
      -vnc 0.0.0.0:8100,websocket=14600,to=16000 \
      -chardev socket,id=chr0,path=/run/vhost_user_1234567890_net_1,reconnect=1 \
      -netdev type=vhost-user,id=net0,chardev=chr0,vhostforce=on,queues=4 \
      -device virtio-net-pci,host_tso4=on,host_tso6=on,romfile=,mac=FE:FC:FE:3A:11:22,netdev=net0,bus=pci.0,addr=0x12,id=net0,vectors=9 \
      -object memory-backend-file,id=mem-node0,size=2048M,mem-path=/qemu_vhost_user,share=on \
      -numa node,nodeid=0,memdev=mem-node0,cpus=0
    1. vhost_net:
      1
      2
      3
      4
      ./qemu-system-x86_64 -L /usr/share/kvm/ -m 2048 -enable-kvm alpine.img -nographic \
      -net nic -net user,hostfwd=::22345-:22 -vnc 0.0.0.0:8100,websocket=14600,to=16000 \
      -netdev type=tap,id=net0,ifname=virt0,vhost=on,script=no,downscript=no,queues=2 \
      -device virtio-net-pci,host_tso4=on,host_tso6=on,romfile=,mac=FE:FC:FE:3A:11:22,netdev=net0,bus=pci.0,addr=0x12,id=net0
    1. 在qemu的启动参数里加入参数, 在外部环境中使用 telnet localhost 4444 登录
      1
      -chardev socket,id=mon0,host=localhost,port=4444,server,nowait -mon chardev=mon0,mode=readline

github仓库