9

I need a Linux distro that will give me the following:

  • Run on a Raspberry Pi
  • Can reliably survive power loss (like via a read-only filesystem)

I've been able to hunt down some documentation on how to change a regular Linux distro over to read-only mode. I was hoping that there would be a distro already built that was designed to run in an embedded environment.

I don't need many packages or drivers, just enough to have the Pi working with USB/Ethernet. I don't need any GUI interface or anything, this will just be running a custom service built in C.

Does anyone know of a distro that would fit?

2
  • Given this is for an embedded application, two quick things you might want to note. First, if you decide to go the Linux route, look into using a real-time kernel to ensure your system can respond quickly enough. Second, you might also want to set the watchdog timer up to take action in-case of any software loops/errors. Dec 8, 2013 at 8:25
  • Check out raspberrypi.stackexchange.com but I think this question is off topic there too as it is a shopping type question, Dec 8, 2013 at 22:50

7 Answers 7

3

Most embedded systems use a custom built kernel. One tool to facilitate that is Buildroot, a set of scripts to build the GNU gcc toolchain, the uClibc library in place of the (huge) GNU libc, the Linux kernel, BusyBox and other utilities/packages for an embedded board's root filesystem. The RaspberryPi is a relatively new board, so support of it in Buildroot is still under development, but there are apparently a project, another project and an individual's work. There will probably be more as RP manufacturing ramps up and distribution improves.

Using Buildroot, you could build a Linux kernel and root filesystem exactly as you described in your question. Depending on your Internet connection speed and your development PC's capabilities, you might have the binaries in 1 to 4+ hours. The downside is that resultant binaries are not tested nor guaranteed to successfully boot & execute. A system console is mandatory for debugging the boot sequence. See my answer for How can I access my mini-pc (RaspberryPi / MK802 / Mele A1000 / VIA APC) via ethernet/wifi without having Monitor? But given that the RaspberryPi was designed to be unbrickable, this downside should not be a deterrent to building your custom kernel and RFS.

Regarding "survive power loss": Proper selection of the filesystem can usually alleviate this concern. The MTD device layer plus a journaling filesystem (e.g. jffs2) has shown to be quite robust from experience. For near-absolute protection, there's the initramfs that uses ramfs (not a fixed-sized ramdisk) with no switchover to a R/W filesystem.

Addendum

A 30-slide intro on features of Buildroot is here
At the end (#27) there is mention of a few similar and alternate tools to build embedded systems.

2

TinyCoreLinux is read only by default (persistence is optional): http://www.tinycorelinux.net/ports.html

2
  • Didn't you see "Arm V6 Raspberry Pi" title on the link I have provided?
    – avra
    Dec 8, 2013 at 1:31
  • I seem to have missed that. Upvoted ;)
    – Journeyman Geek
    Dec 8, 2013 at 5:54
1

Having had a Seagate Dockstar with console access, I installed Debian squeeze onto it. As a starting point to make it run on read-only root, I used this excellent article 1 by Jeff Doozan. The basic strategy involves creating a script that, upon every boot, mounts the necessary writable directories as a tmpfs. I quote the script by Jeff 2 here (kudos to Jeff!)

#!/bin/bash
DIRS="/tmp /var/log /var/run /var/lock /var/tmp /var/lib/urandom /var/lib/dhcp /etc/network/run"
for DIR in $DIRS; do
  echo "Mounting $DIR as tmpfs"
  mount -n -t tmpfs tmpfs $DIR
  if [ -d "$DIR-saved" ]; then
    echo "Restoring $DIR-saved to $DIR"
    tar -C "$DIR-saved" -cf - ./ | tar -C "$DIR" -xpf -
  fi
done

echo "nameserver 4.2.2.1" > /var/tmp/resolv.conf
touch /var/lib/dhcp/dhcpd.leases

exec /sbin/init

Save the lines above as a script called /sbin/init-ro on your target rootfs and make it executable.

chmod 755 /sbin/init-ro

In order to use this script during boot-time, you have to prepare the system rootfs a bit (all quoted from Jeff's script 2 (adapt $ROOT to the actual location of your mounted rootfs).

# Configure dhcp-client to write resolv.conf to /tmp instead of /etc
sed -i 's/\/etc\/resolv.conf/\/var\/tmp\/resolv.conf/' $ROOT/sbin/dhclient-script > /dev/null 2>&1
rm $ROOT/etc/resolv.conf
ln -s /var/tmp/resolv.conf $ROOT/etc/resolv.conf


# make /etc/network/run/ a symlink to /tmp/network/
rm -rf $ROOT/etc/network/run
ln -s /var/tmp/network $ROOT/etc/network/run


# Fixes from http://wiki.debian.org/ReadonlyRoot

rm $ROOT/etc/blkid.tab  > /dev/null 2>&1
ln -s /dev/null $ROOT/etc/blkid.tab

rm $ROOT/etc/mtab  > /dev/null 2>&1
ln -s /proc/mounts $ROOT/etc/mtab

rm $ROOT/etc/rcS.d/S12udev-mtab

rm -rf $ROOT/var/log/*

After having the rootfs prepared like above, you can mount the rootfs read-only in /etc/fstab (replace ext2 with the filesystem you're using or just use rootfs instead).

/dev/root  /                 ext2  noatime,ro   0 1

Finally, you have to append the following to your kernel parameters (i.e. in /boot/cmdline.txt on Raspi) in order to run the script before the actual /sbin/init. (the following is just an example of root and rootdelay parameters. the important part that has to be appended to the line in cmdline.txt is init=/sbin/init-ro.)

root=/dev/mmcblk0p2 rootdelay=2 init=/sbin/init-ro

But be aware that for any software requiring write-access on the rootfs you have to mount the appropriate tmpfs locations or write to external storage.

1

My 2 cents, it's far easier (and nicer in the end) to make a reliable battery backup for a Pi than it is to live with a read only OS. Of course, that'd mean you'll need some very basic knowledge of electronic (and I mean BASIC; we're talking 3-4 components). This guy made a fancy one with just a few more: https://raspberrypi.stackexchange.com/questions/1360/how-do-i-build-a-ups-like-battery-backup-system

If you do this, DON'T use LiPo; NiCad are what you want. LiPo can't take constant over charging; you've been warned.

Also, you seem to be very worried about something that, in my experience, is a very minor issue. I thrash my Linux boxes all the time and a sudden unscheduled shutdown is a matter of course when I can't be bothered. If you disable the logs then you'll rarely even get any complaints about doing that.

To disable all the logs you can add the following line as the first rule in /etc/rsyslog.conf :

*.* ~

Even when there is a problem, 99.9999% of the time (by that I mean nearly every time in my personal experience) that problem is taken care of when the disk is next scanned. When that happens mainly depends on weather the OS noticed what you did (strangely enough it usually doesn't). Since a Pi uses SD cards I'd imagine that this happens even less on a Pi than it does on my PC.

1

If I remember correctly a read only filesystem will not 'secure' the SD-card. I have 10 Pi's running at a customer (current uptime over 80 days for half of them) where the power isn't as stable as you'd expect / want. Took me a while to find power supplies (cheap chargers rated at 3A and 'expensive' iPad chargers rated at 2.3A) that could actually keep the Pi's running for more than a couple of days, before that I had all kinds of SD corruption issues, including with one that was only used read-only IIRC.

My issue is mostly solved now (because of the new supplies), but for future projects I'm looking at doing an NFS root filesystem. There are a lot of tutorials about this already, mostly resolving around the normal Pi fs images, but it's fairly easy to do a minimal debootstrap and change it into a read only root filesystem over NFS. Pair this with uboot for the Pi and a smart uboot script, and your SD-card will contain just a few megs worth of RPi firmware, uboot image and uboot script.

0

I have heard good things about Puppy Linux for this. Although I must admit that I have not tried it.

It can be set to not write back to the SD card.

-1

There are four images available from raspberrypi.org's download page:

  • Debian "wheezy"
  • Soft-float Debian "wheezy"
  • Arch Linux ARM
  • QtonPi

And here is the only one which is read only by default (but that can be changed if needed):

Hopefully, one of these will meet your needs.

3
  • None of these are read-only by default. Sep 18, 2012 at 14:27
  • @alex, Good point.
    – Yedric
    Sep 19, 2012 at 3:08
  • TinyCoreLinux added to the list. It is read only by default.
    – avra
    Dec 8, 2013 at 1:31

Not the answer you're looking for? Browse other questions tagged .