Skip to content

Commit a484f78

Browse files
Merge pull request #26209 from jankaluza/26190
Recreate the Rootfs in mountStorage for infra-container.
2 parents e98e128 + e0b08fc commit a484f78

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

libpod/container_internal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,15 @@ func (c *Container) mountStorage() (_ string, deferredErr error) {
17731773
if err != nil {
17741774
return "", fmt.Errorf("rootfs-overlay: failed to create TempDir in the %s directory: %w", overlayDest, err)
17751775
}
1776+
1777+
// Recreate the rootfs for infra container. It can be missing after system reboot if it's stored on tmpfs.
1778+
if c.IsDefaultInfra() || c.IsService() {
1779+
err := c.createInitRootfs()
1780+
if err != nil {
1781+
return "", err
1782+
}
1783+
}
1784+
17761785
overlayMount, err := overlay.Mount(contentDir, c.config.Rootfs, overlayDest, c.RootUID(), c.RootGID(), c.runtime.store.GraphOptions())
17771786
if err != nil {
17781787
return "", fmt.Errorf("rootfs-overlay: creating overlay failed %q: %w", c.config.Rootfs, err)

libpod/container_internal_common.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,33 @@ func getOverlayUpperAndWorkDir(options []string) (string, string, error) {
179179
}
180180

181181
// Internal only function which creates the Rootfs for default internal
182-
// pause image, configures the Rootfs in the Container and returns
183-
// the mount-point for the /catatonit. This mount-point should be added
184-
// to the Container spec.
185-
func (c *Container) prepareInitRootfs() (spec.Mount, error) {
186-
newMount := spec.Mount{
187-
Type: define.TypeBind,
188-
Source: "",
189-
Destination: "",
190-
Options: append(bindOptions, "ro", "nosuid", "nodev"),
191-
}
192-
182+
// pause image and configures the Rootfs in the Container.
183+
func (c *Container) createInitRootfs() error {
193184
tmpDir, err := c.runtime.TmpDir()
194185
if err != nil {
195-
return newMount, fmt.Errorf("getting runtime temporary directory: %w", err)
186+
return fmt.Errorf("getting runtime temporary directory: %w", err)
196187
}
197188
tmpDir = filepath.Join(tmpDir, "infra-container")
198189
err = os.MkdirAll(tmpDir, 0755)
199190
if err != nil {
200-
return newMount, fmt.Errorf("creating infra container temporary directory: %w", err)
191+
return fmt.Errorf("creating infra container temporary directory: %w", err)
192+
}
193+
194+
c.config.Rootfs = tmpDir
195+
c.config.RootfsOverlay = true
196+
return nil
197+
}
198+
199+
// Internal only function which returns the mount-point for the /catatonit.
200+
// This mount-point should be added to the Container spec.
201+
func (c *Container) prepareCatatonitMount() (spec.Mount, error) {
202+
newMount := spec.Mount{
203+
Type: define.TypeBind,
204+
Source: "",
205+
Destination: "",
206+
Options: append(bindOptions, "ro", "nosuid", "nodev"),
201207
}
208+
202209
// Also look into the path as some distributions install catatonit in
203210
// /usr/bin.
204211
catatonitPath, err := c.runtime.config.FindInitBinary()
@@ -213,8 +220,6 @@ func (c *Container) prepareInitRootfs() (spec.Mount, error) {
213220
newMount.Source = catatonitPath
214221
newMount.Destination = "/" + filepath.Base(catatonitPath)
215222

216-
c.config.Rootfs = tmpDir
217-
c.config.RootfsOverlay = true
218223
if len(c.config.Entrypoint) == 0 {
219224
c.config.Entrypoint = []string{"/" + filepath.Base(catatonitPath), "-P"}
220225
c.config.Spec.Process.Args = c.config.Entrypoint
@@ -426,7 +431,7 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
426431
c.setMountLabel(&g)
427432

428433
if c.IsDefaultInfra() || c.IsService() {
429-
newMount, err := c.prepareInitRootfs()
434+
newMount, err := c.prepareCatatonitMount()
430435
if err != nil {
431436
return nil, nil, err
432437
}

libpod/runtime_ctr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options ..
250250

251251
func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Container, retErr error) {
252252
if ctr.IsDefaultInfra() || ctr.IsService() {
253-
_, err := ctr.prepareInitRootfs()
253+
err := ctr.createInitRootfs()
254+
if err != nil {
255+
return nil, err
256+
}
257+
_, err = ctr.prepareCatatonitMount()
254258
if err != nil {
255259
return nil, err
256260
}

test/e2e/run_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,27 @@ VOLUME %s`, ALPINE, volPath, volPath)
14751475
Expect(numContainers).To(Equal(0))
14761476
})
14771477

1478+
It("podman run after infra-container rootfs removed", func() {
1479+
// Regression test for #26190
1480+
podmanTest.PodmanExitCleanly("run", "--name", "test", "--pod", "new:foobar", ALPINE, "ls")
1481+
1482+
podInspect := podmanTest.PodmanExitCleanly("pod", "inspect", "foobar", "--format", "{{.InfraContainerID}}")
1483+
infraID := podInspect.OutputToString()
1484+
1485+
infraInspect := podmanTest.PodmanExitCleanly("inspect", infraID, "--format", "{{.Rootfs}}")
1486+
rootfs := infraInspect.OutputToString()
1487+
1488+
podmanTest.PodmanExitCleanly("pod", "stop", "foobar")
1489+
1490+
_, statErr := os.Stat(rootfs)
1491+
Expect(statErr).ToNot(HaveOccurred())
1492+
1493+
err := os.RemoveAll(rootfs)
1494+
Expect(err).ToNot(HaveOccurred())
1495+
1496+
podmanTest.PodmanExitCleanly("run", "--replace", "--name", "test", "--pod", "foobar", ALPINE, "ls")
1497+
})
1498+
14781499
It("podman run --rm failed container should delete itself", func() {
14791500
session := podmanTest.Podman([]string{"run", "--name", "test", "--rm", ALPINE, "foo"})
14801501
session.WaitWithDefaultTimeout()

0 commit comments

Comments
 (0)