Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,8 @@ func createContainer(container, image, release, authFile string, showCommandToEn
logrus.Debugf("%s", arg)
}

s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s.Prefix = fmt.Sprintf("Creating container %s: ", container)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Creating container %s: ", container))
defer stopSpinner(s)

if err := shell.Run("podman", nil, nil, nil, createArgs...); err != nil {
return fmt.Errorf("failed to create container %s", container)
Expand Down Expand Up @@ -735,12 +731,8 @@ func pullImage(image, release, authFile string) (bool, error) {

logrus.Debugf("Pulling image %s", imageFull)

if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Pulling %s: ", imageFull))
defer stopSpinner(s)

if err := podman.Pull(imageFull, authFile); err != nil {
var builder strings.Builder
Expand Down Expand Up @@ -963,6 +955,22 @@ func showPromptForDownload(imageFull string) bool {
return shouldPullImage
}

func startSpinner(message string) *spinner.Spinner {
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = message
s.Start()
return s
}
return nil
}

func stopSpinner(s *spinner.Spinner) {
if s != nil {
s.Stop()
}
}

// systemdNeedsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped
func systemdNeedsEscape(i int, b byte) bool {
// Escape everything that is not a-z-A-Z-0-9
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"syscall"

"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/nvidia"
"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/utils"
Expand Down Expand Up @@ -375,6 +377,13 @@ func setUpGlobals() error {
return fmt.Errorf("failed to get the working directory: %w", err)
}

architecture.HostArchID, _ = architecture.ParseArgArchValue(runtime.GOARCH)
if architecture.HostArchID != architecture.NotSpecified {
logrus.Debugf("Host architecture: %s", architecture.GetArchNameOCI(architecture.HostArchID))
} else {
logrus.Debugf("Host architecture: %s", runtime.GOARCH)
}

return nil
}

Expand Down
162 changes: 162 additions & 0 deletions src/pkg/architecture/architecture.go
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 ""
}
Comment on lines +106 to +115
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function getArchNameBinfmt is 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.


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
}
Comment thread
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
}
151 changes: 151 additions & 0 deletions src/pkg/architecture/binfmt_misc.go
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If archID is NotSpecified, GetArchNameOCI will log a warning and return an empty string, resulting in a confusing error message: Toolbx does not support architecture ''. You should check for NotSpecified explicitly before attempting registration.

Suggested change
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))
}
func RegisterBinfmtMisc(archID int, interpreterPath string) error {
if archID == NotSpecified {
return fmt.Errorf("cannot register binfmt_misc: architecture not specified")
}
reg := getDefaultRegistration(archID, interpreterPath)


if err := reg.register(); err != nil {
return err
}

return nil
}
Loading
Loading