Thursday, October 2, 2014

Remove Kernel Lock from Unmounted Mass Storage USB Device from the Command Line in Linux


I've searched high and low, and can't figure this one out. I have a older Olympus Camera (2001 or so). When I plug in the USB connection, I get the following log output:


$ dmesg | grep sd
[20047.625076] sd 21:0:0:0: Attached scsi generic sg7 type 0
[20047.627922] sd 21:0:0:0: [sdg] Attached SCSI removable disk

Secondly, the drive is not mounted in the FS, but when I run gphoto2 I get the following error:


$ gphoto2 --list-config
*** Error ***
An error occurred in the io-library ('Could not lock the device'): Camera is already in use.
*** Error (-60: 'Could not lock the device') ***

What command will unmount the drive. For example in Nautilus, I can right click and select "Safely Remove Device". After doing that, the /dev/sg7 and /dev/sdg devices are removed.


The output of gphoto2 is then:


# gphoto2 --list-config
/Camera Configuration/Picture Settings/resolution
/Camera Configuration/Picture Settings/shutter
/Camera Configuration/Picture Settings/aperture
/Camera Configuration/Picture Settings/color
/Camera Configuration/Picture Settings/flash
/Camera Configuration/Picture Settings/whitebalance
/Camera Configuration/Picture Settings/focus-mode
/Camera Configuration/Picture Settings/focus-pos
/Camera Configuration/Picture Settings/exp
/Camera Configuration/Picture Settings/exp-meter
/Camera Configuration/Picture Settings/zoom
/Camera Configuration/Picture Settings/dzoom
/Camera Configuration/Picture Settings/iso
/Camera Configuration/Camera Settings/date-time
/Camera Configuration/Camera Settings/lcd-mode
/Camera Configuration/Camera Settings/lcd-brightness
/Camera Configuration/Camera Settings/lcd-auto-shutoff
/Camera Configuration/Camera Settings/camera-power-save
/Camera Configuration/Camera Settings/host-power-save
/Camera Configuration/Camera Settings/timefmt

Some things I've tried already are sdparm and sg3_utils, however I am unfamiliar with them, so it's possible I just didn't find the right command.


Update 1:


# mount | grep sdg
# mount | grep sg7
# umount /dev/sg7
umount: /dev/sg7: not mounted
# umount /dev/sdg
umount: /dev/sdg: not mounted
# gphoto2 --list-config
*** Error ***
An error occurred in the io-library ('Could not lock the device'): Camera is already in use.
*** Error (-60: 'Could not lock the device') ***

Answer



For a brute force disable of all active mass storage devices:


rmmod usb_storage

I found the following link, basically asking the same question as this. If you want to prevent the kernel from auto-mounting using usb_storage:


echo "blacklist usb_storage" | sudo tee /etc/modprobe.d/blacklist-usb-storage.conf

Instead of disabling all devices, you can target a specific device to ignore using udev rules. There is a specific example here.


I spent a lot of time trying to get this to work in Ubuntu 10.04, but it looks like this functionality was disabled in newer versions of udev.


The last post on this thread worked like a charm.


#include 
#include
#include
#include
#include
#include
int main(int argc, char**argv)
{
struct usbdevfs_ioctl command;
int ret;
int fd;
int i;
if (argc>1) {
fd = open(argv[1],O_RDWR);
if (fd<1){
perror("unable to open file");
return 1;
}
for (i=0;i<255;i++){ // hack: should fetch how many interface there is.
command.ifno = i;
command.ioctl_code = USBDEVFS_DISCONNECT;
command.data = NULL;
ret = ioctl(fd, USBDEVFS_IOCTL, &command);
if(ret!=-1)
printf("un claimed interface %d %d\n",i,ret);
}
}else {
printf ("usage: %s /dev/bus/usb/BUS/DEVICE\n",argv[0]);
printf("Release all interfaces of this usb device for usage in virtualisation\n");
}
}

The previous example is an interesting case, but I also found a much simplified method. You can use the usb-storage driver interface for binding and unbinding devices.


The following command worked, just like the source code from above:


echo -n "1-2.4:1.0" | sudo tee unbind

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...