Enable KSM during boot-time on Linux

Kernel Samepage Merging is a
cool feature of the Linux kernel, which you would really want to
have if you are running a virtualized environment, but you would also
benefit from it on a regular system as well.

Having KVM hypervisors aroud I wanted to have KSM enabled, and you can
easily do that by echo’ing 1 to /sys/kernel/mm/ksm/run, but the
downside of this is that it does not work accross reboots.

The other thing is that there is no kernel parameter that you can drop
in /etc/sysctl.conf in order to enable KSM accross reboots. Looking
at the existing init.d scripts on my Debian Wheezy system I didn’t
find any KSM related script neither.

So, what are our possibilities here to make this simply work? Looking
around on the Net I’ve seen a lot of people recommending to use
/etc/rc.local, but I’m not a big fan of /etc/rc.local. In CentOS
there’s a little script there in /etc/init.d/ksm which does the job,
so I’ve decided to write my own version of it for Debian GNU/Linux.

Below you can find the script that I’ve used in order to enable KSM on
my Debian GNU/Linux systems upon reboots: