1

I would like to automatically mount a usb disk when I plug it.

I believed udev is the solution, so I created /etc/udev/rules.d/99_extern_hdd.rules that only contains:

ACTION=="add", SUBSYSTEM=="block", ENV{ID_VENDOR_ID}=="1058", ENV{ID_MODEL_ID}=="25a2", RUN+="/etc/mount_extern.sh" 

The script /etc/mount_extern.sh contains:

#!/bin/bash ls -dl /media > /tmp/output ls -dl /media/data >> /tmp/output echo "mount ${DEVNAME} /media/data" >> /tmp/output mount ${DEVNAME} /media/data >> /tmp/output 2>&1 

But when I plug my disk the output ( /tmp/output created by /etc/mount_extern.sh) gives me:

drwxr-xr-x 13 root root 4096 Aug 14 10:47 /media drwxr-xr-x 2 root root 4096 Dec 18 2017 /media/data mount /dev/sde1 /media/data mount: /media/data: permission denied. 

I don't understand why I have this permission denied. The rights of the folder seem good no? also when I do sudo mount /dev/sde1 /media/data, it works fine

Any idea?

My OS is Linux archlinux 5.8.10-arch1-1 #1 SMP PREEMPT Thu, 17 Sep 2020 18:01:06 +0000 x86_64 GNU/Linux

1 Answer 1

4

You can't run mount from UDev rule. From UDev manpage:

Note that running programs that access the network or mount/unmount filesystems is not allowed inside of udev rules, due to the default sandbox that is enforced on systemd-udevd.service.

You can use systemd-mount or other tools, see https://wiki.archlinux.org/index.php/Udev#Mounting_drives_in_rules

2
  • Any idea on when this change happened ? A script I wrote in 2016 was able to use mount no problem. Commented Jun 20, 2024 at 1:52
  • 1
    Found out, since about June 22 2018 (or version 239). See: github.com/systemd/systemd/blob/… Commented Jun 21, 2024 at 17:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.