-
Notifications
You must be signed in to change notification settings - Fork 8
ekaos/ab-boot: init A/B boot scheme #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonringer
wants to merge
1
commit into
master
Choose a base branch
from
jonringer/ab-boot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # A/B boot scheme via systemd-boot boot counting | ||
| # | ||
| # Adds boot counting to BLS entries so that a failed boot automatically | ||
| # rolls back to the previous generation. The newest entry gets a +N-0 | ||
| # suffix; the bless-boot service removes it after the health check passes. | ||
| # If N boots fail, systemd-boot falls back to the previous proven entry. | ||
| # | ||
| # No special partition layout, no slot abstraction. The existing | ||
| # generation system and /run/booted-system vs /run/current-system | ||
| # already provide the A/B semantics. | ||
| { | ||
| config, | ||
| lib, | ||
| ... | ||
| }: | ||
|
|
||
| with lib; | ||
|
|
||
| { | ||
| options = { | ||
| boot.ab = { | ||
| enable = mkOption { | ||
| type = types.bool; | ||
| default = false; | ||
| description = '' | ||
| Enable A/B boot with automatic rollback. | ||
|
|
||
| Adds boot counting to the newest BLS entry on the ESP. | ||
| If the health check fails after the configured number of | ||
| boot attempts, systemd-boot automatically falls back to | ||
| the previous proven generation. | ||
| ''; | ||
| }; | ||
|
|
||
| bootCountTriesLeft = mkOption { | ||
| type = types.int; | ||
| default = 3; | ||
| description = '' | ||
| Number of boot attempts before systemd-boot considers | ||
| a generation failed and falls back to the previous one. | ||
| ''; | ||
| }; | ||
|
|
||
| healthCheck = { | ||
| command = mkOption { | ||
| type = types.str; | ||
| default = "systemctl is-system-running --wait"; | ||
| description = '' | ||
| Command that must succeed for the boot to be blessed. | ||
| ''; | ||
| }; | ||
|
|
||
| timeout = mkOption { | ||
| type = types.int; | ||
| default = 120; | ||
| description = '' | ||
| Maximum seconds to wait for the health check. | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = mkIf config.boot.ab.enable { | ||
| assertions = [ | ||
| { | ||
| assertion = config.boot.loader.systemd-boot.enable; | ||
| message = "boot.ab requires boot.loader.systemd-boot.enable = true"; | ||
| } | ||
| ]; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # Bless-boot service for A/B boot | ||
| # Runs a health check after boot and removes the boot counting suffix | ||
| # from the current BLS entry, marking the boot as successful. | ||
| { | ||
| config, | ||
| lib, | ||
| pkgs, | ||
| ... | ||
| }: | ||
|
|
||
| with lib; | ||
|
|
||
| let | ||
| cfg = config.boot.ab; | ||
| espMount = config.boot.loader.efi.efiSysMountPoint; | ||
|
|
||
| blessBootScript = pkgs.writeScript "ekaos-bless-boot" '' | ||
| #!${pkgs.runtimeShell} | ||
| set -e | ||
|
|
||
| # Find the BLS entry that booted us by matching the init path. | ||
| # /run/booted-system/init is the init that systemd-boot loaded, | ||
| # and the BLS entry's "options" line contains init=<that path>. | ||
| BOOTED_INIT=$(readlink -f /run/booted-system/init 2>/dev/null || true) | ||
| if [ -z "$BOOTED_INIT" ]; then | ||
| echo "ekaos-bless-boot: cannot determine booted system, skipping" | ||
| exit 0 | ||
| fi | ||
|
|
||
| ENTRY_DIR="${espMount}/loader/entries" | ||
|
|
||
| # Check if any entry for our booted system has a boot counting suffix. | ||
| # If not, there's nothing to bless (already blessed or boot counting not active). | ||
| FOUND="" | ||
| for f in "$ENTRY_DIR"/*+[0-9]*-[0-9]*.conf; do | ||
| [ -f "$f" ] || continue | ||
| if ${pkgs.gnugrep}/bin/grep -q "init=$BOOTED_INIT" "$f"; then | ||
| FOUND="$f" | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if [ -z "$FOUND" ]; then | ||
| echo "ekaos-bless-boot: no unblessed entry for current boot, nothing to do" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "Running boot health check..." | ||
|
|
||
| # Run the health check. Default is "systemctl is-system-running --wait" | ||
| # which returns 0 for "running" and non-zero for "degraded"/"starting"/etc. | ||
| if ${pkgs.coreutils}/bin/timeout ${toString cfg.healthCheck.timeout} ${cfg.healthCheck.command}; then | ||
| echo "Health check passed." | ||
| else | ||
| echo "Health check FAILED. Boot will NOT be blessed." | ||
| echo "On next reboot, systemd-boot will decrement the try counter." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Bless: rename the entry to remove the +N-M suffix | ||
| BLESSED=$(echo "$FOUND" | ${pkgs.gnused}/bin/sed 's/+[0-9]*-[0-9]*//') | ||
| mv "$FOUND" "$BLESSED" | ||
| echo "Blessed: $(basename "$FOUND") -> $(basename "$BLESSED")" | ||
| ''; | ||
|
|
||
| in | ||
|
|
||
| { | ||
| options = { | ||
| services.ekaos-bless-boot = { | ||
| enable = mkOption { | ||
| type = types.bool; | ||
| default = false; | ||
| description = "Whether to enable the bless-boot service."; | ||
| }; | ||
|
|
||
| description = mkOption { | ||
| type = types.str; | ||
| default = "Bless boot after health check"; | ||
| description = "Service description."; | ||
| }; | ||
|
|
||
| command = mkOption { | ||
| type = types.str; | ||
| internal = true; | ||
| description = "Command to run (set automatically)."; | ||
| }; | ||
|
|
||
| args = mkOption { | ||
| type = types.listOf types.str; | ||
| internal = true; | ||
| default = [ ]; | ||
| description = "Command arguments."; | ||
| }; | ||
|
|
||
| user = mkOption { | ||
| type = types.str; | ||
| default = "root"; | ||
| description = "User to run service as."; | ||
| }; | ||
|
|
||
| restartPolicy = mkOption { | ||
| type = types.str; | ||
| default = "never"; | ||
| description = "Restart policy."; | ||
| }; | ||
|
|
||
| systemd = mkOption { | ||
| type = types.attrsOf types.anything; | ||
| default = { }; | ||
| description = "Systemd-specific options."; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| config = mkIf cfg.enable { | ||
| services.ekaos-bless-boot = { | ||
| enable = true; | ||
| command = "${blessBootScript}"; | ||
| systemd = { | ||
| wantedBy = [ "multi-user.target" ]; | ||
| after = [ "multi-user.target" ]; | ||
| serviceConfig = { | ||
| Type = "oneshot"; | ||
| RemainAfterExit = true; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a stop gap for when boot.spec was introduced, since we don't have any computers installing ekaos on baremetal as of yet, we don't need it.