Changing system unit dependencies from Grub’s kernel command line

Can you do this, sysv-init?

George Shuklin
OpsOps
2 min readSep 2, 2021

--

Today we discussed about special ‘debug mode’ for our deployment system. When we debug and develop new OS images we may want to interrupt a normal deployment sequence and to be able to log into system before any changes were applied.

We discussed some special flag in boot parameters for our scripts, but, turned out, systemd already has a generic mechanism, universal enough to implement everything we need.

Systemd peeks at the kernel command line (/proc/cmdline, the kernel parameters from boot loader, like Grub), and it understood, in particular, those two:

  • systemd.mask=(servicename)
  • systemd.wants=(servicename)

Those two is enough to disable things we don’t want to have, and to enable things we want to have.

Toy example

I got a VM with docker enabled (systemd enable docker) and atop disabled (I disabled it specifically for this test (systemd disable atop).

After a normal boot my server has

  • docker running
  • atop not running

Rebooting and editing the kernel command line:

I added to the end of the command line: systemd.mask=docker systemd.wants=atop

After boot I have docker disabled and atop running.

Exactly as I want.

Second reboot (without changing boot options) returns back to the original (system) configuration.

Mask option is amazing, because it overrides even things like socket activation.

Basically our task for debug service collapsed to a simple set of installed (but not enabled) debug ssh service, and a simplest edit to the kernel command line.

--

--

George Shuklin
OpsOps

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.