1- #! /bin/bash
1+ #! /bin/sh
22
33# Description: This script is designed to mount a DM-Verity root filesystem and
4- # set up OverlayFS. It is driven by kernel parameters and is invoked during the
5- # dracut initramfs phase.
4+ # set up an OverlayFS. It is driven by kernel parameters and is invoked during
5+ # the dracut initramfs phase.
66
77# Kernel Parameters:
88# - root: Specifies the path to the root filesystem. This script is designed to
1212# setups, the script will proceed with the standard OverlayFS setup, ensuring
1313# versatility in its application.
1414# - rd.overlayfs: A comma-separated list defining the OverlayFS configuration.
15- # Each entry should specify the overlay, upper, work directories, and optional
16- # volume for an OverlayFS instance.
15+ # Each entry should specify the overlay, upper, and work directories for an
16+ # OverlayFS instance.
17+ # - rd.overlayfs_persistent_volume: Specifies the path to a persistent storage
18+ # volume to be used by OverlayFS. If not provided, a volatile (tmpfs) overlay
19+ # is created.
1720
1821# Behavior:
1922# - Verifies the presence of the 'dracut-lib' for necessary utilities.
2629# root with the writable overlay, allowing system modifications without
2730# altering the base system.
2831
29- parse_kernel_cmdline_args () {
32+ set -ex
33+
34+ parse_cmdline_args () {
3035 # Ensure that the 'dracut-lib' is present and loaded.
3136 type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
3237
@@ -49,51 +54,49 @@ parse_kernel_cmdline_args() {
4954
5055 # Retrieve the OverlayFS parameters.
5156 [ -z " ${overlayfs} " ] && overlayfs=$( getarg rd.overlayfs=)
57+ # Retrieve the persistent volume for the OverlayFS.
58+ [ -z " ${overlayfs_persistent_volume} " ] && overlayfs_persistent_volume=$( getarg rd.overlayfs_persistent_volume=)
5259}
5360
54- # Modified function to mount volatile or persistent volume.
55- mount_volatile_persistent_volume () {
56- local _volume=$1
57- local _overlay_mount=$2
58-
59- mkdir -p " ${_overlay_mount} "
61+ # Modified function to mount the physical partition
62+ mount_physical_partition () {
63+ mkdir -p " ${OVERLAY_MOUNT} "
64+ # Leverage the partition from cmdline
65+ local partition=" ${overlayfs_persistent_volume} "
6066
61- if [[ " ${_volume } " == " volatile " ] ]; then
62- # Fallback to volatile overlay if no persistent volume is specified.
67+ if [ -z " ${partition } " ]; then
68+ # Fallback to volatile overlay if no persistent volume is specified
6369 echo " No overlayfs persistent volume specified. Creating a volatile overlay."
64- mount -t tmpfs tmpfs -o ${OVERLAY_MNT_OPTS} " ${_overlay_mount } " || \
65- die " Failed to create overlay tmpfs at ${_overlay_mount } "
70+ mount -t tmpfs tmpfs -o ${OVERLAY_MNT_OPTS} " ${OVERLAY_MOUNT } " || \
71+ die " Failed to create overlay tmpfs at ${OVERLAY_MOUNT } "
6672 else
6773 # Check if /etc/mdadm.conf exists.
6874 if [ -f " /etc/mdadm.conf" ]; then
69- mdadm --assemble ${_volume } || \
75+ mdadm --assemble ${partition } || \
7076 die " Failed to assemble RAID volume."
7177 fi
7278
73- # Mount the specified persistent volume.
74- mount " ${_volume } " " ${_overlay_mount } " || \
75- die " Failed to mount ${_volume } at ${_overlay_mount } "
79+ # Mount the specified persistent volume
80+ mount " ${partition } " " ${OVERLAY_MOUNT } " || \
81+ die " Failed to mount ${partition } at ${OVERLAY_MOUNT } "
7682 fi
7783}
7884
79- create_overlayfs () {
80- local _lower=$1
85+ create_overlay () {
86+ local _dir=$1
87+ local _mounted_dir=" ${VERITY_MOUNT} /${_dir} "
8188 local _upper=$2
8289 local _work=$3
8390
84- [ -d " $_lower " ] || die " Unable to create overlay as $_lower does not exist"
91+ [ -d " $_mounted_dir " ] || die " Unable to create overlay as $_dir does not exist"
8592
8693 mkdir -p " ${_upper} " && \
8794 mkdir -p " ${_work} " && \
88- mount -t overlay overlay -o ro,lowerdir=" ${_lower } " ,upperdir=" ${_upper} " ,workdir=" ${_work} " " ${_lower } " || \
89- die " Failed to mount overlay in ${_lower } "
95+ mount -t overlay overlay -o ro,lowerdir=" ${_mounted_dir } " ,upperdir=" ${_upper} " ,workdir=" ${_work} " " ${_mounted_dir } " || \
96+ die " Failed to mount overlay in ${_mounted_dir } "
9097}
9198
92- mount_overlayfs () {
93- local cnt=0
94- local overlay_mount_with_cnt
95- declare -A volume_mount_map
96-
99+ mount_root () {
97100 if [ " $is_verity " = true ]; then
98101 echo " Mounting DM-Verity Target"
99102 mkdir -p " ${VERITY_MOUNT} "
@@ -106,34 +109,19 @@ mount_overlayfs() {
106109 die " Failed to mount root"
107110 fi
108111
112+ mount_physical_partition
113+
109114 echo " Starting to create OverlayFS"
110115 for _group in ${overlayfs} ; do
111- IFS=' ,' read -r overlay upper work volume <<< " $_group"
112-
113- if [[ " $volume " == " " ]]; then
114- overlay_mount_with_cnt=" ${OVERLAY_MOUNT} /${cnt} "
115- mount_volatile_persistent_volume " volatile" $overlay_mount_with_cnt
116- else
117- if [[ -n " ${volume_mount_map[$volume]} " ]]; then
118- # Volume already mounted, retrieve existing mount point from map.
119- overlay_mount_with_cnt=${volume_mount_map[$volume]}
120- else
121- # Not in map, so mount and update the map.
122- overlay_mount_with_cnt=" ${OVERLAY_MOUNT} /${cnt} "
123- mount_volatile_persistent_volume $volume $overlay_mount_with_cnt
124- volume_mount_map[$volume ]=$overlay_mount_with_cnt
125- fi
126- fi
127- cnt=$(( cnt + 1 ))
128-
129- echo " Creating OverlayFS with overlay: $overlay , upper: ${overlay_mount_with_cnt} /${upper} , work: ${overlay_mount_with_cnt} /${work} "
130- create_overlayfs " ${VERITY_MOUNT} /${overlay} " " ${overlay_mount_with_cnt} /${upper} " " ${overlay_mount_with_cnt} /${work} "
116+ IFS=' ,' read -r overlay upper work <<< " $_group"
117+ echo " Creating OverlayFS with overlay: $overlay , upper: ${OVERLAY_MOUNT} /${upper} , work: ${OVERLAY_MOUNT} /${work} "
118+ create_overlay " $overlay " " ${OVERLAY_MOUNT} /${upper} " " ${OVERLAY_MOUNT} /${work} "
131119 done
132120
133121 echo " Done Verity Root Mounting and OverlayFS Mounting"
134122 # Re-mount the verity mount along with overlayfs to the sysroot.
135123 mount --rbind " ${VERITY_MOUNT} " " ${NEWROOT} "
136124}
137125
138- parse_kernel_cmdline_args
139- mount_overlayfs
126+ parse_cmdline_args
127+ mount_root
0 commit comments