Skip to content

Commit 7ae6fab

Browse files
committed
Revert "Revert PR #3534 "sysext: Add OS-dependent sysext compression""
This reverts commit d28ece4.
1 parent 5c1cc4b commit 7ae6fab

7 files changed

Lines changed: 118 additions & 21 deletions

File tree

‎build_library/prod_image_util.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ EOF
170170
# Remove source locale data, only need to ship the compiled archive.
171171
sudo rm -rf ${root_fs_dir}/usr/share/i18n/
172172

173+
# Inject ephemeral sysext signing certificate
174+
sudo mkdir -p "${root_fs_dir}/usr/lib/verity.d"
175+
sudo cp "${SYSEXT_SIGNING_KEY_DIR}/sysexts.crt" "${root_fs_dir}/usr/lib/verity.d"
176+
173177
# Finish image will move files from /etc to /usr/share/flatcar/etc.
174178
# Note that image filesystem contents generated by finish_image will not
175179
# include sysext contents (only the sysext squashfs files themselves).

‎build_library/sysext_prod_builder‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ create_prod_sysext() {
6363
# The --install_root_basename="${name}-base-sysext-rootfs" flag is
6464
# important - it sets the name of a rootfs directory, which is used
6565
# to determine the package target in coreos/base/profile.bashrc
66-
sudo "FLATCAR_BUILD_ID=$FLATCAR_BUILD_ID" "${SCRIPTS_DIR}/build_sysext" \
66+
#
67+
# Built-in sysexts are stored in the compressed /usr partition, so we
68+
# disable compression to avoid double-compression.
69+
sudo -E "FLATCAR_BUILD_ID=$FLATCAR_BUILD_ID" "${SCRIPTS_DIR}/build_sysext" \
6770
--board="${BOARD}" \
6871
--image_builddir="${workdir}/sysext-build" \
6972
--squashfs_base="${base_sysext}" \
7073
--generate_pkginfo \
74+
--compression=none \
7175
--install_root_basename="${name}-base-sysext-rootfs" \
7276
"${build_sysext_opts[@]}" \
7377
"${name}" "${grp_pkg[@]}"
@@ -99,6 +103,14 @@ sysext_mountdir="${BUILD_DIR}/prod-sysext-work/mounts"
99103
sysext_base="${sysext_workdir}/base-os.squashfs"
100104

101105
function cleanup() {
106+
IFS=':' read -r -a mounted_sysexts <<< "$sysext_lowerdirs"
107+
# skip the rootfs
108+
mounted_sysexts=("${mounted_sysexts[@]:1}")
109+
110+
for sysext in "${mounted_sysexts[@]}"; do
111+
sudo systemd-dissect --umount --rmdir "$sysext"
112+
done
113+
102114
sudo umount "${sysext_mountdir}"/* || true
103115
rm -rf "${sysext_workdir}" || true
104116
}
@@ -116,6 +128,7 @@ sudo mksquashfs "${root_fs_dir}" "${sysext_base}" -noappend -xattrs-exclude '^bt
116128
# for combined overlay later.
117129
prev_pkginfo=""
118130
sysext_lowerdirs="${sysext_mountdir}/rootfs-lower"
131+
mkdir -p "${sysext_mountdir}"
119132
for sysext in ${sysexts_list//,/ }; do
120133
# format is "<name>:<group>/<package>"
121134
name="${sysext%|*}"
@@ -129,12 +142,21 @@ for sysext in ${sysexts_list//,/ }; do
129142
"${grp_pkg}" \
130143
"${prev_pkginfo}"
131144

132-
mkdir -p "${sysext_mountdir}/${name}" \
133-
"${sysext_mountdir}/${name}_pkginfo"
134-
sudo mount -rt squashfs -o loop,nodev "${sysext_output_dir}/${name}.raw" \
135-
"${sysext_mountdir}/${name}"
136-
sudo mount -rt squashfs -o loop,nodev "${sysext_output_dir}/${name}_pkginfo.raw" \
137-
"${sysext_mountdir}/${name}_pkginfo"
145+
sudo systemd-dissect \
146+
--read-only \
147+
--mount \
148+
--mkdir \
149+
--image-policy='root=encrypted+unprotected+absent:usr=encrypted+unprotected+absent' \
150+
"${sysext_output_dir}/${name}.raw" \
151+
"${sysext_mountdir}/${name}"
152+
153+
sudo systemd-dissect \
154+
--read-only \
155+
--mount \
156+
--mkdir \
157+
--image-policy='root=encrypted+unprotected+absent:usr=encrypted+unprotected+absent' \
158+
"${sysext_output_dir}/${name}_pkginfo.raw" \
159+
"${sysext_mountdir}/${name}_pkginfo"
138160

139161
sysext_lowerdirs="${sysext_lowerdirs}:${sysext_mountdir}/${name}"
140162
sysext_lowerdirs="${sysext_lowerdirs}:${sysext_mountdir}/${name}_pkginfo"

‎build_library/vm_image_util.sh‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,15 @@ install_oem_sysext() {
585585
# important - it sets the name of a rootfs directory, which is
586586
# used to determine the package target in
587587
# coreos/base/profile.bashrc
588+
#
589+
# OEM sysexts are stored in the compressed partition, so we disable
590+
# compression to avoid double-compression.
588591
local build_sysext_flags=(
589592
--board="${BOARD}"
590593
--squashfs_base="${VM_SRC_SYSEXT_IMG}"
591594
--image_builddir="${built_sysext_dir}"
592595
--metapkgs="${metapkg}"
596+
--compression=none
593597
--install_root_basename="${VM_IMG_TYPE}-oem-sysext-rootfs"
594598
)
595599
local overlay_path mangle_fs
@@ -602,7 +606,7 @@ install_oem_sysext() {
602606
fi
603607

604608
mkdir -p "${built_sysext_dir}"
605-
sudo "${build_sysext_env[@]}" "${SCRIPT_ROOT}/build_sysext" "${build_sysext_flags[@]}" "${oem_sysext}"
609+
sudo -E "${build_sysext_env[@]}" "${SCRIPT_ROOT}/build_sysext" "${build_sysext_flags[@]}" "${oem_sysext}"
606610

607611
local installed_sysext_oem_dir='/oem/sysext'
608612
local installed_sysext_file_prefix="${oem_sysext}-${version}"

‎build_sysext‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ DEFINE_boolean generate_pkginfo "${FLAGS_FALSE}" \
3535
"Generate an additional squashfs '<sysext_name>_pkginfo.raw' with portage package meta-information (/var/db ...). Useful for creating sysext dependencies; see 'base_pkginfo' below."
3636
DEFINE_string base_pkginfo "" \
3737
"Colon-separated list of pkginfo squashfs paths / files generated via 'generate_pkginfo' to base this sysext on. The corresponding base sysexts are expected to be merged with the sysext generated."
38-
DEFINE_string compression "zstd" \
39-
"Compression to use for sysext squashfs. One of 'gzip', 'lzo', 'lz4', 'xz', or 'zstd'. Must be supported by the Flatcar squashfs kernel module in order for the sysext to work."
40-
DEFINE_string mksquashfs_opts "" \
41-
"Additional command line options to pass to mksquashfs. See 'man 1 mksquashfs'. If <compression> is 'zstd' (the default), this option defaults to '-Xcompression-level 22 -b 512K'. Otherwise the default is empty."
38+
DEFINE_string compression "lz4hc" \
39+
"Compression to use for sysext EROFS image. Options: 'lz4', 'lz4hc', 'zstd', or 'none'. Default is 'lz4hc'."
40+
DEFINE_string mkerofs_opts "" \
41+
"Additional mkfs.erofs options to pass via SYSTEMD_REPART_MKFS_OPTIONS_EROFS. If not specified, defaults are used based on compression type."
4242
DEFINE_boolean ignore_version_mismatch "${FLAGS_FALSE}" \
4343
"Ignore version mismatch between SDK board packages and base squashfs. DANGEROUS."
4444
DEFINE_string install_root_basename "${default_install_root_basename}" \
@@ -112,10 +112,6 @@ fi
112112
BUILD_DIR=$(realpath "${FLAGS_image_builddir}")
113113
mkdir -p "${BUILD_DIR}"
114114

115-
if [[ "${FLAGS_compression}" = "zstd" && -z "${FLAGS_mksquashfs_opts}" ]] ; then
116-
FLAGS_mksquashfs_opts="-Xcompression-level 22 -b 512k"
117-
fi
118-
119115
source "${BUILD_LIBRARY_DIR}/toolchain_util.sh" || exit 1
120116
source "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
121117
source "${BUILD_LIBRARY_DIR}/reports_util.sh" || exit 1
@@ -248,7 +244,7 @@ if [[ "$FLAGS_generate_pkginfo" = "${FLAGS_TRUE}" ]] ; then
248244
mkdir -p "${BUILD_DIR}/img-pkginfo/var/db"
249245
cp -R "${BUILD_DIR}/${FLAGS_install_root_basename}/var/db/pkg" "${BUILD_DIR}/img-pkginfo/var/db/"
250246
mksquashfs "${BUILD_DIR}/img-pkginfo" "${BUILD_DIR}/${SYSEXTNAME}_pkginfo.raw" \
251-
-noappend -xattrs-exclude '^btrfs.' -comp "${FLAGS_compression}" ${FLAGS_mksquashfs_opts}
247+
-noappend -xattrs-exclude '^btrfs.' -comp zstd -Xcompression-level 22 -b 512k
252248
fi
253249

254250
info "Writing ${SYSEXTNAME}_packages.txt"
@@ -305,14 +301,44 @@ if [[ -n "${invalid_files}" ]]; then
305301
die "Invalid file ownership: ${invalid_files}"
306302
fi
307303

308-
mksquashfs "${BUILD_DIR}/${FLAGS_install_root_basename}" "${BUILD_DIR}/${SYSEXTNAME}.raw" \
309-
-noappend -xattrs-exclude '^btrfs.' -comp "${FLAGS_compression}" ${FLAGS_mksquashfs_opts}
304+
# Set up EROFS compression options based on compression type
305+
if [[ "${FLAGS_compression}" != "none" ]]; then
306+
export SYSTEMD_REPART_MKFS_OPTIONS_EROFS="-z${FLAGS_compression}"
307+
308+
if [[ -n "${FLAGS_mkerofs_opts}" ]]; then
309+
# User provided custom options
310+
export SYSTEMD_REPART_MKFS_OPTIONS_EROFS="${SYSTEMD_REPART_MKFS_OPTIONS_EROFS} ${FLAGS_mkerofs_opts}"
311+
elif [[ "${FLAGS_compression}" = "lz4hc" ]]; then
312+
# Default options for lz4hc
313+
export SYSTEMD_REPART_MKFS_OPTIONS_EROFS="${SYSTEMD_REPART_MKFS_OPTIONS_EROFS},12 -C65536 -Efragments,ztailpacking"
314+
elif [[ "${FLAGS_compression}" = "zstd" ]]; then
315+
# Default options for zstd
316+
export SYSTEMD_REPART_MKFS_OPTIONS_EROFS="${SYSTEMD_REPART_MKFS_OPTIONS_EROFS},level=22 -C524288 -Efragments,ztailpacking"
317+
fi
318+
info "Building sysext with ${FLAGS_compression} compression"
319+
else
320+
info "Building sysext without compression (built-in sysexts)"
321+
fi
322+
323+
systemd-repart \
324+
--private-key="${SYSEXT_SIGNING_KEY_DIR}/sysexts.key" \
325+
--certificate="${SYSEXT_SIGNING_KEY_DIR}/sysexts.crt" \
326+
--make-ddi=sysext \
327+
--copy-source="${BUILD_DIR}/${FLAGS_install_root_basename}" \
328+
"${BUILD_DIR}/${SYSEXTNAME}.raw"
329+
310330
rm -rf "${BUILD_DIR}"/{fs-root,"${FLAGS_install_root_basename}",workdir}
311331

312332
# Generate reports
313333
mkdir "${BUILD_DIR}/img-rootfs"
314-
mount -rt squashfs -o loop,nodev "${BUILD_DIR}/${SYSEXTNAME}.raw" "${BUILD_DIR}/img-rootfs"
334+
systemd-dissect --read-only \
335+
--mount \
336+
--mkdir \
337+
--image-policy='root=encrypted+unprotected+absent:usr=encrypted+unprotected+absent' \
338+
"${BUILD_DIR}/${SYSEXTNAME}.raw" \
339+
"${BUILD_DIR}/img-rootfs"
340+
315341
write_contents "${BUILD_DIR}/img-rootfs" "${BUILD_DIR}/${SYSEXTNAME}_contents.txt"
316342
write_contents_with_technical_details "${BUILD_DIR}/img-rootfs" "${BUILD_DIR}/${SYSEXTNAME}_contents_wtd.txt"
317343
write_disk_space_usage_in_paths "${BUILD_DIR}/img-rootfs" "${BUILD_DIR}/${SYSEXTNAME}_disk_usage.txt"
318-
umount "${BUILD_DIR}/img-rootfs"
344+
systemd-dissect --umount --rmdir "${BUILD_DIR}/img-rootfs"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- OS-dependent sysexts (e.g., docker-flatcar, containerd-flatcar) are now cryptographically signed using dm-verity roothash signatures. This enables stricter sysext policies via systemd-sysext and provides a foundation for verifying user-provided extensions in future releases. The format changed from squashfs to erofs-based Discoverable Disk Images (DDI). ([scripts#3162](https://github.com/flatcar/scripts/pull/3162))

‎sdk_container/src/third_party/coreos-overlay/profiles/coreos/targets/sdk/package.use‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ x11-libs/pixman static-libs
3232

3333
# Get latest EDK2 firmware for Secure Boot on arm64.
3434
app-emulation/qemu -pin-upstream-blobs
35+
36+
# Needed for signed sysexts using systemd-repart
37+
sys-apps/systemd cryptsetup

‎sdk_lib/sdk_entry.sh‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,43 @@ if ! grep -q 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc; then
8888
fi
8989
fi
9090

91+
# Ensure sysext signing keys exist; regenerate if directory or files missing
92+
if grep -q 'export SYSEXT_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
93+
_existing_sysext_dir=$(source /home/sdk/.bashrc 2>/dev/null; echo "$SYSEXT_SIGNING_KEY_DIR")
94+
if [[ -z "$_existing_sysext_dir" || ! -d "$_existing_sysext_dir" || ! -s "$_existing_sysext_dir/sysexts.key" || ! -s "$_existing_sysext_dir/sysexts.crt" ]]; then
95+
# Drop stale export so block below regenerates
96+
sed -i -e '/export SYSEXT_SIGNING_KEY_DIR=/d' /home/sdk/.bashrc
97+
fi
98+
fi
99+
grep -q 'export SYSEXT_SIGNING_KEY_DIR' /home/sdk/.bashrc || {
100+
if [[ ${COREOS_OFFICIAL:-0} -eq 1 ]]; then
101+
SYSEXT_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
102+
else
103+
SYSEXT_SIGNING_KEY_DIR="/home/sdk/.sysext-signing-keys"
104+
su sdk -c "mkdir -p ${SYSEXT_SIGNING_KEY_DIR@Q}"
105+
fi
106+
if [[ ! "$SYSEXT_SIGNING_KEY_DIR" || ! -d "$SYSEXT_SIGNING_KEY_DIR" ]]; then
107+
echo "Failed to create directory for sysext signing keys."
108+
else
109+
echo "export SYSEXT_SIGNING_KEY_DIR='$SYSEXT_SIGNING_KEY_DIR'" >> /home/sdk/.bashrc
110+
fi
111+
pushd "$SYSEXT_SIGNING_KEY_DIR" > /dev/null
112+
build_id=$(source "/mnt/host/source/.repo/manifests/version.txt"; echo "$FLATCAR_BUILD_ID")
113+
# Generate sysext signing key only if missing or empty
114+
if [[ ! -s sysexts.key || ! -s sysexts.crt ]]; then
115+
su sdk -c "openssl req -new -nodes -utf8 \
116+
-x509 -batch -sha256 \
117+
-days 36000 \
118+
-outform PEM \
119+
-out sysexts.crt \
120+
-keyout sysexts.key \
121+
-newkey 4096 \
122+
-subj '/CN=Flatcar sysext key/OU=$build_id'" \
123+
|| echo "Generating sysext signing key failed"
124+
fi
125+
popd > /dev/null
126+
}
127+
91128
# This is ugly.
92129
# We need to sudo su - sdk -c so the SDK user gets a fresh login.
93130
# 'sdk' is member of multiple groups, and plain docker USER only

0 commit comments

Comments
 (0)