3

I'm running my Pi as an Airplay server(using Shairport). Now I'm wondering if there is any way to prevent the SD-card from getting corrupted if I cycle power unexpectedly? I read about mounting the root filsystem read-only and ramdisks, but couldn't find a good explantation anywhere.

Is it possible to configure the os in such a way that it is safe to cycle power at any time? I don't need to store any changes after the system is booted, just boot the same state every time power is cycled.

Thanks in advance!

Edit:

Ok, after some more research, this is my current /etc/fstab. It works, but / is still mounted in rw mode. At least I'm using sync and noatime which should reduce the probability of the SD card getting corrupted.

#
# /etc/fstab: static file system information
#
# <file system>        <dir>         <type>    <options>          <dump> <pass>
/dev/mmcblk0p1  /boot                   vfat    ro,noatime                 0       0
/dev/mmcblk0p2  /                       ext4    noatime,nodiratime,sync    0       0
ramfs           /tmp                    ramfs   defaults,noatime,mode=1777 0       0
ramfs           /var/cache/pacman       ramfs   defaults,noatime,mode=1777 0       0
ramfs           /var/log                ramfs   defaults,noatime,mode=1777 0       0
ramfs           /var/tmp                ramfs   defaults,noatime,mode=1777 0       0

Just mounting / in ro mode won't work. Well, the Pi boots but some serives (like Logger Daemon) can't start and the shell gets flooded with error messages.

Does anybody know a way to mount /var in a ramfs and load the original files on boot? I found many sources on how to do it, but the Arch distro seems to have changed the way you run bootscript to systemd recently and I can't get it to work that way.

Edit2:

I tried mounting /var on a ramdisk, but the system didn't boot afterwards so I can't post my original code.

What I did was copying /var to /var-new and adding an entry to my fstab

ramfs           /var                ramfs   defaults,noatime,mode=1777 0       0

I removed the entries for the subdirectories of /var, of course. Then I added a systemd "bootscript" called mountramfs.service which ran the script /mountramfs. Activated this with

systemctl enable mountramfs.service
systemctl start mountramfs.service

the /mountramfs contained something like

cp -R /var-new/ /var/

What I tried to achieve with all this was that /var-new would be copied to the empty /var directory on the ramdisk. This would have made it possible to use the same directory every time the system reboots. It started, but couldn't run some services(including ssh) so i couldn't access it anymore and only watch the messages on my tv(via hdmi).

What's wrong with this approach? can anybody help?

1 Answers1

2

Booting from a read only partition guarantees you to be able to boot. Then, if you have to write to other partitions you can always check their integrity first thing after booting. It is advised to write to preallocated file(s) who are ideally size of single sector, or at least an exact multiple. Using something like SquashFS could solve your problem. Berryboot allows you to do it in an easy way. If you want to do it on your own you can find some info here.

avra
  • 1,273
  • 8
  • 9