├── mkinitramfs └── mkinitcpio /mkinitramfs: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | export PATH='/usr/bin:/sbin:/bin' 5 | 6 | usage() 7 | { 8 | cat << EOF 9 | 10 | Usage: mkinitramfs [option]... -o outfile [version] 11 | 12 | Options: 13 | -c compress Override COMPRESS setting in initramfs.conf. 14 | -d destdir Extracted initramfs image dir 15 | -o outfile Write to outfile. 16 | 17 | See mkinitramfs(8) for further details. 18 | 19 | EOF 20 | } 21 | 22 | usage_error() 23 | { 24 | usage >&2 25 | exit 2 26 | } 27 | 28 | OPTIONS=$(getopt -o c:d:hko:r:v --long help -n "$0" -- "$@") || usage_error 29 | 30 | eval set -- "$OPTIONS" 31 | 32 | while true; do 33 | case "$1" in 34 | -c) 35 | compress="$2" 36 | shift 2 37 | ;; 38 | -d) 39 | DESTDIR="$2" 40 | shift 2 41 | if [ ! -d "${DESTDIR}" ]; then 42 | echo "${0}: ${DESTDIR}: Not a directory" >&2 43 | exit 1 44 | fi 45 | ;; 46 | -h|--help) 47 | usage 48 | exit 0 49 | ;; 50 | -o) 51 | outfile="$2" 52 | shift 2 53 | ;; 54 | --) 55 | shift 56 | break 57 | ;; 58 | *) 59 | echo "Internal error!" >&2 60 | exit 1 61 | ;; 62 | esac 63 | done 64 | 65 | case "${compress}" in 66 | gzip) # If we're doing a reproducible build, use gzip -n 67 | if [ -n "${SOURCE_DATE_EPOCH}" ]; then 68 | compress="gzip -n" 69 | # Otherwise, substitute pigz if it's available 70 | elif command -v pigz >/dev/null; then 71 | compress=pigz 72 | fi 73 | ;; 74 | lz4) compress="lz4 -2 -l" ;; 75 | zstd) compress="zstd -q -1 -T0" ;; 76 | xz) compress="xz --check=crc32" 77 | # If we're not doing a reproducible build, enable multithreading 78 | test -z "${SOURCE_DATE_EPOCH}" && compress="$compress --threads=0" 79 | ;; 80 | bzip2|lzma|lzop) 81 | # no parameters needed 82 | ;; 83 | *) echo "W: Unknown compression command ${compress}" >&2 ;; 84 | esac 85 | 86 | 87 | [ "$(id -ru)" != 0 ] && cpio_owner_root="-R 0:0" 88 | 89 | 90 | create_ucode() { 91 | find . -print0 | LC_ALL=C sort -z | cpio --null -R 0:0 -H newc -o --quiet > "$1" 92 | } 93 | 94 | create_maincpio() { 95 | cd "${DESTDIR}" && find . | LC_ALL=C sort | cpio --quiet $cpio_owner_root -o -H newc >>"${outfile}" || exit 1 96 | } 97 | -------------------------------------------------------------------------------- /mkinitcpio: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | build_image() { 4 | local out=$1 compressout=$1 compress=$2 errmsg pipestatus 5 | 6 | case $compress in 7 | cat) 8 | msg "Creating uncompressed initcpio image: %s" "$out" 9 | unset COMPRESSION_OPTIONS 10 | ;; 11 | *) 12 | msg "Creating %s-compressed initcpio image: %s" "$compress" "$out" 13 | ;;& 14 | xz) 15 | COMPRESSION_OPTIONS=('--check=crc32' "${COMPRESSION_OPTIONS[@]}") 16 | ;; 17 | lz4) 18 | COMPRESSION_OPTIONS=('-l' "${COMPRESSION_OPTIONS[@]}") 19 | ;; 20 | zstd) 21 | COMPRESSION_OPTIONS=('-T0' "${COMPRESSION_OPTIONS[@]}") 22 | ;; 23 | esac 24 | 25 | if [[ -f "$out" ]]; then 26 | local curr_size space_left_on_device 27 | 28 | curr_size="$(stat --format="%s" "$out")" 29 | space_left_on_device="$(($(stat -f --format="%a*%S" "$out")))" 30 | 31 | # check if there is enough space on the device to write the image to a tempfile, fallback otherwise 32 | # this assumes that the new image is not more than 1¼ times the size of the old one 33 | (( $(($curr_size + ($curr_size/4))) < $space_left_on_device )) && compressout="$out".tmp 34 | fi 35 | 36 | pushd "$BUILDROOT" >/dev/null 37 | 38 | # Reproducibility: set all timestamps to 0 39 | find . -mindepth 1 -execdir touch -hcd "@0" "{}" + 40 | 41 | # If this pipeline changes, |pipeprogs| below needs to be updated as well. 42 | find . -mindepth 1 -printf '%P\0' | 43 | sort -z | 44 | LANG=C bsdtar --uid 0 --gid 0 --null -cnf - -T - | 45 | LANG=C bsdtar --null -cf - --format=newc @- | 46 | $compress "${COMPRESSION_OPTIONS[@]}" > "$compressout" 47 | 48 | pipestatus=("${PIPESTATUS[@]}") 49 | pipeprogs=('find' 'sort' 'bsdtar (step 1)' 'bsdtar (step 2)' "$compress") 50 | 51 | popd >/dev/null 52 | 53 | for (( i = 0; i < ${#pipestatus[*]}; ++i )); do 54 | if (( pipestatus[i] )); then 55 | errmsg="${pipeprogs[i]} reported an error" 56 | break 57 | fi 58 | done 59 | 60 | if (( _builderrors )); then 61 | warning "errors were encountered during the build. The image may not be complete." 62 | fi 63 | 64 | if [[ $errmsg ]]; then 65 | error "Image generation FAILED: %s" "$errmsg" 66 | return 1 67 | elif (( _builderrors == 0 )); then 68 | msg "Image generation successful" 69 | fi 70 | 71 | # sync and rename as we only wrote to a tempfile so far to ensure consistency 72 | if [[ "$compressout" != "$out" ]]; then 73 | sync -d -- "$compressout" 74 | mv -f -- "$compressout" "$out" 75 | fi 76 | } 77 | --------------------------------------------------------------------------------