1

I want to find out what gnome-disk is doing, how to do the same on the command line and how to undo whatever gnome-disk does. (It can not undo all it does itself.)

I have already experimented a little and found out the following: the USB memory thumb drive ("stick") I played with has at least 3 "state levels" to toggle, 2 of them can be switched with gnome-drive's buttons "eject" (on and off) and "power off" (only off).

From highest level to lowest, I discovered:

  1. eject
    • gnome-drive's eject button
    • drive does not disappear, neither from gnome-drive, nor elsewhere
    • command line: eject /dev/sdb
    • can not be undone with gnome-drive
    • undo with: eject --trayclose /dev/sdb
    • kernel messages (journalctl -k)
      • eject
        • sdb: detected capacity change from 30253056 to 0
      • uneject
        • sd 4:0:0:0: [sdb] 30253056 512-byte logical blocks: (15.5 GB/14.4 GiB)
        • sdb: detected capacity change from 0 to 30253056
        • sdb: [partition details of my drive]
  2. (un)bind
    • did not find equivalent in gnome-drive
    • command line: echo 3-6 > /sys/bus/usb/drivers/usb/unbind
    • device disappears in gnome-drive entirely
    • no kernel messages
    • lsusb -t still sees the device, but does not show class ("Mass Storage") or driver ("usb-storage") any more
    • /sys/bus/usb/drivers/usb/3-6 directory gone
    • undo with echo 3-6 > /sys/bus/usb/drivers/usb/bind
      • this provokes kernel messages
        • usb-storage 3-6:1.0: USB Mass Storage device detected
        • scsi host4: usb-storage 3-6:1.0
        • scsi 4:0:0:0: Direct-Access TOSHIBA TransMemory PMAP PQ: 0 ANSI: 6
        • sd 4:0:0:0: Attached scsi generic sg2 type 0
        • sd 4:0:0:0: [sdb] 30253056 512-byte logical blocks: (15.5 GB/14.4 GiB)
        • sd 4:0:0:0: [sdb] Write Protect is off
        • sd 4:0:0:0: [sdb] Mode Sense: 45 00 00 00
        • sd 4:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
        • sdb: [partition details of my drive...]
        • sd 4:0:0:0: [sdb] Attached SCSI removable disk
  3. power off
    • gnome-drive's power off button
    • device disappears on everything, like physically unplugged
    • indistinguishable from real unplugging
    • kernel message:
      • usb 3-6: USB disconnect, device number 10
    • How to power off via the command line?
    • How to power back on without real re-plugging?

For completeness: re-plugging the stick assigns a new device number (11), bus and port stay the same (3-6) and these kernel messages are show:

usb 3-6: new high-speed USB device number 11 using xhci_hcd
usb 3-6: New USB device found, idVendor=0930, idProduct=6545, bcdDevi>
usb 3-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-6: Product: TransMemory
usb 3-6: Manufacturer: TOSHIBA
usb 3-6: SerialNumber: C03FD5F7713EE2B1B000821E
[plus all kernel messages as quoted under (re-)bind above]
  • Note that gnome works via udisks2 to do its work. You might investigate udisksctl power-off. – meuh Nov 28 '23 at 17:39
  • Depending on what you want to achieve, note that usb has a per-port power switching PPPS protocol that can allow you to power on/off a port on a hub. Not many hubs support it. See hubctl for lots of info, and the real existing uugear hub for raspberry pi. – meuh Nov 28 '23 at 17:43
  • @meuh Yeah, I read that hubctl page before... Haven't tried it yet (no Arch Linux package). It didn't sound like that the gnome-disks power-off does proper PPPS, but something else instead, don't you agree? – Robert Siemer Nov 28 '23 at 19:51

3 Answers3

2

Similar questions (to the one in your title) have been asked over the years e.g. [1], [2], [3] (probably over a dozen if you take the time to search...) most answers no longer work nowadays due to changes in the code...
Anyway, I haven't been using GNOME in years but to focus on the first question in your post:

I want to find out what gnome-disk is doing, how to do the same on the command line

the easiest way is to just look at the source code for gnome-disk e.g. gduwindow.c uses two functions: udisks_drive_call_eject () and udisks_drive_call_power_off () - documented here where it says they invoke the Eject() and respectively the PowerOff() D-Bus methods which means you can invoke those methods on cli via d-bus calls.
Example: a flash drive ADATA identified as sda and not mounted

sda           8:0    1 14,9G  0 disk 
├─sda1        8:1    1 14,6G  0 part

First, get the drive ID corresponding to sda:

busctl get-property org.freedesktop.UDisks2 /org/freedesktop/UDisks2/block_devices/sda org.freedesktop.UDisks2.Block Drive

this will print something like

o "/org/freedesktop/UDisks2/drives/ADATA"

You can then use the part in quotes as the object for the d-bus call to eject or power-off the drive:

busctl call  org.freedesktop.UDisks2 /org/freedesktop/UDisks2/drives/ADATA org.freedesktop.UDisks2.Drive Eject 'a{sv}' 0

busctl call org.freedesktop.UDisks2 /org/freedesktop/UDisks2/drives/ADATA org.freedesktop.UDisks2.Drive PowerOff 'a{sv}' 0

same with gdbus which is GNOME specific:

gdbus call --system --dest org.freedesktop.UDisks2 --object-path /org/freedesktop/UDisks2/drives/ADATA --method org.freedesktop.UDisks2.Drive.Eject {}

gdbus call --system --dest org.freedesktop.UDisks2 --object-path /org/freedesktop/UDisks2/drives/ADATA --method org.freedesktop.UDisks2.Drive.PowerOff {}

Note that these commands will error out if the filesystem is mounted (drive in use). Also, as you can see in the documentation there's no un-eject or un-power-off method which is why gnome-disk cannot undo any of those operations.
I'm not sure why anyone would use the commands above (and I don't quite understand the point of eject() in the context of a flash drive but that's another discussion)...
I (like many others) unmount the filesystem with

udisksctl unmount -b /dev/sda1

and then power the drive off with

udisksctl power-off -b /dev/sda

In order to reconnect a drive that was powered off you'd have to run some sort of usb port reset/rescan/reconfiguration (which can't be done by regular users). None of the solutions in the other posts works for me with kernel 6.6.2 so I cannot suggest anything for now...

don_crissti
  • 82,805
  • Very good digging! +1 My next question is: what do these d-bus commands do? -- If you dig deeper, I can guarantee you, I'll keep reading about all your findings! – Robert Siemer Nov 28 '23 at 19:38
  • ...I would guess they do some ioctl() or operate in /sys/bus/.... -- Maybe those details shed some light on the opposite operation. That eject tool can do it, too. – Robert Siemer Nov 28 '23 at 19:46
  • @RobertSiemer - dbus is just "the messenger"... it communicates with udisks daemon (the source code is here) ... as I said in my post I don't quite understand what is the point of eject or insert (which imo is the right term for undo-ing eject) with flash drives... Here is the source code for eject ... as you can see everything is related to cdrom/scsi/tape/floppy drives. – don_crissti Nov 28 '23 at 20:49
  • That being said, the main problem here imo is that a usb device that was powered off cannot be easily powered back on since the upstream port is disabled once the device is deconfigured. Stuff like uhubctl works only on very few devices... If I had the time I'd probably look into libusb for the answer... – don_crissti Nov 28 '23 at 21:15
  • I'm not sure I follow when you talk about 'powered off', 'deconfigured' or 'upstream port'. -- I think we agree that whatever is happening behind the scenes, can be undone. Maybe the current software design does not properly allow some of it, but the physical & technical possibility exists. – Robert Siemer Nov 29 '23 at 19:04
  • I finally found the sysfs port management interface and added this answer. – Robert Siemer Dec 13 '23 at 00:13
0

How to do power-off on the command line?

echo 1 > /sys/bus/usb/devices/3-2.1/port/disable

The port is bus_nr-port.by.port.hub.chain.seperated.with.dots, i.e. 3-3 or 3-2.4.6. In those two examples the port is on bus 3. First example is just port 3, second example is a chain of two hubs, where the first is in port 2 of bus 3 and the second hub is in port 4 of the first. The device is on port 6 of the second hub.

How to power back on without real re-plugging?

echo 0 > '/sys/devices/pci0000:00/0000:00:14.0/usb3/3-0:1.0/usb3-port3/disable'

or for 3-2.1 it could be

echo 0 > '/sys/devices/pci0000:00/0000:00:14.0/usb3/3-2/3-2:1.0/3-2-port1/disable'

While the port is enabled you can pwd -P in the directory with the disable file to see the real location of the port management interface of the corresponding hub beforehand. For a list of host controllers which establish a USB bus, ls -l /sys/bus/usb/devices/usb* is a good start.

See also how to interpret lsusb for more tips. (Or the kernel docs.)

-1

How to do power-off on the command line?

Update: I finally found out -- see my other answer.

How to power back on without real re-plugging?

A brute method (it temporarily killed my mouse and keyboard, so make sure you can launch both commands...) is to rebind the USB host PCI device. This seems to power cycle all ports like a boot or physical re-plug. At least for me:

echo 0000:00:14.0 > /sys/bus/pci/drivers/xhci_hcd/unbind
echo 0000:00:14.0 > /sys/bus/pci/drivers/xhci_hcd/bind

The number needs to be adjusted to your environment, of course. See what you have:

lspci -t

revealed that that USB bus is driven by xhci_hcd and

ls -la /sys/bus/pci/drivers/xhci_hcd/ | grep ^l

showed only 0000:00:14.0 as device.