Merge pull request #172849 from waldheinz/systemd-boot-builder-downgrade

nixos/systemd-boot: fix systemd-boot-builder dowgrade to fail
This commit is contained in:
Martin Weinelt 2022-05-28 13:23:44 +02:00 committed by GitHub
commit c48756aae2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -204,6 +204,20 @@ def get_profiles() -> List[str]:
else: else:
return [] return []
def should_update(v_from: str, v_to: str) -> bool:
# see https://github.com/systemd/systemd/blob/main/src/boot/bootctl.c compare_product function
len_from = len(v_from)
len_to = len(v_to)
if len_from < len_to:
return False
if len_from > len_to:
return True
return v_from < v_to
def main() -> None: def main() -> None:
parser = argparse.ArgumentParser(description='Update NixOS-related systemd-boot files') parser = argparse.ArgumentParser(description='Update NixOS-related systemd-boot files')
@ -244,27 +258,29 @@ def main() -> None:
subprocess.check_call(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@"] + flags + ["install"]) subprocess.check_call(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@"] + flags + ["install"])
else: else:
# Update bootloader to latest if needed # Update bootloader to latest if needed
systemd_version = subprocess.check_output(["@systemd@/bin/bootctl", "--version"], universal_newlines=True).split()[2] available_out = subprocess.check_output(["@systemd@/bin/bootctl", "--version"], universal_newlines=True).split()[2]
sdboot_status = subprocess.check_output(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@", "status"], universal_newlines=True) installed_out = subprocess.check_output(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@", "status"], universal_newlines=True)
# See status_binaries() in systemd bootctl.c for code which generates this # See status_binaries() in systemd bootctl.c for code which generates this
m = re.search("^\W+File:.*/EFI/(BOOT|systemd)/.*\.efi \(systemd-boot ([\d.]+[^)]*)\)$", installed_match = re.search(r"^\W+File:.*/EFI/(?:BOOT|systemd)/.*\.efi \(systemd-boot ([\d.]+[^)]*)\)$",
sdboot_status, re.IGNORECASE | re.MULTILINE) installed_out, re.IGNORECASE | re.MULTILINE)
needs_install = False available_match = re.search(r"^\((.*)\)$", available_out)
if m is None: if installed_match is None:
print("could not find any previously installed systemd-boot, installing.") raise Exception("could not find any previously installed systemd-boot")
# Let systemd-boot attempt an installation if a previous one wasn't found
needs_install = True
else:
sdboot_version = f'({m.group(2)})'
if systemd_version != sdboot_version:
print("updating systemd-boot from %s to %s" % (sdboot_version, systemd_version))
needs_install = True
if needs_install: if available_match is None:
raise Exception("could not determine systemd-boot version")
installed_version = installed_match.group(1)
available_version = available_match.group(1)
if should_update(installed_version, available_version):
print("updating systemd-boot from %s to %s" % (installed_version, available_version))
subprocess.check_call(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@", "update"]) subprocess.check_call(["@systemd@/bin/bootctl", "--path=@efiSysMountPoint@", "update"])
else:
print("leaving systemd-boot %s in place (%s is not newer)" % (installed_version, available_version))
mkdir_p("@efiSysMountPoint@/efi/nixos") mkdir_p("@efiSysMountPoint@/efi/nixos")
mkdir_p("@efiSysMountPoint@/loader/entries") mkdir_p("@efiSysMountPoint@/loader/entries")