From f8213a6d53af7d40e9b86d27b63f334c0f233ee4 Mon Sep 17 00:00:00 2001 From: Doug Rabson Date: Sun, 9 Jul 2023 08:42:32 +0100 Subject: [PATCH] libpod: don't make a broken symlink for /etc/mtab on FreeBSD This file has not been present in BSD systems since 2.9.1 BSD and as far as I remember /proc/mounts has never existed on BSD systems. [NO NEW TESTS NEEDED] Signed-off-by: Doug Rabson --- libpod/container_internal.go | 14 ++------------ libpod/container_internal_freebsd.go | 5 +++++ libpod/container_internal_linux.go | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 3188f1b40b..be0560efd9 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -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() diff --git a/libpod/container_internal_freebsd.go b/libpod/container_internal_freebsd.go index 5f4538cc0a..6bd872aa45 100644 --- a/libpod/container_internal_freebsd.go +++ b/libpod/container_internal_freebsd.go @@ -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 +} diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go index 08e4df12fd..2f1082907f 100644 --- a/libpod/container_internal_linux.go +++ b/libpod/container_internal_linux.go @@ -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 +}