Linux – How to prevent all USB mass storages from mounting

linuxudevusb

I want to prevent every kind of USB mass storage from mounting using udev rules.

Already I can detect all of USB mass storage devices connected to my system using the following rule:

SUBSYSTEMS=="scsi", SUBSYSTEM=="block" KERNEL=="sd[b-h]1"

But how can I prevent them from mounting?

I know I have to set the authorized file of its relevant USB device to zero! But how can I find the USB device path? The $DEVPATH gives me the path of storage device block for example sdb1!

I have an application which should give permission to some of USB mass storage devices. So the used method for blocking the USB mass storage devices should not be very static!

Best Answer

The following rule prevents all except the first partition from being auto-mounted:

# Rule: Only mount first partition found on /dev/sd* (USB) devices.
ACTION=="add|change", SUBSYSTEM=="block", KERNEL=="sd*[2-9]", ENV{UDISKS_PRESENTATION_NOPOLICY}="1"

Reason: In USB disks with multiple partitions, I only want to automount the first one. I manually mount the others, if I want them.

You should be able to substitute the partition matcher with KERNEL=="sd*", to get:

# Rule: Do not automount any partitions found on /dev/sd* (USB) devices.
ACTION=="add|change", SUBSYSTEM=="block", KERNEL=="sd*", ENV{UDISKS_PRESENTATION_NOPOLICY}="1"

This prevents auto-mounting, which you can then manually manage.

I have only tested this on Ubuntu 10.10