How to re-mount a USB stick after unmounting from Nautilus without disconnecting it?

This is the behavior I have on my Ubuntu Lucid system:

When I plug in my USB flash/thumbdrive which has two partitions, the system reports:

$ ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2

$ udisks --show-info /dev/sdb | grep -A2 'partition table\|by-id'
    by-id:                     /dev/disk/by-id/usb-takeMS_USB_Mini_AA07013000010057-0:0
    by-path:                   /dev/disk/by-path/pci-0000:00:1d.7-usb-0:2:1.0-scsi-0:0:0:0
  detected at:                 Mon 29 Apr 2013 02:16:50 PM CEST
--
  partition table:
    scheme:                    mbr
    count:                     2

$ udisks --show-info /dev/sdb1 | grep -A2 'partition table\|by-id'
    by-id:                     /dev/disk/by-id/usb-takeMS_USB_Mini_AA07013000010057-0:0-part1
    by-id:                     /dev/disk/by-uuid/581E-EE0B
    by-path:                   /dev/disk/by-path/pci-0000:00:1d.7-usb-0:2:1.0-scsi-0:0:0:0-part1
  detected at:                 Mon 29 Apr 2013 04:24:25 PM CEST

$ udisks --show-info /dev/sdb2 | grep -A2 'partition table\|by-id'
    by-id:                     /dev/disk/by-id/usb-takeMS_USB_Mini_AA07013000010057-0:0-part2
    by-id:                     /dev/disk/by-uuid/4A0E-A166
    by-path:                   /dev/disk/by-path/pci-0000:00:1d.7-usb-0:2:1.0-scsi-0:0:0:0-part2
  detected at:                 Mon 29 Apr 2013 04:24:25 PM CEST

$ udevadm info --query=all --name=/dev/sdb | grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0/host13/target13:0:0/13:0:0:0/block/sdb    
$ udevadm info --query=all --name=/dev/sdb1 | grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0/host13/target13:0:0/13:0:0:0/block/sdb/sdb1
$ udevadm info --query=all --name=/dev/sdb2 | grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0/host13/target13:0:0/13:0:0:0/block/sdb/sdb2
$ sudo partprobe /dev/sdb
$

So, only partitions have /dev/disk/by-uuid; while both disk and partitions have (different) /dev/disk/by-id.

Now, if I left-click on either of the partition entries in the Disk Mounter applet in the Gnome2 desktop in Lucid, I get only two entries: "Open" or "Eject" (as mentioned in the [SOLVED] How do you remount a usb drive with nautilus in lynx?, from @Thomas' answer, referring to a 16_hide_unmount.patch). Also, palimpsest here shows two partitions for the disk.

When I click on the Eject on either one of the partitions - they both dissapear; at this point, palimpsest shows the disk, but no partitions, and a "No medium found" message instead; also:

$ ls /dev/sdb*
/dev/sdb

$ udisks --show-info /dev/sdb | grep -A2 'partition table\|by-id'
    by-id:                     /dev/disk/by-id/usb-takeMS_USB_Mini_AA07013000010057-0:0
    by-path:                   /dev/disk/by-path/pci-0000:00:1d.7-usb-0:2:1.0-scsi-0:0:0:0
  detected at:                 Mon 29 Apr 2013 02:16:50 PM CEST

$ udisks --show-info /dev/sdb1 | grep -A2 'partition table\|by-id'
$

$ udevadm info --query=all --name=/dev/sdb | grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0/host13/target13:0:0/13:0:0:0/block/sdb
$ udevadm info --query=all --name=/dev/sdb1 | grep DEVPATH
device node not found

$ sudo partprobe /dev/sdb
Error: Error opening /dev/sdb: No medium found

So, as mentioned in comment by @ultrasawblade, indeed the disk is there, but not the partitions - although, I think I'd need the partitions as devices, in order to mount.

Also, the same behavior as left-click+"Eject" in desktop GUI, I can get in terminal with sudo eject /dev/sdb. Note that one can also open palimpsest, from there click on each of the two partitions, and click "Unmount Volume" - at that point, the icons for the partitions will still be shown in Disk Mounter applet, but will be noted as unmounted; and then one can do sudo udisks --detach /dev/sdb: this makes the icons disappear from Disk Mounter applet - but also the /dev/sdb device will not be present on the system anymore (however, lsusb will still show the device)!

After searching quite a bit for the answer (tried stuff from udevadm monitor --environment during plug and unplug, with replicating all actions via udevadm trigger, to looking into what the BLKRRPART ioctl does), without any success - I finally stumbled upon How To Remount A USB Stick Without Removing And Reinserting - Ubuntu/Debian, which has the answer:

sudo eject -t /dev/sdb

After this command is ran - the two partitions appear again in Disk Mounter applet - without unplugging/re-plugging the USB key!! This is all the stranger, because if you read man eject:

   -t   With  this  option the drive is given a CD-ROM tray close command.
        Not all devices support this command.

... you'd never expect it's applicable to USB Flash/Thumb drive! (And I even tried looking into sys-utils/eject.c, and I cannot see anything obvious :) )

Of course, if sudo udisks --detach /dev/sdb has been ran previously, then /dev/sdb won't exist, so eject -t /dev/sdb will fail with eject: unable to find or open device for: '/dev/sdb'. In that case, the USB power cycle as noted in #7457 How to reconnect a logically disconnected USB device? helped me recover:

$ sudo sh -c 'echo suspend > /sys/bus/usb/devices/1-2/power/level'
$ sudo sh -c 'echo auto > /sys/bus/usb/devices/1-2/power/level'

This made the partitions' icons show again in Disk Mounter applet, directly as mounted; to find the right address, try while the disk is mounted:

$ udevadm info --attribute-walk --name=/dev/sdb | grep "looking\|busnum\|devnum" | grep -B1 'busnum\|devnum'
  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-2':
    ATTRS{busnum}=="1"
    ATTRS{devnum}=="13"
  looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1':
    ATTRS{busnum}=="1"
    ATTRS{devnum}=="1"

# the first (.../usb1/1-2) should be the right one, check bus/devnum:
$ lsusb -s 1:13
Bus 001 Device 013: ID 090c:1000 Feiya Technology Corp. Flash Drive

# looks good; check for symlinks in `sys/bus/usb/devices` as per "parent device" .../usb1/1-2
$ ls -la /sys/bus/usb/devices/ | grep "usb1/1-2"
lrwxrwxrwx 1 root root 0 2013-04-29 17:17 1-2 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-2
lrwxrwxrwx 1 root root 0 2013-04-29 17:21 1-2:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.0

# so it should be /sys/bus/usb/devices/1-2; check if it has power/level:
$ ls /sys/bus/usb/devices/1-2/power/level 
/sys/bus/usb/devices/1-2/power/level

From my experience in Ubuntu, when you "eject" a USB stick from within Nautilus, the device actually disappears from the system. I'm not sure why this is, but neither Nautilus nor the command line can get it back. I guess the logic is that once you eject a USB stick you don't want it back, but are going to disconnect it.

The way I work around this (when needed), is by using umount instead of Nautilus. You could also just call sync to flush the filesystem buffers to the disk.

Just found a thread which has more info : http://ubuntuforums.org/showthread.php?t=1477247

So basically either a) Rebuild nautilus from source without that patch (and keep it up to date when you update your system...) or b) use another file manager (at least when unmounting ^^).


On Debian based systems, at least, you should be able to do this:

  1. figure out what the drive letter should be... I'm gong to say /dev/sdb here for the example.
  2. Double eject the 'would be' drive letter to effectively power the device back on.
  3. Profit!

This works because... magic.

# determine 'would be drive letter'
lsblk
# do magic
eject /dev/sdb; sleep 1; eject -t /dev/sdb