35

I find that in order to re-mount a USB stick, I have to physically disconnect it, and then re-connect it. How can I do this without such tiring physical action?

tshepang
  • 65,642
  • 3
    The USB device is logically disconnected (there should be a message to that effect your kernel logs). It won't have a /dev entry any more, and IIRC won't show up in lsusb either. I don't know if there's a way to reconnect it. – Gilles 'SO- stop being evil' Feb 09 '11 at 22:49
  • This question is very similar to: https://superuser.com/questions/1163375/linux-cant-format-nor-access-a-detected-usb-key-no-medium-found – Dennis Benzinger Apr 02 '18 at 13:40

3 Answers3

22

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
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
sdaau
  • 6,778
10

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 ^^).

Thomas
  • 321
  • 2
    Check /dev/disk/by-id and see if the device is truly gone. I'll bet it's not, and can be remounted from there with a mount command. – LawrenceC Feb 09 '11 at 22:55
  • 2
    @ultrasawblade /dev/disk/by-id contains symlinks to device files in /dev, for example /dev/disk/by-id/usb-Kingston_DT_100_G2_001CC0C60DBDFC90D32923CE-0:0 -> ../../sdb. When the device is gone after udisks --eject (which I assume happens here), the symlink is stale and cannot be used for re-mounting. – PointedEars Jan 30 '14 at 10:58
  • 1
    @LawrenceC It seemed ultrasawblade was right. I got mount: /mnt: no medium found on /dev/sdc But as noted in the answer by sdaau, this works: sudo eject -t /dev/sdc – nealmcb Feb 24 '20 at 16:16
  • Thanks! Note there is an "unmount" option in the launcher in 22.04 (or at least Pop!OS 22.04), so you don't need a whole new file manager or a re-built Nautilus. – nealmcb Jun 02 '23 at 17:52
9

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
  • This answer is pretty short and low on details, but it does seem to do the trick. I actually used eject -t without the initial eject and sleep and it worked. I notice that even though my drive was mounted as /dev/sdb1 (note the partition number), ejecting /dev/sdb (whole disk) was necessary. – echristopherson Nov 12 '21 at 00:32
  • This sort of worked for me, so thank you! The device showed back up on some other processes I needed, but I was not able to remount it through the GUI in the convenient way. – C.D. Mar 25 '22 at 02:24