Merge pull request #227633 from ElvishJerricco/systemd-stage-1-fix-initrd-commands

Systemd stage 1 assert initrd commands are empty
This commit is contained in:
K900 2023-10-30 20:03:44 +03:00 committed by GitHub
commit 7bc18b63da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 22 deletions

View file

@ -350,7 +350,7 @@ in
boot.kernelParams = mkIf (!config.networking.usePredictableInterfaceNames) [ "net.ifnames=0" ]; boot.kernelParams = mkIf (!config.networking.usePredictableInterfaceNames) [ "net.ifnames=0" ];
boot.initrd.extraUdevRulesCommands = optionalString (!config.boot.initrd.systemd.enable && config.boot.initrd.services.udev.rules != "") boot.initrd.extraUdevRulesCommands = mkIf (!config.boot.initrd.systemd.enable && config.boot.initrd.services.udev.rules != "")
'' ''
cat <<'EOF' > $out/99-local.rules cat <<'EOF' > $out/99-local.rules
${config.boot.initrd.services.udev.rules} ${config.boot.initrd.services.udev.rules}

View file

@ -546,8 +546,9 @@ in {
# We do not have systemd in stage-1 boot so must invoke `multipathd` # We do not have systemd in stage-1 boot so must invoke `multipathd`
# with the `-1` argument which disables systemd calls. Invoke `multipath` # with the `-1` argument which disables systemd calls. Invoke `multipath`
# to display the multipath mappings in the output of `journalctl -b`. # to display the multipath mappings in the output of `journalctl -b`.
# TODO: Implement for systemd stage 1
boot.initrd.kernelModules = [ "dm-multipath" "dm-service-time" ]; boot.initrd.kernelModules = [ "dm-multipath" "dm-service-time" ];
boot.initrd.postDeviceCommands = '' boot.initrd.postDeviceCommands = mkIf (!config.boot.initrd.systemd.enable) ''
modprobe -a dm-multipath dm-service-time modprobe -a dm-multipath dm-service-time
multipathd -s multipathd -s
(set -x && sleep 1 && multipath -ll) (set -x && sleep 1 && multipath -ll)

View file

@ -116,11 +116,11 @@ in
boot.initrd.kernelModules = [ "af_packet" ]; boot.initrd.kernelModules = [ "af_packet" ];
boot.initrd.extraUtilsCommands = '' boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
copy_bin_and_libs ${pkgs.klibc}/lib/klibc/bin.static/ipconfig copy_bin_and_libs ${pkgs.klibc}/lib/klibc/bin.static/ipconfig
''; '';
boot.initrd.preLVMCommands = mkBefore ( boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) (mkBefore (
# Search for interface definitions in command line. # Search for interface definitions in command line.
'' ''
ifaces="" ifaces=""
@ -148,9 +148,9 @@ in
done done
'' ''
+ cfg.postCommands); + cfg.postCommands));
boot.initrd.postMountCommands = mkIf cfg.flushBeforeStage2 '' boot.initrd.postMountCommands = mkIf (cfg.flushBeforeStage2 && !config.boot.initrd.systemd.enable) ''
for iface in $ifaces; do for iface in $ifaces; do
ip address flush dev "$iface" ip address flush dev "$iface"
ip link set dev "$iface" down ip link set dev "$iface" down

View file

@ -348,6 +348,27 @@ in {
}; };
config = mkIf (config.boot.initrd.enable && cfg.enable) { config = mkIf (config.boot.initrd.enable && cfg.enable) {
assertions = map (name: {
assertion = lib.attrByPath name (throw "impossible") config.boot.initrd == "";
message = ''
systemd stage 1 does not support 'boot.initrd.${lib.concatStringsSep "." name}'. Please
convert it to analogous systemd units in 'boot.initrd.systemd'.
Definitions:
${lib.concatMapStringsSep "\n" ({ file, ... }: " - ${file}") (lib.attrByPath name (throw "impossible") options.boot.initrd).definitionsWithLocations}
'';
}) [
[ "preFailCommands" ]
[ "preDeviceCommands" ]
[ "preLVMCommands" ]
[ "postDeviceCommands" ]
[ "postMountCommands" ]
[ "extraUdevRulesCommands" ]
[ "extraUtilsCommands" ]
[ "extraUtilsCommandsTest" ]
[ "network" "postCommands" ]
];
system.build = { inherit initialRamdisk; }; system.build = { inherit initialRamdisk; };
boot.initrd.availableKernelModules = [ boot.initrd.availableKernelModules = [

View file

@ -110,10 +110,11 @@ in
}) earlyEncDevs); }) earlyEncDevs);
forceLuksSupportInInitrd = true; forceLuksSupportInInitrd = true;
}; };
postMountCommands = # TODO: systemd stage 1
concatMapStrings (dev: postMountCommands = lib.mkIf (!config.boot.initrd.systemd.enable)
(concatMapStrings (dev:
"cryptsetup luksOpen --key-file ${dev.encrypted.keyFile} ${dev.encrypted.blkDev} ${dev.encrypted.label};\n" "cryptsetup luksOpen --key-file ${dev.encrypted.keyFile} ${dev.encrypted.blkDev} ${dev.encrypted.label};\n"
) lateEncDevs; ) lateEncDevs);
}; };
}; };
} }

View file

@ -102,11 +102,11 @@ in
copy_bin_and_libs ${pkgs.bcachefs-tools}/bin/bcachefs copy_bin_and_libs ${pkgs.bcachefs-tools}/bin/bcachefs
copy_bin_and_libs ${mountCommand}/bin/mount.bcachefs copy_bin_and_libs ${mountCommand}/bin/mount.bcachefs
''; '';
boot.initrd.extraUtilsCommandsTest = '' boot.initrd.extraUtilsCommandsTest = lib.mkIf (!config.boot.initrd.systemd.enable) ''
$out/bin/bcachefs version $out/bin/bcachefs version
''; '';
boot.initrd.postDeviceCommands = commonFunctions + concatStrings (mapAttrsToList openCommand bootFs); boot.initrd.postDeviceCommands = lib.mkIf (!config.boot.initrd.systemd.enable) (commonFunctions + concatStrings (mapAttrsToList openCommand bootFs));
boot.initrd.systemd.services = lib.mapAttrs' (mkUnits "/sysroot") bootFs; boot.initrd.systemd.services = lib.mapAttrs' (mkUnits "/sysroot") bootFs;
}) })

View file

@ -584,17 +584,17 @@ in
boot.initrd = mkIf inInitrd { boot.initrd = mkIf inInitrd {
kernelModules = [ "zfs" ] ++ optional (!cfgZfs.enableUnstable) "spl"; kernelModules = [ "zfs" ] ++ optional (!cfgZfs.enableUnstable) "spl";
extraUtilsCommands = extraUtilsCommands =
'' mkIf (!config.boot.initrd.systemd.enable) ''
copy_bin_and_libs ${cfgZfs.package}/sbin/zfs copy_bin_and_libs ${cfgZfs.package}/sbin/zfs
copy_bin_and_libs ${cfgZfs.package}/sbin/zdb copy_bin_and_libs ${cfgZfs.package}/sbin/zdb
copy_bin_and_libs ${cfgZfs.package}/sbin/zpool copy_bin_and_libs ${cfgZfs.package}/sbin/zpool
''; '';
extraUtilsCommandsTest = mkIf inInitrd extraUtilsCommandsTest =
'' mkIf (!config.boot.initrd.systemd.enable) ''
$out/bin/zfs --help >/dev/null 2>&1 $out/bin/zfs --help >/dev/null 2>&1
$out/bin/zpool --help >/dev/null 2>&1 $out/bin/zpool --help >/dev/null 2>&1
''; '';
postDeviceCommands = concatStringsSep "\n" (['' postDeviceCommands = mkIf (!config.boot.initrd.systemd.enable) (concatStringsSep "\n" ([''
ZFS_FORCE="${optionalString cfgZfs.forceImportRoot "-f"}" ZFS_FORCE="${optionalString cfgZfs.forceImportRoot "-f"}"
''] ++ [(importLib { ''] ++ [(importLib {
# See comments at importLib definition. # See comments at importLib definition.
@ -623,10 +623,10 @@ in
else concatMapStrings (fs: '' else concatMapStrings (fs: ''
zfs load-key -- ${escapeShellArg fs} zfs load-key -- ${escapeShellArg fs}
'') (filter (x: datasetToPool x == pool) cfgZfs.requestEncryptionCredentials)} '') (filter (x: datasetToPool x == pool) cfgZfs.requestEncryptionCredentials)}
'') rootPools)); '') rootPools)));
# Systemd in stage 1 # Systemd in stage 1
systemd = { systemd = mkIf config.boot.initrd.systemd.enable {
packages = [cfgZfs.package]; packages = [cfgZfs.package];
services = listToAttrs (map (pool: createImportService { services = listToAttrs (map (pool: createImportService {
inherit pool; inherit pool;

View file

@ -62,13 +62,13 @@ in {
cp -v ${pkgs.mdadm}/lib/udev/rules.d/*.rules $out/ cp -v ${pkgs.mdadm}/lib/udev/rules.d/*.rules $out/
''; '';
extraUtilsCommands = '' extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
# Add RAID mdadm tool. # Add RAID mdadm tool.
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon
''; '';
extraUtilsCommandsTest = '' extraUtilsCommandsTest = lib.mkIf (!config.boot.initrd.systemd.enable) ''
$out/bin/mdadm --version $out/bin/mdadm --version
''; '';

View file

@ -5,19 +5,19 @@
# `virtualisation.fileSystems."/".autoFormat = true;` # `virtualisation.fileSystems."/".autoFormat = true;`
# instead. # instead.
{ config, pkgs, ... }: { lib, config, pkgs, ... }:
let let
rootDevice = config.virtualisation.rootDevice; rootDevice = config.virtualisation.rootDevice;
in in
{ {
boot.initrd.extraUtilsCommands = '' boot.initrd.extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
# We need mke2fs in the initrd. # We need mke2fs in the initrd.
copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs
''; '';
boot.initrd.postDeviceCommands = '' boot.initrd.postDeviceCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
# If the disk image appears to be empty, run mke2fs to # If the disk image appears to be empty, run mke2fs to
# initialise. # initialise.
FSTYPE=$(blkid -o value -s TYPE ${rootDevice} || true) FSTYPE=$(blkid -o value -s TYPE ${rootDevice} || true)