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.
Best Answer
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 callingudevadm info /dev/sdc
. E.g. for an NTFS on an external drive I get: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 againstKERNEL=="sd?2"
to make sure I'm only handling block devices with the correct partition number. The complete rule would look like this: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 viajournalctl -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 elsewhereUdev 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: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.