Sunday, June 21, 2009

Ok, although these days I'm using KVM more and more exclusively, as I'm convinced it's the "one VM tool to rule them all" I have upgraded my rc.hvm script.

Here 'tis.

#!/bin/sh
# Start/stop/restart kernel hardware virtualization support (hvm)
# This loads modules for VMWare/VirtualBox/KVM one at a time
# current default is KVM
#
# Written for Slackware Linux by Fred Richards (fredr/at/geexology.org)
# TODO: - sanity checking on functions
#

CPU_VENDOR="amd" # or intel

chk_mods() {
if grep -q kvm_amd /proc/modules || grep -q kvm_intel /proc/modules || grep -q kvm /proc/modules ; then
MOD="kvm"
elif grep -q vmmon /proc/modules || grep -q vmnet /proc/modules ; then
MOD="vmware"
elif grep -q vboxdrv /proc/modules || grep -q vboxnetflt /proc/modules ; then
MOD="vbox"
else MOD="none"
fi
}

kvm_unload() {
if grep -q kvm_amd /proc/modules || grep -q kvm_intel /proc/modules || grep -q kvm /proc/modules ; then
/sbin/rmmod kvm-$CPU_VENDOR
/sbin/rmmod kvm
echo "Removed $CPU_VENDOR KVM modules."
fi
}

vmware_unload() {
if grep -q vmmon /proc/modules || grep -q vmnet /proc/modules ; then
/etc/rc.d/hvm/vmware stop
fi
}

vbox_unload() {
if grep -q vboxdrv /proc/modules || grep -q vboxnetflt /proc/modules ; then
/etc/rc.d/hvm/rc.vboxdrv stop
fi
}

kvm_load() {
vbox_unload
vmware_unload
/sbin/modprobe kvm-$CPU_VENDOR
echo "Loaded $CPU_VENDOR KVM modules."
}

vmware_load() {
kvm_unload
vbox_unload
/etc/rc.d/hvm/vmware start
}

vbox_load() {
kvm_unload
vmware_unload
/etc/rc.d/hvm/rc.vboxdrv start
}

stop_all() {
vmware_unload
vbox_unload
kvm_unload

}

status() {
chk_mods
echo "Currently loaded modules: $MOD"
}

case "$1" in
'start')
kvm_load
;;
'kvm')
kvm_load
;;
'vmware')
vmware_load
;;
'vbox')
vbox_load
;;
'stop')
stop_all
;;
'none')
stop_all
;;
'restart')
stop_all
kvm_load
;;
'status')
status
;;
'check')
status
;;
*)
echo "usage $0 start|kvm|vmware|vbox|stop|none|restart|status|check"
esac