11#! /bin/sh
22# Mount the root filesystem.
3- # Based on https://unix.stackexchange.com/a/445141, modified for initramfs-tools and Raspberry Pi.
3+ # Based on https://unix.stackexchange.com/a/445141, added error handling and modified for initramfs-tools and
4+ # Raspberry Pi.
45
56PREREQ=" "
67prereqs ()
1617. /scripts/functions
1718# Begin real processing below this line
1819
19- # Debugging
20- set -x
21-
22- # load module
20+ # load modules
2321modprobe overlay
2422if [ $? -ne 0 ]; then
2523 panic " ERROR: missing overlay kernel module"
@@ -29,7 +27,7 @@ if [ $? -ne 0 ]; then
2927 panic " ERROR: missing squashfs kernel module"
3028fi
3129
32- log_begin_msg " Setting up tmpfs overlay..."
30+ log_begin_msg " Mounting root filesystem overlay..."
3331
3432# create some temporary directories under the initramfs's /run
3533# they will be our mountpoints and such, which will get moved
@@ -48,6 +46,7 @@ if [ $? -ne 0 ]; then
4846fi
4947
5048# mount the squashfs and then the overlay to our designated locations
49+ # The mount command available here doesn't know how to create loopback devices, so need to do it separately.
5150losetup /dev/loop0 /run/rootfs/drive/filesystem.squashfs
5251if [ $? -ne 0 ]; then
5352 panic " ERROR: failed to mount compressed filesystem loopback"
@@ -56,17 +55,19 @@ mount -t squashfs -o ro /dev/loop0 /run/rootfs/ro
5655if [ $? -ne 0 ]; then
5756 panic " ERROR: failed to mount compressed filesystem directory"
5857fi
58+ # Now we have both the real filesystem and the tmpfs writable layer, create the overlay.
5959mount -t overlay -o lowerdir=/run/rootfs/ro,upperdir=/run/rootfs/rw,workdir=/run/rootfs/.workdir root " ${rootmnt} "
6060if [ $? -ne 0 ]; then
6161 panic " ERROR: failed to mount overlay"
6262fi
63+ # At this point we have our overlay root at ${rootmnt}!
6364
64- # at this point we have our overlay root at ${rootmnt}!
65- # move the drive's filesystem mount to where RPI's firmware partition should be mounted.
65+ # Move the drive's filesystem mount to where RPI's firmware partition should be mounted.
6666mkdir -p " ${rootmnt} /boot/firmware"
6767mount -n -o move /run/rootfs/drive " ${rootmnt} /boot/firmware"
6868if [ $? -ne 0 ]; then
6969 panic " ERROR: failed to relocate firmware partition"
7070fi
71+ # Cleanup the scratch dir.
7172rm -d /run/rootfs/drive
7273log_end_msg
0 commit comments