Skip to content

Commit

Permalink
Merge pull request #19170 from dfr/freebsd-mtab
Browse files Browse the repository at this point in the history
libpod: don't make a broken symlink for /etc/mtab on FreeBSD
  • Loading branch information
openshift-merge-robot authored Jul 10, 2023
2 parents eb9d442 + f8213a6 commit d31b9eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
14 changes: 2 additions & 12 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1638,18 +1638,8 @@ func (c *Container) mountStorage() (_ string, deferredErr error) {
}
defer unix.Close(etcInTheContainerFd)

// If /etc/mtab does not exist in container image, then we need to
// create it, so that mount command within the container will work.
err = unix.Symlinkat("/proc/mounts", etcInTheContainerFd, "mtab")
if err != nil && !os.IsExist(err) {
return "", fmt.Errorf("creating /etc/mtab symlink: %w", err)
}
// If the symlink was created, then also chown it to root in the container
if err == nil && (rootUID != 0 || rootGID != 0) {
err = unix.Fchownat(etcInTheContainerFd, "mtab", rootUID, rootGID, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return "", fmt.Errorf("chown /etc/mtab: %w", err)
}
if err := c.makePlatformMtabLink(etcInTheContainerFd, rootUID, rootGID); err != nil {
return "", err
}

tz := c.Timezone()
Expand Down
5 changes: 5 additions & 0 deletions libpod/container_internal_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,8 @@ func (s *safeMountInfo) Close() {
func (c *Container) safeMountSubPath(mountPoint, subpath string) (s *safeMountInfo, err error) {
return &safeMountInfo{mountPoint: filepath.Join(mountPoint, subpath)}, nil
}

func (c *Container) makePlatformMtabLink(etcInTheContainerFd, rootUID, rootGID int) error {
// /etc/mtab does not exist on FreeBSD
return nil
}
17 changes: 17 additions & 0 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,20 @@ func (c *Container) safeMountSubPath(mountPoint, subpath string) (s *safeMountIn
mountPoint: npath,
}, nil
}

func (c *Container) makePlatformMtabLink(etcInTheContainerFd, rootUID, rootGID int) error {
// If /etc/mtab does not exist in container image, then we need to
// create it, so that mount command within the container will work.
err := unix.Symlinkat("/proc/mounts", etcInTheContainerFd, "mtab")
if err != nil && !os.IsExist(err) {
return fmt.Errorf("creating /etc/mtab symlink: %w", err)
}
// If the symlink was created, then also chown it to root in the container
if err == nil && (rootUID != 0 || rootGID != 0) {
err = unix.Fchownat(etcInTheContainerFd, "mtab", rootUID, rootGID, unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return fmt.Errorf("chown /etc/mtab: %w", err)
}
}
return nil
}

0 comments on commit d31b9eb

Please sign in to comment.