14

Since the first version of Raspberry Pi, I use a customized version of Raspbian in order to mount the rootfs in read-only. Basically I created symlinks in RAM for /var directories so applications don't log to the sd-card anymore.

Because RPi4 has plenty of RAM I would like to try to load the whole rootfs in RAM, to measure performances and avoid those customization.

How can I load the rootfs in RAM at boot? After, I should be able to unmount the partition completely without any effect to the os.

1 Answer 1

17

The short answer is this is possible but there is no simple setting that you change and it just works. It takes a lot of effort.

You can't do this directly because ext4 is not a valid format for an initial ramdisk. You must convert the filesystem prior to boot. Mount the Raspbian root partition on another Linux system and then create a CPIO archive of all the files on it. The command to do this on Ubuntu 18.04 would look something like this:

proot -0 -q qemu-arm -w / -r /mnt/root sh -c "cd / && find * -xdev -not \( \
              -path host-rootfs -prune \
              -path run -prune \
              -path proc -prune \
              -path sys -prune \
              -path boot -prune \
           \) | cpio --create -H newc" | xz -C crc32 -9 > initramfs

Then you put the resulting archive in the /boot partition of your SD card and specify it as the initramfs in config.txt.

The potential performance benefits of doing this are great. Boot takes longer because it takes a long time to copy the whole filesystem into RAM and decompress it, but once that is done the system boots in about 3 seconds and is very responsive.

However simply converting the default Raspbian image to an initramfs is unlikely to work even with 4GB RAM. The decompression step requires approximately twice as much RAM as the size of the initramfs, and if you get past that the system isn't set up to run from RAM and will likely run out due to temporary files and logs etc.

You also cannot modify the filesystem at all this way. If you want to change a file you have to rebuild the initramfs file on another system.

The best way to do this is to build a completely customized Raspbian image from packages. You can get the size down as low as 50MB for the base system and then add only the things you need.

I have made a tool to do this which you can find at: https://github.com/ali1234/rpi-ramdisk

My tool is capable of building Raspbian ramdisks which can run even on a Pi Zero with 256MB of RAM, and which can also be loaded over PXE or with rpiboot. Unfortunately it is not designed to be easy to use, so if you really want to go full ramdisk you will have to do a lot more research, as there are loads of details I've skipped here for clarity. This answer would have been several pages long otherwise...

Also note that I haven't yet finished adding support for the Pi 4, although it should at least boot.

2
  • Thanks for the complete answer. By the way, the size of my customized Rasbian rootfs is just above 1 GB. But I understand for my application is not worth to do such a work.
    – Mark
    Aug 20, 2019 at 16:54
  • Thanks for the detailed info. I haven't tried your tool yet, but I see that it hasn't been updated in a couple of years; do you know if it is still functional today? Do you have documentation what can be specified in your config files? I haven't read through the python code yet but from what I gather, I would need to custom code every single package definition that I want? Or are there default package names that can be used? Thanks!
    – Eric B.
    Jan 7, 2022 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.