make-initrd: various improvements

- Generate a link to the initramfs file with an appropriate file
extension, guessed based on the compressor by default
- Use correct metadata in u-boot images if generated, up to now this
was hardcoded to gzip and would silently generate an erroneous image
if another compressor was specified
- Document all the parameters
- Improve cross-building compatibility, by allowing passing either a
string as before, or a function taking a package set and returning the
path to a compressor in the "compressor" argument of the
function.
- Support more compression algorithms
- Place compressor executable function and arguments in passthru, for
reuse when appending initramfses

Co-Authored-By: Dominik Xaver Hörl <hoe.dom@gmx.de>

+135 -18
+53
pkgs/build-support/kernel/initrd-compressor-meta.nix
···
··· 1 + rec { 2 + cat = { 3 + executable = pkgs: "cat"; 4 + ubootName = "none"; 5 + extension = ".cpio"; 6 + }; 7 + gzip = { 8 + executable = pkgs: "${pkgs.gzip}/bin/gzip"; 9 + defaultArgs = ["-9n"]; 10 + ubootName = "gzip"; 11 + extension = ".gz"; 12 + }; 13 + bzip2 = { 14 + executable = pkgs: "${pkgs.bzip2}/bin/bzip2"; 15 + ubootName = "bzip2"; 16 + extension = ".bz2"; 17 + }; 18 + xz = { 19 + executable = pkgs: "${pkgs.xz}/bin/xz"; 20 + defaultArgs = ["--check=crc32" "--lzma2=dict=512KiB"]; 21 + extension = ".xz"; 22 + }; 23 + lzma = { 24 + executable = pkgs: "${pkgs.xz}/bin/lzma"; 25 + defaultArgs = ["--check=crc32" "--lzma1=dict=512KiB"]; 26 + ubootName = "lzma"; 27 + extension = ".lzma"; 28 + }; 29 + lz4 = { 30 + executable = pkgs: "${pkgs.lz4}/bin/lz4"; 31 + defaultArgs = ["-l"]; 32 + ubootName = "lz4"; 33 + extension = ".lz4"; 34 + }; 35 + lzop = { 36 + executable = pkgs: "${pkgs.lzop}/bin/lzop"; 37 + ubootName = "lzo"; 38 + extension = ".lzo"; 39 + }; 40 + zstd = { 41 + executable = pkgs: "${pkgs.zstd}/bin/zstd"; 42 + defaultArgs = ["-10"]; 43 + ubootName = "zstd"; 44 + extension = ".zst"; 45 + }; 46 + pigz = gzip // { 47 + executable = pkgs: "${pkgs.pigz}/bin/pigz"; 48 + }; 49 + pixz = xz // { 50 + executable = pkgs: "${pkgs.pixz}/bin/pixz"; 51 + defaultArgs = []; 52 + }; 53 + }
+76 -15
pkgs/build-support/kernel/make-initrd.nix
··· 1 - # Create an initial ramdisk containing the closure of the specified 2 - # file system objects. An initial ramdisk is used during the initial 3 # stages of booting a Linux system. It is loaded by the boot loader 4 # along with the kernel image. It's supposed to contain everything 5 # (such as kernel modules) necessary to allow us to mount the root 6 # file system. Once the root file system is mounted, the `real' boot 7 # script can be called. 8 # 9 - # An initrd is really just a gzipped cpio archive. 10 - # 11 - # Symlinks are created for each top-level file system object. E.g., 12 - # `contents = {object = ...; symlink = /init;}' is a typical 13 - # argument. 14 15 - { stdenvNoCC, perl, cpio, contents, ubootTools 16 - , name ? "initrd" 17 - , compressor ? "gzip -9n" 18 , prepend ? [] 19 - , lib 20 , makeUInitrd ? stdenvNoCC.hostPlatform.platform.kernelTarget == "uImage" 21 - , uInitrdArch ? stdenvNoCC.hostPlatform.parsed.cpu.family 22 }: 23 let 24 # !!! Move this into a public lib function, it is probably useful for others ··· 26 lib.concatStringsSep "-" (filter (x: !(isList x)) (split "[^a-zA-Z0-9_=.?-]+" x)); 27 28 in stdenvNoCC.mkDerivation rec { 29 - inherit name makeUInitrd uInitrdArch; 30 31 builder = ./make-initrd.sh; 32 33 nativeBuildInputs = [ perl cpio ] 34 ++ stdenvNoCC.lib.optional makeUInitrd ubootTools; 35 36 # !!! should use XML. 37 objects = map (x: x.object) contents; 38 symlinks = map (x: x.symlink) contents; ··· 47 contents 48 (lib.range 0 (lib.length contents - 1)); 49 pathsFromGraph = ./paths-from-graph.pl; 50 - 51 - inherit compressor prepend; 52 }
··· 1 + # Create an initramfs containing the closure of the specified 2 + # file system objects. An initramfs is used during the initial 3 # stages of booting a Linux system. It is loaded by the boot loader 4 # along with the kernel image. It's supposed to contain everything 5 # (such as kernel modules) necessary to allow us to mount the root 6 # file system. Once the root file system is mounted, the `real' boot 7 # script can be called. 8 # 9 + # An initramfs is a cpio archive, and may be compressed with a number 10 + # of algorithms. 11 + let 12 + # Some metadata on various compression programs, relevant to naming 13 + # the initramfs file and, if applicable, generating a u-boot image 14 + # from it. 15 + compressors = import ./initrd-compressor-meta.nix; 16 + # Get the basename of the actual compression program from the whole 17 + # compression command, for the purpose of guessing the u-boot 18 + # compression type and filename extension. 19 + compressorName = fullCommand: builtins.elemAt (builtins.match "([^ ]*/)?([^ ]+).*" fullCommand) 1; 20 + in 21 + { stdenvNoCC, perl, cpio, ubootTools, lib, pkgsBuildHost 22 + # Name of the derivation (not of the resulting file!) 23 + , name ? "initrd" 24 + 25 + # Program used to compress the cpio archive; use "cat" for no compression. 26 + # This can also be a function which takes a package set and returns the path to the compressor, 27 + # such as `pkgs: "${pkgs.lzop}/bin/lzop"`. 28 + , compressor ? "gzip" 29 + , _compressorFunction ? 30 + if lib.isFunction compressor then compressor 31 + else if ! builtins.hasContext compressor && builtins.hasAttr compressor compressors then compressors.${compressor}.executable 32 + else _: compressor 33 + , _compressorExecutable ? _compressorFunction pkgsBuildHost 34 + , _compressorName ? compressorName _compressorExecutable 35 + , _compressorMeta ? compressors.${_compressorName} or {} 36 + 37 + # List of arguments to pass to the compressor program, or null to use its defaults 38 + , compressorArgs ? null 39 + , _compressorArgsReal ? if compressorArgs == null then _compressorMeta.defaultArgs or [] else compressorArgs 40 + 41 + # Filename extension to use for the compressed initramfs. This is 42 + # included for clarity, but $out/initrd will always be a symlink to 43 + # the final image. 44 + # If this isn't guessed, you may want to complete the metadata above and send a PR :) 45 + , extension ? _compressorMeta.extension or 46 + (throw "Unrecognised compressor ${_compressorName}, please specify filename extension") 47 + 48 + # List of { object = path_or_derivation; symlink = "/path"; } 49 + # The paths are copied into the initramfs in their nix store path 50 + # form, then linked at the root according to `symlink`. 51 + , contents 52 53 + # List of uncompressed cpio files to prepend to the initramfs. This 54 + # can be used to add files in specified paths without them becoming 55 + # symlinks to store paths. 56 , prepend ? [] 57 + 58 + # Whether to wrap the initramfs in a u-boot image. 59 , makeUInitrd ? stdenvNoCC.hostPlatform.platform.kernelTarget == "uImage" 60 + 61 + # If generating a u-boot image, the architecture to use. The default 62 + # guess may not align with u-boot's nomenclature correctly, so it can 63 + # be overridden. 64 + # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L81-106 for a list. 65 + , uInitrdArch ? stdenvNoCC.hostPlatform.kernelArch 66 + 67 + # The name of the compression, as recognised by u-boot. 68 + # See https://gitlab.denx.de/u-boot/u-boot/-/blob/9bfb567e5f1bfe7de8eb41f8c6d00f49d2b9a426/common/image.c#L195-204 for a list. 69 + # If this isn't guessed, you may want to complete the metadata above and send a PR :) 70 + , uInitrdCompression ? _compressorMeta.ubootName or 71 + (throw "Unrecognised compressor ${_compressorName}, please specify uInitrdCompression") 72 }: 73 let 74 # !!! Move this into a public lib function, it is probably useful for others ··· 76 lib.concatStringsSep "-" (filter (x: !(isList x)) (split "[^a-zA-Z0-9_=.?-]+" x)); 77 78 in stdenvNoCC.mkDerivation rec { 79 + inherit name makeUInitrd extension uInitrdArch prepend; 80 + 81 + ${if makeUInitrd then "uinitrdCompression" else null} = uInitrdCompression; 82 83 builder = ./make-initrd.sh; 84 85 nativeBuildInputs = [ perl cpio ] 86 ++ stdenvNoCC.lib.optional makeUInitrd ubootTools; 87 88 + compress = "${_compressorExecutable} ${lib.escapeShellArgs _compressorArgsReal}"; 89 + 90 + # Pass the function through, for reuse in append-initrd-secrets. The 91 + # function is used instead of the string, in order to support 92 + # cross-compilation (append-initrd-secrets running on a different 93 + # architecture than what the main initramfs is built on). 94 + passthru = { 95 + compressorExecutableFunction = _compressorFunction; 96 + compressorArgs = _compressorArgsReal; 97 + }; 98 + 99 # !!! should use XML. 100 objects = map (x: x.object) contents; 101 symlinks = map (x: x.symlink) contents; ··· 110 contents 111 (lib.range 0 (lib.length contents - 1)); 112 pathsFromGraph = ./paths-from-graph.pl; 113 }
+6 -3
pkgs/build-support/kernel/make-initrd.sh
··· 40 cat $PREP >> $out/initrd 41 done 42 (cd root && find * .[^.*] -exec touch -h -d '@1' '{}' +) 43 - (cd root && find * .[^.*] -print0 | sort -z | cpio -o -H newc -R +0:+0 --reproducible --null | $compressor >> $out/initrd) 44 45 if [ -n "$makeUInitrd" ]; then 46 - mv $out/initrd $out/initrd.gz 47 - mkimage -A $uInitrdArch -O linux -T ramdisk -C gzip -d $out/initrd.gz $out/initrd 48 fi
··· 40 cat $PREP >> $out/initrd 41 done 42 (cd root && find * .[^.*] -exec touch -h -d '@1' '{}' +) 43 + (cd root && find * .[^.*] -print0 | sort -z | cpio -o -H newc -R +0:+0 --reproducible --null | eval -- $compress >> "$out/initrd") 44 45 if [ -n "$makeUInitrd" ]; then 46 + mkimage -A $uInitrdArch -O linux -T ramdisk -C "$uInitrdCompression" -d $out/initrd"$extension" $out/initrd.img 47 + # Compatibility symlink 48 + ln -s "initrd.img" "$out/initrd" 49 + else 50 + ln -s "initrd" "$out/initrd$extension" 51 fi