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

1

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') ***       

cmcginty

Posted 2010-06-03T11:07:20.010

Reputation: 2 249

If I recall correctly, gphoto2 only deals with MTP/PTP devices, not mass storage... – user1686 – 2010-06-03T11:22:05.170

sorry, maybe my terminology is off, but it work when I unmount from Nautilus – cmcginty – 2010-06-03T11:53:35.033

no solution so far, any ideas? – cmcginty – 2010-06-04T11:15:29.260

gvfs-mount doesn't work then? (see my answer) – Peter Jaric – 2010-06-04T11:22:59.040

Peter Janic's solution worked great for me. A ptp mounted device does not show up with the "mount" command, but "gvfs-mount -l" and "gvfs-mount -u" do work on my ptp mounted nikon d70. Thanks Peter! – cdaaawg – 2012-03-30T17:14:39.673

Answers

1

Quick and Dirty Method

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

rmmod usb_storage

Prevent Any Device from Loading usb_storage Module

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

Prevent Single Device from Loading usb_storage Module

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.

"Safely Remove Disk" Unbind/Unclaim Source Code

The last post on this thread worked like a charm.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <linux/usbdevice_fs.h>

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");
   }
}

Simple Script for Binding / Unbinding Device

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    

cmcginty

Posted 2010-06-03T11:07:20.010

Reputation: 2 249

0

First, run mount as root. This should list all mounted filesystems. If /dev/sdg and /dev/sg7 are not in the list, then the camera is no longer mounted.

If the camera is still mounted, you can unmount it with the command umount (note the missing 'n'). An example would be umount /dev/sg7 or umount /dev/sdg.

If the camera has mounted filesystems, then you can probably just browse to the photos in Nautilus. The output of mount will tell you where the devices are mounted to in the filesystem - just browse to the folder and start looking around for the photos.

Darth Android

Posted 2010-06-03T11:07:20.010

Reputation: 35 133

I added the output of the commands in the question .. I'm sure I tried that a few days ago ;) – cmcginty – 2010-06-03T11:56:01.097

Also, you assume I am trying to download photos, that is not the case. – cmcginty – 2010-06-03T11:57:35.787

@casey: /dev/sg7 wouldn't have been mounted anyway -- that's a scsi control device, not a drive. /dev/sdg might be a mountable device, and might be a partitioned drive (in which case you'd have to mount /dev/sdg1 or /dev/sdg2 or similar). – quack quixote – 2010-06-03T13:38:50.217

There are no partitions shown on /dev/sdg – cmcginty – 2010-06-03T19:41:51.513

0

Are you using Ubuntu? There is talk of a bug in 8.10, and while I'm not sure if this is fixed in the latest version, there is a workaround which might let you do... whatever it is you're trying to do (since you aren't just downloading pictures). :P

Darth Android

Posted 2010-06-03T11:07:20.010

Reputation: 35 133

It looks similar, but I can get gphoto to work if I simply unmount the device from Nautilus first. I'll make sure to read it all and try and of the workarounds. – cmcginty – 2010-06-03T20:00:20.070

0

I do this in a script:

# If camera is mounted we need to unmount it
export CAMERA_MOUNT_POINTS=`gvfs-mount -l | grep gphoto2 | sed 's/.*\(gphoto2.*\)$/\1/' | uniq 2> $LOG_FILE`
for CAMERA_MOUNT_POINT in $CAMERA_MOUNT_POINTS
do
    echo Unmounting mounted camera from $CAMERA_MOUNT_POINT.
    gvfs-mount -u $CAMERA_MOUNT_POINT &> $LOG_FILE
done

I am not sure this is what you need, but it might be, since I've had similar problems.

EDIT: To explain a little: gvfs-mount -l lists mounted stuff The rest of that line cleans the output to produce just a list of mounted cameras (and pipes errors to a log file). Then the loop unmounts all cameras.

Peter Jaric

Posted 2010-06-03T11:07:20.010

Reputation: 1 756

I changed something, and now gvfs_mount does not list the device. I'll try again after a reboot. – cmcginty – 2010-06-04T11:46:38.843

It didn't work then, I guess, since you pursued another line? – Peter Jaric – 2010-06-06T09:48:23.390