Use DeactivateLayer to recover layers that we cannot rename

It seems that something has shifted in an API, and vhd.DetachVhd is
returning "failed to open virtual disk: invalid argument" on Windows
Server LTSC 2019.

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
This commit is contained in:
Paul "TBBle" Hampson 2021-04-25 04:28:43 +10:00
parent 04f73e3f8a
commit f216270595

View File

@ -31,7 +31,6 @@ import (
"github.com/Microsoft/go-winio" "github.com/Microsoft/go-winio"
winfs "github.com/Microsoft/go-winio/pkg/fs" winfs "github.com/Microsoft/go-winio/pkg/fs"
"github.com/Microsoft/go-winio/vhd"
"github.com/Microsoft/hcsshim" "github.com/Microsoft/hcsshim"
"github.com/Microsoft/hcsshim/computestorage" "github.com/Microsoft/hcsshim/computestorage"
"github.com/Microsoft/hcsshim/pkg/ociwclayer" "github.com/Microsoft/hcsshim/pkg/ociwclayer"
@ -256,11 +255,19 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
return err return err
} }
// If permission denied, it's possible that the scratch is still mounted, an // If permission denied, it's possible that the scratch is still mounted, an
// artifact after a hard daemon crash for example. Worth a shot to try detaching it // artifact after a hard daemon crash for example. Worth a shot to try deactivating it
// before retrying the rename. // before retrying the rename.
if detachErr := vhd.DetachVhd(filepath.Join(path, "sandbox.vhdx")); detachErr != nil { var (
return errors.Wrapf(err, "failed to detach VHD: %s", detachErr) home, layerID = filepath.Split(path)
di = hcsshim.DriverInfo{
HomeDir: home,
}
)
if deactvateErr := hcsshim.DeactivateLayer(di, layerID); deactvateErr != nil {
return errors.Wrapf(err, "failed to deactivate layer following failed rename: %s", deactvateErr)
} }
if renameErr := os.Rename(path, renamed); renameErr != nil && !os.IsNotExist(renameErr) { if renameErr := os.Rename(path, renamed); renameErr != nil && !os.IsNotExist(renameErr) {
return errors.Wrapf(err, "second rename attempt following detach failed: %s", renameErr) return errors.Wrapf(err, "second rename attempt following detach failed: %s", renameErr)
} }