I've been using some shell scripts to start and stop KVM based virtual machines on my Gentoo Linux box. Maybe someone finds this stuff useful.
$ mkdir -p ~/kvm/{bin,disks,lib,tmp}
%kvm ALL=(ALL) NOPASSWD: YOUR_HOME_DIRECTORY/kvm/bin/taputiltaputil is called by kvmlib.sh right before launching qemu and after stopping a virtual machine. It creates interfaces named brXX and tapXXYY with XX being the XX-nth network card, while YY denotes the number of your VM. For example, if you have two virtual machines each with one network card, and a third one with two network cards, you will end up having these bridges and tap interfaces:
$ brctl show bridge name bridge id STP enabled interfaces br00 8000.00000000000 no bond0 # physical card(s) in your host tap0000 # 1st nic of 1st vm tap0001 # 1st nic of 2nd vm tap0002 # 1st nic of 3rd vm br01 8000.000000000000 no tap0102 # 2nd nic of 3rd vmOn Gentoo put something like this to /etc/conf.d/net:
# bundle ethernet and wireless card and create a failover trunk slaves_bond0="eno2 wlo1" mode_bond0="active-backup" miimon_bond0="500" config_bond0="null" # physically enable ethernet card config_eno2="null" # activate wireless card via wpa_supplicant modules="wpa_supplicant" config_wlo1="null" # bond0 becomes first member of bridge br00 # each first nic of any VM will be connected to br00 resulting in "bridged networking" bridge_br00="bond0" config_br00="192.168.23.42/24" routes_br00="default via 192.168.23.1 initrwnd 32 initcwnd 32" # bridge br01 gets any secondary nic of all VMs, and serves as host-only networking bridge_br01="" config_br01="null" # lower timeout for IPv6 duplicate address detection to 3 seconds dad_timeout=3
$ truncate -s 10G ~/kvm/disks/some.host.name.rawOr:
$ dd if=/dev/zero of=~/kvm/disks/some.host.name.raw bs=1 count=0 seek=10GAlternatively, download my shell script mkdisk, put it into ~/kvm/bin/, make it executable, and call
$ ~/kvm/bin/mkdisk some.host.name 10If you prefer qcow, then you have to use qemu-img:
$ qemu-img create -f qcow2 some.host.name.qcow2 10GIn this case, also add VM_FILE_FORMAT0="qcow2" to shell script of this VM (see next step).
#!/bin/sh . "`dirname "$0"`/../lib/kvmlib.sh"
#!/bin/sh # give this vm 2 instead of 1 cpu VM_CPUS=2 # give this vm 4 instead of 1 gig ram VM_MEM=4096 . "`dirname "$0"`/../lib/kvmlib.sh"
#!/bin/sh VM_FILE_NAME0="iscsi://nas.local/iqn.tgt0/0" . "`dirname "$0"`/../lib/kvmlib.sh"
CONFIG_VFIO_IOMMU_TYPE1=y CONFIG_VFIO_VIRQFD=y CONFIG_VFIO=y CONFIG_VFIO_PCI=y CONFIG_VFIO_PCI_VGA=y CONFIG_VFIO_PCI_MMAP=y CONFIG_VFIO_PCI_INTX=y CONFIG_VFIO_PCI_IGD=y CONFIG_KVM_VFIO=y CONFIG_INTEL_IOMMU=y CONFIG_INTEL_IOMMU_SVM=yPass the iommu commandline option to the kernel, e.g. if you're using the grub bootloader on an Intel based cpu:
menuentry "Linux" { linux /boot/vmlinuz intel_iommu=on }Then, use VM_PCI_PASSTHROUGH to pass PCI device 04:01.0 to the guest (check the output of lspci to get the id of all PCI devices):
#!/bin/sh VM_PCI_PASSTHROUGH=04:01.0" . "`dirname "$0"`/../lib/kvmlib.sh"
menuentry "Linux GPU pass-thru" { linux /boot/vmlinuz intel_iommu=on vfio-pci.ids=10de:1234,10de:1235 vfio-pci.disable_vga=1 }Please note that vfio-pci.ids are vendor and device ids as shown by lspci -n. Some graphics cards also provide an audio card. Most drivers expect all subdevices to be accessible by the VM. Thus, pass any optional sound device to the guest as well.
#!/bin/sh VM_CPUS=2 VM_MEM=8192 VM_FILE_CACHE0="none" VM_FILE_FORMAT0="qcow2" VM_CPU_NOKVM="1" VM_SOUNDHW="intel-hda" VM_VIRTIO_SCSI="1" VM_UEFI="1" VM_VGA="none" VM_USB="1" VM_LOCALTIME="1" VM_PCI_PASSTHROUGH="01:00.0 01:00.1" VM_USB_KEYBOARD="<device name of your USB keyboard>" . "`dirname "$0"`/../lib/kvmlib.sh"In contrast to the kernel, VM_PCI_PASSTHROUGH expects the bus id of each PCI device as seen by the host system.
$ ls /dev/input/by-id usb-Vendor_Inc._Mechanical_Keyboard-event-if02 usb-Vendor_Inc._Mechanical_Keyboard-event-kbd usb-Vendor_Inc._Mechanical_Keyboard-if02-event-kbd usb-Vendor_Corporation_Power_Mouse_2000-event-mouse usb-Vendor_Corporation_Power_Mouse_2000-mouse
VM_USB_KEYBOARD="Vendor_Inc._Mechanical_Keyboard" VM_USB_MOUSE="Vendor_Corporation_Power_Mouse_2000"
$ ~/kvm/bin/0some.host.name.sh startTo stop it:
$ ~/kvm/bin/0some.host.name.sh stopAny additional parameters are directly passed to qemu-kvm:
$ ~/kvm/bin/0some.host.name.sh start -cdrom /data/isos/freebsd.iso
$ ~/kvm/bin/0some.host.name.sh vncOr call vncviewer directly:
$ vncviewer :0
$ telnet localhost 6666 (qemu) change ide1-cd0 /data/isos/netbsd.isoTo eject it:
(qemu) eject ide1-cd0
#!/bin/sh kvm_stop () { local vm vm="$1" [ -z "$vm" ] && return [ ! -f "$vm" ] && return shift kvm_stop "$@" sh "./$vm" "stop" & } myself=`readlink "$0"` if [ -z "$myself" ]; then myself="$0" fi cd "`dirname "$myself"`/../bin" || exit 1 action="$1" case "$action" in start|stop) ;; *) action="${0##*.}" ;; esac case "$action" in start) for vm in *.sh; do [ -x "$vm" ] && "./$vm" "$action" & done ;; stop) kvm_stop *.sh ;; *) echo "usage: $0 <start | stop>" exit 1 ;; esac waitThen symlink the above script as /etc/local.d/kvm.start and /etc/local.d/kvm.stop. That way, any running virtual machine will be shut down when your host system reboots.
SUBSYSTEM=="net",ACTION=="add",KERNEL=="br*",ATTR{bridge/multicast_snooping}="0"and do a udevadm trigger (or reboot your host). Or create /etc/local.d/nobrsnoop.start (don't forget to make it executable):
#!/bin/sh for f in /sys/class/net/*/bridge/multicast_snooping; do [ -f "$f" ] && echo 0 > "$f" doneThen run it once or reboot your host.
C:\Users\dude>\Windows\Microsoft.NET\Framework\<latest .net version>\csc.exe zeroes.csThen, from time to time, simply start zeroes.exe. It will write zeroes (ASCII 0) to a file called _ZEREOS.BIN in the current directory. This give qemu a chance to reclaim disk space.