Ignoring devices with udev rules

3 03 2009

I have a Dane Elec 8GB USB stick. It was cheap, it seems to be reliable, and you never know when 8GB of pluggable free space might come in useful… It did come ready built with a minor irritation, however. When you plug the stick in it gets seen as two separate drives – one 1MB drive with some windows-only software on it and another with the full 8GB of space. I have no use for the software on the 1MB drive, so I’d prefer not to see that drive when I plug the stick in.

“Aha, you idiot!” I hear you cry. “Why don’t you just run up fdisk and delete that partition. Easy!” Tried that – It’s not quite that simple. It’s a composite USB device, meaning that it is seen as two separate SCSI devices rather than two separate partitions. Instead I put a text file on the small partition called “__THIS IS THE WRONG PARTITION__” and resolved to just ignore it.

This weekend I was reading about creating udev rules here (Yeah, I know. My life is just one long thrill ride…) and I hit upon an idea. Surely it must be possible to ignore a device with a udev rule? Indeed it is, and it’s pretty straightforward…

I plugged the device into my laptop and checked the output of dmesg to see that the device was picked up as /dev/sdb (The large drive) and /dev/sdc (The small, ignorable one.) The udev system includes a tool which can give an enormous amount of information about a plugged in device, so I ran “udevinfo -a -p /sys/block/sdc”. /sys/block/sdc is the place under the sys directory that contains the information about the block device /dev/sdc. udevinfo first provides information about the device itself, and then walks up through the parent devices, giving information about each one in turn. A trimmed version of the information about my device is shown below.

looking at device ‘/block/sdc’:
ATTR{stat}==” 3 0 24 4 0 0 0 0 0 4 4″

looking at parent device ‘/devices/pci0000:00/0000:00:1d.7/usb5/5-5/5-5:1.0/host3/target3:0:0/3:0:0:1’:
ATTRS{vendor}==” ”
ATTRS{model}==”USB DISK Pro ”

You are allowed to use the attributes from the device itself and one parent when writing a udev rule. It’s important to use attributes that aren’t going to change. KERNEL==”sdc” is obviously a bad idea – if I have another block device plugged in before I insert the usb stick next time I’m going to get a different block device name. In the end I grabbed the model name from the parent and the subsystem and size from the device itself and wrote a rule that says:

SUBSYSTEM==”block” ATTR{size}==”2880″ ATTRS{model}==”USB DISK Pro ” OPTIONS==”ignore_device”

and put it into a new file: /etc/udev/rules.d/10-local.rules. The rules files in that directory are processed in name order, so the 10- part puts my rules pretty close to the start of the list. This rule basically says “If you get a block device where its size is 2880 and its model is “USB DISK Pro ” then ignore it. The four spaces in the product name were what was reported by udevinfo, so I copied it exactly.

If you’re running a kernel without inotify support (I think almost all recent kernels will have inotify support built in) you’ll need to either reboot or force udev to reload its rules. My kernel does have inotify support, so I ejected the device, unplugged it, plugged it back in and crossed my fingers. Lo and behold, only the large drive appears. A look at dmesg shows that the smaller drive was still seen, and it still appears as /dev/sdc, but it wasn’t mounted. I was surprised it got assigned a device name at all, but at least I’ll never see it again. Scratch one minor irritation from my life, and I got to learn something into the bargain. I’m calling that a win… đŸ™‚




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: