QEMU LibVirt & Kernel Shared Memory (KSM)


I’m running IPFire on a APU board with 4 gig of memory. I am using LibVirt to run 8 little virtual servers
for my internal network. All running the same OS.

After already tweaking the memory settings of libvirt, my memory usage is about 80%.

I stumbled on KSM this afternoon and enabled it with:

echo 1 > /sys/kernel/mm/ksm/run

Check if it was running with:

grep . /sys/kernel/mm/ksm/*

To my surprise the memory uses dropped to 60%. Which is significant. An 20% decrease of memory, on 4gb. It just freed up 800 megs of memory. The small downsite it increases the cpu usage with about 6%. From an average 11% to 17%.

Now I have one little question: How do I make this persistent between reboots. In which file do I add
the echo 1 > /sys/kernel/mm/ksm/run command?

I am not familiar with init.d and I do not want to reinstall ipfire again… with me fooling around.

Can I make a file in /etc/init.d/

with the following content, and will that work? (I copy/paste & change it from the other files there)

. /etc/sysconfig/rc
. $rc_functions

case $1 in
		boot_mesg "Starting KSM..."
		echo 1 > /sys/kernel/mm/ksm/run


		boot_mesg "Stopping KSM..."
		echo 0 > /sys/kernel/mm/ksm/run

		grep . /sys/kernel/mm/ksm/*

		echo "Usage: $0 {start|stop|status}"
		exit 1


If the above file is called ksm and located in /etc/init.d and is executable… This did the trick…

cd /etc/rc.d/rc3.d/
ln -s ../init.d/ksm S02ksm