From dd476f0a5ded2fed156ae4f2bb5abff65f884ae8 Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 27 Mar 2025 00:09:58 +0100 Subject: [PATCH] Implement Mount Fix Signed-off-by: Antonio --- libpod/common_test.go | 11 ++++++++++- libpod/container.go | 4 ++-- libpod/container_config.go | 2 +- libpod/container_inspect.go | 6 +++--- libpod/container_internal.go | 6 +++--- libpod/oci_conmon_exec_linux.go | 2 +- libpod/runtime_ctr.go | 10 ++++++++-- pkg/domain/entities/types/container_ps.go | 3 ++- pkg/ps/ps.go | 18 +++++++++++++++++- pkg/specgen/generate/container.go | 2 +- 10 files changed, 48 insertions(+), 16 deletions(-) diff --git a/libpod/common_test.go b/libpod/common_test.go index a4af91d297..619496e070 100644 --- a/libpod/common_test.go +++ b/libpod/common_test.go @@ -27,7 +27,16 @@ func getTestContainer(id, name string, manager lock.Manager) (*Container, error) RootfsImageID: id, RootfsImageName: "testimg", StaticDir: "/does/not/exist/", - Mounts: []string{"/does/not/exist"}, + Mounts: []define.InspectMount{ + { + Type: "bind", + Source: "/dummy/source", + Destination: "/dummy/destination", + Mode: "", + RW: true, + Propagation: "rprivate", + }, + }, }, ContainerMiscConfig: ContainerMiscConfig{ LogPath: "/does/not/exist/", diff --git a/libpod/container.go b/libpod/container.go index 7c04e1cf0b..0a3b839c9e 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -379,10 +379,10 @@ func (c *Container) Spec() *spec.Spec { return returnSpec } -// specFromState returns the unmarshalled json config of the container. If the +// SpecFromState returns the unmarshalled json config of the container. If the // config does not exist (e.g., because the container was never started) return // the spec from the config. -func (c *Container) specFromState() (*spec.Spec, error) { +func (c *Container) SpecFromState() (*spec.Spec, error) { returnSpec := c.config.Spec if f, err := os.Open(c.state.ConfigPath); err == nil { diff --git a/libpod/container_config.go b/libpod/container_config.go index 6178b0624e..f9090d8079 100644 --- a/libpod/container_config.go +++ b/libpod/container_config.go @@ -147,7 +147,7 @@ type ContainerRootFSConfig struct { // Mounts contains all additional mounts into the container rootfs. // It is presently only used for the container's SHM directory. // These must be unmounted before the container's rootfs is unmounted. - Mounts []string `json:"mounts,omitempty"` + Mounts []define.InspectMount `json:"mounts,omitempty"` // NamedVolumes lists the Libpod named volumes to mount into the // container. Each named volume is guaranteed to exist so long as this // container exists. diff --git a/libpod/container_inspect.go b/libpod/container_inspect.go index 19c3178089..e9869ddcd7 100644 --- a/libpod/container_inspect.go +++ b/libpod/container_inspect.go @@ -50,7 +50,7 @@ func (c *Container) Inspect(size bool) (*define.InspectContainerData, error) { } func (c *Container) volumesFrom() ([]string, error) { - ctrSpec, err := c.specFromState() + ctrSpec, err := c.SpecFromState() if err != nil { return nil, err } @@ -63,7 +63,7 @@ func (c *Container) volumesFrom() ([]string, error) { func (c *Container) getContainerInspectData(size bool, driverData *define.DriverData) (*define.InspectContainerData, error) { config := c.config runtimeInfo := c.state - ctrSpec, err := c.specFromState() + ctrSpec, err := c.SpecFromState() if err != nil { return nil, err } @@ -664,7 +664,7 @@ func (c *Container) inHostPidNS() (bool, error) { if c.config.PIDNsCtr != "" { return false, nil } - ctrSpec, err := c.specFromState() + ctrSpec, err := c.SpecFromState() if err != nil { return false, err } diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 51be31196a..8179fef615 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -575,7 +575,7 @@ func (c *Container) processLabel(processLabel string) (string, error) { if !c.Systemd() && !c.ociRuntime.SupportsKVM() { return processLabel, nil } - ctrSpec, err := c.specFromState() + ctrSpec, err := c.SpecFromState() if err != nil { return "", err } @@ -2068,7 +2068,7 @@ func (c *Container) cleanupStorage() error { } for _, containerMount := range c.config.Mounts { - if err := c.unmountSHM(containerMount); err != nil { + if err := c.unmountSHM(containerMount.Destination); err != nil { reportErrorf("unmounting container %s: %w", c.ID(), err) } } @@ -2870,7 +2870,7 @@ func (c *Container) update(updateOptions *entities.ContainerUpdateOptions) error (updateOptions.Resources != nil || updateOptions.Env != nil || updateOptions.UnsetEnv != nil) { // So `podman inspect` on running containers sources its OCI spec from disk. // To keep inspect accurate we need to update the on-disk OCI spec. - onDiskSpec, err := c.specFromState() + onDiskSpec, err := c.SpecFromState() if err != nil { return fmt.Errorf("retrieving on-disk OCI spec to update: %w", err) } diff --git a/libpod/oci_conmon_exec_linux.go b/libpod/oci_conmon_exec_linux.go index e0d2f58422..1075e20350 100644 --- a/libpod/oci_conmon_exec_linux.go +++ b/libpod/oci_conmon_exec_linux.go @@ -9,7 +9,7 @@ import ( ) func (c *Container) setProcessCapabilitiesExec(options *ExecOptions, user string, execUser *user.ExecUser, pspec *spec.Process) error { - ctrSpec, err := c.specFromState() + ctrSpec, err := c.SpecFromState() if err != nil { return err } diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 87d8484783..1271930095 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -557,7 +557,13 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai return nil, fmt.Errorf("unable to create shm dir: %w", err) } } - ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir) + ctr.config.Mounts = append(ctr.config.Mounts, define.InspectMount{ // HERE I ADDED SOME DEFAULT VALUES. MUST THEY BE CHANGED? If yes, where should I take those values? + Type: "bind", // default mount type + Source: "/opt", // assuming source is same as destination, or set appropriately + Destination: ctr.config.ShmDir, // the destination in the container + RW: true, // default read/write setting + Propagation: "rprivate", // default propagation + }) } // Add the container to the state @@ -1167,7 +1173,7 @@ func (r *Runtime) evictContainer(ctx context.Context, idOrName string, removeVol // Unmount container mount points for _, mount := range c.config.Mounts { - Unmount(mount) + Unmount(mount.Destination) } // Remove container from c/storage diff --git a/pkg/domain/entities/types/container_ps.go b/pkg/domain/entities/types/container_ps.go index 139a87c036..ee698d48dc 100644 --- a/pkg/domain/entities/types/container_ps.go +++ b/pkg/domain/entities/types/container_ps.go @@ -4,6 +4,7 @@ import ( "time" netTypes "github.com/containers/common/libnetwork/types" + pod_define "github.com/containers/podman/v5/libpod/define" define "github.com/containers/podman/v5/pkg/ps/define" ) @@ -41,7 +42,7 @@ type ListContainer struct { // Labels for container Labels map[string]string // User volume mounts - Mounts []string + Mounts []pod_define.InspectMount // The names assigned to the container Names []string // Namespaces the container belongs to. Requires the diff --git a/pkg/ps/ps.go b/pkg/ps/ps.go index 7a77621e75..b05c036a42 100644 --- a/pkg/ps/ps.go +++ b/pkg/ps/ps.go @@ -21,6 +21,7 @@ import ( psdefine "github.com/containers/podman/v5/pkg/ps/define" "github.com/containers/storage" "github.com/containers/storage/types" + spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) @@ -153,6 +154,10 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities healthStatus string restartCount uint podName string + ctrSpec *spec.Spec + namedVolumes []*libpod.ContainerNamedVolume + mounts []spec.Mount + inspectMounts []define.InspectMount ) batchErr := ctr.Batch(func(c *libpod.Container) error { @@ -206,6 +211,17 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities return err } + ctrSpec, err = c.SpecFromState() + if err != nil { + return err + } + + namedVolumes, mounts = c.SortUserVolumes(ctrSpec) + inspectMounts, err = c.GetMounts(namedVolumes, conConfig.ImageVolumes, mounts) + if err != nil { + return err + } + if opts.Namespace { ctrPID := strconv.Itoa(pid) cgroup, _ = getNamespaceInfo(filepath.Join("/proc", ctrPID, "ns", "cgroup")) @@ -260,7 +276,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities ImageID: conConfig.RootfsImageID, IsInfra: conConfig.IsInfra, Labels: conConfig.Labels, - Mounts: ctr.UserVolumes(), + Mounts: inspectMounts, Names: []string{conConfig.Name}, Networks: networks, Pid: pid, diff --git a/pkg/specgen/generate/container.go b/pkg/specgen/generate/container.go index d5973e6984..18b633a577 100644 --- a/pkg/specgen/generate/container.go +++ b/pkg/specgen/generate/container.go @@ -333,7 +333,7 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, containerID tmpMounts := conf.Mounts conf.Systemd = nil - conf.Mounts = []string{} + conf.Mounts = []define.InspectMount{} if specg == nil { specg = &specgen.SpecGenerator{}