repart: Restore early fstab/crypttab generation (#40473)

fstab/crypttab generation were moved to the late stage to support
'fixate-volume-key' option. This, however, broke the use-case when
fstab/crypttab are put to the generated partition/image.

Fixes #40444
This commit is contained in:
Yu Watanabe
2026-01-30 23:32:07 +09:00
committed by GitHub
3 changed files with 76 additions and 8 deletions
+3 -1
View File
@@ -884,7 +884,9 @@
</para>
<para>Note that this setting is only taken into account when <option>--generate-crypttab=</option>
is specified on the <command>systemd-repart</command> command line.</para>
is specified on the <command>systemd-repart</command> command line. As the crypttab with the expected
hashes can only be generated after LUKS volumes are formatted, the crypttab itself cannot be put to
the generated volume.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>
+11 -7
View File
@@ -8501,7 +8501,7 @@ static bool need_crypttab(Context *context) {
return false;
}
static int context_crypttab(Context *context) {
static int context_crypttab(Context *context, bool late) {
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *path = NULL;
@@ -8544,7 +8544,7 @@ static int context_crypttab(Context *context) {
strempty(p->encrypted_volume->options));
}
r = flink_tmpfile(f, t, path, 0);
r = flink_tmpfile(f, t, path, late ? LINK_TMPFILE_REPLACE : 0);
if (r < 0)
return log_error_errno(r, "Failed to link temporary file to %s: %m", path);
@@ -10755,6 +10755,14 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
r = context_fstab(context);
if (r < 0)
return r;
r = context_crypttab(context, /* late= */ false);
if (r < 0)
return r;
r = context_update_verity_size(context);
if (r < 0)
return r;
@@ -10810,11 +10818,7 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
r = context_fstab(context);
if (r < 0)
return r;
r = context_crypttab(context);
r = context_crypttab(context, /* late= */ true);
if (r < 0)
return r;
+62
View File
@@ -1997,6 +1997,68 @@ EOF
losetup -d "$loop"
}
testcase_fstab_crypttab_in_repart() {
local defs imgs root volume
defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")"
root="$(mktemp --directory "/var/test-repart.root.XXXXXXXXXX")"
# shellcheck disable=SC2064
trap "rm -rf '$defs' '$imgs' '$root'" RETURN
chmod 0755 "$defs"
echo "*** testcase for including fstab/crypttab into repart created volume ***"
volume="test-repart-fstab-crypttab-$RANDOM"
mkdir -p "$root/etc"
tee "$defs/root.conf" <<EOF
[Partition]
Type=linux-generic
Format=ext4
CopyFiles=/etc
Encrypt=key-file
EncryptedVolume=$volume
MountPoint=/mnt/volume
EOF
systemd-repart --pretty=yes \
--definitions "$defs" \
--empty=create \
--size=100M \
--seed="$seed" \
--dry-run=no \
--offline="$OFFLINE" \
--generate-fstab="/etc/fstab" \
--generate-crypttab="/etc/crypttab" \
--root="$root" \
"$imgs/fstabcrypttabrepart.img"
loop="$(losetup -P --show --find "$imgs/fstabcrypttabrepart.img")"
udevadm wait --timeout=60 --settle "${loop:?}p1"
touch "$imgs/empty-password"
mkdir -p "$imgs/mount"
systemd-cryptsetup attach "$volume" "${loop}p1" "$imgs/empty-password"
mount -t ext4 "/dev/mapper/$volume" "$imgs/mount"
echo "Testing /etc/fstab presence"
test -f "$imgs/mount/etc/fstab"
grep -q "/mnt/volume" "$imgs/mount/etc/fstab"
echo "Testing /etc/crypttab presence"
test -f "$imgs/mount/etc/crypttab"
grep -q "$volume" "$imgs/mount/etc/crypttab"
umount "$imgs/mount"
systemd-cryptsetup detach "$volume"
losetup -d "$loop"
}
OFFLINE="yes"
run_testcases