Skip to content
Closed
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
30 changes: 15 additions & 15 deletions src/cmd/initContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func initContainer(cmd *cobra.Command, args []string) error {

toolboxEnvFile, err := os.Create("/run/.toolboxenv")
if err != nil {
return errors.New("failed to create /run/.toolboxenv")
return fmt.Errorf("failed to create /run/.toolboxenv: %w", err)
}

defer toolboxEnvFile.Close()
Expand Down Expand Up @@ -241,7 +241,7 @@ func initContainer(cmd *cobra.Command, args []string) error {

sudoGroup, err := utils.GetGroupForSudo()
if err != nil {
return fmt.Errorf("failed to add user %s: %s", initContainerFlags.user, err)
return fmt.Errorf("failed to add user %s: %w", initContainerFlags.user, err)
}

logrus.Debugf("Adding user %s with UID %d:", initContainerFlags.user, initContainerFlags.uid)
Expand Down Expand Up @@ -269,13 +269,13 @@ func initContainer(cmd *cobra.Command, args []string) error {
logrus.Debugf("Removing password for user %s", initContainerFlags.user)

if err := shell.Run("passwd", nil, nil, nil, "--delete", initContainerFlags.user); err != nil {
return fmt.Errorf("failed to remove password for user %s", initContainerFlags.user)
return fmt.Errorf("failed to remove password for user %s: %w", initContainerFlags.user, err)
}

logrus.Debug("Removing password for user root")

if err := shell.Run("passwd", nil, nil, nil, "--delete", "root"); err != nil {
return errors.New("failed to remove password for root")
return fmt.Errorf("failed to remove password for root: %w", err)
}
}

Expand All @@ -295,7 +295,7 @@ func initContainer(cmd *cobra.Command, args []string) error {
if err := ioutil.WriteFile("/etc/krb5.conf.d/kcm_default_ccache",
kcmConfigBytes,
0644); err != nil {
return errors.New("failed to set KCM as the defult Kerberos credential cache")
return fmt.Errorf("failed to set KCM as the defult Kerberos credential cache: %w", err)
}
}

Expand All @@ -305,12 +305,12 @@ func initContainer(cmd *cobra.Command, args []string) error {
logrus.Debugf("Creating runtime directory %s", toolboxRuntimeDirectory)

if err := os.MkdirAll(toolboxRuntimeDirectory, 0700); err != nil {
return fmt.Errorf("failed to create runtime directory %s", toolboxRuntimeDirectory)
return fmt.Errorf("failed to create runtime directory %s: %w", toolboxRuntimeDirectory, err)
}

if err := os.Chown(toolboxRuntimeDirectory, initContainerFlags.uid, initContainerFlags.uid); err != nil {
return fmt.Errorf("failed to change ownership of the runtime directory %s",
toolboxRuntimeDirectory)
return fmt.Errorf("failed to change ownership of the runtime directory %s: %w",
toolboxRuntimeDirectory, err)
}

pid := os.Getpid()
Expand All @@ -320,13 +320,13 @@ func initContainer(cmd *cobra.Command, args []string) error {

initializedStampFile, err := os.Create(initializedStamp)
if err != nil {
return errors.New("failed to create initialization stamp")
return fmt.Errorf("failed to create initialization stamp: %w", err)
}

defer initializedStampFile.Close()

if err := initializedStampFile.Chown(initContainerFlags.uid, initContainerFlags.uid); err != nil {
return errors.New("failed to change ownership of initialization stamp")
return fmt.Errorf("failed to change ownership of initialization stamp: %w", err)
}

logrus.Debug("Going to sleep")
Expand All @@ -337,14 +337,14 @@ func initContainer(cmd *cobra.Command, args []string) error {
return errors.New("sleep(1) not found")
}

return errors.New("failed to lookup sleep(1)")
return fmt.Errorf("failed to lookup sleep(1): %w", err)
}

sleepArgs := []string{"sleep", "+Inf"}
env := os.Environ()

if err := syscall.Exec(sleepBinary, sleepArgs, env); err != nil {
return errors.New("failed to invoke sleep(1)")
return fmt.Errorf("failed to invoke sleep(1): %w", err)
}

return nil
Expand Down Expand Up @@ -378,13 +378,13 @@ func mountBind(containerPath, source, flags string) error {
return nil
}

return fmt.Errorf("failed to stat %s", source)
return fmt.Errorf("failed to stat %s: %w", source, err)
}

if fi.IsDir() {
logrus.Debugf("Creating %s", containerPath)
if err := os.MkdirAll(containerPath, 0755); err != nil {
return fmt.Errorf("failed to create %s", containerPath)
return fmt.Errorf("failed to create %s: %w", containerPath, err)
}
}

Expand All @@ -401,7 +401,7 @@ func mountBind(containerPath, source, flags string) error {
args = append(args, []string{source, containerPath}...)

if err := shell.Run("mount", nil, nil, nil, args...); err != nil {
return fmt.Errorf("failed to bind %s to %s", containerPath, source)
return fmt.Errorf("failed to bind %s to %s: %w", containerPath, source, err)
}

return nil
Expand Down
15 changes: 14 additions & 1 deletion src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,21 @@ func rm(cmd *cobra.Command, args []string) error {
}

for _, container := range args {
if exists, err := podman.ContainerExists(container); !exists {
if errors.Is(err, podman.ErrContainerNotExist) {
fmt.Fprintf(os.Stderr, "Error: container %s does not exist\n", container)
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
continue
}

if _, err := podman.IsToolboxContainer(container); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if errors.Is(err, podman.ErrContainerNotToolbox) {
fmt.Fprintf(os.Stderr, "Error: container %s is not a toolbox container\n", container)
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
continue
}

Expand Down
15 changes: 14 additions & 1 deletion src/cmd/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ func rmi(cmd *cobra.Command, args []string) error {
}

for _, image := range args {
if exists, err := podman.ImageExists(image); !exists {
if errors.Is(err, podman.ErrImageNotExist) {
fmt.Fprintf(os.Stderr, "Error: image %s does not exist\n", image)
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
continue
}

if _, err := podman.IsToolboxImage(image); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if errors.Is(err, podman.ErrImageNotToolbox) {
fmt.Fprintf(os.Stderr, "Error: image %s is not a toolbox image\n", image)
} else {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
continue
}

Expand Down
30 changes: 15 additions & 15 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,20 @@ func migrate() error {

podmanVersion, err := podman.GetVersion()
if err != nil {
return fmt.Errorf("failed to get the Podman version")
return fmt.Errorf("failed to get the Podman version: %w", err)
}

logrus.Debugf("Current Podman version is %s", podmanVersion)

err = os.MkdirAll(toolboxConfigDir, 0775)
if err != nil {
return fmt.Errorf("failed to create configuration directory")
return fmt.Errorf("failed to create configuration directory: %w", err)
}

runtimeDirectory := os.Getenv("XDG_RUNTIME_DIR")
toolboxRuntimeDirectory := runtimeDirectory + "/toolbox"
if err := os.MkdirAll(toolboxRuntimeDirectory, 0700); err != nil {
return fmt.Errorf("failed to create runtime directory %s", toolboxRuntimeDirectory)
return fmt.Errorf("failed to create runtime directory %s: %w", toolboxRuntimeDirectory, err)
}

lockFile := toolboxRuntimeDirectory + "/migrate.lock"
Expand All @@ -238,20 +238,20 @@ func migrate() error {
syscall.O_CREAT|syscall.O_WRONLY,
syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IRGRP|syscall.S_IWGRP|syscall.S_IROTH)
if err != nil {
return fmt.Errorf("failed to open migration lock file")
return fmt.Errorf("failed to open migration lock file: %w", err)
}

defer syscall.Close(lockFD)

err = syscall.Flock(lockFD, syscall.LOCK_EX)
if err != nil {
return fmt.Errorf("failed to acquire migration lock")
return fmt.Errorf("failed to acquire migration lock: %w", err)
}

stampBytes, err := ioutil.ReadFile(stampPath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read migration stamp file")
return fmt.Errorf("failed to read migration stamp file: %w", err)
}
} else {
stampString := string(stampBytes)
Expand All @@ -274,7 +274,7 @@ func migrate() error {
}

if err = podman.SystemMigrate(""); err != nil {
return fmt.Errorf("failed to migrate containers")
return fmt.Errorf("failed to migrate containers: %w", err)
}

logrus.Debugf("Migration to Podman version %s was ok", podmanVersion)
Expand All @@ -283,7 +283,7 @@ func migrate() error {
podmanVersionBytes := []byte(podmanVersion + "\n")
err = ioutil.WriteFile(stampPath, podmanVersionBytes, 0664)
if err != nil {
return fmt.Errorf("failed to update Podman version in migration stamp file")
return fmt.Errorf("failed to update Podman version in migration stamp file: %w", err)
}

return nil
Expand All @@ -305,30 +305,30 @@ func setUpGlobals() error {
if !utils.IsInsideContainer() {
cgroupsVersion, err = utils.GetCgroupsVersion()
if err != nil {
return errors.New("failed to get the cgroups version")
return fmt.Errorf("failed to get the cgroups version: %w", err)
}
}

currentUser, err = user.Current()
if err != nil {
return errors.New("failed to get the current user")
return fmt.Errorf("failed to get the current user: %w", err)
}

executable, err = os.Executable()
if err != nil {
return errors.New("failed to get the path to the executable")
return fmt.Errorf("failed to get the path to the executable: %w", err)
}

executable, err = filepath.EvalSymlinks(executable)
if err != nil {
return errors.New("failed to resolve absolute path to the executable")
return fmt.Errorf("failed to resolve absolute path to the executable: %w", err)
}

executableBase = filepath.Base(executable)

workingDirectory, err = os.Getwd()
if err != nil {
return errors.New("failed to get the working directory")
return fmt.Errorf("failed to get the working directory: %w", err)
}

return nil
Expand All @@ -346,7 +346,7 @@ func setUpLoggers() error {

logLevel, err := logrus.ParseLevel(rootFlags.logLevel)
if err != nil {
return errors.New("failed to parse log-level")
return fmt.Errorf("failed to parse log-level: %w", err)
}

logrus.SetLevel(logLevel)
Expand All @@ -365,7 +365,7 @@ func setUpLoggers() error {
func validateSubIDFile(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, fmt.Errorf("failed to open %s", path)
return false, fmt.Errorf("failed to open %s: %w", path, err)
}

scanner := bufio.NewScanner(file)
Expand Down
Loading