I wanted to write a bash script to stop certain idVendors from mounting, my thought was to use tail -f /var/log/messages
and greping it to idVendor and blocking that certain vendor upon detection, can someone help me on this please?
Asked
Active
Viewed 88 times
-1
-
Have you already tried something, have you a minimum of code we can work on? Any additional info may be useful. If I understand you are trying to identify lines like `kernel: usb 3-1: New USB device found, idVendor=03f0, idProduct=5a07` and trying to unmount them if mounted? Am I correct? – furins Dec 26 '14 at 16:27
-
actually i tried alot of things to get this to work but since im new to linux none of them did what i wanted and yes thats exactly my question, thanks for replying so fast – mike Dec 26 '14 at 16:31
-
OK, still we need some info. Which distribution of Linux are you using? why do you need to rely on `/var/log/messages` and `idVendors` in a script instead of using `noauto` option for the specific device id in your `fstab`? which kind of peripherals are you trying to unmount? if not strictly related to a scripting language, please consider also to post this question to http://superuser.com/ – furins Dec 26 '14 at 17:03
-
I'm using centos 7 and well that was the only way that came to my mind:) and what Im trying to do is write a script to disallow mounting of certain usb thumb drive's brand like silicon-power and the reason i need it to be scriptlike is to be able to change the disallowed brand in future, sorry if my question was unclear... – mike Dec 26 '14 at 17:18
-
And i will post it there too for sure thank you so much. – mike Dec 26 '14 at 17:20
1 Answers
1
A udev
rule would come in handy here. On my arch system I would simply add a rule like this (didn't try it out though). create a file in
/etc/udev/rules.d/40-do-not-mount.rules
and add a line like this (replacing [VENDOR ID]
and [PRODUCT ID]
with the ones from your device, wildcards are allowed)
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]", ATTR{idProduct}=="[PRODUCT ID]",ENV{UDISKS_IGNORE}="1"
then reload
udevadm control --reload-rules
test your rule:
udevadm test /sys/dev/block/[device]
it should output something like
...
UDISKS_IGNORE=1
USEC_INITIALIZED=760036464
unload module index
Unloaded link configuration context
Some references:
-
this is the same approach I would like to suggest, you were faster than me :) – furins Dec 26 '14 at 17:38
-
-
Okay, I did all you said but it didn't work:( no error showed up and the device could still be mounted... – mike Dec 26 '14 at 18:37
-
-
when i edited that udev and did the test it started reading files from /usr/lib/udev/rules.d and there is something wrong with /sys/dev/block/ i guess cause there are only 9 links in there like 11:0 2:0 253:0 253:1 8:0 8:1 8:16 8:17 8:2 am i doing something wrong here? and there were these two errors at the end of the test: unable to open device '/sys/dev/block/[device]' unload module index – mike Dec 26 '14 at 19:39
-
here is what a guy from superuser.com said: "Write an udev rule that matches devices by their idVendor attribute (ATTR{idVendor}) and writes 0 to the authorized and/or 1 to remove attributes, causing Linux to eject the device." can you please tell me how to do the auth and remove att part? – mike Dec 26 '14 at 19:53
-
1you have to replace [device] with your device,in my case it's e.g. 8\:32. you can also use "udevadm test $(udevadm info -q path -n /dev/sdN)" where N should be replaced with the device name your usb stick gets when inserted. or just skip the test. – wgitscht Dec 26 '14 at 19:54
-
okay i did the testing with each of `OPTIONS+="ignore_device" , ENV{UDISKS_IGNORE}="1"` but none of them seem to work:/ any help? – mike Dec 27 '14 at 07:27