Skip to content

Commit

Permalink
Allow specifying user shell at toolbox creation
Browse files Browse the repository at this point in the history
When creating a toolbox the user shell can be specified and this shell will be
used when entering the toolbox.

Signed-off-by: Yann Soubeyrand <[email protected]>
  • Loading branch information
yann-soubeyrand committed Jan 7, 2021
1 parent f850f1e commit 9b520a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
26 changes: 22 additions & 4 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
container string
image string
release string
shell string
}

createToolboxShMounts = []struct {
Expand Down Expand Up @@ -83,6 +84,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)
}
Expand Down Expand Up @@ -143,14 +150,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")
}
Expand Down Expand Up @@ -345,9 +361,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{
Expand Down
18 changes: 3 additions & 15 deletions src/cmd/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
42 changes: 17 additions & 25 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -258,18 +253,15 @@ func runCommand(container string,

logrus.Debugf("Container %s is initialized", container)

if _, err := isCommandPresent(container, command[0]); err != nil {
if fallbackToBash {
fmt.Fprintf(os.Stderr,
"Error: command %s not found in container %s\n",
command[0],
container)
fmt.Fprintf(os.Stderr, "Using /bin/bash instead.\n")
commandLauncher := "export SHELL=\"$(getent passwd \"$(whoami)\" | cut -d : -f 7)\"; "

command = []string{"/bin/bash", "-l"}
} 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\""
}

logrus.Debug("Checking if 'podman exec' supports disabling the detach keys")
Expand Down Expand Up @@ -302,10 +294,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;%s\033\\", container, currentUser.Uid)
Expand Down Expand Up @@ -342,11 +338,7 @@ func runCommand(container string,
err = nil
}

if err != nil {
return err
}

return nil
return err
}

func runHelp(cmd *cobra.Command, args []string) {
Expand Down
1 change: 0 additions & 1 deletion src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ var (
"DESKTOP_SESSION",
"DISPLAY",
"LANG",
"SHELL",
"SSH_AUTH_SOCK",
"TERM",
"TOOLBOX_PATH",
Expand Down

0 comments on commit 9b520a0

Please sign in to comment.