View previous topic :: View next topic |
Author |
Message |
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Wed May 03, 2017 7:20 pm Post subject: Powering down a specific USB port? |
|
|
I am doing more and more data recovery where the attached disk will work for ten minutes then disappear until it cools down. This means I get to babysit it and unplug the USB connection every time it disappears, wait ten minutes, reattach, and repeat. What I would like to do is write a small BASH script to start the recovery, then after five or ten minutes, stop the recovery and issue CTRL+C to ddrescue, and turn the USB port completely off. It will wait ten minutes, then repeat.
I can figure out the rest, but how do I turn off a USB port completely using a script? _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
eccerr0r Watchman
Joined: 01 Jul 2004 Posts: 9882 Location: almost Mile High in the USA
|
Posted: Wed May 03, 2017 7:31 pm Post subject: |
|
|
Depends on the hardware, some hardware it's not possible to completely remove power from the device. _________________ Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching? |
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Wed May 03, 2017 8:27 pm Post subject: |
|
|
Well where would I start? This is an older laptop with Intel USB2.0 ports. _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3491
|
Posted: Wed May 03, 2017 9:43 pm Post subject: |
|
|
AFAIK you can send a command to USB device that demands it to shut down, but some devices will not comply. It's independent of the hub, so you can't e.g. control an USB flashlight this way - it has connectors for power supply, but not for data lines and it will not receive your command.
You could try powering off the whole hub though, if you're not worried about other devices connected there. I'd expect this to actually cut any power that would otherwise be passed to the external connectors. |
|
Back to top |
|
|
eccerr0r Watchman
Joined: 01 Jul 2004 Posts: 9882 Location: almost Mile High in the USA
|
Posted: Wed May 03, 2017 10:50 pm Post subject: |
|
|
The_Great_Sephiroth wrote: | Well where would I start? This is an older laptop with Intel USB2.0 ports. |
If it's an "expensive" laptop like a business class machine, there's a possibility there is a switching transistor per port to turn it on and off; if not and it's a "consumer" low end machine, then likely you can't - it's just fused at most.
Since the hardware is not common among all machines, driver support is spotty and a machine specific driver may be necessary. You're probably going to have to make your own external power switcher dongle most likely. _________________ Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching? |
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Thu May 04, 2017 5:09 am Post subject: |
|
|
It is a Dell Latitude E6400. I believe they were $2500 new, without the nVidia Quadro card, which mine lacks. I do not mind powering down the hub if needed. I need to simulate unplugging the USB cable so the externally attached drive loses power and can cool inside the fridge. I put these drives in a fridge with a 10ft active USB 2.0 cable coming out. It has helped save some data before. I just hate baby-sitting it! _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
chithanh Developer
Joined: 05 Aug 2006 Posts: 2158 Location: Berlin, Germany
|
Posted: Thu May 04, 2017 9:41 am Post subject: |
|
|
You need a USB hub that supports per port power switching. Intel USB root hubs don't support power switching in my experience. However, many USB hubs, even cheap ones, support this. Unfortunately this feature usually not mentioned in marketing material, so you may have to try a bunch until you find one that works.
lsusb -v will tell you whether the hub advertises power switching. |
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Sat May 06, 2017 3:26 am Post subject: |
|
|
Could I shutdown the entire Intel USB hub somehow? _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
eccerr0r Watchman
Joined: 01 Jul 2004 Posts: 9882 Location: almost Mile High in the USA
|
Posted: Sat May 06, 2017 3:34 am Post subject: |
|
|
chithanh wrote: | However, many USB hubs, even cheap ones, support this. |
Of the USB hubs that I've taken apart, this is not the case. All of the cheap hubs I've opened up have power hooked straight up to the main power source - some aren't even fused. Finding one that have switchable ports will be tricky without opening them. _________________ Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching? |
|
Back to top |
|
|
NeddySeagoon Administrator
Joined: 05 Jul 2003 Posts: 54805 Location: 56N 3W
|
Posted: Sat May 06, 2017 10:08 am Post subject: |
|
|
The_Great_Sephiroth,
hdparm may let you power down the drive. See the -S option.
That might even be enough to do 10 min access (or whatever), wait 10 min, and have the drive spin down. _________________ Regards,
NeddySeagoon
Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail. |
|
Back to top |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3491
|
Posted: Sat May 06, 2017 12:54 pm Post subject: |
|
|
Since the root hub tends to be a PCI device:
Quote: | Try echo 0 > /sys/bus/pci/slot/$N/power for appropriate slot |
|
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Tue May 09, 2017 11:30 pm Post subject: |
|
|
Neddy, I did not think about that. I will try that first. Thanks! _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
chithanh Developer
Joined: 05 Aug 2006 Posts: 2158 Location: Berlin, Germany
|
Posted: Wed May 10, 2017 1:09 pm Post subject: |
|
|
eccerr0r wrote: | chithanh wrote: | However, many USB hubs, even cheap ones, support this. |
Of the USB hubs that I've taken apart, this is not the case. All of the cheap hubs I've opened up have power hooked straight up to the main power source - some aren't even fused. Finding one that have switchable ports will be tricky without opening them. | Indeed, you need to buy them randomly until you get your hands at one that works.
Here is a German forum where one user searched for USB hubs that support power switching. Cheapest with working power switching was a 5€ hub from Delock.
https://www.mikrocontroller.net/topic/311841 |
|
Back to top |
|
|
niceflower Tux's lil' helper
Joined: 31 Mar 2017 Posts: 76
|
Posted: Tue May 16, 2017 4:04 pm Post subject: |
|
|
If the usb is connected with a cable if it is on the front of your computer, you can disconnect it.
If the usb is on the motherboard you can try to desolder it that is a bit more tricky.
Or even if you have multiple usb ports lets say 1.0 - 2.0 - 3.0 you can use one of them, and denode the other usb in kernel. |
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Wed May 17, 2017 3:00 pm Post subject: |
|
|
I have a script almost done for this. Not sure how to do the loop though. I use "timeout" to time the length of recovery and then sleep with the HDD powered down, but I am not sure to how to figure out when ddrescue finishes to break the loop.
Code: |
# Tell the disk to spin-down after five seconds of non-use
hdparm -S 1
while [ <ddrescue is not finished> ]; do
timeout 300 ddrescue /dev/sdX /home/user/sdX.img /home/user/sdX.map
<get return value in case it finished?>
sleep 600
done
|
Not sure how to figure this one out. My guess is to set some variable to zero and somehow catch the ddrescue return value and if that value equals "I am done" then we have broken the loop. _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
krinn Watchman
Joined: 02 May 2003 Posts: 7470
|
Posted: Wed May 17, 2017 4:50 pm Post subject: |
|
|
none suggest it if i'm not wrong, but you could had set your usb as module and unload/reload it, it will power down the device. (if the hdd is powered only by usb) |
|
Back to top |
|
|
The_Great_Sephiroth Veteran
Joined: 03 Oct 2014 Posts: 1606 Location: Fayetteville, NC, USA
|
Posted: Wed May 17, 2017 5:06 pm Post subject: |
|
|
I knew of that solution but I build all hardware drivers for hardware permanently attached to my laptop into my kernel. I would prefer to keep it that way, and if I can figure this script issue out I may have a solution.
*UPDATE*
I believe I have it.
Code: |
#!/bin/bash
# Set variables
Reading = true
# Check parameters
if [ "$#" -ne 6 ]; then
echo "Usage: $0 <device> <image> <map> <sector size> <read secs> <cooldown secs>"
echo "Ex: $0 /dev/sdb /home/user/recover.img /home/user/recover.map 512 750 1500"
echo "Ex: $0 /dev/sdb2 /home/user/sdb2.img /home/user/sdb2.map 4096 300 600"
exit 1
fi
# Verify the device exists
if [ ! -b "$1" ]; then
echo "Error: $1 does not appear to exist!"
exit 2
fi
# Set aggressive power management for spin-down
hdparm -B 1 -S 1 $1
# Recover loop
while [ $Reading ]; do
# Run the recovery for the specified amount of time
timeout $5 ddrescue -b $4 -M -d -r 16 $1 $2 $3
# See if the timeout was hit or if ddrescue exited
if [ $? -ne 124 ]; then
$Reading = false
break
fi
# Give the disk time to cool down
sleep $6
done
# Note that the disk recovery completed
echo "Disk recovery completed or ddrescue exited for some other reason."
exit 0
|
Timeout returns 124 if the timeout was hit, otherwise it returns whatever ddrescue returns. If ddrescue exits, we probably want to figure out why anyway! _________________ Ever picture systemd as what runs "The Borg"? |
|
Back to top |
|
|
|