diff --git a/src/cmd/create.go b/src/cmd/create.go index 80e4f8c36..f3821bfd0 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -44,6 +44,7 @@ var ( container string image string release string + shell string } createToolboxShMounts = []struct { @@ -82,6 +83,12 @@ func init() { "", "Create a toolbox container for a different operating system release than the host.") + flags.StringVarP(&createFlags.shell, + "shell", + "s", + "", + "Specify the shell to use in the toolbox.") + createCmd.SetHelpFunc(createHelp) rootCmd.AddCommand(createCmd) } @@ -139,14 +146,23 @@ func create(cmd *cobra.Command, args []string) error { return err } - if err := createContainer(container, image, release, true); err != nil { + var userShell string + if createFlags.shell != "" { + userShell = createFlags.shell + if !filepath.IsAbs(userShell) { + return fmt.Errorf("Invalid shell '%s': must be an absolute path", userShell) + } + userShell = filepath.Clean(userShell) + } + + if err := createContainer(container, image, release, userShell, true); err != nil { return err } return nil } -func createContainer(container, image, release string, showCommandToEnter bool) error { +func createContainer(container, image, release string, userShell string, showCommandToEnter bool) error { if container == "" { panic("container not specified") } @@ -311,9 +327,11 @@ func createContainer(container, image, release string, showCommandToEnter bool) logLevelString := podman.LogLevel.String() - userShell := os.Getenv("SHELL") if userShell == "" { - return errors.New("failed to get the current user's default shell") + userShell = os.Getenv("SHELL") + if userShell == "" { + return errors.New("failed to get the current user's default shell") + } } entryPoint := []string{ diff --git a/src/cmd/enter.go b/src/cmd/enter.go index cf8d4fb19..14f78c752 100644 --- a/src/cmd/enter.go +++ b/src/cmd/enter.go @@ -114,13 +114,6 @@ func enter(cmd *cobra.Command, args []string) error { return err } - userShell := os.Getenv("SHELL") - if userShell == "" { - return errors.New("failed to get the current user's default shell") - } - - command := []string{userShell, "-l"} - hostID, err := utils.GetHostID() if err != nil { return errors.New("failed to get the host ID") @@ -137,18 +130,13 @@ func enter(cmd *cobra.Command, args []string) error { emitEscapeSequence = true } - if err := runCommand(container, + return runCommand(container, !nonDefaultContainer, image, release, - command, + nil, emitEscapeSequence, - true, - false); err != nil { - return err - } - - return nil + false) } func enterHelp(cmd *cobra.Command, args []string) { diff --git a/src/cmd/run.go b/src/cmd/run.go index 250275c71..fb5077c6f 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -120,25 +120,20 @@ func run(cmd *cobra.Command, args []string) error { return err } - if err := runCommand(container, + return runCommand(container, !nonDefaultContainer, image, release, command, false, - false, - true); err != nil { - return err - } - - return nil + true) } func runCommand(container string, defaultContainer bool, image, release string, command []string, - emitEscapeSequence, fallbackToBash, pedantic bool) error { + emitEscapeSequence, pedantic bool) error { if !pedantic { if image == "" { panic("image not specified") @@ -188,7 +183,7 @@ func runCommand(container string, return nil } - if err := createContainer(container, image, release, false); err != nil { + if err := createContainer(container, image, release, "", false); err != nil { return err } } else if containersCount == 1 && defaultContainer { @@ -255,16 +250,15 @@ func runCommand(container string, logrus.Debugf("Container %s is initialized", container) - if _, err := isCommandPresent(container, command[0]); err != nil { - if fallbackToBash { - logrus.Debugf("command %s not found in container %s; using /bin/bash instead", - command[0], - container) + commandLauncher := "export SHELL=\"$(getent passwd \"$(whoami)\" | cut -d : -f 7)\"; " - command = []string{"/bin/bash"} - } else { + if command != nil { + if _, err := isCommandPresent(container, command[0]); err != nil { return fmt.Errorf("command %s not found in container %s", command[0], container) } + commandLauncher += "exec \"$@\"" + } else { + commandLauncher += "exec -l \"$SHELL\"" } envOptions := utils.GetEnvOptionsForPreservedVariables() @@ -283,10 +277,14 @@ func runCommand(container string, execArgs = append(execArgs, []string{ container, - "capsh", "--caps=", "--", "-c", "exec \"$@\"", "/bin/sh", + "capsh", "--caps=", "--", "-c", commandLauncher, "toolbox", }...) - execArgs = append(execArgs, command...) + if command != nil { + execArgs = append(execArgs, command...) + } else { + command = []string{"\"$SHELL\""} + } if emitEscapeSequence { fmt.Printf("\033]777;container;push;%s;toolbox\033\\", container) @@ -319,11 +317,7 @@ func runCommand(container string, err = nil } - if err != nil { - return err - } - - return nil + return err } func runHelp(cmd *cobra.Command, args []string) { diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index 08de29978..eba56f5b3 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -57,7 +57,6 @@ var ( "DESKTOP_SESSION", "DISPLAY", "LANG", - "SHELL", "SSH_AUTH_SOCK", "TERM", "TOOLBOX_PATH",