Project import generated by Copybara.
GitOrigin-RevId: 2deb07f3ac4eeb5de1c12c4ba2911a2eb1f6ed61
This commit is contained in:
parent
4aa1d3a395
commit
3d76146d87
2 changed files with 73 additions and 23 deletions
|
@ -17,19 +17,28 @@ import glob
|
||||||
import os.path
|
import os.path
|
||||||
from typing import Tuple, List, Optional
|
from typing import Tuple, List, Optional
|
||||||
|
|
||||||
|
SystemIdentifier = Tuple[Optional[str], int, Optional[str]]
|
||||||
|
|
||||||
|
|
||||||
def copy_if_not_exists(source: str, dest: str) -> None:
|
def copy_if_not_exists(source: str, dest: str) -> None:
|
||||||
if not os.path.exists(dest):
|
if not os.path.exists(dest):
|
||||||
shutil.copyfile(source, dest)
|
shutil.copyfile(source, dest)
|
||||||
|
|
||||||
|
|
||||||
def system_dir(profile: Optional[str], generation: int) -> str:
|
def generation_dir(profile: Optional[str], generation: int) -> str:
|
||||||
if profile:
|
if profile:
|
||||||
return "/nix/var/nix/profiles/system-profiles/%s-%d-link" % (profile, generation)
|
return "/nix/var/nix/profiles/system-profiles/%s-%d-link" % (profile, generation)
|
||||||
else:
|
else:
|
||||||
return "/nix/var/nix/profiles/system-%d-link" % (generation)
|
return "/nix/var/nix/profiles/system-%d-link" % (generation)
|
||||||
|
|
||||||
BOOT_ENTRY = """title NixOS{profile}
|
def system_dir(profile: Optional[str], generation: int, specialisation: Optional[str]) -> str:
|
||||||
|
d = generation_dir(profile, generation)
|
||||||
|
if specialisation:
|
||||||
|
return os.path.join(d, "specialisation", specialisation)
|
||||||
|
else:
|
||||||
|
return d
|
||||||
|
|
||||||
|
BOOT_ENTRY = """title NixOS{profile}{specialisation}
|
||||||
version Generation {generation} {description}
|
version Generation {generation} {description}
|
||||||
linux {kernel}
|
linux {kernel}
|
||||||
initrd {initrd}
|
initrd {initrd}
|
||||||
|
@ -46,26 +55,34 @@ efi /efi/memtest86/BOOTX64.efi
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def write_loader_conf(profile: Optional[str], generation: int) -> None:
|
def generation_conf_filename(profile: Optional[str], generation: int, specialisation: Optional[str]) -> str:
|
||||||
|
pieces = [
|
||||||
|
"nixos",
|
||||||
|
profile or None,
|
||||||
|
"generation",
|
||||||
|
str(generation),
|
||||||
|
f"specialisation-{specialisation}" if specialisation else None,
|
||||||
|
]
|
||||||
|
return "-".join(p for p in pieces if p) + ".conf"
|
||||||
|
|
||||||
|
|
||||||
|
def write_loader_conf(profile: Optional[str], generation: int, specialisation: Optional[str]) -> None:
|
||||||
with open("@efiSysMountPoint@/loader/loader.conf.tmp", 'w') as f:
|
with open("@efiSysMountPoint@/loader/loader.conf.tmp", 'w') as f:
|
||||||
if "@timeout@" != "":
|
if "@timeout@" != "":
|
||||||
f.write("timeout @timeout@\n")
|
f.write("timeout @timeout@\n")
|
||||||
if profile:
|
f.write("default %s\n" % generation_conf_filename(profile, generation, specialisation))
|
||||||
f.write("default nixos-%s-generation-%d.conf\n" % (profile, generation))
|
|
||||||
else:
|
|
||||||
f.write("default nixos-generation-%d.conf\n" % (generation))
|
|
||||||
if not @editor@:
|
if not @editor@:
|
||||||
f.write("editor 0\n");
|
f.write("editor 0\n");
|
||||||
f.write("console-mode @consoleMode@\n");
|
f.write("console-mode @consoleMode@\n");
|
||||||
os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf")
|
os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf")
|
||||||
|
|
||||||
|
|
||||||
def profile_path(profile: Optional[str], generation: int, name: str) -> str:
|
def profile_path(profile: Optional[str], generation: int, specialisation: Optional[str], name: str) -> str:
|
||||||
return os.path.realpath("%s/%s" % (system_dir(profile, generation), name))
|
return os.path.realpath("%s/%s" % (system_dir(profile, generation, specialisation), name))
|
||||||
|
|
||||||
|
|
||||||
def copy_from_profile(profile: Optional[str], generation: int, name: str, dry_run: bool = False) -> str:
|
def copy_from_profile(profile: Optional[str], generation: int, specialisation: Optional[str], name: str, dry_run: bool = False) -> str:
|
||||||
store_file_path = profile_path(profile, generation, name)
|
store_file_path = profile_path(profile, generation, specialisation, name)
|
||||||
suffix = os.path.basename(store_file_path)
|
suffix = os.path.basename(store_file_path)
|
||||||
store_dir = os.path.basename(os.path.dirname(store_file_path))
|
store_dir = os.path.basename(os.path.dirname(store_file_path))
|
||||||
efi_file_path = "/efi/nixos/%s-%s.efi" % (store_dir, suffix)
|
efi_file_path = "/efi/nixos/%s-%s.efi" % (store_dir, suffix)
|
||||||
|
@ -95,19 +112,17 @@ def describe_generation(generation_dir: str) -> str:
|
||||||
return description
|
return description
|
||||||
|
|
||||||
|
|
||||||
def write_entry(profile: Optional[str], generation: int, machine_id: str) -> None:
|
def write_entry(profile: Optional[str], generation: int, specialisation: Optional[str], machine_id: str) -> None:
|
||||||
kernel = copy_from_profile(profile, generation, "kernel")
|
kernel = copy_from_profile(profile, generation, specialisation, "kernel")
|
||||||
initrd = copy_from_profile(profile, generation, "initrd")
|
initrd = copy_from_profile(profile, generation, specialisation, "initrd")
|
||||||
try:
|
try:
|
||||||
append_initrd_secrets = profile_path(profile, generation, "append-initrd-secrets")
|
append_initrd_secrets = profile_path(profile, generation, specialisation, "append-initrd-secrets")
|
||||||
subprocess.check_call([append_initrd_secrets, "@efiSysMountPoint@%s" % (initrd)])
|
subprocess.check_call([append_initrd_secrets, "@efiSysMountPoint@%s" % (initrd)])
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
if profile:
|
entry_file = "@efiSysMountPoint@/loader/entries/%s" % (
|
||||||
entry_file = "@efiSysMountPoint@/loader/entries/nixos-%s-generation-%d.conf" % (profile, generation)
|
generation_conf_filename(profile, generation, specialisation))
|
||||||
else:
|
generation_dir = os.readlink(system_dir(profile, generation, specialisation))
|
||||||
entry_file = "@efiSysMountPoint@/loader/entries/nixos-generation-%d.conf" % (generation)
|
|
||||||
generation_dir = os.readlink(system_dir(profile, generation))
|
|
||||||
tmp_path = "%s.tmp" % (entry_file)
|
tmp_path = "%s.tmp" % (entry_file)
|
||||||
kernel_params = "init=%s/init " % generation_dir
|
kernel_params = "init=%s/init " % generation_dir
|
||||||
|
|
||||||
|
@ -115,6 +130,7 @@ def write_entry(profile: Optional[str], generation: int, machine_id: str) -> Non
|
||||||
kernel_params = kernel_params + params_file.read()
|
kernel_params = kernel_params + params_file.read()
|
||||||
with open(tmp_path, 'w') as f:
|
with open(tmp_path, 'w') as f:
|
||||||
f.write(BOOT_ENTRY.format(profile=" [" + profile + "]" if profile else "",
|
f.write(BOOT_ENTRY.format(profile=" [" + profile + "]" if profile else "",
|
||||||
|
specialisation=" (%s)" % specialisation if specialisation else "",
|
||||||
generation=generation,
|
generation=generation,
|
||||||
kernel=kernel,
|
kernel=kernel,
|
||||||
initrd=initrd,
|
initrd=initrd,
|
||||||
|
@ -133,7 +149,7 @@ def mkdir_p(path: str) -> None:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def get_generations(profile: Optional[str] = None) -> List[Tuple[Optional[str], int]]:
|
def get_generations(profile: Optional[str] = None) -> List[SystemIdentifier]:
|
||||||
gen_list = subprocess.check_output([
|
gen_list = subprocess.check_output([
|
||||||
"@nix@/bin/nix-env",
|
"@nix@/bin/nix-env",
|
||||||
"--list-generations",
|
"--list-generations",
|
||||||
|
@ -145,10 +161,19 @@ def get_generations(profile: Optional[str] = None) -> List[Tuple[Optional[str],
|
||||||
gen_lines.pop()
|
gen_lines.pop()
|
||||||
|
|
||||||
configurationLimit = @configurationLimit@
|
configurationLimit = @configurationLimit@
|
||||||
return [ (profile, int(line.split()[0])) for line in gen_lines ][-configurationLimit:]
|
configurations: List[SystemIdentifier] = [ (profile, int(line.split()[0]), None) for line in gen_lines ]
|
||||||
|
return configurations[-configurationLimit:]
|
||||||
|
|
||||||
|
|
||||||
def remove_old_entries(gens: List[Tuple[Optional[str], int]]) -> None:
|
def get_specialisations(profile: Optional[str], generation: int, _: Optional[str]) -> List[SystemIdentifier]:
|
||||||
|
specialisations_dir = os.path.join(
|
||||||
|
system_dir(profile, generation, None), "specialisation")
|
||||||
|
if not os.path.exists(specialisations_dir):
|
||||||
|
return []
|
||||||
|
return [(profile, generation, spec) for spec in os.listdir(specialisations_dir)]
|
||||||
|
|
||||||
|
|
||||||
|
def remove_old_entries(gens: List[SystemIdentifier]) -> None:
|
||||||
rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$")
|
rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$")
|
||||||
rex_generation = re.compile("^@efiSysMountPoint@/loader/entries/nixos.*-generation-(.*)\.conf$")
|
rex_generation = re.compile("^@efiSysMountPoint@/loader/entries/nixos.*-generation-(.*)\.conf$")
|
||||||
known_paths = []
|
known_paths = []
|
||||||
|
@ -243,6 +268,8 @@ def main() -> None:
|
||||||
for gen in gens:
|
for gen in gens:
|
||||||
try:
|
try:
|
||||||
write_entry(*gen, machine_id)
|
write_entry(*gen, machine_id)
|
||||||
|
for specialisation in get_specialisations(*gen):
|
||||||
|
write_entry(*specialisation, machine_id)
|
||||||
if os.readlink(system_dir(*gen)) == args.default_config:
|
if os.readlink(system_dir(*gen)) == args.default_config:
|
||||||
write_loader_conf(*gen)
|
write_loader_conf(*gen)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
|
|
23
third_party/nixpkgs/nixos/tests/systemd-boot.nix
vendored
23
third_party/nixpkgs/nixos/tests/systemd-boot.nix
vendored
|
@ -39,6 +39,29 @@ in
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Check that specialisations create corresponding boot entries.
|
||||||
|
specialisation = makeTest {
|
||||||
|
name = "systemd-boot-specialisation";
|
||||||
|
meta.maintainers = with pkgs.stdenv.lib.maintainers; [ lukegb ];
|
||||||
|
|
||||||
|
machine = { pkgs, lib, ... }: {
|
||||||
|
imports = [ common ];
|
||||||
|
specialisation.something.configuration = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
testScript = ''
|
||||||
|
machine.start()
|
||||||
|
machine.wait_for_unit("multi-user.target")
|
||||||
|
|
||||||
|
machine.succeed(
|
||||||
|
"test -e /boot/loader/entries/nixos-generation-1-specialisation-something.conf"
|
||||||
|
)
|
||||||
|
machine.succeed(
|
||||||
|
"grep -q 'title NixOS (something)' /boot/loader/entries/nixos-generation-1-specialisation-something.conf"
|
||||||
|
)
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
# Boot without having created an EFI entry--instead using default "/EFI/BOOT/BOOTX64.EFI"
|
# Boot without having created an EFI entry--instead using default "/EFI/BOOT/BOOTX64.EFI"
|
||||||
fallback = makeTest {
|
fallback = makeTest {
|
||||||
name = "systemd-boot-fallback";
|
name = "systemd-boot-fallback";
|
||||||
|
|
Loading…
Reference in a new issue