About

KSM can be used to make virtual machines share memory pools and has to be enabled individually per virtual machine. The init script below can be dropped into /etc/init.d/ and made to run at system bootup on Debian with:

update-rc.d ksm defaults

Code

#!/bin/sh
#
# Author:             Marin Atanasov Nikolov <dnaeon@gmail.com>
#
### BEGIN INIT INFO
# Provides:          ksm
# Required-Start:    
# Required-Stop:
# X-Start-Before:    
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6 
# Short-Description: Enable and disable KVM KSM
# Description:       Enables and disables the KVM  Kernel Samepage Merging
#                     feature of the kernel
### END INIT INFO
 
set -e
 
. /lib/lsb/init-functions
 
do_start() {
    echo 1 > /sys/kernel/mm/ksm/run
    log_success_msg "Enabling Kernel Samepage Merging"
}
 
do_stop() {
    echo 0 > /sys/kernel/mm/ksm/run
    log_success_msg "Disabling Kernel Samepage Merging"
}
 
do_status() {
    local ksm_status
    ksm_status=$( cat /sys/kernel/mm/ksm/run )
 
    if [ ${ksm_status} -eq 1 ]; then
        log_success_msg "Kernel Samepage Merging is enabled"
    else
        log_success_msg "Kernel Samepage Merging is disabled"
    fi
}
 
case "${1}" in
    start)
        do_start
        ;;
    reset|stop)
        do_stop
        ;;
    status)
        do_status
        ;;
    reload|restart|force-reload)
        do_stop
        do_start
        ;;
    *)
        log_success_msg "usage: ${0} {start|stop|status|reload|restart|force-reload|reset}" >&2
        ;;
esac