I am building a custom Live Linux boot image based on Arch Linux. I've modified the stock initrd to contain a custom script which does the following:
- After boot, only the initramfs exists. I first locate and mount the boot media itself on
- Then, I loopback-mount my squashfs root filesystem on
- Then, I mount a tmpfs on
workdirectories in that directory.
- Finally, I mount an overlayfs using
/roas the bottom layer,
/rw/dataas the top layer and
/rw/workas the work directory. I mount this overlayfs on
- I now use
switch_rootto switch to
/new_rootand continue booting.
The problem with this approach is that the initramfs contents remain in RAM well after the system has booted.
Is there any way to rework the process such that, after switching to the real root, the initramfs can be removed from memory? The reason this is important is because I am trying to boot my live Linux on a small memory footprint machine (192MB) and the size of the initrd (I based it on initramfs-fallback) means that after everything has booted, I only have about 30MB of free RAM. In contrast, a typical installed Arch Linux will be using only about 18MB of RAM. This indicates to me that the unpacked initramfs (about 66MB) is definitely still in RAM, along with another 66MB or so of RAM which I can't figure out the usage from. (Perhaps for some reason the initramfs is actually duplicated in RAM?)
I could obviously shrink my initramfs (I'm working on figuring out how to specify specific modules to include in the package) but even so, a large chunk of RAM will remain used after boot on low-memory systems.
EDIT: I tried reworking my initramfs to simply mount one tmpfs on
/image, and then the script creates the
work directories inside of
/image. Then, the script does the mounts and then mounts the overlayfs on
/new_root. The result is exactly the same - the RAM usage is approximately double the size of the initramfs plus the actual executables in RAM.
I requested a pre-mount break and, at that stage, only the size of the initramfs is used in RAM - this makes sense. I then requested a post-mount break and found that the RAM usage was the double amount that I observed in the booted system. So it looks like something in the various mounts is causing the RAM usage. I'm going to try tracing the process by adding
sleep commands to the initrd scripts to see if I can isolate which command is causing the memory usage...
EDIT AGAIN: So the culprit of the "double" RAM usage was SquashFS. So, it looks like I should recompress the filesystem using gzip instead of using lzma. Ok, part of the mystery solved.
But this still doesn't deal with the initramfs continuing to use RAM after the root is switched. So that's still a mystery.
I'm wondering if perhaps the kernel tries to unmount the initramfs internally but can't do so because there's things mounted on it? But that wouldn't make sense because you will always have something mounted off the initramfs before you switch root anyway...