3

How to create a setup such that if I unmount an USB disk then it automatically powers off too?

2
  • 1
    What do you mean by power off? Oct 1, 2017 at 15:19
  • He probably means ejecting it - which will often spin down usb drives.
    – vidarlo
    Oct 1, 2017 at 15:54

2 Answers 2

1

Generally speaking, udisksctl should do the trick if the hardware supports it. Not all USB disks adhere to the standards equally well...

udisksctl power-off -b /dev/sdX will attempt to power off the disk:

$ udisksctl power-off --help 
Usage:
udisksctl power-off [OPTION...]

Safely power off a drive.

Options:
  -p, --object-path         Object path for ATA device
  -b, --block-device        Device file for ATA device
  --no-user-interaction     Do not authenticate the user if needed

Just tested this with a couple of different drives (Seagate BackupPlus 1000GB, ICY-box with a 500GB drive), and it seems to work. It did not work on a chinese super-cheap ($0.90) S-ATA-USB adapter...

You may also try the eject command - which sends the eject command to the device. This will in many cases cause spin down. sudo eject /dev/sdX - but there's slim chance of it working, if udisksctl does not work.

2
  • 2
    we can use udisksctl indeed. But OP asks for a setup that automates powering off the drive...
    – Zanna
    Oct 1, 2017 at 19:31
  • @vidarlo Is there any way make it automatically power off ?
    – Allen
    Oct 4, 2017 at 20:47
0

As far as I know some desktops do this automatically, others dont. Plasma does not do it (https://bugs.kde.org/show_bug.cgi?id=270808) a simple work-around has been posted over there (https://bugs.kde.org/show_bug.cgi?id=270808#c64). Here is a slightly improved version that will show notifications.

The work-around consists of adding a new action to the DeviceNotifier, that will run a bash script that will unmount and power down the device. It is not testing all edge cases, but it should work in most cases. Just add the following two files. After a restart of kded5 (or after logging out and back on again) removable devices should have a new action "Unmount & Power-Off USB device".

~/.local/bin/power-device-off (make sure to chmod +x this)

#!/bin/bash

if udisksctl unmount -b $1; then
    if udisksctl power-off -b $1; then
        notify-send "Device $1 powered off and can be removed safely."
    else
        notify-send "Unmounted $1, but could not power down."
    fi
else
    notify-send "Could not unmount $1."
fi

~/.local/share/solid/actions/power-devices-off.desktop

[Desktop Entry]
X-KDE-Solid-Predicate=[ [ [ StorageVolume.ignored == false AND StorageVolume.usage == 'FileSystem' ] OR [ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ] ] OR StorageAccess.ignored == false ]
Type=Service
Actions=open;

[Desktop Action open]
Name=Unmount and Power-Off USB device
Exec=power-device-off "%d"
Icon=emblem-unmounted

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .