Skip to content

Commit 9b92aee

Browse files
committed
initramfs: unpack and repack cpio's like the kernel does
- unpack and repack cpio's like the kernel does; order vs timestamps matter, since the kernel overwrites without mtime comparision Signed-off-by: Ricardo Pardini <ricardo@pardini.net>
1 parent 6219234 commit 9b92aee

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

bash/linuxkit.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,25 @@ function linuxkit_build() {
163163
declare initramfs_compressor_dockerfile="${lk_output_dir}/Dockerfile.initramfs_compressor"
164164
declare -r output_compressed_initramfs_name="initramfs-compressed.img" output_report_name="report.md"
165165

166+
declare find_same_name_files_command cpio_extract_like_the_kernel_does_command cpio_repack_for_kernel_command
166167
# I *really* don't want to escape this; bear with me
167168
find_same_name_files_command="$(
168169
cat <<- 'FIND_SAME_NAME_FILES_COMMAND'
169170
find . -type f -size +512k -printf "%f %p\n" | sort | awk '{files[$1]=files[$1] ? files[$1] "\n"$2 : $2; count[$1]++} END {for (f in count) if (count[f]>1) print f "\n" files[f]}' | while read -r line; do if [[ -f "$line" ]]; then stat --printf="%s bytes " "$line"; md5sum "$line"; else echo "### duplicate: '$line'"; fi; done
170171
FIND_SAME_NAME_FILES_COMMAND
171172
)"
172173

174+
# cpio command that mimics what the kernel does when extracting initramfs
175+
# -i: extract from archive (copy-in)
176+
# -d: create directories as needed
177+
# -m: preserve modification times (so files get the archive mtime, like the kernel does)
178+
# -u: unconditionally replace existing files (this is the key to "kernel-like" behavior)
179+
# --no-absolute-filenames: avoid writing absolute paths (shouldn't be any anyway, but don't trust - not a kernel concern)
180+
cpio_extract_like_the_kernel_does_command="cpio -idmu --no-absolute-filenames"
181+
182+
# cpio repack, newc is the format the kernel expects
183+
cpio_repack_for_kernel_command="cpio -o -H newc"
184+
173185
log info "Creating Dockerfile '${initramfs_compressor_dockerfile}'... "
174186
cat <<- INITRAMFS_COMPRESSOR_DOCKERFILE > "${initramfs_compressor_dockerfile}"
175187
FROM debian:stable AS builder
@@ -189,7 +201,7 @@ function linuxkit_build() {
189201
190202
RUN { echo -n "## ungzipped input magic: " && file /input/initramfs_decompress.cpio; }>> /output/${output_report_name}
191203
192-
RUN cat /input/initramfs_decompress.cpio | cpio -idm
204+
RUN cat /input/initramfs_decompress.cpio | ${cpio_extract_like_the_kernel_does_command}
193205
194206
# Reporting on original...
195207
RUN { echo "## original: dust report: " && dust -x --no-colors --no-percent-bars ; }>> /output/${output_report_name}
@@ -204,7 +216,7 @@ function linuxkit_build() {
204216
RUN { echo "## deduped: dust report: " && dust -x --no-colors --no-percent-bars ; }>> /output/${output_report_name}
205217
RUN { echo -n "## deduped: hardlinked files: " && find . -type f -links +1 | wc -l ; }>> /output/${output_report_name}
206218
207-
RUN find . | cpio -o -H newc > /output/repacked.cpio
219+
RUN find . | ${cpio_repack_for_kernel_command} > /output/repacked.cpio
208220
RUN { echo -n "## output, pre compression magic: " && file /output/repacked.cpio; }>> /output/${output_report_name}
209221
210222
RUN zstdmt -9 -o /output/${output_compressed_initramfs_name} /output/repacked.cpio
@@ -223,6 +235,9 @@ function linuxkit_build() {
223235
"--progress=${DOCKER_BUILDX_PROGRESS_TYPE}" # show progress
224236
-f "${initramfs_compressor_dockerfile}" # Dockerfile path
225237
"${lk_output_dir}") # build context, for easy access to the input initramfs file
238+
239+
log_file_bat "${initramfs_compressor_dockerfile}" "debug" "Dockerfile for initramfs compressor"
240+
226241
docker buildx build "${compressor_docker_buildx_args[@]}"
227242

228243
# If output not in place, something went wrong

0 commit comments

Comments
 (0)