What you're asking to do is done at least once on just about every commonly configured Linux system out there. Most use the tool made available by
switch_root does is delete all the files out of rootfs (to free up the memory) and then
chroot into a new filesystem and exec a new init process out of the new filesystem.
This happens during system initialization. When a linux system is booted the kernel brings the system up in stages. At first the kernel is executed in memory by some other system - such as a bootloader or the firmware - and at this point the kernel is kind of just left to fend for itself - with no real frame-of-reference for the system on which it was just executed.
This is what the
initramfs image usually appended to its memory space (but possibly also directly compiled into the kernel) is designed to handle. The initramfs is a real linux root (complete w/
/proc and what-have-you) filesystem image - it is the first root filesystem ever mounted by a linux kernel. It includes a root filesystem archive containing any/all system-specific modules/configuration files that are necessary to get the kernel on its feet - to bootstrap it.
Anyway, the kernel mounts that archive as rootfs (basically a tmpfs) and then does whatever is necessary to find some other
/ and mount that one over it. It does this every time you boot your system. It can do it again, and without resorting to unnecessary hacks like unionfs or aufs - both of which are likely to engender all kinds of implementation-specific complications and configuration details (which is not to mention instability).
In the quoted
switch_root description above you'll probably notice the phrase delete all of the files out of rootfs. Obviously this would not be a desirable behavior when switching out of a disk-based rootfs. But it only happens that way with
switch_root to free the memory for the ram-based filesystem anyway - and is completely unnecessary. Here is some more from the earlier quoted article:
The following shell script fragment demonstrates how to use switch_root:
# First, find and mount the new filesystem.
mount /dev/whatever /newroot
# Unmount everything else you've attached to rootfs. (Moving the filesystems
# into newroot is something useful to do with them.)
mount --move /sys /newroot/sys
mount --move /proc /newroot/proc
mount --move /dev /newroot/dev
# Now switch to the new filesystem, and run /sbin/init out of it. Don't
# forget the "exec" here, because you want the new init program to inherit
# PID 1.
exec switch_root /newroot /sbin/init
As you can see above, handling the
/sys related problems can be very simply accomplished. If you were to layer any of
mount --moved mounts, by the way, you would have to deal not only with
mount but also whatever other complications are introduced by your layering system. It is more simple to do just as you describe in the question - mount root from somewhere else.
You'll need to do basically all of the stuff that happens in a typical initramfs configuration and very little else - (which is not meant to include Debian or Redhat's initramfs images - both of which are way over-engineered). The only real issue you might encounter is how to get PID1 to follow suit - if you leave your system's init stranded on some orphaned rootfs very weird stuff could soon start happening on your system. The obvious way to handle this is to prepare from initramfs on. Just make sure that your hard-disk's
init process is prepared to
exec another later down the road when you want to switch roots. If you're using a
init then this complication is already handled for you:
switch-root ROOT [INIT] Change to a different root file system
If you are using a
init you should study the unit files in
/usr/lib/systemd/system/initrd* to get an idea of what the commonly scripted
systemd-style switch-root situation is like.
Another way to go about it might be to mimic
switch_root in initramfs - but to leave out the part where you delete all of the initial root's files. Arch Linux systems configured with
systemd in initramfs do this. On those, the initramfs root mounts itself into
/run/initramfs before doing the switch-root and is what the system falls back to at shutdown to cleanly handle sleep/suspend and similar. That may be the best way to go for your case, actually - just a tiny, ram-persistent root-system which you ping-pong off of for your various individually rooted applications.