Skip to content

Commit cf6db37

Browse files
committed
cmd/create, cmd/initContainer: Mount the devpts file system at runtime
Anything that's specified during 'podman create ...' gets statically baked into the container's configuration, and is either difficult or impossible to change afterwards. This means that Toolbx containers created with older versions of Toolbx keep diverging from those created with newer versions. Hence, making it complicated to keep older containers working with newer Toolbx. Mounting the devpts file system at runtime as part of the Toolbx container's entry point will make it possible to update the attributes of the mount, if necessary, for both existing and newly created containers. For what it's worth, this does alter the mount options by removing 'context'. With 'podman create --mount type=devpts,destination=/dev/pts' it was: $ mount | grep ... devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime, context="system_u:object_r:container_file_t:s0:c1022,c1023", gid=100005,mode=620,ptmxmode=666) Now with 'mount -t devpts -o noexec,nosuid,gid=5,mode=620,ptmxmode=666' it is: $ mount | grep devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel, gid=100005,mode=620,ptmxmode=666) #1016
1 parent 2129e28 commit cf6db37

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

doc/toolbox-init-container.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ toolbox\-init\-container - Initialize a running container
99
*--home-link*
1010
*--media-link*
1111
*--mnt-link*
12+
*--mount-devpts*
1213
*--shell SHELL*
1314
*--uid UID*
1415
*--user USER*
@@ -82,6 +83,10 @@ synchronized with their counterparts on the host, and various subsets of the
8283
host's file system hierarchy are always bind mounted to their corresponding
8384
locations inside the toolbox container.
8485

86+
**--mount-devpts**
87+
88+
Mount a `devpts` file system at `/dev/pts`.
89+
8590
**--shell** SHELL
8691

8792
Create a user inside the toolbox container whose login shell is SHELL. This

src/cmd/create.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,6 @@ func createContainer(container, image, release, authFile string, showCommandToEn
245245

246246
runtimeDirectoryMountArg := runtimeDirectory + ":" + runtimeDirectory
247247

248-
logrus.Debug("Checking if 'podman create' supports '--mount type=devpts'")
249-
250-
var devPtsMount []string
251-
252-
if podman.CheckVersion("2.1.0") {
253-
logrus.Debug("'podman create' supports '--mount type=devpts'")
254-
devPtsMount = []string{"--mount", "type=devpts,destination=/dev/pts"}
255-
}
256-
257248
var usernsArg string
258249
if currentUser.Uid == "0" {
259250
usernsArg = "host"
@@ -390,6 +381,10 @@ func createContainer(container, image, release, authFile string, showCommandToEn
390381
entryPoint = append(entryPoint, mediaLink...)
391382
entryPoint = append(entryPoint, mntLink...)
392383

384+
entryPoint = append(entryPoint, []string{
385+
"--mount-devpts",
386+
}...)
387+
393388
createArgs := []string{
394389
"--log-level", logLevelString,
395390
"create",
@@ -404,11 +399,6 @@ func createContainer(container, image, release, authFile string, showCommandToEn
404399
"--hostname", "toolbox",
405400
"--ipc", "host",
406401
"--label", "com.github.containers.toolbox=true",
407-
}...)
408-
409-
createArgs = append(createArgs, devPtsMount...)
410-
411-
createArgs = append(createArgs, []string{
412402
"--name", container,
413403
"--network", "host",
414404
"--no-hosts",

src/cmd/initContainer.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
mediaLink bool
4343
mntLink bool
4444
monitorHost bool
45+
mountDevPts bool
4546
shell string
4647
uid int
4748
user string
@@ -114,6 +115,11 @@ func init() {
114115
panic(panicMsg)
115116
}
116117

118+
flags.BoolVar(&initContainerFlags.mountDevPts,
119+
"mount-devpts",
120+
false,
121+
"Mount a devpts file system at /dev/pts")
122+
117123
flags.StringVar(&initContainerFlags.shell,
118124
"shell",
119125
"",
@@ -256,6 +262,12 @@ func initContainer(cmd *cobra.Command, args []string) error {
256262
}
257263
}
258264

265+
if initContainerFlags.mountDevPts {
266+
if err := mountDevPts(); err != nil {
267+
return err
268+
}
269+
}
270+
259271
if utils.PathExists("/etc/krb5.conf.d") && !utils.PathExists("/etc/krb5.conf.d/kcm_default_ccache") {
260272
logrus.Debug("Setting KCM as the default Kerberos credential cache")
261273

@@ -522,6 +534,48 @@ func mountBind(containerPath, source, flags string) error {
522534
return nil
523535
}
524536

537+
func mountDevPts() error {
538+
optionsArgs := []string{
539+
"noexec",
540+
"nosuid",
541+
}
542+
543+
const ttyGroup = "tty"
544+
logrus.Debugf("Looking up group %s", ttyGroup)
545+
546+
if _, err := user.LookupGroup(ttyGroup); err != nil {
547+
logrus.Debugf("Looking up group %s failed: %s", ttyGroup, err)
548+
} else {
549+
const optionsGIDArg = "gid=" + ttyGroup
550+
optionsArgs = append(optionsArgs, []string{
551+
optionsGIDArg,
552+
}...)
553+
}
554+
555+
optionsArgs = append(optionsArgs, []string{
556+
"mode=620",
557+
"ptmxmode=666",
558+
}...)
559+
560+
optionsArg := strings.Join(optionsArgs, ",")
561+
562+
const devPtsFS = "devpts"
563+
const devPtsMountPoint = "/dev/pts"
564+
565+
mountArgs := []string{
566+
"--types", devPtsFS,
567+
"--options", optionsArg,
568+
devPtsFS,
569+
devPtsMountPoint,
570+
}
571+
572+
if err := shell.Run("mount", nil, nil, nil, mountArgs...); err != nil {
573+
return fmt.Errorf("failed to mount a %s file system at %s: %w", devPtsFS, devPtsMountPoint, err)
574+
}
575+
576+
return nil
577+
}
578+
525579
// redirectPath serves for creating symbolic links for crucial system
526580
// configuration files to their counterparts on the host's file system.
527581
//

0 commit comments

Comments
 (0)