How can I prevent all USB mass storages from mounting? - linux

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!

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

The easiest way to do this is to blacklist the usb-storage kernel module. This will only work if it has been compiled as a module however, rather than directly into the kernel. You can check with modprobe -n usb-storage.ko, or by looking for it in /lib/modules/$(uname -r)/kernel/drivers/usb/storage/
If it's compiled as a module, you can black list it by adding an entry to /etc/modprobe.d/blacklist(.conf) For Debian, see this guide

Related

Does Linux have mount limit for device/hardware?

I noticed for general USB storage device, as long as I have enough USB end points. The Kernel will recognize the device.
Another example is I noticed when I mount two cdc-acm devices(USB modem) on a board, the Kernel only recognized one of them.
And when I configured the Kernel, there's no option for the number of the device I can mount, but only the option to turn it on/off.

How can BeagleBone Black be used as Mass Storage Device?

Is it possible to use the BB as Mass Storage Device?
I want it to be connected to an audio player that can read files from USB connectivity (such as USB flash drive) and act as data storage device containing one specific folder (and its sub-folders) from the file system (if possible, on a flash drive connected to the board.).
As the device specs says, it has connectivity of:
USB client for power & communications
USB host
Operating system will probably be Ubuntu but can be changed.
What drivers or configurations needs to be done in order to achieve this?
The latest images have already the mass storage usb gadget active, so a mass storage peripheral should be recognized by your system upon connection.
A quick google search reveals this discussion about a user trying to disable the USB MS gadget:
From the discussion, the files where the magic happens are:
Debian: /opt/scripts/boot/am335x_evm.sh
Ubuntu: /opt/scripts/boot/am335*
Armstrong: /usr/bin/g-ether-load.sh
In my Debian image the line you want to modify is:
modprobe g_multi file=${gadget_partition} cdrom=0 stall=0 removable=1 nofua=1 iSerialNumber=${SERIAL_NUMBER} iManufacturer=Circuitco iProduct=BeagleBone${BLACK} host_addr=${cpsw_1_mac}
and the corresponding $gadget_partition variable that is set just before that in order to customize the folder you want to expose.
Note that the g_multi gadget in its standard configuration presents 3 different devices: an ethernet interface, a mass storage peripheral and a serial interface. If you want to customize the parameters you can refer to the g_multi documentation (kernel.org)

opendir for usb device /dev/sdb

I am trying to implement in c program a way to detect if usb is connected or not.
I noticed that when the usb is connected, then the following command from shell will result as following:
root:~# ls /dev/sdb
/dev/sdb
If usb is disconnected then I get
root:~# ls /dev/sdb
ls: /dev/sdb: No such file or directory
I therefore assumed that best way to detect usb connected from c program shall be by doing opendir("/dev/sdb"), but the open call is always failed.
Do you have any idea what's best methd to do this detection ?
The main goal, is knowing in run-time program where the udev mounted the harddisk, and where the usb flash drive (if plugged in).
Thanks,
Ran
A) Why your opendir call failed? It's easy to understand. Because /dev/sdb is not a directory. For me it's hard to understand what you expect, when you try to open device as a directory. If you want to get file list from you usb drive, you need to mount the partition (/dev/sdb1, /dev/sdb2, etc. not the /dev/sdb) to some mount point (directory). And if you mount it successfully, then you can open the directory with your call. Many linux distributions mount usb drive automatically. You can look to your distribution documentation to get the information about automatical mounting of the usb drives.
B) I think it's easy to understand why detecting the usb device this way is bad idea.
=> Different computers have different amount of drives. So on many computers
/dev/sdb - is a hard drive.
=> Asuming you know that there is only one hard drive, the disk naming still can change after computer reboot, so it's possible that after reboot the usb drive will become sda and hard drive will become sdb (but usually this doesn't happen).
=> Generally, it's not possible to predict a new letter (sdb or sdc or sde).
=> You can't access to the usb devices, that are not usb drives.
=> There are tonns of other problems with such solution.
C) I didn't understand your goal completely, but if you want your program to be make some action if the usb device is plugged in, that the best solution you can do is read about udev and the event system of your distribution. If you want to make with your usb device some low level operations, you can read about libusb. You can get the general information about usb devices with lsusb command which is usually a part of a distributions. You can google for some other infermer commands.
The device /dev/sdb is not a directory, it's just a device, so you should use a system call like stat that will tell you whether a file exists instead of trying to calling opendir on it.
Please note that /dev/sdb could be any kind of hard drive depending on the setup of your specific system; it is not necessarily a USB drive.
Alternatively, you could execute the lsusb utility and parse its output.

Mounting a read only drive as write

I have a special bespoke device with a USB interface. When plugging in the device to my laptop - Ubuntu 12.04 it mounts as a read only USB drive - with a file on it. This file is created by the device and writes to the file when the device scans stuff.
I however, want to be able to write to the drive so the device 'thinks' it has already scanned x amount of entries.
Basically I want to replace the file 'File1.txt' my version of 'File1.txt' however I cannot because the drive is mounting as Read only.
I have tried the following commands:
andy#andy-ThinkPad-W530:/media/iRead$ touch giveme.txt
touch: cannot touch `giveme.txt': Read-only file system
andy#andy-ThinkPad-W530:/media/iRead$ sudo mount -o remount,rw '/media/iRead'
[sudo] password for andy:
mount: cannot remount block device /dev/sdb read-write, is write-protected
andy#andy-ThinkPad-W530:/media/iRead$
Can anybody suggest anything I can try to mount this as writable drive?
I have a very strong feeling that the chip which is storing this data is the following:
ARM STM32F103 RBT6 22oUP _ 93 MLT22950
Hope this helps somebody to help me!
If processing write commands coming from USB wasn't part of the requirements, it's very unlikely that the device processes write commands.
A read-only USB mass storage device is not a read-write mass storage device with write-protection slapped on top. It's a USB device that doesn't have logic for understanding write requests at all.
After the device is finished and delivered is a little late for deciding
I want to be able to write to the drive so the device 'thinks' it has already scanned x amount of entries.
Of course the flash memory used inside the device is written during its operation. But the way data is stored inside might not look anything like its USB presentation, and the conversion is most likely one-way only.
Since the developer probably did not implement mass storage support from scratch, and the library they used probably has write support, they may be able to easily supply you with firmware modified to be writable and do something with the written data. But without changing the firmware, you get nowhere.

Get access to USB device on Linux (libusb-1.0)?

I am writing a small program to communicate with a specific USB HID product (identified by vendor and product IDs), using libusb-1.0 on Linux. Right now, I have to run the program as root because "libusb requires write access to USB device nodes". Is there a way to change the permissions on the device I need so that I don't need to run my program as root?
On modern Linux systems, udevd (man 7 udev) creates the device nodes for USB devices when they're plugged in. Add a udev rule that matches your device (eg. you could match by USB Vendor and Product IDs), and sets the OWNER / GROUP / MODE of the device node.
The best approach is probably to create a new group for users who should be able to access the device, then set that as the group owner in the udev rule. You may also need to use MODE to ensure that it has group read/write permissions. Eg. your rule will probably look something like:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="ffee", ATTRS{idProduct}=="5a5a", MODE="0660", GROUP="foobar"

Resources