LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
LinuxQuestions.org Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here.

Notices


Reply
  Search this Thread
Old 09-30-2018, 11:19 PM   #1
marler8997
LQ Newbie
 
Registered: Sep 2018
Posts: 5

Rep: Reputation: Disabled
A readonly rootfs with a writeable overlay...without an initramfs


Thought I should share and discuss a solution I came up with for having a readonly rootfs without having to have an initramfs. What I wanted was a readonly rootfs in its own block along with a writeable block for storing persistent changes (an overlay works great for this). My first thought was to create an initramfs to set this up but I think I came up with a better solution. The problem with an initramfs is that pretty much everything in it is just a copy of what's already in the rootfs. So the more you put into your initramfs (maybe you want it to more tools in case it fails and you want to recover) the more duplication you create (NOTE: bootloaders have the same conundrum with the kernel).

So the solution I came up with was to boot directly to the readonly rootfs and setup the overlay from there before starting systemd/sysv or whatever init program is installed. I created a small BASH script that sets up the rootfs overlay and then "exec"s to the real init process and you're done, no need for an initramfs. Since the rootfs is readonly, you can use a squashfs and you just put it in its own partition on the disk. I call the script "reinit" and it's also configurable via the kernel command line. Here's an example of what it might look like:

NOTE: in this example, I'm creating two disks, one that contains the bootloader/kernel/rootfs, and the second which is meant to hold all the changes to the rootfs. This allows you to 1) see all the changes by looking at the contents of the second disk and/or 2) swap out disks to update the rootfs and/or revert back to the stock rootfs

sda1: contains the rootfs (can be squashfs because it's readonly!)
sdb: a whole disk formatted with some filesystem that contains the upper/work dirs for the rootfs overlay

root=/dev/sda1 init=/sbin/reinit reinit.mountsrc=/dev/sdb

I thought it was a pretty neat idea. Now I don't have to bother with an initramfs, what should I have in it, should I sign it, how do I duplicate error handling with what the rootfs does etc. But I'm new to linux so there might be better ways to do what I've done here. Let me know if anyone knows of better solutions.

The reinit bash script is included below:
Code:
#!/bin/bash
#
# Support parameters (via command line or kernel parameter):
# ------------------------------------------------------------------------------
# reinit.mountsrc=<source_device>
# todo: maybe support reinit.dir (use a sub-directory in the mounted filesystem)
# todo: maybe support mounttype when an explicit mountsrc is supplied
# todo: add support for reinit.init=...

onfail() {
    echo Error occurred, dropping to bash
    /bin/bash
    exit 1
}
trap onfail ERR

echorun() {
    echo "$@"
    "$@"
}
echorun mount -t proc none /proc

# process kernel and passed in command-line parameters
set -- $(cat /proc/cmdline) "$@"
for arg in "$@"; do
  case "$arg" in
    reinit.mountsrc=*)
      mountsrc="${arg#reinit.mountsrc=}"
      ;;
    reinit.dir=*)
      dir="${arg#reinit.dir=}"
      echo Error: reinit.dir not supported yet
      onfail
      ;;
  esac
done

echo "reinit.mountsrc='$mountsrc'"
#echo "reinit.dir='$dir'"

if [[ -z "$mountsrc" ]]; then
  echorun mount -t tmpfs none /reinit_dir
else
  # todo: maybe support an explicit mount type
  echorun mount $mountsrc /reinit_dir
fi

echorun mkdir -p /reinit_dir/rootfs /reinit_dir/rootfs_upper /reinit_dir/rootfs_work
echorun mount -t overlay overlay /reinit_dir/rootfs -o lowerdir=/,upperdir=/reinit_dir/rootfs_upper,workdir=/reinit_dir/rootfs_work

echorun mkdir -p /reinit_dir/rootfs/oldroot
echorun cd /reinit_dir/rootfs
echorun pivot_root . oldroot
echorun exec /sbin/init
echo Error: reinit: exec /sbin/init failed
onfail
 
Old 04-20-2020, 05:09 AM   #2
MCon
LQ Newbie
 
Registered: Apr 2020
Distribution: Debian Sid
Posts: 2

Rep: Reputation: Disabled
Question News?

Hi,
I stumbled on this and it looks interesting.
Do You have newer versions of this?
TiA!
 
Old 04-21-2020, 01:39 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by MCon View Post
Hi,
I stumbled on this and it looks interesting.
Do You have newer versions of this?
TiA!
Why do you think it requires an update?
Have you tried it?
 
Old 04-21-2020, 01:49 AM   #4
MCon
LQ Newbie
 
Registered: Apr 2020
Distribution: Debian Sid
Posts: 2

Rep: Reputation: Disabled
Hi,
I asked for a new revision, if available, because it has several issues marked "TODO" and one such thing (reinit.dir) would be useful to me.
Since it is "old code" I also wanted to avoid starting from something somewhat obsolete.
Code is simple enough I went ahead and modified to suit my needs.

Thanks
Mauro
 
Old 04-22-2020, 05:57 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by MCon View Post
Code is simple enough I went ahead and modified to suit my needs.
You might want to share that; others might benefit. That's how FOSS works, even if it's only small things.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What's is supposed to be in mtab when using overlay and / readonly? Weber Kai Linux - Newbie 2 03-25-2017 06:03 PM
initramfs - switch_root unable to load new rootfs ExternalUse Linux - Kernel 2 02-11-2015 08:51 AM
[SOLVED] What are the differences of rootfs and initramfs? ayyasprings Linux - Embedded & Single-board computer 1 11-26-2014 11:48 AM
questions on rootfs mouting and on initramfs functionning details heavymetalhero Linux - General 2 08-14-2012 05:02 AM
Can rootfs and /proc be readonly? coffeecoffee Linux - Newbie 3 09-27-2009 04:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 05:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration