Browse code

Windows: Don't attempt detach VHD for R/O layers

Signed-off-by: John Howard <jhoward@microsoft.com>
(cherry picked from commit 293c74ba79f0008f48073985507b34af59b45fa6)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

John Howard authored on 2019/06/05 05:38:52
Showing 1 changed files
... ...
@@ -338,11 +338,14 @@ func (d *Driver) Remove(id string) error {
338 338
 		// If permission denied, it's possible that the scratch is still mounted, an
339 339
 		// artifact after a hard daemon crash for example. Worth a shot to try detaching it
340 340
 		// before retrying the rename.
341
-		if detachErr := vhd.DetachVhd(filepath.Join(layerPath, "sandbox.vhdx")); detachErr != nil {
342
-			return errors.Wrapf(err, "failed to detach VHD: %s", detachErr)
343
-		}
344
-		if renameErr := os.Rename(layerPath, tmpLayerPath); renameErr != nil && !os.IsNotExist(renameErr) {
345
-			return errors.Wrapf(err, "second rename attempt following detach failed: %s", renameErr)
341
+		sandbox := filepath.Join(layerPath, "sandbox.vhdx")
342
+		if _, statErr := os.Stat(sandbox); statErr == nil {
343
+			if detachErr := vhd.DetachVhd(sandbox); detachErr != nil {
344
+				return errors.Wrapf(err, "failed to detach VHD: %s", detachErr)
345
+			}
346
+			if renameErr := os.Rename(layerPath, tmpLayerPath); renameErr != nil && !os.IsNotExist(renameErr) {
347
+				return errors.Wrapf(err, "second rename attempt following detach failed: %s", renameErr)
348
+			}
346 349
 		}
347 350
 	}
348 351
 	if err := hcsshim.DestroyLayer(d.info, tmpID); err != nil {