#!/bin/sh -efu . shell-error OVERLAY="${1:-tmpfs}" BASE=/usr/share/make-initrd/data case "$OVERLAY" in -i|--install) OVERLAY="${2:-tmpfs}" ;; -u|--uninstall) [ -d "$BASE/lib/initrd/boot/scripts" ] || fatal "make-initrd is not installed." [ -x "$BASE/lib/initrd/boot/scripts/$PROG" ] || fatal "The $PROG feature is not installed." sed -i -E "/\.\/$PROG/d" "$BASE"/lib/initrd/boot/method/localdev/check sed -i -E "/register_parameter string OVERLAYROOT/d" \ "$BASE"/etc/initrd/cmdline.d/base sed -i -E "/MODULES_TRY_ADD \+= overlay/d" /etc/initrd.mk sed -i -E "/PUT_FILES \+= \/etc\/$PROG\.conf/d" /etc/initrd.mk rm -f -- "/etc/$PROG.conf" "$BASE/lib/initrd/boot/scripts/$PROG" echo "The $PROG feature was uninstalled!" exit 0 ;; -h|--help) cat <<-EOF Usage $PROG [ | UUID=... | LABEL=... | ] Commands: -i, --install Install $PROG feature. -u, --uninstall Uninstall $PROG feature. -h, --help Show this help message and exit. UUID=, LABEL= and /dev/device specified a device with the R/W layer. By default R/W layer will be created on the TMPFS at the install time. After install or uninstall don't forget to run make-initrd []... EOF exit 0 ;; esac # Install feature [ -d "$BASE/lib/initrd/boot/scripts" ] || fatal "make-initrd is not installed." [ ! -f "$BASE/lib/initrd/boot/scripts/$PROG" ] || fatal "The $PROG feature already installed." grep -qw "./$PROG" "$BASE"/lib/initrd/boot/method/localdev/check || echo "./$PROG" >> "$BASE"/lib/initrd/boot/method/localdev/check grep -qw "OVERLAYROOT" "$BASE"/etc/initrd/cmdline.d/base || echo "register_parameter string OVERLAYROOT" \ >> "$BASE"/etc/initrd/cmdline.d/base echo "$PROG=$OVERLAY" > "/etc/$PROG.conf" grep -qw "/etc/$PROG.conf" /etc/initrd.mk || echo "PUT_FILES += /etc/$PROG.conf" >> /etc/initrd.mk grep -w "MODULES_TRY_ADD" /etc/initrd.mk |grep -qw overlay || echo "MODULES_TRY_ADD += overlay" >> /etc/initrd.mk cat > "$BASE/lib/initrd/boot/scripts/$PROG" < /.initrd/$PROG case "\${$PROG-}" in "") # Silent use default boot exit 0 ;; disabled) echo "rootfs has return back to the read/write mode" >&2 exit 0 ;; tmpfs) echo "rootfs switched to the read-only mode, using tmpfs as overlay" >&2 device= ;; UUID=?*) device="/dev/disk/by-uuid/\${$PROG##UUID=}" ;; LABEL=?*) device="/dev/disk/by-label/\${$PROG##LABEL=}" ;; /dev/?*) device="\$$PROG" ;; *) fatal "\$$PROG: invalid $PROG" ;; esac # Wait the device if [ -n "\$device" ]; then i=10 while [ "\$i" != 0 ]; do dev="\$(readlink-e "\$device" 2>/dev/null ||:)" [ -z "\$dev" ] || [ ! -b "\$dev" ] || break i="\$((\$i - 1))" sleep .5 done [ -n "\$dev" ] && [ -b "\$dev" ] || fatal "\$$PROG: invalid device specification" echo "rootfs switched to the read-only mode, using \$dev as overlay" >&2 device="\$dev" fi echo "remounting / with overlayfs" >&2 mkdir -p -- "\$rootmnt.rw" "\$rootmnt.ro" if [ -n "\$device" ]; then mount -- "\$device" "\$rootmnt.rw" else mount -t tmpfs -o mode=755 -- none "\$rootmnt.rw" fi opts="lowerdir=\$rootmnt.ro" opts="\$opts,upperdir=\$rootmnt.rw/rw" opts="\$opts,workdir=\$rootmnt.rw/wk" mkdir -p -- "\$rootmnt.rw/rw" "\$rootmnt.rw/wk" mount --move -- "\$rootmnt" "\$rootmnt.ro" mount -t overlay -o "\$opts" -- overlay "\$rootmnt" mkdir -p -- "\$rootmnt/.ro" "\$rootmnt/.rw" mount --move -- "\$rootmnt.ro" "\$rootmnt/.ro" mount --move -- "\$rootmnt.rw" "\$rootmnt/.rw" rmdir -- "\$rootmnt.rw" "\$rootmnt.ro" echo "rootfs overlayed with overlayfs" >&2 EOF chmod +x "$BASE/lib/initrd/boot/scripts/$PROG" echo "The $PROG feature was installed!"