kexecboot.nix (1841B)
1 { 2 config 3 , pkgs 4 , lib 5 , ... 6 }: 7 let 8 inherit (lib) mkOption mkForce types concatStringsSep; 9 in { 10 imports = [ ../ramdisk.nix ]; 11 options.system.outputs = { 12 kexecboot = mkOption { 13 type = types.package; 14 description = '' 15 Directory containing files needed for kexec booting. 16 Can be copied onto the target device using ssh or similar 17 ''; 18 }; 19 boot-sh = mkOption { 20 type = types.package; 21 description = '' 22 Shell script to run on the target device that invokes 23 kexec with appropriate options 24 ''; 25 }; 26 }; 27 config = { 28 boot.ramdisk.enable = true; 29 system.outputs = { 30 kexecboot = 31 let o = config.system.outputs; in 32 pkgs.runCommand "kexecboot" {} '' 33 mkdir $out 34 cd $out 35 ln -s ${o.rootfs} rootfs 36 ln -s ${o.kernel} kernel 37 ln -s ${o.manifest} manifest 38 ln -s ${o.boot-sh} boot.sh 39 ln -s ${pkgs.kexec-tools-static}/bin/kexec ./kexec 40 ln -s ${o.dtb} dtb 41 ''; 42 43 boot-sh = 44 let 45 inherit (pkgs.lib.trivial) toHexString; 46 inherit (config.system.outputs) rootfs kernel; 47 cmdline = concatStringsSep " " config.boot.commandLine; 48 in 49 pkgs.buildPackages.runCommand "boot.sh.sh" { 50 } '' 51 rootfsStart=${toString (100 * 1024 * 1024)} 52 rootfsBytes=$(stat -L -c %s ${rootfs}) 53 append_cmd="mtdparts=phram0:''${rootfsBytes}(rootfs) phram.phram=phram0,''${rootfsStart},''${rootfsBytes} memmap=''${rootfsBytes}\$''${rootfsStart}"; 54 cat > $out <<EOF 55 #!/bin/sh 56 test -d \$1 57 cd \$1 58 ./kexec -f -d --map-file rootfs@$rootfsStart --dtb dtb --command-line '${cmdline} $append_cmd' kernel 59 EOF 60 ''; 61 }; 62 }; 63 }