Skip to content

Commit e9dec98

Browse files
authored
Merge pull request moby#51715 from jschfflr/45939-init-layer-cleanup
layer: Clean up init layer if initialization fails
2 parents 9c68897 + 26bb1af commit e9dec98

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

daemon/internal/layer/layer_store.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ func (ls *layerStore) saveMount(mount *mountedLayer) error {
648648
return nil
649649
}
650650

651-
func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit, storageOpt map[string]string) (string, error) {
651+
func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit, storageOpt map[string]string) (_ string, retErr error) {
652652
// Use "<graph-id>-init" to maintain compatibility with graph drivers
653653
// which are expecting this layer with this special name. If all
654654
// graph drivers can be updated to not rely on knowing about this layer
@@ -663,6 +663,16 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou
663663
if err := ls.driver.CreateReadWrite(initID, parent, createOpts); err != nil {
664664
return "", err
665665
}
666+
667+
// Clean up init layer if any subsequent operation fails
668+
defer func() {
669+
if retErr != nil {
670+
if err := ls.driver.Remove(initID); err != nil {
671+
log.G(context.TODO()).WithFields(log.Fields{"init-id": initID, "error": err}).Error("Failed to clean up init layer after error")
672+
}
673+
}
674+
}()
675+
666676
p, err := ls.driver.Get(initID, "")
667677
if err != nil {
668678
return "", err

0 commit comments

Comments
 (0)