Skip to content

Commit 592875b

Browse files
liulanzelanzeliu
andauthored
Overlay Dracut Module cherrypick. (#8494)
Co-authored-by: lanzeliu <lanzeliu@microsoft.com>
1 parent 4cb4cf9 commit 592875b

2 files changed

Lines changed: 53 additions & 41 deletions

File tree

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# Description: This script is designed to mount a DM-Verity root filesystem and
4-
# set up an OverlayFS. It is driven by kernel parameters and is invoked during
5-
# the dracut initramfs phase.
4+
# set up OverlayFS. It is driven by kernel parameters and is invoked during the
5+
# dracut initramfs phase.
66

77
# Kernel Parameters:
88
# - root: Specifies the path to the root filesystem. This script is designed to
@@ -12,11 +12,8 @@
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, 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.
15+
# Each entry should specify the overlay, upper, work directories, and optional
16+
# volume for an OverlayFS instance.
2017

2118
# Behavior:
2219
# - Verifies the presence of the 'dracut-lib' for necessary utilities.
@@ -29,9 +26,7 @@
2926
# root with the writable overlay, allowing system modifications without
3027
# altering the base system.
3128

32-
set -ex
33-
34-
parse_cmdline_args() {
29+
parse_kernel_cmdline_args() {
3530
# Ensure that the 'dracut-lib' is present and loaded.
3631
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
3732

@@ -54,49 +49,51 @@ parse_cmdline_args() {
5449

5550
# Retrieve the OverlayFS parameters.
5651
[ -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=)
5952
}
6053

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}"
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}"
6660

67-
if [ -z "${partition}" ]; then
68-
# Fallback to volatile overlay if no persistent volume is specified
61+
if [[ "${_volume}" == "volatile" ]]; then
62+
# Fallback to volatile overlay if no persistent volume is specified.
6963
echo "No overlayfs persistent volume specified. Creating a volatile overlay."
70-
mount -t tmpfs tmpfs -o ${OVERLAY_MNT_OPTS} "${OVERLAY_MOUNT}" || \
71-
die "Failed to create overlay tmpfs at ${OVERLAY_MOUNT}"
64+
mount -t tmpfs tmpfs -o ${OVERLAY_MNT_OPTS} "${_overlay_mount}" || \
65+
die "Failed to create overlay tmpfs at ${_overlay_mount}"
7266
else
7367
# Check if /etc/mdadm.conf exists.
7468
if [ -f "/etc/mdadm.conf" ]; then
75-
mdadm --assemble ${partition} || \
69+
mdadm --assemble ${_volume} || \
7670
die "Failed to assemble RAID volume."
7771
fi
7872

79-
# Mount the specified persistent volume
80-
mount "${partition}" "${OVERLAY_MOUNT}" || \
81-
die "Failed to mount ${partition} at ${OVERLAY_MOUNT}"
73+
# Mount the specified persistent volume.
74+
mount "${_volume}" "${_overlay_mount}" || \
75+
die "Failed to mount ${_volume} at ${_overlay_mount}"
8276
fi
8377
}
8478

85-
create_overlay() {
86-
local _dir=$1
87-
local _mounted_dir="${VERITY_MOUNT}/${_dir}"
79+
create_overlayfs() {
80+
local _lower=$1
8881
local _upper=$2
8982
local _work=$3
9083

91-
[ -d "$_mounted_dir" ] || die "Unable to create overlay as $_dir does not exist"
84+
[ -d "$_lower" ] || die "Unable to create overlay as $_lower does not exist"
9285

9386
mkdir -p "${_upper}" && \
9487
mkdir -p "${_work}" && \
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}"
88+
mount -t overlay overlay -o ro,lowerdir="${_lower}",upperdir="${_upper}",workdir="${_work}" "${_lower}" || \
89+
die "Failed to mount overlay in ${_lower}"
9790
}
9891

99-
mount_root() {
92+
mount_overlayfs() {
93+
local cnt=0
94+
local overlay_mount_with_cnt
95+
declare -A volume_mount_map
96+
10097
if [ "$is_verity" = true ]; then
10198
echo "Mounting DM-Verity Target"
10299
mkdir -p "${VERITY_MOUNT}"
@@ -109,19 +106,34 @@ mount_root() {
109106
die "Failed to mount root"
110107
fi
111108

112-
mount_physical_partition
113-
114109
echo "Starting to create OverlayFS"
115110
for _group in ${overlayfs}; do
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}"
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}"
119131
done
120132

121133
echo "Done Verity Root Mounting and OverlayFS Mounting"
122134
# Re-mount the verity mount along with overlayfs to the sysroot.
123135
mount --rbind "${VERITY_MOUNT}" "${NEWROOT}"
124136
}
125137

126-
parse_cmdline_args
127-
mount_root
138+
parse_kernel_cmdline_args
139+
mount_overlayfs

SPECS/dracut/dracut.signatures.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"megaraid.conf": "914824cdbe0c525b71efa05a75e453335b0068beb8bc28bef2a5866d74bf7dd4",
66
"mkinitrd": "32a0e19de954a356a0f1903a1f75d3266493b55d71b0eeab6bd07b585e955dcf",
77
"module-setup.sh": "330af5c105793fb37434730ce0ff59467a9cc60a81a5e32193dc53235e9744c1",
8-
"overlayfs-mount.sh": "63552c4115b5c37ed04c5311367301483e2dfe1091dc5717b2bd22d9cdda044b"
8+
"overlayfs-mount.sh": "d6c064e383f00abae1ee621b9657e439e698dba5d49807e38c94fac795f45ea4"
99
}
1010
}

0 commit comments

Comments
 (0)