Skip to content

Commit aaf5db9

Browse files
authored
Pass disk handle for computestorage.FormatWritableLayerVhd on RS5 (#1204)
Pass disk handle for FormatWritableLayerVhd on RS5 On RS5 the HcsFormatWritableLayerVhd call expects to receive a disk handle. On 19h1+ you can pass a vhd handle and internally they will do the same work we're doing in this change to grab a disk handle to perform the format. Signed-off-by: Daniel Canter <[email protected]>
1 parent a1756af commit aaf5db9

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

computestorage/format.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,41 @@ package computestorage
22

33
import (
44
"context"
5+
"os"
6+
"syscall"
57

8+
"github.com/Microsoft/go-winio/vhd"
69
"github.com/Microsoft/hcsshim/internal/oc"
10+
"github.com/Microsoft/hcsshim/osversion"
711
"github.com/pkg/errors"
812
"go.opencensus.io/trace"
913
"golang.org/x/sys/windows"
1014
)
1115

16+
func openDisk(path string) (windows.Handle, error) {
17+
u16, err := windows.UTF16PtrFromString(path)
18+
if err != nil {
19+
return 0, err
20+
}
21+
h, err := windows.CreateFile(
22+
u16,
23+
windows.GENERIC_READ|windows.GENERIC_WRITE,
24+
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE,
25+
nil,
26+
windows.OPEN_EXISTING,
27+
windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_NO_BUFFERING,
28+
0,
29+
)
30+
if err != nil {
31+
return 0, &os.PathError{
32+
Op: "CreateFile",
33+
Path: path,
34+
Err: err,
35+
}
36+
}
37+
return h, nil
38+
}
39+
1240
// FormatWritableLayerVhd formats a virtual disk for use as a writable container layer.
1341
//
1442
// If the VHD is not mounted it will be temporarily mounted.
@@ -18,9 +46,31 @@ func FormatWritableLayerVhd(ctx context.Context, vhdHandle windows.Handle) (err
1846
defer span.End()
1947
defer func() { oc.SetSpanStatus(span, err) }()
2048

21-
err = hcsFormatWritableLayerVhd(vhdHandle)
49+
h := vhdHandle
50+
// On RS5 HcsFormatWritableLayerVhd expects to receive a disk handle instead of a vhd handle.
51+
if osversion.Build() < osversion.V19H1 {
52+
if err := vhd.AttachVirtualDisk(syscall.Handle(vhdHandle), vhd.AttachVirtualDiskFlagNone, &vhd.AttachVirtualDiskParameters{Version: 1}); err != nil {
53+
return err
54+
}
55+
defer func() {
56+
if detachErr := vhd.DetachVirtualDisk(syscall.Handle(vhdHandle)); err == nil && detachErr != nil {
57+
err = detachErr
58+
}
59+
}()
60+
diskPath, err := vhd.GetVirtualDiskPhysicalPath(syscall.Handle(vhdHandle))
61+
if err != nil {
62+
return err
63+
}
64+
diskHandle, err := openDisk(diskPath)
65+
if err != nil {
66+
return err
67+
}
68+
defer windows.CloseHandle(diskHandle) // nolint: errcheck
69+
h = diskHandle
70+
}
71+
err = hcsFormatWritableLayerVhd(h)
2272
if err != nil {
2373
return errors.Wrap(err, "failed to format writable layer vhd")
2474
}
25-
return nil
75+
return
2676
}

test/vendor/github.com/Microsoft/hcsshim/computestorage/format.go

Lines changed: 52 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)