Score:0

Initramfs-tools script for mounting compressed folders on tmpfs

ug flag

I'm trying to create a small rescue partition (~800MB spare space after all other system and storage partitions) for a laptop with specialized hardware that requires special kernel and kernel modules such that stock rescue tools such as Clonezilla Live and GParted Live don't work very well. The partition is too small to include the modules and software needed for what I'd like to build, so the bulk of the system needs to be compressed and loaded into RAM at runtime. Additionally, I prefer to have the system fully extracted instead of relying on squashfs, and I would like the ability to save changes made to the system as much as the partition allows. Using Ubuntu 20.04 as the base system, I devised the following scheme for this purpose:

  1. After building the initial system in chroot, create and empty folder /ram as the mount point for the tmpfs to house decompressed system and user files at runtime, move all system and user file folders (home,usr,var,opt,etc, etc.) into /ram and replace each with a symbolic link to /ram/$dir. Compress /ram as/ram.tar.gz and leave the special mount points (dev, sys ,run ,tmp, and proc) as well as boot alone on rootfs. Delete /ram/* and write the rootfs onto the partition.
  2. At boot, after kernel and initrd are loaded, premount rootfs, mount tmpfs on /ram, then extract the content of /ram.tar.gz into /ram. This has to happen before rootfs replaces initramfs as root since otherwise rootfs can't access the utilities needed to mount /ram and extract from the compressed archive.
  3. Mount rootfs and proceed with boot.
  4. At shutdown, back up /ram.tar.gz as /ram.tar.gz.bak then compress the modified /ram into /ram.tar.gz. This can be achieved using systemd.

Obviously things can go wrong in step 4 and therefore step 2 needs to include the integrity of /ram.tar.gz and load the backup if that fails, but this should be just a few additional commands to include in the initramfs-tools script and beyond the scope of my question, which is:

Without altering /usr/share/initramfs-tools/init, how do I create a script in /etc/initramfs-tools/scripts to accomplish step 2? The commands should be easy enough as

mount -t tmpfs -o $OPTIONS none /ram
tar xvzf ram.tar.gz

But I'm a bit at loss as to which folder to include the script (init? local? top? bottom? premount?) and how to work with the PREREQ header in the script itself.

Score:0
ug flag

It turned out to be pretty straightforward. Create:

/etc/initramfs-tools/scripts/local-bottom/ramfiles
#!/bin/sh

set -e

PREREQ=""

prereqs()
{
        echo "${PREREQ}"
}

case "${1}" in
        prereqs)
                prereqs
                exit 0
                ;;
esac

mount -t tmpfs -o size=100% none ${rootmnt}/ram
tar xvzf ${rootmnt}/ram.tar.gz -C ${rootmnt}


then

sudo chmod +x /etc/initramfs-tools/scripts/local-bottom/ramfiles
sudo update-initramfs -u
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.