CLI Magic: Sharing more with sysctl

88

Author: Joe Barr

You lily-livered GUI lizards who get chills at the thought of entering commands at the CLI might want to call your mommy over to sit with you and hold your hand this week, because we’re going to become one with the kernel. We’re going to take control with sysctl. Enough talk. Let’s bust open a console.Let’s start with man sysctl instead of saving it for the last, as we usually do. Why? Because I just used sysctl for the first time today and I’m not exactly what you would call an expert on the subject. Man tells us that by using sysctl we can “configure kernel parameters at runtime.”

That’s really cool, because I had need to do that recently. When I was installing an application, I got errors that told me it couldn’t find sufficient shared memory. The application gave me an out. I could change the default configuration and reduce the number of buffers used to store video images, and that would reduce the amount of shared memory required. Naturally, I took the easy way out and did it through the application rather than learn how to do it the correct way: increasing the amount of shared memory available.

I started asking around as to how you could do that, and Brian Jones — the no-goodski sysadmin who writes the weekly SysAdmin-to-SysAdmin column here on Linux.com — pointed me towards the sysctl command. So I asked the man what he thought of sysctl, and sure enough, it looked like the right tool for the job.

Shared memory, if you don’t know, has nothing at all to do with Jung’s theory of a “collective unconscious” or Joseph Campbell’s notions about myth and symbols. It’s simply a way to allow multiple programs to share the same areas of memory. Normally, the operating system stands guard over memory to make sure one program doesn’t get into another’s space. But when you need to share an area in memory in order for programs to work together in harmony, it’s a good thing.

In fact, in an article for LinuxDevices.com, IBM’s Sachin Agrawal wrote “shared memory is probably the most efficient inter-process communication channel provided by all modern operating systems.”

I read through the man pages for sysctl a time or two. After learning that you could use sysctl to write new values for a long list of kernel, I decided to query sysctl for what it knew about tunable parameters. Here’s how the conversation went. I asked the question by entering:


>sysctl -a

sysctl replied with a long list of keywords and values, including these:


kernel.mmap-hugepages-min-mapping = 256
kernel.mmap-use-hugepages = 0
kernel.shm-use-hugepages = 0
kernel.HZ = 1000
kernel.min-timeslice = 2000
kernel.max-timeslice = 60000
kernel.ngroups_max = 65536
kernel.printk_ratelimit_burst = 10
kernel.printk_ratelimit = 5
kernel.panic_on_oops = 0
kernel.pid_max = 32768
kernel.overflowgid = 65534
kernel.overflowuid = 65534
kernel.check_deadlocks = 1
kernel.pty.nr = 6
kernel.pty.max = 4096
kernel.random.uuid = fff5d011-e729-43e2-9922-91fcaf43788b
kernel.random.boot_id = e25df4a7-d5e3-443e-9597-f828815770bd
kernel.random.write_wakeup_threshold = 128
kernel.random.read_wakeup_threshold = 64
kernel.random.entropy_avail = 4096
kernel.random.poolsize = 512
kernel.threads-max = 8175
kernel.cad_pid = 1
kernel.sysrq = 0
kernel.sem = 250 32000 32 128
kernel.msgmnb = 16384
kernel.msgmni = 16
kernel.msgmax = 8192
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.shmmax = 33554432
kernel.acct = 4 2 30
kernel.hotplug = /sbin/hotplug
kernel.modprobe = /sbin/modprobe
kernel.printk = 1 4 1 7
kernel.ctrl-alt-del = 0
kernel.real-root-dev = 769
kernel.cap-bound = -257
kernel.tainted = 17
kernel.core_pattern = core
kernel.core_uses_pid = 0
kernel.panic = 0
kernel.domainname =
kernel.hostname = linux
kernel.version = #1 Wed Aug 25 13:34:40 UTC 2004
kernel.osrelease = 2.6.5-7.108-default
kernel.ostype = Linux

The default number of image buffers in Philip Coombes’ great Linux home security camera app ZoneMinder is 100. I had to cut that number to 50 to get it working with one camera, and cut it again to 25 to get it working with two. I decided to increase the total (kernel.shmmax) shared memory allowed to see if I could run ZoneMinder with larger buffers.

To increase the allotment, I simply typed:


sysctl -w kernel.shmmax=63554432

After resetting the ZoneMinder option for the number of buffers, I restarted the app, and lo and behold, it worked without error. Not only that, it worked better. The images moved more crisply and the program issued alarms for movement that it had missed before.

Not only can you modify kernel parameters one at a time as I did, you can save parameter values in a file (like /etc/sysctl.conf) and load them all at once by pointing sysctl at the file with the -p option.

sysctl is not a command you’re apt to use regularly (unless you’re a sysadmin like that Brian Jones fella), but it sure is handy to have around when you need it. And it’s yet one more example of how a little CLI magic can make life in the GUI a little nicer.