4

Assuming that I do not have keyboard or ssh access to my Pi, but I do have physical access. What is the safest way to power it off and preserve the SD card? (This is the case if I've just done a silent install and need to power off the pi, and I don't have a USB keyboard or wifi access configured yet.)

I can imagine a few approaches:

  1. Turn off the power at mains. Maybe this is bad because the 5v power supply will drop relatively slowly, causing bad things to happen electronically. (I am not an electrical engineer)
  2. Unplug the micro usb power plug. Maybe this is better than the above because the power will drop more abruptly.
  3. Eject the SD card first, then power off using 1 or 2 above. Maybe this is good because I see that the contacts on SD cards are different lengths. Maybe this is so that power to the SD card is lost in an orderly fashion as the different pins lose contact in a defined order when ejected.
  4. Some other way I've not thought of.

Edit: I do not believe this is a duplicate of the questions suggested:

In my case I'm talking about when I do not have ssh or keyboard access in order to type shutdown commands such as sudo poweroff. I called out in my question that this is the case immediately after doing a silent install of an OS, and you may not have network configured yet.

In these cases I found I had to uncleanly power off, plug the SD card into another machine in order to edit files in /etc to configure the network, then plug back into the Pi and boot. So, just once or twice I found I had to uncleanly power off my Pi, and I would like to know the safest way of doing that. I think this is a reasonable question to ask, and is different from the suggested duplicates which are about how to switch off your Pi in the normal day-to-day case.

pauldoo
  • 161
  • 1
  • 4
  • I usually make sure the system is fully idle (no flashing SD access light) and only the terminal interface is active, then unplug. If you unplug while the desktop interface is open some nasty things can happen. – TheJamaicanGuy Mar 01 '15 at 20:00
  • Ejecting the SD card each and every time might lead to more trouble. It's been reported that the contacts wear quite fast. – Ghanima Mar 01 '15 at 20:01
  • @Ghanima that's an interesting point I hadn't considered before. – pauldoo Mar 01 '15 at 20:04
  • @TheJamaicanGuy do you unplug the power adapter from the mains, or the micro usb from the Pi? – pauldoo Mar 01 '15 at 20:06
  • @pauldoo I unplug the micro USB from the Pi first. – TheJamaicanGuy Mar 01 '15 at 20:16
  • 1
    None of those. All those options have a risk of critical failure. Pulling the SD card out has the highest. A good way would be to have a script run shut-down when you trigger a GPIO (frankly, that should be a script bundled in RaspBian and the Pi should have at least 1 button that binds to this, or a safe jumper for halt or reset). The least risky is pulling out the plug, but nobody, from any OS endorses this. You can still SSH into the Pi and halt it. If its standalone then you do heartbeats to reboot the system or write a CRON for safe reboots. – Piotr Kula Mar 01 '15 at 21:41
  • 1
    This is a nice answser. http://raspberrypi.stackexchange.com/questions/6914/safe-way-to-shut-of-pi-without-keyboard-access-to-command-line-etc Basically if you pull somwthing out the USB port it will trigger a halt. I vote for that. Hmm.. Goldilocks answer... weird. – Piotr Kula Mar 01 '15 at 21:46
  • If you have no SSH and no keyboard how have you made changes to the System (SD Card)? – Steve Robillard Mar 02 '15 at 02:06
  • @SteveRobillard - I power off (uncleanly), eject the card and plug it into another machine. I make the necessary config changes there, then plug back into the Pi and boot. This is why I'm interested in the safest way to power off when I am forced to do it uncleanly. I do not expect to need this often. Only once or twice after a silent install while I get networking and remote access configured. – pauldoo Mar 03 '15 at 21:14
  • in this admittedly corner case wouldn't having a backup of the disk image (and perhaps a second SD Card) be a better solution? – Steve Robillard Mar 03 '15 at 21:52

2 Answers2

2

I vote #2, just cut the power as quickly and cleanly as possible. Pulling the SD card while there's power does NOT sound like a good idea.

If you cut the power after the system has been totally idle for a few minutes you should not run into too many problems -- just there's no guarantee. Because of that, it's a good idea to add this to /etc/rc.local (presuming you are using raspbian or something similar):

echo '-y' > /forcefsck

[edit Jan/17] I'm not sure if jessie respects this method; for current versions of Raspbian and other systemd based distros instead add this to /boot/cmdline.txt:

fsck.mode=force

You'll probably notice fsck.repair=yes is already there; together these should give you maximum fsck-ness ;) See also man systemd-fsck. Beware that cmdline.txt must remain all one line; separate parameters with a space.

This will force fsck to run on the root filesystem at every boot. It may add 5-10 seconds or more to your boot time depending on how big the fs is, but it will decrease your chances of ending up with an inconsistent filesystem.

Alternately, I have a suggestion here about how to rig the system to shut down properly when you remove something from a USB jack.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
0

I have run into this problem a few times. So far method #2 never caused damage to the card. Pulling the card seems less ideal because of wearing the contacts. There are a few things you have to consider:
1) I've configured my device to keep log files in memory so the card is less often written to.
2) If you shutdown the device unclean, the device will notice it anyway and keeps complaining about possible damage until you check the file system.

The safest option still remains to shutdown the device as it is supposed to be.

Recently a safe method to shutdown/reset has appeared on the internet. 3 Methods are featured in this article: http://www.linuxuser.co.uk/tutorials/raspberry-pi-reset-switch-tutorial of which one involves some soldering and the other two only use cheap components that are available everywhere and involve no soldering or any type of modification of your device.

wie5Ooma
  • 296
  • 1
  • 3
  • 11