# # generate initramfs of directory # usage: # generate_initramfs # example: # generate_initramfs "./server/boot/stage32_sqfs" "./mnt/openslx.sqfs" "./server/boot/initramfs2" # generate_initramfs "./server/build/stage31" "." "./server/boot/initramfs" generate_initramfs() { [ $# -lt 3 ] && perror "Sanity check failed: generate_initramfs needs at least three params, but $# were given." cd "$1" || perror "Cannot cd to '$1'" rm -f -- "$3" local cmd="gzip" local args= if grep -qF "CONFIG_RD_ZSTD=y" "$1/../kernel/config" && command -v zstd; then cmd="zstd" args="-T0 -16" pinfo "Accompanying kernel supports zstd, using that" fi find $2 | cpio --format="newc" --create | $cmd $args > "$3" local PS=(${PIPESTATUS[*]}) [ "x${PS[0]}" != "x0" ] && perror "'find $2' in '$(pwd)' failed." [ "x${PS[1]}" != "x0" ] && perror "cpio create failed." [ "x${PS[2]}" != "x0" ] && perror "gzip to '$3' failed." cd - > /dev/null pinfo "Created initramfs of $1 at $3" pinfo "Size: $(du -bsh "$3" | awk 'END {print $1}')" } # md5list() { ( cd "$1" || perror "Cannot cd to $1" while IFS='' read -r line || [[ -n "$line" ]]; do md5sum "$line" done < "$2" ) } # generates squashfs of directory # usage: # generate_squashfs [ignore_root] generate_squashfs() { [ $# -lt 2 ] && perror "Sanity check failed: generate_squashfs needs at least two params, but $# were given." [ -d "$1" ] || perror "$1 is not a directory." local IGNORE=$(mktemp) truncate -s 0 "$IGNORE" if [ -n "$3" ]; then [ -d "$3" ] || perror "Ignore dir does not exist!" local list_this=$(mktemp) local list_other=$(mktemp) local list_common=$(mktemp) local hash_this=$(mktemp) local hash_other=$(mktemp) ( cd "$3" && find -type f | sort > "$list_other" ) ( cd "$1" && find -type f | sort > "$list_this" ) comm -12 "$list_this" "$list_other" > "$list_common" echo "Common to both:" cat "$list_common" md5list "$3" "$list_common" | sort > "$hash_other" md5list "$1" "$list_common" | sort > "$hash_this" echo "Hash other:" cat "$hash_other" echo "Hash this:" cat "$hash_this" comm -12 "$hash_this" "$hash_other" > "$list_common" echo "Identical:" cat "$list_common" awk '{gsub(/^\.?\//, "", $2); print $2}' "$list_common" > "$IGNORE" rm -f -- "$list_this" "$list_other" "$hash_this" "$hash_other" "$list_common" fi # Detect mksquashfs xz support local PARAMS if mksquashfs 2>&1 | grep -A 10 '^-comp' | grep -q '\szstd' && grep -qF "CONFIG_SQUASHFS_ZSTD=y" "$1/../kernel/config"; then pinfo "Using zstd compression" PARAMS="-comp zstd -Xcompression-level 20" elif mksquashfs 2>&1 | grep -A 10 '^-comp' | grep -q '\sxz'; then pinfo "Using xz compression" PARAMS="-comp xz" else pinfo "Using default compression" PARAMS="" fi rm -f -- "$2" # -no-exports ? mksquashfs "$1" "$2" -ef "$IGNORE" $PARAMS -b 1M -no-recovery -always-use-fragments >&6 \ || perror "mksquashfs failed ($?)." pinfo "Created squashfs of $1 at $2" pinfo "Size: $(du -bsh "$2" | awk 'END {print $1}')" rm -f -- "$IGNORE" }