| 1 |
# Local filesystem mounting -*- shell-script -*- |
| 2 |
# shellcheck shell=sh |
| 3 |
|
| 4 |
local_top() |
| 5 |
{ |
| 6 |
if [ "${local_top_used}" != "yes" ]; then |
| 7 |
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/local-top" |
| 8 |
run_scripts /scripts/local-top |
| 9 |
[ "$quiet" != "y" ] && log_end_msg |
| 10 |
fi |
| 11 |
local_top_used=yes |
| 12 |
} |
| 13 |
|
| 14 |
local_block() |
| 15 |
{ |
| 16 |
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/local-block" |
| 17 |
run_scripts /scripts/local-block "$@" |
| 18 |
[ "$quiet" != "y" ] && log_end_msg |
| 19 |
} |
| 20 |
|
| 21 |
local_premount() |
| 22 |
{ |
| 23 |
if [ "${local_premount_used}" != "yes" ]; then |
| 24 |
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/local-premount" |
| 25 |
run_scripts /scripts/local-premount |
| 26 |
[ "$quiet" != "y" ] && log_end_msg |
| 27 |
fi |
| 28 |
local_premount_used=yes |
| 29 |
} |
| 30 |
|
| 31 |
local_bottom() |
| 32 |
{ |
| 33 |
if [ "${local_premount_used}" = "yes" ] || [ "${local_top_used}" = "yes" ]; then |
| 34 |
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/local-bottom" |
| 35 |
run_scripts /scripts/local-bottom |
| 36 |
[ "$quiet" != "y" ] && log_end_msg |
| 37 |
fi |
| 38 |
local_premount_used=no |
| 39 |
local_top_used=no |
| 40 |
} |
| 41 |
|
| 42 |
# $1=device ID to mount |
| 43 |
# $2=optionname (for root and etc) |
| 44 |
# $3=panic if device is missing (true or false, default: true) |
| 45 |
# Sets $DEV to the resolved device node |
| 46 |
local_device_setup() |
| 47 |
{ |
| 48 |
local dev_id="$1" |
| 49 |
local name="$2" |
| 50 |
local may_panic="${3:-true}" |
| 51 |
local real_dev |
| 52 |
local time_elapsed |
| 53 |
local count |
| 54 |
|
| 55 |
wait_for_udev 10 |
| 56 |
|
| 57 |
# Load ubi with the correct MTD partition and return since fstype |
| 58 |
# doesn't work with a char device like ubi. |
| 59 |
if [ -n "$UBIMTD" ]; then |
| 60 |
/sbin/modprobe ubi "mtd=$UBIMTD" |
| 61 |
DEV="${dev_id}" |
| 62 |
return |
| 63 |
fi |
| 64 |
|
| 65 |
# Don't wait for a device that doesn't have a corresponding |
| 66 |
# device in /dev and isn't resolvable by blkid (e.g. mtd0) |
| 67 |
if [ "${dev_id#/dev}" = "${dev_id}" ] && |
| 68 |
[ "${dev_id#*=}" = "${dev_id}" ]; then |
| 69 |
DEV="${dev_id}" |
| 70 |
return |
| 71 |
fi |
| 72 |
|
| 73 |
# If the root device hasn't shown up yet, give it a little while |
| 74 |
# to allow for asynchronous device discovery (e.g. USB). We |
| 75 |
# also need to keep invoking the local-block scripts in case |
| 76 |
# there are devices stacked on top of those. |
| 77 |
if ! real_dev=$(resolve_device "${dev_id}") || |
| 78 |
! get_fstype "${real_dev}" >/dev/null; then |
| 79 |
log_begin_msg "Waiting for ${name}" |
| 80 |
|
| 81 |
# Timeout is max(30, rootdelay) seconds (approximately) |
| 82 |
slumber=30 |
| 83 |
if [ "${ROOTDELAY:-0}" -gt $slumber ]; then |
| 84 |
slumber=$ROOTDELAY |
| 85 |
fi |
| 86 |
|
| 87 |
while true; do |
| 88 |
sleep 1 |
| 89 |
time_elapsed="$(time_elapsed)" |
| 90 |
|
| 91 |
local_block "${dev_id}" |
| 92 |
|
| 93 |
# If mdadm's local-block script counts the |
| 94 |
# number of times it is run, make sure to |
| 95 |
# run it the expected number of times. |
| 96 |
while true; do |
| 97 |
if [ -f /run/count.mdadm.initrd ]; then |
| 98 |
count="$(cat /run/count.mdadm.initrd)" |
| 99 |
elif [ -n "${count}" ]; then |
| 100 |
# mdadm script deleted it; put it back |
| 101 |
count=$((count + 1)) |
| 102 |
echo "${count}" >/run/count.mdadm.initrd |
| 103 |
else |
| 104 |
break |
| 105 |
fi |
| 106 |
if [ "${count}" -ge "${time_elapsed}" ]; then |
| 107 |
break; |
| 108 |
fi |
| 109 |
/scripts/local-block/mdadm "${dev_id}" |
| 110 |
done |
| 111 |
|
| 112 |
if real_dev=$(resolve_device "${dev_id}") && |
| 113 |
get_fstype "${real_dev}" >/dev/null; then |
| 114 |
wait_for_udev 10 |
| 115 |
log_end_msg 0 |
| 116 |
break |
| 117 |
fi |
| 118 |
if [ "${time_elapsed}" -ge "${slumber}" ]; then |
| 119 |
log_end_msg 1 || true |
| 120 |
break |
| 121 |
fi |
| 122 |
done |
| 123 |
fi |
| 124 |
|
| 125 |
# We've given up, but we'll let the user fix matters if they can |
| 126 |
while ! real_dev=$(resolve_device "${dev_id}") || |
| 127 |
! get_fstype "${real_dev}" >/dev/null; do |
| 128 |
if ! $may_panic; then |
| 129 |
echo "Gave up waiting for ${name}" |
| 130 |
return 1 |
| 131 |
fi |
| 132 |
echo "Gave up waiting for ${name} device. Common problems:" |
| 133 |
echo " - Boot args (cat /proc/cmdline)" |
| 134 |
echo " - Check rootdelay= (did the system wait long enough?)" |
| 135 |
if [ "${name}" = root ]; then |
| 136 |
echo " - Check root= (did the system wait for the right device?)" |
| 137 |
fi |
| 138 |
echo " - Missing modules (cat /proc/modules; ls /dev)" |
| 139 |
panic "ALERT! ${dev_id} does not exist. Dropping to a shell!" |
| 140 |
done |
| 141 |
|
| 142 |
DEV="${real_dev}" |
| 143 |
} |
| 144 |
|
| 145 |
local_mount_root() |
| 146 |
{ |
| 147 |
local_top |
| 148 |
if [ -z "${ROOT}" ]; then |
| 149 |
panic "No root device specified. Boot arguments must include a root= parameter." |
| 150 |
fi |
| 151 |
local_device_setup "${ROOT}" "root file system" |
| 152 |
ROOT="${DEV}" |
| 153 |
|
| 154 |
# Get the root filesystem type if not set |
| 155 |
if [ -z "${ROOTFSTYPE}" ] || [ "${ROOTFSTYPE}" = auto ]; then |
| 156 |
FSTYPE=$(get_fstype "${ROOT}") |
| 157 |
else |
| 158 |
FSTYPE=${ROOTFSTYPE} |
| 159 |
fi |
| 160 |
|
| 161 |
local_premount |
| 162 |
|
| 163 |
if [ "${readonly?}" = "y" ]; then |
| 164 |
roflag=-r |
| 165 |
else |
| 166 |
roflag=-w |
| 167 |
fi |
| 168 |
|
| 169 |
checkfs "${ROOT}" root "${FSTYPE}" |
| 170 |
|
| 171 |
# Mount root |
| 172 |
# shellcheck disable=SC2086 |
| 173 |
if ! mount ${roflag} ${FSTYPE:+-t "${FSTYPE}"} ${ROOTFLAGS} "${ROOT}" "${rootmnt?}"; then |
| 174 |
panic "Failed to mount ${ROOT} as root file system." |
| 175 |
fi |
| 176 |
} |
| 177 |
|
| 178 |
local_mount_fs() |
| 179 |
{ |
| 180 |
read_fstab_entry "$1" |
| 181 |
|
| 182 |
local_device_setup "$MNT_FSNAME" "$1 file system" |
| 183 |
MNT_FSNAME="${DEV}" |
| 184 |
|
| 185 |
local_premount |
| 186 |
|
| 187 |
if [ "${readonly}" = "y" ]; then |
| 188 |
roflag=-r |
| 189 |
else |
| 190 |
roflag=-w |
| 191 |
fi |
| 192 |
|
| 193 |
if [ "$MNT_PASS" != 0 ]; then |
| 194 |
checkfs "$MNT_FSNAME" "$MNT_DIR" "${MNT_TYPE}" |
| 195 |
fi |
| 196 |
|
| 197 |
# Mount filesystem |
| 198 |
if ! mount ${roflag} -t "${MNT_TYPE}" -o "${MNT_OPTS}" "$MNT_FSNAME" "${rootmnt}${MNT_DIR}"; then |
| 199 |
panic "Failed to mount ${MNT_FSNAME} as $MNT_DIR file system." |
| 200 |
fi |
| 201 |
} |
| 202 |
|
| 203 |
mountroot() |
| 204 |
{ |
| 205 |
local_mount_root |
| 206 |
} |
| 207 |
|
| 208 |
mount_top() |
| 209 |
{ |
| 210 |
# Note, also called directly in case it's overridden. |
| 211 |
local_top |
| 212 |
} |
| 213 |
|
| 214 |
mount_premount() |
| 215 |
{ |
| 216 |
# Note, also called directly in case it's overridden. |
| 217 |
local_premount |
| 218 |
} |
| 219 |
|
| 220 |
mount_bottom() |
| 221 |
{ |
| 222 |
# Note, also called directly in case it's overridden. |
| 223 |
local_bottom |
| 224 |
} |