There is a headless machine, with Debian as its OS:
$ cat /etc/issue
Debian GNU/Linux 11 \n \l
$ uname -a
Linux mymachine 6.1.99 #33 SMP Tue Jan 21 11:32:39 CST 2025 aarch64 GNU/Linux
What I wish to achieve is to mount an SD card automatically onto /media/[SD_CARD_LABEL] upon insertion.
Again, this is a headless machine:
- does not have monitor
- does not have keyboard
- does not have GUI
- does have a few buttons on GPIO
For this task I wish to use (if there are no better alternatives) udisks2 and udiskie.
Udiskie's polkit rule:
$ cat /etc/polkit-1/rules.d/50-udiskie.rules
polkit.addRule(function(action, subject) {
var YES = polkit.Result.YES;
var permission = {
// required for udisks1:
"org.freedesktop.udisks.filesystem-mount": YES,
"org.freedesktop.udisks.luks-unlock": YES,
"org.freedesktop.udisks.drive-eject": YES,
"org.freedesktop.udisks.drive-detach": YES,
// required for udisks2:
"org.freedesktop.udisks2.filesystem-mount": YES,
"org.freedesktop.udisks2.encrypted-unlock": YES,
"org.freedesktop.udisks2.eject-media": YES,
"org.freedesktop.udisks2.power-off-drive": YES,
// required for udisks2 if using udiskie from another seat (e.g. systemd):
"org.freedesktop.udisks2.filesystem-mount-other-seat": YES,
"org.freedesktop.udisks2.filesystem-unmount-others": YES,
"org.freedesktop.udisks2.encrypted-unlock-other-seat": YES,
"org.freedesktop.udisks2.encrypted-unlock-system": YES,
"org.freedesktop.udisks2.eject-media-other-seat": YES,
"org.freedesktop.udisks2.power-off-drive-other-seat": YES
};
if (subject.isInGroup("plugdev")) {
return permission[action.id];
}
});
I wish testuser user to be set as owner and group for the mount. Therefore I have to make sure testuser is in group plugdev:
groups testuser
testuser : testuser plugdev
This is the config of udiskie:
$ cat /etc/systemd/system/udiskie.service
[Unit]
Description=udiskie mounts drives when plugged in
[Service]
#User=testuser
#Group=testuser
ExecStart=/usr/bin/udiskie -a -N -T -F
[Install]
WantedBy=default.target
I also added an udev rule to mount the SD into /media/, instead of default /media/user/:
$ cat /etc/udev/rules.d/82-udisks2-sd.rules
# This file contains udev rules for udisks 2.x
# ------------------------------------------------------------------------
# rules for external SD
KERNEL=="mmcblk0p*", ENV{UDISKS_FILESYSTEM_SHARED}="1"
So now, when I insert an SD card and watching the journal log of udiskie:
Nov 18 09:10:51 mymachine udiskie[1461]: mounted /org/freedesktop/UDisks2/block_devices/mmcblk0p1 on /media/sdcard
That's great however it's mounted as root:
$ ls -all /media/
total 144
drwxr-xr-x 1 pi pi 4096 Nov 18 09:11 .
drwxr-xr-x 1 root root 4096 Aug 4 2017 ..
drwxr-xr-x 4 root root 4096 Nov 7 19:03 sdcard
I tried removing the comments from the systemd script:
[Service]
User=testuser
Group=testuser
ExecStart=/usr/bin/udiskie -a -N -T -F
However in this case I get:
Nov 18 09:12:42 mymachine udiskie[1596]: failed to mount /org/freedesktop/UDisks2/block_devices/mmcblk0p1: GDBus.Error:org.freedesktop.UDisks2.Error.NotAuthorizedCanObtain: Not authorized to perform operation
I also tried to move udiskie systemd config from system to user, but then I got error immediately after start:
$ systemctl --user status udiskie
● udiskie.service - udiskie mounts drives when plugged in
Loaded: loaded (/etc/xdg/systemd/user/udiskie.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2025-11-18 08:53:43 CET; 51s ago
Process: 1009 ExecStart=/usr/bin/udiskie -a -N -T -F (code=exited, status=216/GROUP)
Main PID: 1009 (code=exited, status=216/GROUP)
CPU: 0
Nov 18 08:53:43 mymachine systemd[1009]: udiskie.service: Failed at step GROUP spawning /usr/bin/udiskie: Operation not permitted
I do not want post-mount shell scripts. I wish to solve this by systemd, udev, polkit-1, and other configs.