Skip to content

Commit 9fbfd75

Browse files
committed
00-rootdev.sh: ensure_initramfs_mounts(): fsck fs before mount
This change adds support for checking the integrity of filesystems mounted from within the initramfs. Systemd wants the filesystem on which /usr resides mounted when it finally takes over from initramfs, but does not seem to launch a systemd-fsck job for that filesystem later on, even if the filesystem is mounted ro. The result is that the /usr-hosting filesystem could go unchecked for a long time. The problem is not limited to /usr, but all early mounts mentioned in initramfs.mounts. Furthermore, fsck'ing should work on the physical device, final mounting should take into account existing symbolic or logical disk/LVM names for the purpose of enhanced readability.
1 parent 3738bd2 commit 9fbfd75

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

defaults/initrd.d/00-rootdev.sh

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ real_init_init() {
180180

181181
# Read /etc/initramfs.mounts from ${NEW_ROOT} and mount the
182182
# listed filesystem mountpoints. For instance, /usr, which is
183-
# required by udev & systemd.
183+
# required by udev & systemd. Prior to mounting, make an effort
184+
# to check the filesystem's integrity.
184185
ensure_initramfs_mounts() {
185186
local fslist=
186187

@@ -191,6 +192,11 @@ ensure_initramfs_mounts() {
191192
fi
192193

193194
local dev= fstype= opts= mnt= cmd=
195+
196+
# For mount points like /dev/mapper/vgxz-... or
197+
# /dev/disk/by-... in /etc/fstab.
198+
local logical_dev=
199+
194200
for fs in ${fslist}; do
195201

196202
mnt="${NEW_ROOT}${fs}"
@@ -199,29 +205,53 @@ ensure_initramfs_mounts() {
199205
continue
200206
fi
201207

202-
dev=$(_get_mount_device "${fs}")
203-
[ -z "${dev}" ] && continue
204-
# Resolve it like util-linux mount does
205-
[ -L "${dev}" ] && dev=$(realpath "${dev}")
206-
# In this case, it's probably part of the filesystem
207-
# and not a mountpoint
208+
logical_dev=$(_get_mount_device "${fs}")
209+
[ -z "${logical_dev}" ] && continue
210+
# Find the corresponding physical device, as this is the
211+
# one we want to use for the fsck.
212+
dev=$(realpath "${logical_dev}")
213+
# Dunno if this is required, if logical_dev is
214+
# non-empty, then so is dev.
208215
[ -z "${dev}" ] && continue
209216

217+
good_msg "Attempting to fsck ${fs} via ${dev} (aka ${logical_dev})..."
218+
if [ -e "${REAL_E2FSCK_BIN}" ]
219+
then
220+
# Use the real one, not the busybox variant...
221+
${REAL_E2FSCK_BIN} ${REAL_E2FSCK_BIN_OPTS} ${dev}
222+
elif [ -e "${BUSYBOX_BIN}" ]
223+
then
224+
# Fallback.
225+
${BUSYBOX_BIN} fsck ${dev}
226+
else
227+
warn_msg "No filesystem check tool found..."
228+
warn_msg "${fs} will *not* be checked before mounting is attempted."
229+
warn_msg "If you want the filesystems earmarked for"
230+
warn_msg "early mounting to be checked beforehand, then please"
231+
warn_msg "rebuild your initramfs with E2FSPROGS=yes,"
232+
warn_msg "or at least BUSYBOX=yes set in /etc/genkernel.conf."
233+
fi
234+
210235
fstype=$(_get_mount_fstype "${fs}")
211236
if _get_mount_options "${fs}" | fgrep -q bind; then
212237
opts="bind"
213238
dev="${NEW_ROOT}${dev}"
239+
# We want to mount via the logical dev below. This
240+
# provides for much more readability in future
241+
# invocations of df. In this case here, we simply
242+
# must pick up the updated dev name.
243+
logical_dev=${dev}
214244
else
215245
# ro must be trailing, and the options will always
216246
# contain at least 'defaults'
217247
opts="$(_get_mount_options ${fs} | _strip_mount_options)"
218248
opts="${opts},ro"
219249
fi
220250

221-
cmd="mount -t ${fstype} -o ${opts} ${dev} ${mnt}"
222-
good_msg "Mounting ${dev} as ${fs}: ${cmd}"
251+
cmd="mount -t ${fstype} -o ${opts} ${logical_dev} ${mnt}"
252+
good_msg "Mounting ${logical_dev} (aka ${dev}) as ${fs}: ${cmd}"
223253
if ! ${cmd}; then
224-
bad_msg "Unable to mount ${dev} for ${fs}"
254+
bad_msg "Unable to mount ${logical_dev} for ${fs}"
225255
fi
226256
done
227257
}

0 commit comments

Comments
 (0)