Persistent home directory


Recommended Posts

Good evening!
I'd like to not only request this as a feature, but explain how one could implement this on their own!

Basically, the idea is that all user customization done via configuration files located in "/root" are lost on each boot.
I know this is intentional, but there's an "easy" way to implement this with clever failsafe mechanics.
I also know that one can work around this by adding a couple of lines to /boot/config/go, and storing the configuration files on the flash drive. This isn't as desirable as Fat32 doesn't properly handle Linux permissions, and can require other manual edits to the go file down the road.


Enter OverlayFS (a feature built into the Linux kernel for eons)

 

First we create the container for our data. I use the truncate command as it is safe and "quick" (note: we are writing over USB so this step will take time no matter which option we use)

truncate -s 4000M /boot/config/root.persist

 


I chose to go with 4000M as it is close to the Fat32 ceiling of "4gb" (note: if you specify 4G you will receive an error)
 

 

Next we format that image, and set up some important directories within it:

mkfs.ext4 /boot/config/root.persist
mkdir /tmp/overlay
mount /boot/config/root.persist /tmp/overlay
mkdir /tmp/overlay/upper
mkdir /tmp/overlay/workdir


 

Finally the special sauce that overlays the image we created on top of the normal unraid /root/ directory:

mount -t overlay -o lowerdir=/root,upperdir=/tmp/overlay/upper,workdir=/tmp/overlay/workdir none /root


 

Anything written to /root/ after this command is run will actually be writting to /tmp/overlay/upperdir, and permanently stored there. The lowerdir will never be modified in this situation as it isn't addressable since we are placing the overlay on top of lowerdir.

 

And to make it persistent, we add this block to /boot/config/go:

if [ -f /boot/config/root.persist ]; then
        mkdir /tmp/overlay
        mount /boot/config/root.persist /tmp/overlay
        mount -t overlay -o lowerdir=/root,upperdir=/tmp/overlay/upper,workdir=/tmp/overlay/workdir none /root
fi


 

A couple of notes:

The if statement above makes sure that we don't try doing anything if there isn't a persistent image for the root folder. It's kind of redundant (the first and second mount commands will just fail and regurgitate errors if the file isn't there) but I prefer a clean console log.
If the image becomes corrupt, or unusable you can safely discard it this way.

Safe mode shouldn't use /boot/config/go so if anything goes wrong safe mode will undo any of the changes contained in the image. Meaning you can boot into safe mode, manually mount the image, and undo whatever you did in upperdir and be back up and running. I'm not sure what you could do to cause those sorts of things.

This also allows for:
Persistent bash history (forget that command you ran before you rebooted? No more.)
Persistent config file storage (tmux preferences, terminal colors, and htop profiles? Oh my.)
Persistent KNOWN_HOSTS and AUTHORIZED_KEYS for ssh.
Anything you would normally want a home directory to be useful for in LinuxLand.

 

 

Edited by Xaero
Fixed two typos
  • Like 3
  • Thanks 4
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.