Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions computestorage/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@ package computestorage

import (
"context"
"os"
"syscall"

"github.com/Microsoft/go-winio/vhd"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/osversion"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"golang.org/x/sys/windows"
)

func openDisk(path string) (windows.Handle, error) {
u16, err := windows.UTF16PtrFromString(path)
if err != nil {
return 0, err
}
h, err := windows.CreateFile(
u16,
windows.GENERIC_READ|windows.GENERIC_WRITE,
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE,
nil,
windows.OPEN_EXISTING,
windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_NO_BUFFERING,
0,
)
if err != nil {
return 0, &os.PathError{
Op: "CreateFile",
Path: path,
Err: err,
}
}
return h, nil
}

// FormatWritableLayerVhd formats a virtual disk for use as a writable container layer.
//
// If the VHD is not mounted it will be temporarily mounted.
Expand All @@ -18,9 +46,31 @@ func FormatWritableLayerVhd(ctx context.Context, vhdHandle windows.Handle) (err
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()

err = hcsFormatWritableLayerVhd(vhdHandle)
h := vhdHandle
// On RS5 HcsFormatWritableLayerVhd expects to receive a disk handle instead of a vhd handle.
if osversion.Build() < osversion.V19H1 {
if err := vhd.AttachVirtualDisk(syscall.Handle(vhdHandle), vhd.AttachVirtualDiskFlagNone, &vhd.AttachVirtualDiskParameters{Version: 1}); err != nil {
return err
}
defer func() {
if detachErr := vhd.DetachVirtualDisk(syscall.Handle(vhdHandle)); err == nil && detachErr != nil {
err = detachErr
}
}()
diskPath, err := vhd.GetVirtualDiskPhysicalPath(syscall.Handle(vhdHandle))
if err != nil {
return err
}
diskHandle, err := openDisk(diskPath)
if err != nil {
return err
}
defer windows.CloseHandle(diskHandle) // nolint: errcheck
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to close this handle but we don't close the handle that's created if this section of code doesn't run?

Copy link
Copy Markdown
Contributor Author

@dcantah dcantah Nov 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't handle closing the vhd handle passed in as it's expected the caller will close it. This function owns this new disk handle essentially so we need to close it ourselves if we hit this path.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, makes sense

h = diskHandle
}
err = hcsFormatWritableLayerVhd(h)
if err != nil {
return errors.Wrap(err, "failed to format writable layer vhd")
}
return nil
return
}
54 changes: 52 additions & 2 deletions test/vendor/github.com/Microsoft/hcsshim/computestorage/format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.