很多时候为了方便自己手动编译和调试虚拟平台,我们需要自己编译qemu等组件并给虚拟机配置网卡等。其中稍微麻烦点的就是配置网卡这块,目前最方便的就是给虚拟机配置一个vhost-net网卡了。
vhost-net网卡的后端默认使用linux的虚拟网桥tap设备,qemu和虚拟机内部使用virtio-net虚拟网卡。
步骤1: 创建linux网桥和tap设备(对于fedora,centos,redhat等默认有创建好的虚拟网卡)
brctl addbr virbr0
brctl stp virbr0 on
ip tuntap add name virbr0-nic mode tap
ip link set dev virbr0-nic up
步骤二:将host网卡添加到virbr0的一个port,并把ip配置给virbr0
brctl addif virbr0 eth0
brctl addif virbr0 virbr0-nic
dhclient virbr0
步骤三:用命令行起一个虚拟机
sudo x86_64-softmmu/qemu-system-x86_64 --enable-kvm -m 5120 -drive file=/home/fang/vm/centos.img,if=virtio -net nic,model=virtio -net tap,ifname=virbr0-nic,script=no -vnc :0
如果使用xml
<interface type='bridge'>
<mac address='fa:16:3e:b6:59:73'/>
<source bridge='br0'/>
<target dev='tap0'/>
<model type='virtio'/>
<driver name='vhost' queues='4'/>
</interface>
如果要起一个 e1000网卡的虚拟机:
x86_64-softmmu/qemu-system-x86_64
--enable-kvm -m 4096
-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x4 -device usb-bt-dongle
/mnt/sdb/fangying/vm/pvc_ubuntu_16.10_64_server.qcow2
-vnc :88 -smp 4 -device e1000,netdev=tapnet -netdev tap,id=tapnet,ifname=virbr0-nic,script=no
如果要从cdrom安装系统
qemu-kvm --enable-kvm -boot d -cdrom CentOS-7-x86_64-Minimal-1611.iso -m 4096 -drive driver=qcow2,file=centos7.qcow2
vga vbe
http://cvs.savannah.nongnu.org/viewvc/*checkout*/vgabios/vgabios/vbe_display_api.txt?revision=1.14
安装win10 虚拟机,配置virtio-scsi磁盘
qemu-kvm -drive if=none,id=hd,file=./win10.qcow2 -device virtio-scsi-pci,id=scsi --enable-kvm -device scsi-hd,drive=hd -cdrom /mnt/os/cn_windows_10_multiple_editions_version_1703_updated_march_2017_x64_dvd_10194190.iso -fda virtio-win-0.1.141_amd64.vfd -boot d -m 8192 -smp 4
/root/nemu/build-x86_64_virt/x86_64_virt-softmmu/qemu-system-x86_64_virt -bios ./OVMF.fd
-machine q35,accel=kvm,kernel_irqchip -cpu host -m 4096,slots=4,maxmem=16950M -smp 4
-device virtio-serial-pci,id=virtio-serial0 -device virtconsole,chardev=charconsole0,id=console0 -chardev stdio,id=charconsole0
-device sysbus-debugcon,iobase=0x402,chardev=debugcon -chardev file,path=/tmp/debug-log,id=debugcon
-device sysbus-debugcon,iobase=0x3f8,chardev=serialcon -chardev file,path=/tmp/serial-log,id=serialcon
-device virtio-blk-pci,drive=disk -drive if=none,id=disk,file=diskvm.img
-device virtio-blk-pci,drive=iso
-drive if=none,id=iso,file=CentOS-7-x86_64-DVD-1708.iso
-netdev user,id=mynet0,hostfwd=tcp::2222-:22,hostname=nemuvm -device virtio-net-pci,netdev=mynet0
https://github.com/tianocore/tianocore.github.io/wiki/How-to-build-OVMF
/home/fang/build-x86_64_virt/x86_64_virt-softmmu/qemu-system-x86_64_virt -bios ./OVMF.fd
-machine virt,accel=kvm,kernel_irqchip
-cpu host
-m 4096,slots=4,maxmem=16950M
-smp 4
-device virtio-serial-pci,id=virtio-serial0
-device virtconsole,chardev=charconsole0,id=console0
-chardev stdio,id=charconsole0
-device sysbus-debugcon,iobase=0x402,chardev=debugcon
-chardev file,path=/tmp/debug-log,id=debugcon
-device sysbus-debugcon,iobase=0x3f8,chardev=serialcon
-chardev file,path=/tmp/serial-log,id=serialcon
-device virtio-blk-pci,drive=disk
-drive if=none,id=disk,file=diskvm.img
-netdev tap,id=tap0,ifname=virbr0-nic,script=no
-device virtio-net-pci,netdev=tap0