USB 3.0 Poweroff after Unmount

Technical issues/questions of an intermediate or advanced nature.
Testuser
Samurai
Samurai
Posts: 137
Joined: 26 May 2021, 15:11
Distribution: Porteus-v5.0-64-LXDE

USB 3.0 Poweroff after Unmount

Post#1 by Testuser » 10 Oct 2022, 18:09

USB 2.0 & 3.0 drives not getting powered off after Umount or Eject. Any USB 3.0 users can check (noticed when it is still hot even after unmounting it 30 min ago). :no:

If we forget to unplug (after copy Sync will be happening and we will wait) lot of energy is wasted in form of heat energy (will reduce battery backup in laptop as well).

In Windows, I beleive it is powerd off. :unknown:

To test this simply run udisksctl power-off -b /dev/sdb and you will see it is executed, again run it and it will say no device to poweroff (means while eject it is not powered off). :no:

Found a command to powerdown the usb after unmount - udisksctl power-off -b /dev/sdb (Please correct me if I am wrong)

Now the step is to automate it, not manually running the script.

Created a file nano /etc/udev/rules.d/90-usb-notify-poweroff.rules with below contents to poweroff any connected and unmounted USB

# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.

ACTION=="remove", ENV{ID_PART_TABLE_TYPE}!="", ENV{DEVTYPE}=="partition", RUN+="/usr/bin/sudo -u guest DISPLAY=:0 /usr/bin/Usb-Poweroff.sh"


This will poweroff after unmount of any USB drive connected.

Content of Usb-Poweroff is below. Searched internet for the script and any changes or modification to below script is welcome for better result.

We intent to help other and hope this will help somebody. :)

Code: Select all

#!/bin/bash

for DEV in /sys/block/sd*
do
	if readlink $DEV | grep -q usb
	then
		DEV=`basename $DEV`
udisksctl power-off -b /dev/$DEV
	fi
done
exit 0

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

USB 3.0 Poweroff after Unmount

Post#2 by Rava » 16 Oct 2022, 17:52

Testuser wrote:
10 Oct 2022, 18:09

Code: Select all

#!/bin/bash

for DEV in /sys/block/sd*
do
if readlink $DEV | grep -q usb
then
DEV=`basename $DEV`
udisksctl power-off -b /dev/$DEV
fi
done
exit 0
Since you look for sd* - wouldn't that also power off sda?

Or, when the drive you want to power off is sdb - and you also have a sdc external harddrive, wouldn't that script try to power off not only sdb (the intended target) but sda and sdc?
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

USB 3.0 Poweroff after Unmount

Post#3 by Ed_P » 17 Oct 2022, 02:53

So, I finish with a USB drive and unmount it. And rather than remove it, which is why people normally unmount it, I turn the power off to it. I eventually remove it and plug my cell phone into the port to charge it, how do I turn the power back on?
Ed

gnintilgyes
Black ninja
Black ninja
Posts: 73
Joined: 14 Sep 2022, 17:52
Distribution: Debian

USB 3.0 Poweroff after Unmount

Post#4 by gnintilgyes » 20 Oct 2022, 07:00

Rava wrote:
16 Oct 2022, 17:52
Since you look for sd* - wouldn't that also power off sda?

Or, when the drive you want to power off is sdb - and you also have a sdc external harddrive, wouldn't that script try to power off not only sdb (the intended target) but sda and sdc?
I agree that the script should check for "/dev/sda" and pass it along. I discovered a computer that uses "/dev/mmcblk0" or something like that as the internal disk, but "/dev/sda" set as the first USB disk plugged in. Yikes!

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

USB 3.0 Poweroff after Unmount

Post#5 by Ed_P » 20 Oct 2022, 17:03

Yup, I have one of those. /dev/nvmen0p1
Ed

Testuser
Samurai
Samurai
Posts: 137
Joined: 26 May 2021, 15:11
Distribution: Porteus-v5.0-64-LXDE

USB 3.0 Poweroff after Unmount

Post#6 by Testuser » 20 Oct 2022, 17:44

Guys,

The USB 3.0 heating I noticed only for high speed USB 3 ports and when connected with USB 3 or 3.1 devices.
Rava wrote:
16 Oct 2022, 17:52
Since you look for sd* - wouldn't that also power off sda?

I think this part will take care of that
for DEV in /sys/block/sd*
do
if readlink $DEV | grep -q usb


somehow grep usb will detect only Usb drive, though i was not able to see it in readlink output manually. (need to remove -q which is the option "quiet")

If it tries to poweroff a mounted drive the output will be this
guest@porteus:~$ udisksctl power-off -b /dev/sda
Error powering off drive: The drive in use: Device /dev/sda11 is mounted (udisks-error-quark, 14)

Also once the device is powerdoff, when we reinsert it will be mounted on the same port.

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

USB 3.0 Poweroff after Unmount

Post#7 by Rava » 21 Oct 2022, 01:33

Testuser wrote:
20 Oct 2022, 17:44
Also once the device is powerdoff, when we reinsert it will be mounted on the same port.
I presume… unless there was a change, e.g. you plugged in an unrelated USB thumbdrive in the meantime.
E.g. your internal drive: sda
your external USB3 drive with heating issues: sdb
some unrelated USB thumbdrive: sdc

you poweroff and unplug sdb

you insert yet another unrelated USB thumbdrive: that now gets to be sdb since at that time sdb is the one with the lowest character that is free
you plug in again your USB3 device with heating issues - now that gets to be sdd since sdb is no longer available (nor is sda or sdc, see above) and sdd is now the one with the lowest character that is free
Cheers!
Yours Rava

Testuser
Samurai
Samurai
Posts: 137
Joined: 26 May 2021, 15:11
Distribution: Porteus-v5.0-64-LXDE

USB 3.0 Poweroff after Unmount

Post#8 by Testuser » 22 Oct 2022, 17:37

Hi Rava,

What you said above is correct. :)

But little confused here, is there any problem with this approach :%) :no:

Please mention and will work on that. :)

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

USB 3.0 Poweroff after Unmount

Post#9 by Rava » 23 Oct 2022, 08:52

Testuser wrote:
22 Oct 2022, 17:37
But little confused here, is there any problem with this approach :%) :no:
Not that I can think of, unless you specify what problem / issue there could be.

The system always assigns the next free slot that is of the lowest character (as in: a= lowest, z=highest)

When you would simply unplug a drive without unmounting it - which is not recommended at all! - say that was a USB thumbdrive, a simple VFAT only one on sdb1 - and you have your harddrive on sda and another USB thumbdrive on sdc and an external harddrive on sdd - then the next free slot to assign would still be sde (and not sdb) since for the system sdb is still occupied.
Cheers!
Yours Rava

Testuser
Samurai
Samurai
Posts: 137
Joined: 26 May 2021, 15:11
Distribution: Porteus-v5.0-64-LXDE

USB 3.0 Poweroff after Unmount

Post#10 by Testuser » 23 Oct 2022, 11:28

Yes Rava,

This unplug command will only be executed by /etc/udev/rules.d/90-usb-notify-poweroff.rules rule when I manually unmount any USB drive (consider it is sdb, the other USB which is in sdc still mounted will not be powerd off) :)

Post Reply