Home > udev > Use UUID in udev rules and mount usb drive on /media/$UUID

Use UUID in udev rules and mount usb drive on /media/$UUID

November 20Hits:1

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.


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

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.

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.


Related Articles

  • Use UUID in udev rules and mount usb drive on /media/$UUIDNovember 20

    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 UU

  • Trying to create UDEV rule to mount removable drive exfat partitionMay 15

    So I am trying to create a udev rule to automatically mount a drives exfat partition to /media/offsite. My goal is to automatically mount a drive to /media/offsite and then kick off a .sh script to copy files around. I have been reading many guides a

  • UDEV-Rule to mount USB-Stick / execute scriptMarch 1

    I have a problem with an udev-rule. i simply want to mount a usb stick when it is plugged in. the rule correctly executes a script which mkdir the mount point and then calls mount. everything seems to work, but the stick is never mounted. if I execut

  • udev rule for a USB device and ignoring othersJanuary 22

    Is it possible to create a udev rule for an USB device and ignore other USB devices? I want my system to support HID USB touchscreen (hid.ko) and ignore HID devices such as keyboards, mice, etc. --------------Solutions------------- By the time udev i

  • udev rules targeting every USB device

    udev rules targeting every USB deviceSeptember 4

    I'm trying to execute a script whenever a USB is plugged. What I currently have is a demo script in /usr/local/test.sh: #!/bin/bash touch /home/kfir/test.txt I also have a rules file which tries to match ANY USB in /etc/udev/rules.d/100-usb.rules: AC

  • How do I write a bash script to detect a mounted USB drive?September 27

    I'm triggering a script from cron. I want it to run rsync ONLY if a USB disk called "data_3" is mounted, otherwise not. How do I write a bash script to detect a mounted USB drive ? My current pseudocode: #!/bin/sh if ( mount | grep /media/data_3

  • How can I make ubuntu ask for my password before mounting usb-drives? December 12

    This question already has an answer here: How to make Ubuntu ask for password when mounting partitions? 3 answers THIS POST IS NOT A DUPLICATE ALTHOUGH IT HAS BEEN MARKED AS SUCH. NO WHERE ON THE ALLEGED DUPLICATE PAGE DOES IT MENTION EXTERNAL DRIVES

  • RW mounted usb drive regarded read-only by cpAugust 1

    Context Can any of you help me with the following, I've got a micro sd card inserted via a usb stick into my Compaq 8510w laptop running the latest Arch Linux (x86_64) release. I have a udev rule that automounts the drive upon insert like this: KERNE

  • Cannot mount USB drive - FSTAB errorNovember 13

    Recently, one of my desktops seems to have developed a problem with the fstab and mounting USB drives. The specific error I receive is... Unable to mount TRAVELDRIVE Error mounting: mount exited with exit code 1: helper failed with: Unprivileged user

  • How to Find the Device Associated with a Mounted USB Drive in LinuxJune 23

    How do you find the device (e.g. /dev/*) for a mounted USB drive in Linux (Ubuntu 10.04)? I'm trying to format a Cruzer USB flash drive, and when I plug it in, the icon for the mounted filesystem appears on my desktop. However, when I open GParted, i

  • is it possible to make ubuntu ask for my password before mounting usb-drives?August 14

    I don't want anybody to be able to copy my files anyhow, so I am asking if it is possible to make ubuntu ask for my password before mounting usb-drives and if it is, how to do it? --------------Solutions------------- You could try encrypting your usb

  • Automatically mount external drives to /media/LABEL on boot without a user logged in?October 11

    This question is similar, but kind of the opposite of what I want. I want external USB drives to be mounted automatically at boot, without anyone logged in, to locations like /media/<label>. I don't want to have to enter all the data into fstab, par

  • How can I create a udev rule to mount a USB drive read only?March 15

    I'm trying to set a particular USB drive to always mount read only. If I plug it in, it is seen as sdb with a single partition, sdb1. Here are some relevant udevadm lines (not the entire output of course): $ udevadm info -a -n /dev/sdb1 looking at de

  • udev rule to rename usb network interface don't work on startupApril 1

    I have an embedded system that has a smsc9512 (USB 2 port hub with Ethernet converter) running a distribution generated with poky and kernel 3.0.20. I need to have a fixed name for the interface and added the rule /etc/udev/rules.d/81-hmi.rules: SUBS

  • Mounting USB drive on 3G dongleFebruary 15

    I've got an Huawei HiLink E303 USB G3-internet dongle that I'd like to use to connect to the internet. I'm using Arch Linux. According to this page, the Linux drivers are actually on the device and can be installed relatively easy, including on Arch.

  • Auto-mount USB drives in Ubunto 10.04?

    Auto-mount USB drives in Ubunto 10.04?September 27

    I have a cron-scheduled backup task, that writes to a USB drive. (It's a SATA drive stuck in an external dock that is connected to my machine via USB.) This usually auto-mounts at /media/backups (since "backups" is the partition label for the si

  • Auto-mount USB drives read-onlyOctober 6

    I'm currently setting up a laptop for my grandparents to use. They have never used a computer before, so I'm trying to make this as fool-proof as possible. The system will be setup with Ubuntu 10.10. In order to save them the hassle of having to unmo

  • How do I mount USB drives as given user automatically?October 26

    How do I get my USB drive to auto-mount as owned by 'me' rather than by whomever is logged in to the computer screen at the time? Often I SSH into my computer while someone else is using it, and so the read-write permissions of devices I mount go to

  • Mounting USB drive that is not recognizedDecember 14

    Using Arch Linux / XFCE, I frequently have problems with USB drives that are not properly mounted. Sometimes they automatically show up in Thunar and I can mount them with one click. However, at other times (it's about fifty/fifty) the drive is just

  • How to mount USB drive as user writableJune 29

    I want to mount my USB hard drive as myself, or in my group, whatever it takes to be able to move stuff onto the disk without sudo. I have tried setting up a rule in /etc/udev/rules.d/mountasuser.rules (also tried the filename 99-mountasuser.rules).

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.646 s.