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:
- 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.
- 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.
- Mount
rootfs
and proceed with boot.
- 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.