2

My task is to have a custom mount point and for the mount itself to happen automatically. that is, the device is connected, a folder with its name is created in some place (/mnt/usb/{name}) and the data is mounted there. when the device is disconnected, the folder is unmounted and deleted

Wrote a udev rule

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[b-z][0-9]", ENV{ID_BUS}=="usb", \
ENV{ID_TYPE}=="disk", ENV{ID_FS_USAGE}=="filesystem", \
RUN+="/bin/mount -w -o umask=0000,uid=0,gid=0 /dev/%k /mnt/usb_drives"

ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd[b-z][0-9]", ENV{ID_BUS}=="usb", \
ENV{ID_TYPE}=="disk", ENV{ID_FS_USAGE}=="filesystem", RUN+="/bin/umount /mnt/usb_drives"

I look at df -h and I don't see my mounted device there, I tried writing a rule via pmount and still no result. Nothing is written in the logs, or it gives an error with a return value of 1

I tried to write commands in scripts, but as I understand it, the problem is that HE DOESN'T WANT TO RUN THROUGH THE ROOT USER. HOW IS THIS??

SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
ENV{UDISKS_AUTO}="0"

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
RUN+="sh /etc/udev/rules.d/mount-usb.sh %k"
#!/usr/bin/sudo bash
sleep 5
NAME_DEV=$1
mkdir "/mnt/usb/$NAME_DEV"
sudo /bin/mount "/dev/$NAME_DEV" "/mnt/usb/$NAME_DEV"

The device folder is created but the data is not mounted in it

EDIT

Changed the rule and the script, and also moved the script to /usr/bin/

/usr/bin/mount-usb.sh

#!/usr/bin/sudo bash
NAME_DEV=$1
/usr/bin/pmount --umask=000 -s -w "/dev/$NAME_DEV"

/etc/udev/rules.d/99-usb-mount.rules

SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", ENV{UDISKS_AUTO}="0"

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", RUN+="/usr/bin/mount-usb.sh %k"

After connecting a USB flash drive, a folder is created in /media, but data is still not mounted in it

NEW EDIT

SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
ENV{UDISKS_AUTO}="0"

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
RUN+="mkdir /mnt/usb/%k && systemd-mount --no-block -o umask=000,rw /dev/%k /mnt/usb/%k"

Returns an error as if it were access rights...

systemd-udevd[3099]: sdb1: Process 'mkdir /mnt/usb/sdb1 && systemd-mount --no-block -o umask=000,rw /dev/sdb1 /mnt/usb/sdb1' failed with exit code 1.

TRYING WITH SCRIPT

/usr/bin/mount-usb.sh

#!/usr/bin/sudo bash

NAME_DEV=$1
MOUNT_POINT="/mnt/usb/${NAME_DEV}"

mkdir "${MOUNT_POINT}"

/usr/bin/mount -o umask=000,rw "/dev/${NAME_DEV}" "${MOUNT_POINT}"

/etc/udev/rules.d/99-usb-mount.rules

SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
ENV{UDISKS_AUTO}="0"

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \
ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \
RUN+="/usr/bin/mount-usb.sh %k"

The script runs, but the device still doesn't mount in the folder I create. the folder I mount in (/mnt/usb) is set to chmod 777.

Folder /mnt/usb/sdb1 was created, but there is no data from the usb device in it

richstog-VirtualBox sudo[4342]:     root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/bin/bash /usr/bin/mount-usb.sh sdb1
9
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Aug 25 at 14:07
  • 4
    1. Why are you using sudo all over the place? udev already runs as root, there's no need to use sudo as the shebang line or use sudo within the script. sudo is not intended as a brute-force tool to sprinkle everywhere like magic pixie dust to "fix" (or, more often, break) things you haven't made an effort to understand. 2. did you run udevadm trigger? Commented Aug 25 at 14:20
  • 1. I realized my mistake about sudo. 2. Yes, I run the command udevadm control --reload-rules && udevadm trigger every time. At the moment, the rule and script look like this: ``` SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \ ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \ ENV{UDISKS_AUTO}="0" ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", \ ENV{ID_BUS}=="usb", ENV{ID_TYPE}=="disk", \ RUN+="/usr/bin/mount-usb.sh %k" ``` ``` #!/usr/bin/sudo bash NAME_DEV=$1 /usr/bin/pmount --umask=000 -s -w "/dev/$NAME_DEV" ``` Commented Aug 25 at 14:29
  • Comments are designed for us to ask you questions about your Question. Please Edit your question rather than replying in comments. How to Ask a new question if you have a new problem. Commented Aug 25 at 14:42
  • 1
    I see what you're trying to do, but your device isn't made available under /dev until that rule finishes executing ... You can't mount it that way. Please see similar issue discussed here: Different behaviour of bash script in udev Commented Aug 25 at 14:55

1 Answer 1

6

Systemd-udev doesn't support directly mounting filesystems from within udev rules. It's not a matter of user privileges – udev isolates the RUN subprocess in a new namespace, so that any mounts it creates are only visible to itself and will disappear when the process exits. This is done deliberately, from what I remember.

Either call systemd-mount --no-block instead (which mounts things indirectly, by sending off a request to systemd) if you have a recent enough version of systemd, or define a static /etc/fstab entry and invoke it through ENV{SYSTEMD_WANTS} or through systemctl --no-block start. Each fstab entry generates a .mount unit like media-foo.mount; systemd-mount will generate those dynamically.

(In both cases the --no-block is needed because systemd only considers the device "active" after its rules have finished.)

The other approach is to avoid RUN completely, but instead to have some daemon waiting and reacting to the final "rules complete" event that udev emits after it's done with all .rules for /dev/sdx. Systemd itself can do this, through the aforementioned ENV{SYSTEMD_WANTS}. But other programs can as well – that's how removable media gets automounted in graphical desktop environments like GNOME or KDE: your desktop listens for udev events.

Graphical environments usually have udisks2, which mounts things under /run/media/$USER (though there's an udev ENV that can move it back to /media). Udisks2 still does nothing on its own but needs to be triggered by something like udiskie if you don't already have a "full" that would do so. (And no, not by udev RUN; it doesn't "see" disks until after udev rules are done.)

(There are certainly also some "lightweight" options if udiskie is too much, but I can't remember any offhand.)

7
  • Or at least it behaves that when when systemd is available. The standard set of Yocto recipes for USB live booting puts a udev rule that calls a mounting script into the initramfs to conditionally automount all new storage so a later script can look for the rootfs image. But that's a shell init system not systemd. Commented Aug 25 at 22:23
  • @davolfman: IIRC the sandboxing is reduced in non-systemd builds since there's no ENV{SYSTEMD_WANTS} to be relied on, but I don't remember the details clearly enough. Commented Aug 26 at 4:14
  • @grawity: maybe I misunderstood you RUN+="mkdir /mnt/usb/%k && systemd-mount --no-block -o umask=000,rw /dev/%k /mnt/usb/%k" sdb1: Process 'mkdir /mnt/usb/sdb1 && systemd-mount --no-block -o umask=000,rw /dev/sdb1 /mnt/usb/sdb1' failed with exit code 1. Commented Aug 26 at 7:27
  • @grawity: you see what the problem is, udisks does not allow you to make a custom mount point. I need to mount devices to the folder that I specify. I tried to make soft links to udisks mount points, but it's a total pain. maybe there is some specific way to mount via pmount, mount to a specific mount point Commented Aug 26 at 7:40
  • 1
    @grawity: Regarding fstab: isn't it configured for specific devices? I should have automatic mounting of any USB devices. About the bash script: I added the script's operation to the end of the question. Commented Aug 26 at 8:33

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.