-
Notifications
You must be signed in to change notification settings - Fork 249
pkg/architecture: Introduce architecture types and binfmt_misc registration #1782
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
DaliborKr
wants to merge
5
commits into
containers:main
Choose a base branch
from
DaliborKr:pr03-arch-core-binfmt
base: main
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d28cb8c
pkg/shell: Preserve error types in shell command execution
DaliborKr 270f3f2
cmd/create: Extract spinner setup into helper functions
DaliborKr 4afa672
pkg/utils: Add IsSupportedDistroImage for image to supported distro m…
DaliborKr 1baaf5e
pkg/architecture: Define core architecture types and constants
DaliborKr 0911638
pkg/architecture: Add sandboxed binfmt_misc registration support
DaliborKr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Copyright © 2019 – 2026 Red Hat Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package architecture | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/containers/toolbox/pkg/utils" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| type Architecture struct { | ||
| ID int | ||
| NameBinfmt string | ||
| NameOCI string | ||
| Aliases []string | ||
| ELFMagic []byte | ||
| ELFMask []byte | ||
|
|
||
| BinfmtFlags string | ||
| BinfmtName string | ||
| BinfmtMagicType string | ||
| BinfmtOffset string | ||
| } | ||
|
|
||
| type Config struct { | ||
| ID int | ||
| QemuEmulatorPath string | ||
| } | ||
|
|
||
| const ( | ||
| NotSpecified = iota | ||
| Aarch64 | ||
| Ppc64le | ||
| X86_64 | ||
| ) | ||
|
|
||
| var supportedArchitectures = map[int]Architecture{ | ||
| Aarch64: { | ||
| ID: Aarch64, | ||
| NameBinfmt: "aarch64", | ||
| NameOCI: "arm64", | ||
| Aliases: []string{"aarch64", "arm64"}, | ||
| ELFMagic: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xb7, 0x00}, | ||
| ELFMask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff}, | ||
| }, | ||
| Ppc64le: { | ||
| ID: Ppc64le, | ||
| NameBinfmt: "ppc64le", | ||
| NameOCI: "ppc64le", | ||
| Aliases: []string{"ppc64le"}, | ||
| ELFMagic: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x15, 0x00}, | ||
| ELFMask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0x00}, | ||
| }, | ||
| X86_64: { | ||
| ID: X86_64, | ||
| NameBinfmt: "x86_64", | ||
| NameOCI: "amd64", | ||
| Aliases: []string{"x86_64", "amd64"}, | ||
| ELFMagic: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00}, | ||
| ELFMask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff}, | ||
| }, | ||
| } | ||
|
|
||
| var ( | ||
| HostArchID int | ||
| supportedArgArchValues map[string]int | ||
| ) | ||
|
|
||
| func init() { | ||
| supportedArgArchValues = make(map[string]int) | ||
| for archID, arch := range supportedArchitectures { | ||
| for _, alias := range arch.Aliases { | ||
| supportedArgArchValues[alias] = archID | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func GetArchConfigDefault() Config { | ||
| return Config{ | ||
| ID: HostArchID, | ||
| QemuEmulatorPath: "", | ||
| } | ||
| } | ||
|
|
||
| func getArchitecture(archID int) (Architecture, bool) { | ||
| arch, exists := supportedArchitectures[archID] | ||
| return arch, exists | ||
| } | ||
|
|
||
| func getArchNameBinfmt(arch int) string { | ||
| if arch == NotSpecified { | ||
| logrus.Warnf("Getting arch name for not specified architecture") | ||
| return "arch_not_specified" | ||
| } | ||
| if archObj, exists := supportedArchitectures[arch]; exists { | ||
| return archObj.NameBinfmt | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func GetArchNameOCI(arch int) string { | ||
| if arch == NotSpecified { | ||
| logrus.Warnf("Getting arch name for not specified architecture") | ||
| return "arch_not_specified" | ||
| } | ||
| if archObj, exists := supportedArchitectures[arch]; exists { | ||
| return archObj.NameOCI | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func HasContainerNativeArch(archID int) bool { | ||
| return archID == HostArchID | ||
| } | ||
|
DaliborKr marked this conversation as resolved.
|
||
|
|
||
| func ImageReferenceGetArchFromTag(image string) int { | ||
| tag := utils.ImageReferenceGetTag(image) | ||
|
|
||
| if tag == "" { | ||
| return NotSpecified | ||
| } | ||
|
|
||
| i := strings.LastIndexByte(tag, '-') | ||
| if i == -1 { | ||
| return NotSpecified | ||
| } | ||
|
|
||
| archInTag := tag[i+1:] | ||
|
|
||
| for archID, arch := range supportedArchitectures { | ||
| if arch.NameBinfmt == archInTag || arch.NameOCI == archInTag { | ||
| return archID | ||
| } | ||
| } | ||
|
|
||
| return NotSpecified | ||
| } | ||
|
|
||
| func ParseArgArchValue(value string) (int, error) { | ||
| archID, exists := supportedArgArchValues[value] | ||
| if !exists { | ||
| return NotSpecified, fmt.Errorf("architecture '%s' is not supported by Toolbx", value) | ||
| } | ||
|
|
||
| return archID, 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,151 @@ | ||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||
| * Copyright © 2019 – 2026 Red Hat Inc. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| package architecture | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||
| "bytes" | ||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| "github.com/containers/toolbox/pkg/shell" | ||||||||||||||||||||||||||
| "github.com/sirupsen/logrus" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type Registration struct { | ||||||||||||||||||||||||||
| Name string | ||||||||||||||||||||||||||
| MagicType string | ||||||||||||||||||||||||||
| Offset string | ||||||||||||||||||||||||||
| Magic []byte | ||||||||||||||||||||||||||
| Mask []byte | ||||||||||||||||||||||||||
| Interpreter string | ||||||||||||||||||||||||||
| Flags string | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||
| defaultMagicType = "M" | ||||||||||||||||||||||||||
| defaultFlags = "FC" | ||||||||||||||||||||||||||
| defaultOffset = "0" | ||||||||||||||||||||||||||
| binfmtMiscPath = "/proc/sys/fs/binfmt_misc" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (r *Registration) buildRegistrationString() string { | ||||||||||||||||||||||||||
| return fmt.Sprintf(":%s:%s:%s:%s:%s:%s:%s", | ||||||||||||||||||||||||||
| r.Name, r.MagicType, r.Offset, | ||||||||||||||||||||||||||
| bytesToEscapedString(r.Magic), | ||||||||||||||||||||||||||
| bytesToEscapedString(r.Mask), | ||||||||||||||||||||||||||
| r.Interpreter, r.Flags) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (r *Registration) register() error { | ||||||||||||||||||||||||||
| logrus.Debugf("Registering binfmt_misc for %s", r.Name) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| regString := r.buildRegistrationString() | ||||||||||||||||||||||||||
| logrus.Debugf("Registration string: %s", regString) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if err := os.WriteFile(filepath.Join(binfmtMiscPath, "register"), []byte(regString), 0200); err != nil { | ||||||||||||||||||||||||||
| return fmt.Errorf("failed to register binfmt_misc handler: %w", err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func bytesToEscapedString(bytes []byte) string { | ||||||||||||||||||||||||||
| var result strings.Builder | ||||||||||||||||||||||||||
| for _, b := range bytes { | ||||||||||||||||||||||||||
| result.WriteString(fmt.Sprintf("\\x%02x", b)) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return result.String() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func getDefaultRegistration(archID int, interpreterPath string) *Registration { | ||||||||||||||||||||||||||
| arch, exists := getArchitecture(archID) | ||||||||||||||||||||||||||
| if !exists { | ||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var name string | ||||||||||||||||||||||||||
| flags := defaultFlags | ||||||||||||||||||||||||||
| magicType := defaultMagicType | ||||||||||||||||||||||||||
| offset := defaultOffset | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if arch.BinfmtName != "" { | ||||||||||||||||||||||||||
| name = arch.BinfmtName | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| name = "qemu-" + arch.NameBinfmt | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if arch.BinfmtFlags != "" { | ||||||||||||||||||||||||||
| flags = arch.BinfmtFlags | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if arch.BinfmtMagicType != "" { | ||||||||||||||||||||||||||
| magicType = arch.BinfmtMagicType | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if arch.BinfmtOffset != "" { | ||||||||||||||||||||||||||
| offset = arch.BinfmtOffset | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| interpreter := interpreterPath | ||||||||||||||||||||||||||
| if !strings.HasPrefix(interpreterPath, "/run/host/") { | ||||||||||||||||||||||||||
| interpreter = filepath.Join("/run/host", interpreter) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return &Registration{ | ||||||||||||||||||||||||||
| Name: name, | ||||||||||||||||||||||||||
| MagicType: magicType, | ||||||||||||||||||||||||||
| Offset: offset, | ||||||||||||||||||||||||||
| Magic: arch.ELFMagic, | ||||||||||||||||||||||||||
| Mask: arch.ELFMask, | ||||||||||||||||||||||||||
| Interpreter: interpreter, | ||||||||||||||||||||||||||
| Flags: flags, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func MountBinfmtMisc() error { | ||||||||||||||||||||||||||
| args := []string{ | ||||||||||||||||||||||||||
| "binfmt_misc", | ||||||||||||||||||||||||||
| "-t", | ||||||||||||||||||||||||||
| "binfmt_misc", | ||||||||||||||||||||||||||
| binfmtMiscPath, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var stdout bytes.Buffer | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if err := shell.Run("mount", nil, &stdout, nil, args...); err != nil { | ||||||||||||||||||||||||||
| return fmt.Errorf("failed to mount binfmt_misc: %w", err) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| logrus.Debugf("Result of mount command: %s", stdout.String()) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func RegisterBinfmtMisc(archID int, interpreterPath string) error { | ||||||||||||||||||||||||||
| reg := getDefaultRegistration(archID, interpreterPath) | ||||||||||||||||||||||||||
| if reg == nil { | ||||||||||||||||||||||||||
| logrus.Debugf("Unable to register binfmt_misc for architecture '%s'", GetArchNameOCI(archID)) | ||||||||||||||||||||||||||
| return fmt.Errorf("Toolbx does not support architecture '%s'", GetArchNameOCI(archID)) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+139
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if err := reg.register(); err != nil { | ||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
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.
The function
getArchNameBinfmtis unexported and appears to be unused within this package and the rest of the PR. If it's not intended for immediate use, it should be removed to avoid dead code.