Merge pull request #170123 from ElvishJerricco/systemd-shutdown-ramfs-fixup

Systemd shutdown ramfs fixup
This commit is contained in:
Will Fancher 2022-04-28 20:17:23 -04:00 committed by GitHub
commit 2e96b64e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 43 deletions

View File

@ -1288,7 +1288,7 @@
<literal>systemd-shutdown</literal> is now properly linked on <literal>systemd-shutdown</literal> is now properly linked on
shutdown to unmount all filesystems and device mapper devices shutdown to unmount all filesystems and device mapper devices
cleanly. This can be disabled using cleanly. This can be disabled using
<literal>boot.systemd.shutdown.enable</literal>. <literal>systemd.shutdownRamfs.enable</literal>.
</para> </para>
</listitem> </listitem>
<listitem> <listitem>

View File

@ -510,7 +510,7 @@ In addition to numerous new and upgraded packages, this release has the followin
- `systemd-nspawn@.service` settings have been reverted to the default systemd behaviour. User namespaces are now activated by default. If you want to keep running nspawn containers without user namespaces you need to set `systemd.nspawn.<name>.execConfig.PrivateUsers = false` - `systemd-nspawn@.service` settings have been reverted to the default systemd behaviour. User namespaces are now activated by default. If you want to keep running nspawn containers without user namespaces you need to set `systemd.nspawn.<name>.execConfig.PrivateUsers = false`
- `systemd-shutdown` is now properly linked on shutdown to unmount all filesystems and device mapper devices cleanly. This can be disabled using `boot.systemd.shutdown.enable`. - `systemd-shutdown` is now properly linked on shutdown to unmount all filesystems and device mapper devices cleanly. This can be disabled using `systemd.shutdownRamfs.enable`.
- The Tor SOCKS proxy is now actually disabled if `services.tor.client.enable` is set to `false` (the default). If you are using this functionality but didn't change the setting or set it to `false`, you now need to set it to `true`. - The Tor SOCKS proxy is now actually disabled if `services.tor.client.enable` is set to `false` (the default). If you are using this functionality but didn't change the setting or set it to `false`, you now need to set it to `true`.

View File

@ -1,4 +1,4 @@
{ lib, systemdUtils }: { lib, systemdUtils, pkgs }:
with systemdUtils.lib; with systemdUtils.lib;
with systemdUtils.unitOptions; with systemdUtils.unitOptions;
@ -34,4 +34,36 @@ rec {
automounts = with types; listOf (submodule [ stage2AutomountOptions unitConfig automountConfig ]); automounts = with types; listOf (submodule [ stage2AutomountOptions unitConfig automountConfig ]);
initrdAutomounts = with types; attrsOf (submodule [ stage1AutomountOptions unitConfig automountConfig ]); initrdAutomounts = with types; attrsOf (submodule [ stage1AutomountOptions unitConfig automountConfig ]);
initrdContents = types.attrsOf (types.submodule ({ config, options, name, ... }: {
options = {
enable = mkEnableOption "copying of this file and symlinking it" // { default = true; };
target = mkOption {
type = types.path;
description = ''
Path of the symlink.
'';
default = name;
};
text = mkOption {
default = null;
type = types.nullOr types.lines;
description = "Text of the file.";
};
source = mkOption {
type = types.path;
description = "Path of the source file.";
};
};
config = {
source = mkIf (config.text != null) (
let name' = "initrd-" + baseNameOf name;
in mkDerivedConfig options.text (pkgs.writeText name')
);
};
}));
} }

View File

@ -213,6 +213,6 @@ rec {
systemdUtils = { systemdUtils = {
lib = import ./systemd-lib.nix { inherit lib config pkgs; }; lib = import ./systemd-lib.nix { inherit lib config pkgs; };
unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; }; unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; };
types = import ./systemd-types.nix { inherit lib systemdUtils; }; types = import ./systemd-types.nix { inherit lib systemdUtils pkgs; };
}; };
} }

View File

@ -155,37 +155,7 @@ in {
''; '';
visible = false; visible = false;
default = {}; default = {};
type = types.attrsOf (types.submodule ({ config, options, name, ... }: { type = utils.systemdUtils.types.initrdContents;
options = {
enable = mkEnableOption "copying of this file to initrd and symlinking it" // { default = true; };
target = mkOption {
type = types.path;
description = ''
Path of the symlink.
'';
default = name;
};
text = mkOption {
default = null;
type = types.nullOr types.lines;
description = "Text of the file.";
};
source = mkOption {
type = types.path;
description = "Path of the source file.";
};
};
config = {
source = mkIf (config.text != null) (
let name' = "initrd-" + baseNameOf name;
in mkDerivedConfig options.text (pkgs.writeText name')
);
};
}));
}; };
storePaths = mkOption { storePaths = mkOption {

View File

@ -1,31 +1,57 @@
{ config, lib, ... }: let { config, lib, utils, pkgs, ... }: let
cfg = config.boot.systemd.shutdown; cfg = config.systemd.shutdownRamfs;
ramfsContents = let
storePaths = map (p: "${p}\n") cfg.storePaths;
contents = lib.mapAttrsToList (_: v: "${v.source}\n${v.target}") (lib.filterAttrs (_: v: v.enable) cfg.contents);
in pkgs.writeText "shutdown-ramfs-contents" (lib.concatStringsSep "\n" (storePaths ++ contents));
in { in {
options.boot.systemd.shutdown = { options.systemd.shutdownRamfs = {
enable = lib.mkEnableOption "pivoting back to an initramfs for shutdown" // { default = true; }; enable = lib.mkEnableOption "pivoting back to an initramfs for shutdown" // { default = true; };
contents = lib.mkOption {
description = "Set of files that have to be linked into the shutdown ramfs";
example = lib.literalExpression ''
{
"/lib/systemd/system-shutdown/zpool-sync-shutdown".source = writeShellScript "zpool" "exec ''${zfs}/bin/zpool sync"
}
'';
type = utils.systemdUtils.types.initrdContents;
};
storePaths = lib.mkOption {
description = ''
Store paths to copy into the shutdown ramfs as well.
'';
type = lib.types.listOf lib.types.singleLineStr;
default = [];
};
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
systemd.shutdownRamfs.contents."/shutdown".source = "${config.systemd.package}/lib/systemd/systemd-shutdown";
systemd.shutdownRamfs.storePaths = [pkgs.runtimeShell "${pkgs.coreutils}/bin"];
systemd.services.generate-shutdown-ramfs = { systemd.services.generate-shutdown-ramfs = {
description = "Generate shutdown ramfs"; description = "Generate shutdown ramfs";
wantedBy = [ "shutdown.target" ];
before = [ "shutdown.target" ]; before = [ "shutdown.target" ];
unitConfig = { unitConfig = {
DefaultDependencies = false; DefaultDependencies = false;
ConditionFileIsExecutable = [ ConditionFileIsExecutable = [
"!/run/initramfs/shutdown" "!/run/initramfs/shutdown"
"/run/current-system/systemd/lib/systemd/systemd-shutdown"
]; ];
}; };
path = [pkgs.util-linux pkgs.makeInitrdNGTool pkgs.glibc pkgs.patchelf];
serviceConfig.Type = "oneshot"; serviceConfig.Type = "oneshot";
script = '' script = ''
mkdir -p /run/initramfs mkdir -p /run/initramfs
if ! mountpoint -q /run/initramfs; then if ! mountpoint -q /run/initramfs; then
mount -t tmpfs tmpfs /run/initramfs mount -t tmpfs tmpfs /run/initramfs
fi fi
cp /run/current-system/systemd/lib/systemd/systemd-shutdown /run/initramfs/shutdown make-initrd-ng ${ramfsContents} /run/initramfs
''; '';
}; };
}; };

View File

@ -466,6 +466,11 @@ in
'') rootPools)); '') rootPools));
}; };
systemd.shutdownRamfs.contents."/etc/systemd/system-shutdown/zpool".source = pkgs.writeShellScript "zpool-sync-shutdown" ''
exec ${cfgZfs.package}/bin/zpool sync
'';
systemd.shutdownRamfs.storePaths = ["${cfgZfs.package}/bin/zpool"];
# TODO FIXME See https://github.com/NixOS/nixpkgs/pull/99386#issuecomment-798813567. To not break people's bootloader and as probably not everybody would read release notes that thoroughly add inSystem. # TODO FIXME See https://github.com/NixOS/nixpkgs/pull/99386#issuecomment-798813567. To not break people's bootloader and as probably not everybody would read release notes that thoroughly add inSystem.
boot.loader.grub = mkIf (inInitrd || inSystem) { boot.loader.grub = mkIf (inInitrd || inSystem) {
zfsSupport = true; zfsSupport = true;

View File

@ -1,4 +1,6 @@
import ./make-test-python.nix ({ pkgs, systemdStage1 ? false, ...} : { import ./make-test-python.nix ({ pkgs, systemdStage1 ? false, ...} : let
msg = "Shutting down NixOS";
in {
name = "systemd-shutdown"; name = "systemd-shutdown";
meta = with pkgs.lib.maintainers; { meta = with pkgs.lib.maintainers; {
maintainers = [ das_j ]; maintainers = [ das_j ];
@ -6,7 +8,9 @@ import ./make-test-python.nix ({ pkgs, systemdStage1 ? false, ...} : {
nodes.machine = { nodes.machine = {
imports = [ ../modules/profiles/minimal.nix ]; imports = [ ../modules/profiles/minimal.nix ];
boot.initrd.systemd.enable = systemdStage1; systemd.shutdownRamfs.contents."/etc/systemd/system-shutdown/shutdown-message".source = pkgs.writeShellScript "shutdown-message" ''
echo "${msg}"
'';
}; };
testScript = '' testScript = ''
@ -14,7 +18,8 @@ import ./make-test-python.nix ({ pkgs, systemdStage1 ? false, ...} : {
# .shutdown() would wait for the machine to power off # .shutdown() would wait for the machine to power off
machine.succeed("systemctl poweroff") machine.succeed("systemctl poweroff")
# Message printed by systemd-shutdown # Message printed by systemd-shutdown
machine.wait_for_console_text("All filesystems, swaps, loop devices, MD devices and DM devices detached.") machine.wait_for_console_text("Unmounting '/oldroot'")
machine.wait_for_console_text("${msg}")
# Don't try to sync filesystems # Don't try to sync filesystems
machine.booted = False machine.booted = False
''; '';