Linux – Mount root as overlayfs

linuxnfsoverlayfs

I've been trying mount root (/) as overlayfs.

OS is booting over NFS to RAM. I've added a premount script in initrd, which creates the 'work', 'upper' and 'lower' directories.

During the boot process I'm copying the contents of NFS to the 'lower' dir. Overlayfs is being mounted into ${rootmnt} after that.

Finally, the init script chroots to ${rootmnt} (next, init from real root etc…) and the OS works fine.

Naturally I can't see the 'work' and 'upper' dirs. How can I do this? What must I change in initrd?

Best Answer

The challenge of mounting root as overlayfs has been solved.

Briefly, the 'lower', 'work' and 'upper' directories should be moved to the 'merge' dir. However, you should consider:

1) There is no need to do something if the 'lower' directory is present as a disk image. Just mounting it. If not, create tmpfs mount point on it and copy all needed files over NFS into it.

2) The 'upper' and 'lower' directories must be located in one filesystem. Creating another tmpfs mount point and thus placing 'upper' and 'lower' directories on it will be enough.

3) Ensure that your initrd.img has modules for NFS and Overlayfs. If they do not exist, then add them in /etc/initramfs-tools/modules.

4) Ensure that your initrd.img has the full version of the 'mount' command. It it does not exist, then add it over hooks in /etc/initramfs-tools/hooks.

For example (some details have been omitted):

/etc/initramfs-tools/hooks/mount_full:

#!/bin/sh
PREREQ="/bin/mount"
prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
            prereqs
            exit 0
    ;;
esac

. /usr/share/initramfs-tools/hook-functions
# Begin real processing below this line

copy_exec /bin/mount /bin/mount_full

exit 0 

Finally, add the pre-mount script in /etc/initramfs-tools/scripts/init-premount/. For example:

/etc/initramfs-tools/scripts/init-premount/ramboot:

#!/bin/sh
PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
            prereqs
            exit 0
    ;;
esac

. /scripts/functions
# Begin real processing below this line

# Preparing work dirs
mkdir /overlaytmp
mkdir /overlaytmp/lower
mkdir /overlaytmp/upper_and_work
mkdir /overlaytmp/merge
mkdir /ramboottmp

# Preparing RAM disks and thus layers
mount -t tmpfs -o size=100% none /overlaytmp/lower
mount -t tmpfs -o size=100% none /overlaytmp/upper_and_work
mkdir /overlaytmp/upper_and_work/upper
mkdir /overlaytmp/upper_and_work/work

...
mount nfs_share /ramboottmp
...

# Copy root content over NFS to RAM
echo "Copying / to RAM ..."
cp -rfa /ramboottmp/* /overlaytmp/lower
# Preparing layers mount points
mkdir /overlaytmp/lower/mnt/lower
mkdir /overlaytmp/lower/mnt/upper_and_work
# Lower layer will be read-only
mount -o remount,ro /overlaytmp/lower

# Mounting overlayfs
mount -t overlay -olowerdir=/overlaytmp/lower,upperdir=/overlaytmp/upper_and_work/upper,workdir=/overlaytmp/upper_and_work/work none /overlaytmp/merge

# Moving layers to merge layer
mount --move /overlaytmp/lower /overlaytmp/merge/mnt/lower
mount --move /overlaytmp/upper_and_work /overlaytmp/merge/mnt/upper_and_work

# Moving merge layer to finally root
mount --move /overlaytmp/merge ${rootmnt}

umount /ramboottmp
Related Question