Can I stack on a second overlayfs from RAM to temporarily install more packages?

I think I figured out how to mount a tmpfs overlay on root. The following works on my device, and Id think it would work on others, but idk for sure how portable it is.

Basically, this sets up an identical root filesystem at /newroot using bind mounts, with the only difference being that the overlay uses a tmpfs directory for upper overlay, and uses the /overlay/upper:/rom for lower overlay.

This makes it so that when when you write to the filesystem at / it goes to persistent overlay, and when you write to the filesystem at /newroot it goes to tmpfs overlay.

running pivot_root /newroot /newroot/oldroot switches the system to use the tmpfs overlay.

running pivot_root /oldroot /oldroot/newroot switches the system back to using the persistent overlay. Rebooting also would do this, but is not required to get the original configuration back.

Heres the code. Hope it is of some use to you.

# create directories and setup a dedicated tmpfs for the overlay
mkdir -p /newroot /oldroot /overlay_tmpfs
mount -t tmpfs tmpfs /overlay_tmpfs
chmod 755 /newroot /oldroot /overlay_tmpfs
mkdir -p /overlay_tmpfs/rom /overlay_tmpfs/lower /overlay_tmpfs/upper /overlay_tmpfs/work

# setup read-only bind mounts of '/rom' and '/overlay/upper' into '/overlay_tmpfs'
mount -o bind,ro /rom /overlay_tmpfs/rom
mount -o bind,ro /overlay/upper /overlay_tmpfs/lower

# setup tmpfs overlay (tmpfs <-- /overlay/upper <-- /rom) at '/newroot'
mount -t overlay -o rw,noatime,lowerdir=/overlay_tmpfs/lower:/overlay_tmpfs/rom,upperdir=/overlay_tmpfs/upper,workdir=/overlay_tmpfs/work/ overlayfs:/overlay_tmpfs /newroot

# bind-mount everything else into '/newroot'
cat /proc/mounts | awk '{print $2}' | grep -Fv '/newroot' | grep -Ev '^\/$' | while read -r nn; do mkdir -p "/newroot${nn}"; findmnt "/newroot${nn}" 1>/dev/null 2>/dev/null || mount -o bind "${nn}" "/newroot${nn}"; done

# THIS ENABLES THE TMPFS OVERLAY
# pivot root to '/newroot'
pivot_root /newroot /newroot/oldroot
wifi up
/etc/init.d/wpad restart
/etc/init.d/network restart

# THIS DISABLES THE TMPFS OVERLAY
# pivot root to '/oldroot'
pivot_root /oldroot /oldroot/newroot
wifi up
/etc/init.d/wpad restart
/etc/init.d/network restart
3 Likes

Yep I also started thinking on 4/32 devices it could be a valid strategy to

  • expand overlay with tmpfs ramdisk
  • re-install luci-ssl on every boot

Will give this a shot.