13

I want to mount the usb drive in /media based on the UUID of the disk. How can I use the UUID of a usb drive in a udev rules file and what is a udev rule to mount the usb drive with the UUID number as the folder name.

The blkid command outputs the UUID of a disk but only upon mounting.

  • 1
    Caveat 2019: On modern desktop Linux systems using systemd, udev rules do not mount filesystems in the regular user namespace, i.e. you cannot mount a filesystem using the mount command in a udev rule. Furthermore, in these systems, the fuse (user filesystems) system is configured to handle USB mounts via a desktop icon, so mounting by hand using an fstab rule to some degree conflicts with the fuse system. For mounting USB filesystems by UUID automatically on a headless server you would be best to remove the systemd fuse configuration and use udev rules to trigger one-shot mount services. – Jonathan Ben-Avraham Jul 3 '19 at 8:52
  • See this solution using fstab, systemd and automount, tested on recent Debian: unix.stackexchange.com/a/347007/31228 – Jonathan Ben-Avraham Jul 3 '19 at 11:53
14

Even though UUIDs are not directly accessible by udev, at least in Fedora and Ubuntu they are set as environment variables (ENV). You can read out all environment variables of a device by calling udevadm info /dev/sdc. E.g. for an NTFS on an external drive I get:

$ udevadm info /dev/sdc2
P: /devices/pci0000:00/0000:00:14.0/usb4/4-2/4-2:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc2
N: sdc2
[…]
E: ID_FS_TYPE=ntfs
E: ID_FS_USAGE=filesystem
E: ID_FS_UUID=4A6F2ABC1232FA37
[…]

Everything with an E: in front is set as an environment variable.

Matching against UUIDs

You can match against it with ENV{ID_FS_UUID}=="4A6F2ABC1232FA37". As a first filter I match against KERNEL=="sd?2" to make sure I'm only handling block devices with the correct partition number. The complete rule would look like this:

KERNEL=="sd?2", ENV{ID_FS_UUID}=="4A6F2ABC1232FA37", RUN+="/usr/bin/logger --tag my-manual-usb-mount Mounting the device with UUID 4A6F2ABC1232FA37", RUN+="/usr/bin/mount [Your mount options here]"

The logger command is useful to assert that the rule is actually run. Next you should check that the rule does not contain any syntax errors with udevadm test /dev/sdc2. Your rules file should be listed in the output and no error message should appear next to it. Now you can trigger all rules for your device to check whether your rule works as planned: sudo udevadm trigger /dev/sdc2. If your rule matched you will find the custom log message in the system log (/var/log/syslog or via journalctl -b).

Edit: ali_m has pointed out that the environment variables are set by previous rules and thus only accessibly if your .rules file has a sufficiently high lexicographical ordering. Starting with "60" should be enough. Personally I start my rules with "zz" (if possible) to distinguish them from predefined rules in a glance.

Using UUIDs in the RUN command and elsewhere

Udev has a quite powerful variable and attribute substitution syntax. Specifically every occurrence of $env{ID_FS_UUID} will be replaced with the UUID of the drive. So to mount a device under /media/UUID you could use this rule:

KERNEL=="sd??", RUN+="/usr/bin/mkdir /media/$env{ID_FS_UUID}", RUN+="/usr/bin/mount $devpath /media/$env{ID_FS_UUID}"

You probably want to configure udisks instead, though, it is specialized for the problem space you want to solve. See https://wiki.archlinux.org/index.php/udisks#udisks2:_mount_to_.2Fmedia as a starting point.

My actual use case for mounting devices with udev instead of /etc/fstab or udisks is to mount zfs file systems which have some idiosyncrasies making them not quite fit the usual tools.

| improve this answer | |
  • 1
    One crucial point that took me a while to figure out: in order to match against environment variables, your rule must be applied after those variables are set up. To achieve this, your .rules file name needs to begin with a number > 60. – ali_m Nov 17 '14 at 0:01
  • @ali_m: Thanks, I've added your information to the answer. – Perseids Nov 18 '14 at 8:59
  • 1
    Ubuntu 18.04, some udevadm commands can be pointed to /dev/sdXX (eg. udevadm info), others needs to be pointed to /sys/block/sdXX for some reason (eg. udevadm test). – Greg Bell Feb 15 at 1:30
0

There's a rough solution come in my mind. Set cooresponding fstab entry which IDed by UUID. And in udev rules, execute mount -a everytime there's a new sd* device added.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.