-
Notifications
You must be signed in to change notification settings - Fork 9
Make boot-file architecture config distro-aware #788
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
Merged
vinceaperri
merged 7 commits into
main
from
user/vinceaperri/per-distro-boot-arch-config
Jun 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc21ccb
new changes
vinceaperri 687b630
Pass distroHandler to createPXEArtifacts
vinceaperri 3ba5cd3
Match initrd os-release candidates as relative cpio paths
vinceaperri 0652d30
Assert output artifact shim and bootloader ESP paths per distro
vinceaperri 095970f
address review comments
vinceaperri b4401cc
inline
vinceaperri 8030413
no noprefix
vinceaperri 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package initrdutils | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "os" | ||
|
|
||
| "github.com/cavaliergopher/cpio" | ||
| "github.com/klauspost/compress/zstd" | ||
| "github.com/klauspost/pgzip" | ||
| ) | ||
|
|
||
| // Magic byte signatures for the compression formats we recognize at the start | ||
| // of an initramfs stream. | ||
| // | ||
| // - gzip: RFC 1952 section 2.3.1 ("ID1 ID2" = 1F 8B) | ||
| // https://www.rfc-editor.org/rfc/rfc1952#section-2.3.1 | ||
| // - zstd: RFC 8478 section 3.1.1 ("Magic_Number" = 0xFD2FB528 little-endian) | ||
| // https://www.rfc-editor.org/rfc/rfc8478#section-3.1.1 | ||
| var ( | ||
| magicGzip = []byte{0x1f, 0x8b} | ||
| magicZstd = []byte{0x28, 0xb5, 0x2f, 0xfd} | ||
| longestMagicSize = len(magicZstd) | ||
| ) | ||
|
|
||
| // ReadFirstFileFromInitrd scans an initramfs cpio archive once and returns the content of the first candidate path in | ||
| // the provided list that exists as a regular file. Returns an error wrapping fs.ErrNotExist if none of the candidates | ||
| // is present. | ||
| func ReadFirstFileFromInitrd(initrdPath string, candidates []string) (content []byte, foundPath string, err error) { | ||
| f, err := os.Open(initrdPath) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to open initrd (%s):\n%w", initrdPath, err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| decompressed, err := openInitrdDecompressor(f) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to decompress initrd (%s):\n%w", initrdPath, err) | ||
| } | ||
| defer decompressed.Close() | ||
|
|
||
| wanted := make(map[string]struct{}, len(candidates)) | ||
| for _, c := range candidates { | ||
| wanted[c] = struct{}{} | ||
| } | ||
|
|
||
| found := make(map[string][]byte, len(candidates)) | ||
| cpioReader := cpio.NewReader(decompressed) | ||
| for { | ||
| hdr, err := cpioReader.Next() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to read cpio header from initrd (%s):\n%w", initrdPath, err) | ||
| } | ||
|
|
||
| if _, ok := wanted[hdr.Name]; !ok { | ||
| continue | ||
| } | ||
|
|
||
| // Only read regular files. | ||
| if hdr.Mode&cpio.ModeType != cpio.TypeReg { | ||
| continue | ||
| } | ||
|
|
||
| data, err := io.ReadAll(cpioReader) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("failed to read (%s) from initrd (%s):\n%w", hdr.Name, initrdPath, err) | ||
| } | ||
|
|
||
| found[hdr.Name] = data | ||
| } | ||
|
|
||
| for _, candidate := range candidates { | ||
| if data, ok := found[candidate]; ok { | ||
| return data, candidate, nil | ||
| } | ||
| } | ||
|
|
||
| return nil, "", fmt.Errorf("failed to find any of %v in initrd (%s): %w", candidates, initrdPath, fs.ErrNotExist) | ||
| } | ||
|
|
||
| // openInitrdDecompressor auto-detects the compression format of an initramfs stream from its leading magic bytes and | ||
| // returns a reader over the decompressed (cpio) content. | ||
| func openInitrdDecompressor(r io.Reader) (io.ReadCloser, error) { | ||
| br := bufio.NewReader(r) | ||
| head, err := br.Peek(longestMagicSize) | ||
| if err != nil && err != io.EOF { | ||
| return nil, fmt.Errorf("failed to peek initrd magic bytes:\n%w", err) | ||
| } | ||
|
|
||
| switch { | ||
| case bytes.HasPrefix(head, magicGzip): | ||
| gz, err := pgzip.NewReader(br) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open gzip reader for initrd:\n%w", err) | ||
| } | ||
| return gz, nil | ||
|
|
||
| case bytes.HasPrefix(head, magicZstd): | ||
| zr, err := zstd.NewReader(br) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open zstd reader for initrd:\n%w", err) | ||
| } | ||
|
|
||
| // zstd.Decoder satisfies io.Reader but not io.Closer since its Close() returns no error, so wrap it to | ||
| // implement io.ReadCloser, like pgzip.Reader. | ||
| return zstdReadCloser{Decoder: zr}, nil | ||
|
|
||
| default: | ||
| return nil, fmt.Errorf("unrecognized initrd compression format (leading bytes: % x)", head) | ||
| } | ||
| } | ||
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,16 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package initrdutils | ||
|
|
||
| import ( | ||
| "github.com/klauspost/compress/zstd" | ||
| ) | ||
|
|
||
| // zstdReadCloser adapts *zstd.Decoder (whose Close() returns no error) to io.ReadCloser. | ||
| type zstdReadCloser struct{ *zstd.Decoder } | ||
|
|
||
| func (z zstdReadCloser) Close() error { | ||
| z.Decoder.Close() | ||
| return nil | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.