Categories
Linux

use mount in udev rule with systemd-udevd

If you write your own scripts called by udevd, for example after plugin a specific USB stick, you maybe want to mount a partition, which could fail. I got often return code 32 from mount, which means “mount failure” but without any additional error message.

This happens, because systemd-udevd is started in its own namespace for mounting. So the filesystem of udevd differs from the main system. You should be aware, that running udevd in an own namespace increases security. But if you want to disable this to run your scripts, do the following steps.

First verify the current MountFlags settings of udevd service. It should be “slave”.

systemctl show systemd-udevd | grep -F MountFlags

Now edit the udevd service

systemctl edit systemd-udevd

and set MountFlags to shared:

[Service]
MountFlags=shared

At last you just have to reload systemd settings and restart the udevd daemon:

systemctl daemon-reload
systemctl restart systemd-udevd

You then also can verify that udevd didn’t use the namespace anymore and list all current mount namespaces in use:

lsns -t mnt
Categories
Linux

output systemd daemon to console

Per default setting all services log only to journal and not to console. You can check the settings of a service with:

sudo systemctl show MyService.service | grep -Fi Standard

And you should get something like.

StandardInput=null
StandardInputData=
StandardOutput=journal
StandardError=inherit

If you want to change it, you have to set StandardOutput to journal+console.

The full options are documented in the man pages. You can look into man page of systemd.directives:

man systemd.directives

Search for StandardOutput and you will find a reference to the man page of systemd.exec. There you will find every option of this directive.

To change it you should call:

sudo systemctl edit MyService.service

and then add or change the line of StandardOutput to

[Service]
StandardOutput=journal+console

After all you should reload systemd configuration:

sudo systemctl daemon-reload
Categories
Linux

inspect a systemd service

To inspect a systemd service, you need some handy commands.

Examine service names

First you have to get the correct service name you want to inspect.

systemctl list-units --type=service

You can use grep to filter the list.

Gather information of service

You can show the current status of a service with:

systemctl status servicename

Then you can display all options that describe the service:

systemctl show servicename

And you can show all files which belongs to the service description:

systemctl cat servicename
Categories
Linux

disable shutdown, reboot or suspend temporarily in systemd

Sometimes you have some long running uninterruptible processes or tasks, which should not be terminated or killed under normal circumstances. For this problem systemd provides a simple solution to prevent the shutdown or reboot of a system

You just have to use systemd-inhibit for starting your process. As long as your process is running, systemd-inihibt will delay or block a shutdown or reboot task. The following operations can be inhibited:

  • shutdown
  • sleep
  • idle
  • handle-power-key
  • handle-suspend-key
  • handle-hibernate-key
  • handle-lid-switch

In default settings you would inhibit idle, sleep and shutdown.

You can view all current inhibits with:

systemd-inhibit --list

To block a shutdown while you are burning a CD, you would run:

systemd-inhibit wodim foobar.iso

You also can inhibit a specific behavior by time using the sleep command. To disable automatic sleep mode for one day, when you close the lid of your laptop, you would run:

systemd-inhibit --what=handle-lid-switch sleep 1d