Sunday, September 18, 2016

I Really, Really Hate the SD Card in the Raspberry Pi

so much so, that I tried the beta version of the Boot From MSD (mass storage device) changes the Pi folk blogged about recently.

TL;DR version: It didn't work for me because I messed up, but I found another solution that is pretty darn compelling in the process of trying it out.

First, I found the Pi blog post here <link>, and read it over about three hundred times because it was the kind of thing I've been fighting for years now. Then, in typical form I got on Amazon and ordered a solid state disk and a little box to hold it and adapt it to USB.

Wait, before you go off and do the same thing I did, check out the link above and read the rest of this post. Also, the reason I said to use my link is because it points to the 'official' post about the boot from MSD, there are a lot of copies out there on the web and some of them are wrong. Go to the real source and start from there. I won't go into the details of what to do since I would only be copying someone else's hard work and I might make a mistake that would screw you up.

Here's the hardware I'm using:


These things cost me about $35, and I'm sure I could have gotten them cheaper if I was willing to wait for a boat from Taiwan, but I wanted to try this out right the heck now.

I went into Raspberry Pi's github repository <link> for the instructions and followed them exactly on my Pi 3B, and of course, it didn't work. Not because the instructions were bad, but because, in exuberance, I ordered a USB 3.0 device. They specifically say they don't support USB 3 and I just ignored that when I looked for the stuff I needed. However, it impressed on me something that I had overlooked way back when I tried a USB stick to overcome the problems with SD cards.

You ONLY NEED TO BOOT FROM THE SD CARD !! You don't need to run with it. That means, you put the boot stuff on the card, boot from it, and then ... get ready for this ... you mount it READ ONLY so a power failure won't corrupt the card because of files left open when the little guy dies. If you don't write to the card, it won't get messed up.

Sure, there will be a thing or two to consider doing this, for example when I do an update-upgrade (shudder), it will probably fail if there are changes to the boot code. Fine, I'll just make a note to myself to remember to remount the boot file system RW before I do that; or more likely, I'll fix it after the upgrade fails with some obscure error message. I'm actually running one of my little machines this way right now and it seems to be doing fine, and it boots awesomely fast compared to the SD card.

I've pulled the power plug a few times to see what happens, and YES darn it, I managed to corrupt the file system on the SSD. But, and this is a really big thing to me, fsck fixed it and it just kept on keeping on. I hardly noticed the problem.

Let's think about this a bit more. I don't have to even think about the long tedious process of backing up the SD card any more. I just make a couple of copies of Raspbian lite on cheap little SD cards just big enough to hold it and stick them to the wall taped to a 3x5 index card with a clear label I'll remember later. The SD card dies because of some random act, I grab another card, plug it in, and away the Pi goes because all the stuff that really matters is on the SSD. I can also use this technique on any of the Pi's I have, even the oldest. You just need to be able to read the SSD to finish the boot process; I don't need a whole system on the SD card. An unexpected benefit of this is that you have a fully boot-able system on the SD card that you can use to repair something if you need to.

Here's the cmdline.txt from the /boot on my Pi3B:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/sda2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

And the entry I have in fstab to set the boot to read only:

/dev/mmcblk0p1  /boot           vfat    ro,auto,user,exec,async  0       2

I'm not sure that this fstab entry is totally correct, but it gets the job done; root can't write to the boot partition. I keep two cmdline.txt files for backup in the /root directory: cmdline.txt.ssd for ssd boots, cmdline.txt.sd for sd card boots. I just copy whichever I need to cmdline.txt so I don't have to look up the weird name mmcblk0p1 each time I test something in this area.

To back up the SSD, just do a network 'dd' to a big disk somewhere else, or a file by file copy, rsync, something along those lines. I can even automate it now because the SSD is super quick and I'm not wearing it out the same as with an SD card. My little machine may be a little behind, but the data is stored on my NAS, and who cares if a log file got out of date. I'll definitely be looking into that in the next couple of weeks (well maybe).

I could totally eliminate power fail corruption for all the machines I want to someday have if I can come up with a house-wide power-fail shutdown idea that doesn't include $60 UPS devices out in the yard. Remember, my power goes out every time the weather sneezes, or a random drop of water falls from the sky. I've gone through several SD cards along the way. I tried a USB card to solve the problem, but the USB card died in a particularly annoying fashion <link>. I won't go that route again.

I want to step through the requirements for a UPS of some kind so you folks understand (and maybe suggest something):

1. cheap

2. It has to sense power failure and tell the Pi to halt while holding power on
for a minute or more while the Pi actually halts.

3. It has to turn power off to the Pi.

3. It has to sense power coming back, wait a bit for those messy power failures, and supply power to the Pi if it has turned the power to the Pi off.

4. Small

5. cheap

Requirement 3 is important because there is no power management on the Pi. The only way out of a halt condition is to cycle the power. Requirement 4 is important because the power can bounce around a bit and I don't want a bunch of half boots where the Pi has just enough time to start booting before the power goes out again.

I've thought a bit about a little Arduino to do this job for me. If I combine it with one of those cell phone power extenders and a good wall wart, I might be able to come up with something suitable.

Now, I'm going to check out the Chinese vendors for really good buys.

8 comments:

  1. If you are using a Raspberry Pi 3, have you looked at the Network boot option? Instead of having an SSD hooked up you could have all the "important" stuff on your more protected server?
    Link to RasPi about it, unfortunately I do not have a 3, so this does not help me:
    https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/net_tutorial.md

    ReplyDelete
    Replies
    1. Y'know, I spent about an hour pacing around trying to talk myself into booting from ethernet. That would be really great because I wouldn't have to worry at all about the system disappearing during a power failure. I'd have automatic backup and everything. Well, mostly. Heck, the Pi could catch on fire and all I'd have to do is hook up another one because everything is on my NAS. Thinking about it, it would also protect me from power failures because the NAS would shut down gracefully and everything.

      Frankly, I'm not sure what my reluctance is in doing this.

      Somebody out there convince me that I should try this idea.

      Delete
  2. I have 2 RasPi2s that act as network monitors (I was on a beta for the company that made it, and still beta for a router company). I have basically NOTHING going on with them besides having to load the monitor agent... Yet I still get annoyed when they get corrupted, as has happened way to many times. Even with my battery packs. For some reason the 3rd one I have (a RasPi Zero) dies off even more frequently and it has always had a battery pack. Anything I could do would be an improvement, to the point that I am thinking about buying a 3, just to try this out.

    I have not seen much about "downsides" to the network boot. If you give it a shot, let us know. If I get a RasPi 3, I will let you know, because I will definitely try it.

    ReplyDelete
    Replies
    1. I think you can actually boot any Pi over the ethernet if you use the correct code. Also, I think the reason we don't see people talking about this all the time is because you can't do it over wireless; you have to have wires. If you have to run wires, you should, at least, have Power Over Ethernet to make it worth the time.

      And, I figured out why I'm reluctant to try booting over ethernet. If you read the comments around the web and especially on the Pi blog, it's pretty clear that this is a complex process subject to many mistakes that leave you hanging with a Pi that doesn't boot. The idea is great and has huge advantages, but spending days trying to figure out why it is not working would simply drive people nuts and they'd give up.

      The people that want to automate their house want to automate their house, not run a data center. So, I'm perfectly willing to let the folk who are interested in the science of getting it to work get the bugs out first.

      Meanwhile, I suspect I'll never have another SD card problem that can't be recovered from in just a few minutes since there's practically nothing on the card and it's mounted RO. Also, I've found much cheaper SSDs, and the box to hold them, in small sizes that will fit the bill just fine. Twenty bucks worth of hardware and away we go.

      Delete
  3. I've been seeking the perfect Pi UPS for some time. I have a MoPi, but it doesn't quite fit the bill and they're hard to obtain now: https://pi.gate.ac.uk/pages/mopi.html

    The PiUPS looks promising, but doesn't specifically state that it supports the RPi 3. Unfortunately, it's also quite expensive:
    http://www.piups.net/

    I'm currently using a USB PowerBank that happens to both charge and supply power to its output at the same time. These are surprisingly hard to find - most USB power banks will either charge or supply power to the load. Some will do both, but drop the load and need to be powered back on manually after power is removed, so there are traps here for the unwary. I use a "Plox" brand USB power bank and hope it can keep the Pi alive until power is restored.

    Peter Scargill has blogged about his quest for a suitable Pi UPS here:
    http://tech.scargill.net/category/ups/

    ReplyDelete
    Replies
    1. Isn't that just painful? You get a Pi, it's wonderful, but everything you need to attach to is is crap and we have to try for weeks to get something that lives up to its advertising.

      My requirements are slightly different from most folk. I need a clean power down and then a clean power back up when the mains go down. For most of my devices I don't care if they halt for a while when the power is off; I can't control stuff anyway during that period. What I need is a couple of minutes for the Pi to come to a full halt and then a clean restore so it can boot back up. Most commercial UPS for computers do this kind of thing really well. The APS devices work well, but the cheapest with this kind of software are almost $60.

      Sigh.

      Delete
  4. Have you looked at Felix's UPS option at lowpowerlabs.com. It's a bit pricey $20 + $12 for his moteino to give it the ability to smart shutdown and startup IMO but you could use it as a launchpad to make your own system based on his concepts, or support a fellow maker/tinkerer. While I have not used this system to automatically startup my rpi I have used it to soft shutdown my pi that I use in a mobile/portable application.

    Now that I think about it you could probably just use a simple in line battery chargers like one of the ones from adafruit and have the pi periodically check if it is running off of mains or battery and then runs shutdown script. If you have a NPN transistor set up on the battery line with a capacitor of the right size to the base it could be set to cut battery power a little after the pi has fully shutdown. that way when mains comes back on it will be like the power switch was just thrown and the pi should boot up like you just plugged it in. Wow I wasted some money and time on adding extra equipment. Hope this helps.

    ReplyDelete
    Replies
    1. Thanks a lot for the suggestion. Yes, I did look at it and it just doesn't quite fit the bill, as you already noticed. Like I said, I'm not really interested in keeping the Pi running during an extended power failure, I want to take it down gracefully and then get it back up the same way.

      When the powers off, I can't control the pool or the lights anyway, I don't have power to them ... I need to be able to survive power glitches without writing crap to the disk as the Pi dies.

      If you follow the power related posts, I finally came up with a way of doing this pretty well. It ain't cheap, but it actually costs a lot less than several devices supplying the various Pi's I have and plan to have over time.

      Delete