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 May 15, 2020
1 parent dcaf06a commit 21d82db
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
26 changes: 22 additions & 4 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
container string
image string
release string
shell string
}

createToolboxShMounts = []struct {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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{
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
40 changes: 17 additions & 23 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 @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
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 @@ -57,7 +57,6 @@ var (
"DESKTOP_SESSION",
"DISPLAY",
"LANG",
"SHELL",
"SSH_AUTH_SOCK",
"TERM",
"TOOLBOX_PATH",
Expand Down

0 comments on commit 21d82db

Please sign in to comment.