mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
layer: Clean up init layer if initialization fails
Add cleanup for the init layer directory if any operation fails after driver.CreateReadWrite() succeeds in initMount(). Previously, failures in driver.Get(), initFunc(), or driver.Put() would leave an orphaned overlay2 directory. Related to moby/moby#45939 Signed-off-by: Jan Scheffler <jan.scheffler@qodev.ai>
This commit is contained in:
@@ -660,6 +660,17 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou
|
||||
if err := ls.driver.CreateReadWrite(initID, parent, createOpts); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Clean up init layer if any subsequent operation fails
|
||||
successful := false
|
||||
defer func() {
|
||||
if !successful {
|
||||
if err := ls.driver.Remove(initID); err != nil {
|
||||
log.G(context.TODO()).WithFields(log.Fields{"init-id": initID, "error": err}).Error("Failed to clean up init layer after error")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
p, err := ls.driver.Get(initID, "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -674,6 +685,7 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou
|
||||
return "", err
|
||||
}
|
||||
|
||||
successful = true
|
||||
return initID, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user