nixos/etc-overlay: always create the metadata mountpoints in /run

This avoids the dependence on the presence of /tmp, which causes issues
with nixos-install

r-vdp df7c405f 2187d197

Changed files
+26 -18
nixos
+3 -3
nixos/modules/system/etc/etc-activation.nix
···
boot.initrd.systemd = {
mounts = [
{
-
where = "/run/etc-metadata";
what = "/etc-metadata-image";
type = "erofs";
options = "loop,ro";
···
"relatime"
"redirect_dir=on"
"metacopy=on"
-
"lowerdir=/run/etc-metadata::/etc-basedir"
]
++ lib.optionals config.system.etc.overlay.mutable [
"rw"
···
unitConfig = {
RequiresMountsFor = [
"/sysroot/nix/store"
-
"/run/etc-metadata"
];
DefaultDependencies = false;
};
···
boot.initrd.systemd = {
mounts = [
{
+
where = "/run/nixos-etc-metadata";
what = "/etc-metadata-image";
type = "erofs";
options = "loop,ro";
···
"relatime"
"redirect_dir=on"
"metacopy=on"
+
"lowerdir=/run/nixos-etc-metadata::/etc-basedir"
]
++ lib.optionals config.system.etc.overlay.mutable [
"rw"
···
unitConfig = {
RequiresMountsFor = [
"/sysroot/nix/store"
+
"/run/nixos-etc-metadata"
];
DefaultDependencies = false;
};
+3 -3
nixos/modules/system/etc/etc.nix
···
chmod --recursive 0755 /.rw-etc
''}
-
tmpMetadataMount=$(TMPDIR="" mktemp --tmpdir=/tmp --directory -t nixos-etc-metadata.XXXXXXXXXX)
mount --type erofs -o ro ${config.system.build.etcMetadataImage} $tmpMetadataMount
# There was no previous /etc mounted. This happens when we're called
···
# Mount the new /etc overlay to a temporary private mount.
# This needs the indirection via a private bind mount because you
# cannot move shared mounts.
-
tmpEtcMount=$(TMPDIR="" mktemp --tmpdir=/tmp --directory -t nixos-etc.XXXXXXXXXX)
mount --bind --make-private $tmpEtcMount $tmpEtcMount
mount --type overlay overlay \
--options lowerdir=$tmpMetadataMount::${config.system.build.etcBasedir},${etcOverlayOptions} \
···
# mounts. So we'll just find all mounts of type erofs and filter on the
# name of the mountpoint.
findmnt --type erofs --list --kernel --output TARGET | while read -r mountPoint; do
-
if [[ "$mountPoint" =~ ^/tmp/nixos-etc-metadata\..{10}$ &&
"$mountPoint" != "$tmpMetadataMount" ]]; then
umount --lazy "$mountPoint"
rmdir "$mountPoint"
···
chmod --recursive 0755 /.rw-etc
''}
+
tmpMetadataMount=$(TMPDIR="/run" mktemp --directory -t nixos-etc-metadata.XXXXXXXXXX)
mount --type erofs -o ro ${config.system.build.etcMetadataImage} $tmpMetadataMount
# There was no previous /etc mounted. This happens when we're called
···
# Mount the new /etc overlay to a temporary private mount.
# This needs the indirection via a private bind mount because you
# cannot move shared mounts.
+
tmpEtcMount=$(TMPDIR="/run" mktemp --directory -t nixos-etc.XXXXXXXXXX)
mount --bind --make-private $tmpEtcMount $tmpEtcMount
mount --type overlay overlay \
--options lowerdir=$tmpMetadataMount::${config.system.build.etcBasedir},${etcOverlayOptions} \
···
# mounts. So we'll just find all mounts of type erofs and filter on the
# name of the mountpoint.
findmnt --type erofs --list --kernel --output TARGET | while read -r mountPoint; do
+
if [[ ("$mountPoint" =~ ^/run/nixos-etc-metadata\..{10}$ || "$mountPoint" =~ ^/run/nixos-etc-metadata$ ) &&
"$mountPoint" != "$tmpMetadataMount" ]]; then
umount --lazy "$mountPoint"
rmdir "$mountPoint"
+10 -6
nixos/tests/activation/etc-overlay-immutable.nix
···
''
newergen = machine.succeed("realpath /run/current-system/specialisation/newer-generation/bin/switch-to-configuration").rstrip()
-
with subtest("/run/etc-metadata/ is mounted"):
-
print(machine.succeed("mountpoint /run/etc-metadata"))
with subtest("No temporary files leaked into stage 2"):
machine.succeed("[ ! -e /etc-metadata-image ]")
···
machine.succeed(f"{newergen} switch")
-
tmpMounts = machine.succeed("find /tmp -maxdepth 1 -type d -regex '/tmp/nixos-etc\\..*' | wc -l").rstrip()
-
metaMounts = machine.succeed("find /tmp -maxdepth 1 -type d -regex '/tmp/nixos-etc-metadata\\..*' | wc -l").rstrip()
-
assert tmpMounts == "0", f"Found {tmpMounts} remaining tmpmounts"
-
assert metaMounts == "1", f"Found {metaMounts} remaining metamounts"
'';
}
···
''
newergen = machine.succeed("realpath /run/current-system/specialisation/newer-generation/bin/switch-to-configuration").rstrip()
+
with subtest("/run/nixos-etc-metadata/ is mounted"):
+
print(machine.succeed("mountpoint /run/nixos-etc-metadata"))
with subtest("No temporary files leaked into stage 2"):
machine.succeed("[ ! -e /etc-metadata-image ]")
···
machine.succeed(f"{newergen} switch")
+
tmpMounts = machine.succeed("find /run -maxdepth 1 -type d -regex '/run/nixos-etc\\..*'").rstrip()
+
print(tmpMounts)
+
metaMounts = machine.succeed("find /run -maxdepth 1 -type d -regex '/run/nixos-etc-metadata.*'").rstrip()
+
print(metaMounts)
+
numOfTmpMounts = len(tmpMounts.splitlines())
+
numOfMetaMounts = len(metaMounts.splitlines())
+
assert numOfTmpMounts == 0, f"Found {numOfTmpMounts} remaining tmpmounts"
+
assert numOfMetaMounts == 1, f"Found {numOfMetaMounts} remaining metamounts"
'';
}
+10 -6
nixos/tests/activation/etc-overlay-mutable.nix
···
''
newergen = machine.succeed("realpath /run/current-system/specialisation/newer-generation/bin/switch-to-configuration").rstrip()
-
with subtest("/run/etc-metadata/ is mounted"):
-
print(machine.succeed("mountpoint /run/etc-metadata"))
with subtest("No temporary files leaked into stage 2"):
machine.succeed("[ ! -e /etc-metadata-image ]")
···
machine.succeed(f"{newergen} switch")
assert machine.succeed("cat /etc/newergen") == "newergen"
-
tmpMounts = machine.succeed("find /tmp -maxdepth 1 -type d -regex '/tmp/nixos-etc\\..*' | wc -l").rstrip()
-
metaMounts = machine.succeed("find /tmp -maxdepth 1 -type d -regex '/tmp/nixos-etc-metadata\\..*' | wc -l").rstrip()
-
assert tmpMounts == "0", f"Found {tmpMounts} remaining tmpmounts"
-
assert metaMounts == "1", f"Found {metaMounts} remaining metamounts"
'';
}
···
''
newergen = machine.succeed("realpath /run/current-system/specialisation/newer-generation/bin/switch-to-configuration").rstrip()
+
with subtest("/run/nixos-etc-metadata/ is mounted"):
+
print(machine.succeed("mountpoint /run/nixos-etc-metadata"))
with subtest("No temporary files leaked into stage 2"):
machine.succeed("[ ! -e /etc-metadata-image ]")
···
machine.succeed(f"{newergen} switch")
assert machine.succeed("cat /etc/newergen") == "newergen"
+
tmpMounts = machine.succeed("find /run -maxdepth 1 -type d -regex '/run/nixos-etc\\..*'").rstrip()
+
print(tmpMounts)
+
metaMounts = machine.succeed("find /run -maxdepth 1 -type d -regex '/run/nixos-etc-metadata.*'").rstrip()
+
print(metaMounts)
+
numOfTmpMounts = len(tmpMounts.splitlines())
+
numOfMetaMounts = len(metaMounts.splitlines())
+
assert numOfTmpMounts == 0, f"Found {numOfTmpMounts} remaining tmpmounts"
+
assert numOfMetaMounts == 1, f"Found {numOfMetaMounts} remaining metamounts"
'';
}