Skip to content

Commit de2447c

Browse files
committed
daemon: overlay2: Write layer metadata atomically
When the daemon process or the host running it is abruptly terminated, the layer metadata file can become inconsistent on the file system. Specifically, `link` and `lower` files may exist but be empty, leading to overlay mounting errors during layer extraction, such as: "failed to register layer: error creating overlay mount to <path>: too many levels of symbolic links." This commit introduces the use of `AtomicWriteFile` to ensure that the layer metadata files contain correct data when they exist on the file system. Signed-off-by: Mike <[email protected]>
1 parent 76915b1 commit de2447c

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

daemon/graphdriver/overlay2/overlay.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/docker/docker/pkg/containerfs"
2424
"github.com/docker/docker/pkg/directory"
2525
"github.com/docker/docker/pkg/idtools"
26+
"github.com/docker/docker/pkg/ioutils"
2627
"github.com/docker/docker/pkg/parsers"
2728
"github.com/docker/docker/quota"
2829
units "github.com/docker/go-units"
@@ -383,7 +384,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
383384
}
384385

385386
// Write link id to link file
386-
if err := os.WriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
387+
if err := ioutils.AtomicWriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
387388
return err
388389
}
389390

@@ -396,7 +397,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
396397
return err
397398
}
398399

399-
if err := os.WriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
400+
if err := ioutils.AtomicWriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
400401
return err
401402
}
402403

@@ -405,7 +406,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
405406
return err
406407
}
407408
if lower != "" {
408-
if err := os.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0o666); err != nil {
409+
if err := ioutils.AtomicWriteFile(path.Join(dir, lowerFile), []byte(lower), 0o666); err != nil {
409410
return err
410411
}
411412
}

0 commit comments

Comments
 (0)