目标:构造 *nix 高速虚拟机平台
特点:KVM支持、大页内存、内存预分配、virtio存储设备、桥接网卡(或VFIO直接分配物理网卡)
USE/Gentoo:vhost-net -nls -iscsi -nfs -opengl -jpeg -png -usb -usbredir -curl -selinux -spice -ncurses -sdl -gtk -xen
INT.sh:
#!/bin/bash
qemu-system-x86_64 -enable-kvm \
-machine q35,accel=kvm -device intel-iommu \
-cpu host -smp 4,sockets=1,cores=4,threads=1 \
-m 4096 -mem-path=/var/2Mpages -mem-prealloc \
####################网络设备#############################################################
-netdev tap,ifname=T_0,script=TAP.sh,downscript=no,smb=/home/QemuSMB,vhost=on,id=N_0 \
-device virtio-net-pci,mac=00:11:22:33:44:07,netdev=N_0 \
####################磁盘设备#############################################################
-drive file=RHEL.img,if=none,cache=writeback,media=disk,discard=on,id=RHEL
-device virtio-blk-pci,drive=RHEL \
#########################################################################################
-drive file=RHEL.iso,readonly=on,media=cdrom \
-boot order=cdn \
-display none -monitor stdio
/*基于硬件IOMMU支持的硬件直接分配技术:VFIO*/ #modprobe vfio-pci #echo 8086 8c20 > /sys/bus/pci/drivers/vfio-pci/new_id #echo 0000:00:1b.0 > /sys/bus/pci/devices/0000:00:1b.0/driver/unbind #echo 0000:00:1b.0 > /sys/bus/pci/drivers/vfio-pci/bind /*Qemu设置直连物理网卡示例*/ #-device vfio-pci,host=00:1b.0 #-net none
CONF.sh:
#!/bin/bashIpAddr="10.4.4.4/8"GateWay="10.0.0.1" Switch=br0If_0=eth0If_1=eth1 Mem=4096 #All interfaces‘ IPs have been deleted except "lo". Del_IP() { IFs=$(ip addr | grep -oP ‘\w+(?=:\s+<)‘ | grep -v ‘lo‘) local I for InterFace in $IFs do ip addr flush dev $InterFace done } Set_IP() { if [[ ‘0‘ == $(ip link | grep -c $Swith) ]] then ip link add $switch type bridge fi ip link set $switch up ip link set $If_0 up ip link set $If_0 master $switch ip link set $If_0 up ip link set $If_0 master $switch ip addr add $IpAddr dev $switch ip route replace default via $GateWay dev $switch}
2M_hugepages() { local Num=$(sysctl vm.nr_hugepages) if [[ $Num -lt $Mem ]] thenmkdir -p /var/2Mpagesmount -t hugetlbfs hugetlbfs /var/2Mpages #可在 /etc/fstab 中挂载 sysctl vm.nr_hugepages=$Memfi } (2M_hugepages; del_IP; Set_IP) 2>/dev/null
TAP.sh:
ip link set $1 up sleep 0.5s ip link set $1 master br0