# qemu-kvm

description "KVM"
author "Dustin Kirkland <kirkland@canonical.com>"

start on runlevel [2345]

pre-start script
	# Silently exit if the package isn't installed anymore
	if [ ! -e /usr/bin/kvm ]; then
		exit 0
	fi
	# Load the appropriate module, respecting blacklists
	if grep -qs "^flags.* vmx" /proc/cpuinfo; then
		modprobe -b kvm_intel
	elif grep -qs "^flags.* svm" /proc/cpuinfo; then
		modprobe -b kvm_amd
	fi
	# Enable KSM, respecting the default configuration file
	[ -r /etc/default/qemu-kvm ] && . /etc/default/qemu-kvm
	if [ "$KSM_ENABLED" = "1" ]; then
		[ -w /sys/kernel/mm/ksm/run ] && echo 1 > /sys/kernel/mm/ksm/run
		if [ -w /sys/kernel/mm/ksm/sleep_millisecs ]; then
			if [ -n "$SLEEP_MILLISECS" ]; then
				echo "$SLEEP_MILLISECS" > /sys/kernel/mm/ksm/sleep_millisecs
			fi
		fi
	else
		[ -w /sys/kernel/mm/ksm/run ] && echo 0 > /sys/kernel/mm/ksm/run
	fi
end script

post-stop script
	# Unload the module
	if grep -qs "^flags.* vmx" /proc/cpuinfo; then
		modprobe -r kvm_intel
	elif grep -qs "^flags.* svm" /proc/cpuinfo; then
		modprobe -r kvm_amd
	fi
end script
