Skip to content

Commit 57bd388

Browse files
mike-sulthaJeztah
authored andcommitted
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]> (cherry picked from commit de2447c) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent c2e7c32 commit 57bd388

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"
@@ -386,7 +387,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
386387
}
387388

388389
// Write link id to link file
389-
if err := os.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil {
390+
if err := ioutils.AtomicWriteFile(path.Join(dir, "link"), []byte(lid), 0o644); err != nil {
390391
return err
391392
}
392393

@@ -399,7 +400,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
399400
return err
400401
}
401402

402-
if err := os.WriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0600); err != nil {
403+
if err := ioutils.AtomicWriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0o600); err != nil {
403404
return err
404405
}
405406

@@ -408,7 +409,7 @@ func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr
408409
return err
409410
}
410411
if lower != "" {
411-
if err := os.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil {
412+
if err := ioutils.AtomicWriteFile(path.Join(dir, lowerFile), []byte(lower), 0o666); err != nil {
412413
return err
413414
}
414415
}

0 commit comments

Comments
 (0)