Skip to content

Instantly share code, notes, and snippets.

@Hritik14
Created May 27, 2021 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hritik14/fe576764c5ce24776d7adef32815ce14 to your computer and use it in GitHub Desktop.
Save Hritik14/fe576764c5ce24776d7adef32815ce14 to your computer and use it in GitHub Desktop.
build_image function from mkinitcpio
#!/bin/bash
build_image() {
local out=$1 compress=$2 errmsg pipestatus
case $compress in
cat)
msg "Creating uncompressed initcpio image: %s" "$out"
unset COMPRESSION_OPTIONS
;;
*)
msg "Creating %s-compressed initcpio image: %s" "$compress" "$out"
;;&
xz)
COMPRESSION_OPTIONS=('--check=crc32' "${COMPRESSION_OPTIONS[@]}")
;;
lz4)
COMPRESSION_OPTIONS=('-l' "${COMPRESSION_OPTIONS[@]}")
;;
zstd)
COMPRESSION_OPTIONS=('-T0' "${COMPRESSION_OPTIONS[@]}")
;;
esac
pushd "$BUILDROOT" >/dev/null
# Reproducibility: set all timestamps to 0
find . -mindepth 1 -execdir touch -hcd "@0" "{}" +
# If this pipeline changes, |pipeprogs| below needs to be updated as well.
find . -mindepth 1 -printf '%P\0' |
sort -z |
LANG=C bsdtar --null -cnf - -T - |
LANG=C bsdtar --uid 0 --gid 0 --null -cf - --format=newc @- |
$compress "${COMPRESSION_OPTIONS[@]}" > "$out"
pipestatus=("${PIPESTATUS[@]}")
pipeprogs=('find' 'sort' 'bsdtar (step 1)' 'bsdtar (step 2)' "$compress")
popd >/dev/null
for (( i = 0; i < ${#pipestatus[*]}; ++i )); do
if (( pipestatus[i] )); then
errmsg="${pipeprogs[i]} reported an error"
break
fi
done
if (( _builderrors )); then
warning "errors were encountered during the build. The image may not be complete."
fi
if [[ $errmsg ]]; then
error "Image generation FAILED: %s" "$errmsg"
elif (( _builderrors == 0 )); then
msg "Image generation successful"
fi
}
alias msg=echo
if [[ $# != 2 ]]; then
echo "Usage: $0 initramfs.img compression_method"
exit 1
fi
build_image "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment