Skip to content

Commit 9af3d04

Browse files
committed
fix(os): stabilize squashfs packing order
1 parent a923084 commit 9af3d04

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

os/mkosi/scripts/make-release-artifacts.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,34 @@ install -m0644 "$kernel" "$OUT/files/bzImage"
1919

2020
# Combined squashfs + dm-verity tree, matching Yocto's separate-hash=0 output.
2121
rootfs="$OUT/files/rootfs.squashfs.verity"
22+
sort_file="$OUT/.squashfs-sort"
2223
# mksquashfs -noappend overwrites its filesystem but does not reliably remove
2324
# a longer dm-verity tail left by a previous invocation.
2425
truncate -s 0 "$rootfs"
26+
# mksquashfs otherwise uses source-directory inode order as a tiebreaker. That
27+
# order differs between overlayfs/user-namespace and privileged CI builds even
28+
# when every path, mode and file byte is identical. Give every path a stable,
29+
# unique priority so packing order is independent of the backing filesystem.
30+
find "$TREE" -mindepth 1 -printf '%P\n' | LC_ALL=C sort > "$sort_file.paths"
31+
if grep -q '[[:space:]]' "$sort_file.paths"; then
32+
echo 'rootfs paths containing whitespace are unsupported by mksquashfs -sort' >&2
33+
exit 1
34+
fi
35+
if (( $(wc -l < "$sort_file.paths") > 65535 )); then
36+
echo 'rootfs has too many paths for unique mksquashfs priorities' >&2
37+
exit 1
38+
fi
39+
awk '{ print $0, 32767 - NR }' "$sort_file.paths" > "$sort_file"
40+
rm -f "$sort_file.paths"
2541
# Privileged mkosi runs can inherit host default ACLs from their workspace.
2642
# The guest rootfs does not rely on xattrs, so exclude this host-only metadata.
2743
# A single compressor worker also avoids host-CPU-dependent fragment ordering.
2844
env -u SOURCE_DATE_EPOCH mksquashfs "$TREE" "$rootfs" \
2945
-noappend -all-root -no-progress \
46+
-sort "$sort_file" \
3047
-no-xattrs -processors 1 -comp zstd -mkfs-time "$SOURCE_DATE_EPOCH" \
3148
-all-time "$SOURCE_DATE_EPOCH" >/dev/null
49+
rm -f "$sort_file"
3250
data_size=$(stat -c %s "$rootfs")
3351
data_size=$(( (data_size + 4095) / 4096 * 4096 ))
3452
truncate -s "$data_size" "$rootfs"

os/mkosi/tests/acceptance.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ for service in dstack-guest-agent dstack-prepare app-compose wg-checker; do
2929
grep -q "$service" "$D/mkosi.skeleton/usr/lib/systemd/system-preset/80-dstack.preset"
3030
done
3131
grep -q 'artifact-manifest.json' "$D/scripts/make-release-artifacts.sh"
32+
grep -q -- '-sort.*sort_file' "$D/scripts/make-release-artifacts.sh"
3233
grep -q 'image/assemble.sh' "$D/mkosi.postoutput"
3334
grep -q 'ovmf-build.sh' "$D/components/ovmf/ovmf.sh"
3435
grep -q 'fbe0805b2091393406952e84724188f8c1941837' "$D/components/ovmf/ovmf-build.sh"

0 commit comments

Comments
 (0)