I'm trying to write a udev rule to not mount USB devices plugged into my system if they're bootable, but I don't know how to specify in udev if a device is bootable in a rule. Is that possible? If so, what needs to go into my rule?

To be clear, by bootable, I mean the device has an OS that I can boot into at start up.

  • 1
    Posting as a comment as UDEV rules is like Greek to me. You cannot make any OS detection on a drive unless you mount its partitions. A work around is to determine if the device has an active partition: if sfdisk -d /dev/sdX 2>/dev/null | grep -q bootable returns a non zero code then the disk contains a bootable partition so mount none. – user86969 May 1 '15 at 18:35
  • You can run a script on a udev rule trigger. The trick would be for your script to scan the first few bytes of the disk for an MBR signature. The solution given by Nasha above is acceptable. Alternatively, roll your own one liner using dd and perl /grep to scan the first sector of the usb disk for an MBR signature. – Lmwangi May 23 '15 at 21:02

udev adds some environment variables to the partition node (leaf node) including partition entry flags for MBR table. Bootable partition should have ID_PART_ENTRY_FLAGS=0x80.

Try this rule and you gonna see all environment variables (source: Pass ATTR{idVendor} as argument in udev script):

KERNEL="sd[a-z][1-9]", RUN+="/bin/sh -c 'echo == >> /home/username/Desktop/usb-storage.txt; env >> /home/username/Desktop/usb-storage.txt'"

A rule that works for me in Ubuntu 14.04:

ACTION=="add", KERNEL=="sd[a-z][1-9]", ENV{ID_PART_ENTRY_FLAGS}=="0x80", RUN+="/bin/sh -c 'echo 0 > /sys%p/../../../../../../../authorized'"

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.