rewsiffer
Posts: 15
Joined: Fri Apr 10, 2015 4:29 am
Location: Bend, Oregon

Shutdown Pi via USB stick.

Mon Nov 28, 2016 10:46 pm

Hi there, first time poster, not first time RasPi user.

I am trying to figure out a failsafe way to shutdown any Pi(w/ rasbian) due to the oft-sited issues with SD card corruption when the power cable is yanked out.

This issue has been addressed many times in the past, the strategies I most often see are a python script and a GPIO button, or with a separate micro-controller via UART or SPI.

I would prefer to find a way of doing so via USB because it doesn't require first installing a script(python/GPIO) or plugging something into the GPIO(UART/SPI). Likewise, if you would like to shutdown a Pi, you just plug in the USB stick(and maybe press a button on said stick)

So, would it be possible to implement something like this on some sort of USB stick?

My first thought was a USB flash drive with an autorun shell script that would run anytime it was plugged into a Pi. I am no Linux guru, but I did some digging and it seems in order for this to work, the Pi's system must first be modified to accept autorun scripts(something to do with UDEV?). This in not ideal because I would like to be able to plug it into any pi(like one with a brand new copy of rasbian) and cause a shutdown.

My next thought was to use something like an Atmega 32u4 on a USB stick to implement a shutdown, but I do not know much about USB protocols so I am not sure where to start. My most basic implementation(far from ideal), would be to use the 32u4 as a USB HID via Arduino to send the keystrokes to open a terminal and enter "sudo shutdown -h now". This might very well work fine, I will test it out when I get home, but it seems pretty janky and I'm sure there's a better way.

Would anyone be able to point me down the right path?

Thanks!

User avatar
HawaiianPi
Posts: 5838
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 1:03 am

rewsiffer wrote:Hi there, first time poster, not first time RasPi user.

I am trying to figure out a failsafe way to shutdown any Pi(w/ rasbian) due to the oft-sited issues with SD card corruption when the power cable is yanked out.

This issue has been addressed many times in the past, the strategies I most often see are a python script and a GPIO button, or with a separate micro-controller via UART or SPI.
SD card corruption occurs when you yank the power BEFORE shutting down the Pi. Buttons and scripts will not help with that. As long as you shut down and wait for all activity to end before removing power you'll be fine. With Raspbian the green activity LED will flash ten times at the end of the shutdown procedure. After that you can safely disconnect the power.

rewsiffer wrote:I would prefer to find a way of doing so via USB because it doesn't require first installing a script(python/GPIO) or plugging something into the GPIO(UART/SPI). Likewise, if you would like to shutdown a Pi, you just plug in the USB stick(and maybe press a button on said stick)

So, would it be possible to implement something like this on some sort of USB stick?

My first thought was a USB flash drive with an autorun shell script that would run anytime it was plugged into a Pi. I am no Linux guru, but I did some digging and it seems in order for this to work, the Pi's system must first be modified to accept autorun scripts(something to do with UDEV?). This in not ideal because I would like to be able to plug it into any pi(like one with a brand new copy of rasbian) and cause a shutdown.

My next thought was to use something like an Atmega 32u4 on a USB stick to implement a shutdown, but I do not know much about USB protocols so I am not sure where to start. My most basic implementation(far from ideal), would be to use the 32u4 as a USB HID via Arduino to send the keystrokes to open a terminal and enter "sudo shutdown -h now". This might very well work fine, I will test it out when I get home, but it seems pretty janky and I'm sure there's a better way.

Would anyone be able to point me down the right path?

Thanks!
Why not just shutdown from the menu, or do a sudo shutdown now from the cli?
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

rewsiffer
Posts: 15
Joined: Fri Apr 10, 2015 4:29 am
Location: Bend, Oregon

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 3:50 am

I aware that it only does damage to the SD card when you disconnect the power before you run shutdown, that was not my question.

I often run into the issue of having a headless Pi running that is either not yet connected to the network or not responding for some reason. I want to make something to manually shutdown the Pi with having the connection via console, SSH, gui, etc.

tweak42
Posts: 532
Joined: Tue Mar 22, 2016 3:16 am
Location: Los Angeles, California

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 5:56 am

rewsiffer wrote:I aware that it only does damage to the SD card when you disconnect the power before you run shutdown, that was not my question.

I often run into the issue of having a headless Pi running that is either not yet connected to the network or not responding for some reason. I want to make something to manually shutdown the Pi with having the connection via console, SSH, gui, etc.
It's not exactly a usb stick, but a USB rubber ducky could do it. You would have to program in the login, password and command you want to run but there's documentation for that. If the usb subsystem is bugged or system is hard locked it obviously won't work.

At $45 Rubber Ducky's aren't cheap.
https://hakshop.com/products/usb-rubber-ducky-deluxe

BUT if you have a Pi Zero laying around you can make your own!
https://hackaday.io/project/17598-diy-usb-rubber-ducky
(the solution for your Pi is more Pi, how meta is that?)

rayjoh
Posts: 27
Joined: Thu May 23, 2013 11:48 am

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 9:04 am

rewsiffer wrote:So, would it be possible to implement something like this on some sort of USB stick?
You can do it in a udev rule.

Insert the USB stick and run the command 'lsusb'. Find the ID-information for the USB stick.

Code: Select all

$ lsusb
Bus 001 Device 006: ID 0781:5567 SanDisk Corp. Cruzer Blade
Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter / SMSC9512/9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Create the file /etc/udev/rules.d/10-poweroff.rules

Code: Select all

ACTION=="add", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5567", RUN+="/sbin/poweroff"
0781 and 5567 is my Sandisk Cruzer Blade.
Remove the USB stick and run "sudo udevadm control --reload-rules"
Insert the USB stick.
-- Raymond

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 11:20 am

I am trying to figure out a failsafe way to shutdown any Pi(w/ rasbian) due to the oft-sited issues with SD card corruption when the power cable is yanked out.
I see what you are attempting: you want the insertion of a USB device to be treated as though a Shutdown button had been pressed?

The fatal flaw in wanting this to work for any RPi may be the fact that early models of RPi did not have hot-plug-safe USB ports, so simply plugging in a dongle would hard-crash the system in a far from failsafe way, which would perpetuate the issue you are trying to mitigate.

You might be able to implement your idea for later models, but maybe not for any RPi.

rewsiffer
Posts: 15
Joined: Fri Apr 10, 2015 4:29 am
Location: Bend, Oregon

Re: Shutdown Pi via USB stick.

Tue Nov 29, 2016 11:55 pm

UPDATE: I attempted my method using an ATMEGA32u4 with the Arduino Keyboard Library and it worked(sort of)! I was able to write a script that opened a terminal, entered the command, and shutdown the machine. It was very quick too, from insertion to shutdown was only about 4 seconds, and I can probably reduce this by modifying the delays in the Arduino sketch. However, this only works with a GUI running, but it's a good start!

It would be super simple to put the code on something like this:

http://www.ebay.com/itm/New-Mini-ATMEGA ... Sw-KFXf3Ey

On another note:
It's not exactly a usb stick, but a USB rubber ducky could do it. You would have to program in the login, password and command you want to run but there's documentation for that. If the usb subsystem is bugged or system is hard locked it obviously won't work.

At $45 Rubber Ducky's aren't cheap.
https://hakshop.com/products/usb-rubber-ducky-deluxe

BUT if you have a Pi Zero laying around you can make your own!
https://hackaday.io/project/17598-diy-usb-rubber-ducky
(the solution for your Pi is more Pi, how meta is that?)
This is a terrific idea! I have a spare ZERO sitting around and it will be put to the task tonight. In it's most basic implementation, Ducky Script just seems to enter key strokes, and since this is what I can do with the 32u4, I would probably stick with the smaller simpler Arduino. However, I will look through the example payloads and the forum and see if there are any more sophisticated ways of accessing a terminal without having to physically open the command window.

Whether utilizing the 32u4 or the PiZero, it would ideal to find a way to open a discreet shell session similar to SSH or UART(I think this is what you proposed) where I could execute my command. However, since this would likely require a login, it would only work on a Pi with a default password or if I pre-program the USB device with the credentials. Likewise, perhaps using keystrokes/HID is the only real way to implement this without modifying the target machine.

There seem to be quite a few examples of using the Pi as other USB devices Serial, Ethernet, Mass Storage, etc., these might yield results as well.

Also,
I see what you are attempting: you want the insertion of a USB device to be treated as though a Shutdown button had been pressed?

The fatal flaw in wanting this to work for any RPi may be the fact that early models of RPi did not have hot-plug-safe USB ports, so simply plugging in a dongle would hard-crash the system in a far from failsafe way, which would perpetuate the issue you are trying to mitigate.

You might be able to implement your idea for later models, but maybe not for any RPi.
Duly noted. The new Pi's would really be the only ones I am working with so I might be alright. However, what I really want to figure out is a way to do this without entering keystrokes. This sort of goes against my original inquiry, but that seems to happen when you open a can of worms like this...

tweak42
Posts: 532
Joined: Tue Mar 22, 2016 3:16 am
Location: Los Angeles, California

Re: Shutdown Pi via USB stick.

Wed Nov 30, 2016 12:24 am

I've never used Arduino's before but that's kinda neat. I think you're pretty much limited to keystrokes using root level credentials if you want to avoid modifying the OS to enable GPIO / UART control.
You should not need to interface with the graphical GUI as you can CTRL-ALT-F1 to F4 to a virtual terminal and run your commands there.

You should be able to make the Zero run as a ethernet gadget, it creates a dhcp network then if the target Pi requests a dhcp lease, the Zero can ssh in and run commands.

rewsiffer
Posts: 15
Joined: Fri Apr 10, 2015 4:29 am
Location: Bend, Oregon

Re: Shutdown Pi via USB stick.

Wed Nov 30, 2016 5:09 pm

tweak42 wrote:
I've never used Arduino's before but that's kinda neat.
It's only possible with the 32u4 chipset arduinos(Micro/Leonard) because they have onchip USB support, but yeah, pretty cool!
tweak42 wrote:
You should not need to interface with the graphical GUI as you can CTRL-ALT-F1 to F4 to a virtual terminal and run your commands there.

You should be able to make the Zero run as a ethernet gadget, it creates a dhcp network then if the target Pi requests a dhcp lease, the Zero can ssh in and run commands.
Good to know, thanks!

Return to “General discussion”