3
votes

I'm trying to create a udev rule that hides block devices (i.e usb drives) with a size less than 64 GB

The rule looks like this:

BUS=="usb", SUBSYSTEM=="block", ACTION=="add", PROGRAM="/data/diskSizeCheck.pl %k", RESULT!="ok", ENV{UDISKS_PRESENTATION_HIDE}="1", GOTO="usb_mount_end"

Where usb_mount_end is just a label at the end of my rules file. %k is supposed to be the device kernel (i.e sdb). But even when I hardcode 'sdb' as a parameter, that parameter never makes it to my perl script, and the disk always fails the size check even if it's large enough. When I pass sdb in via the command line, though, it works.

Here is the perl script I'm using:

#!/usr/bin/perl
use strict;
my $MINIMUM_DISK_SIZE = 64000000000;
my $kernel = $ARGV[0];
my $diskSize = `blockdev --getsize64 /dev/$kernel`;
chomp($diskSize);

if ($diskSize > $MINIMUM_DISK_SIZE) {
    print "ok";
} else {
    print "no";
}

The script is marked as executable and everything, but when I wrote $kernel to a text file, the text file came up blank, leading me to believe the variable is never passed in.

How do I pass in %k to my perl script?

Edit to add: I'm running everything as root.

Edit to further add: I think the real problem is that RESULT isn't properly capturing the output of my script for some reason.

1
After some playing around (setting $kernel = "sdb" and changing the rule so if RESULT=="ok" then the drive is hidden) I figured out the real problem is that the output of my script doesn't seem to be captured by RESULT for some reason. I invoke the script via command line simply with ./diskSizeCheck.pl sdb - bomberblue

1 Answers

2
votes

using

KERNEL=="sdc", SUBSYSTEMS=="block", ACTION=="add", PROGRAM="/usr/local/diskSizeCheck.pl %k", RESULT!="ok", ENV{UDISKS_PRESENTATION_HIDE}="1"

With diskSizeCheck.pl:

#!/usr/bin/perl
use strict;
use warnings;
open FH,">/tmp/diskSizeCheck";
print FH "Disk Size Check\n"; 

my $MINIMUM_DISK_SIZE = 64000000000;
my $kernel = $ARGV[0];
my $diskSize = `sudo blockdev --getsize64 /dev/$kernel`;
chomp($diskSize);

print FH "kernel = $kernel, $diskSize\n";

if ($diskSize > $MINIMUM_DISK_SIZE) {
    print FH "ok\n";
} else {
   print FH "no\n";
}

On inserting a USB stick I got /tmp/diskSpaceSize:

Disk Size Check
sdc, 2005925888
no