Skip to content

Commit 5bfad0d

Browse files
committed
Remove workaround for HcsFormatWritableLayerVhd
The workaround is internalised in hcsshim past v0.9 (not yet named, I guess v0.10?) so we don't need to do it anymore.
1 parent ff1e9b5 commit 5bfad0d

1 file changed

Lines changed: 1 addition & 73 deletions

File tree

pkg/os/os_windows_test.go

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,16 @@ package os
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"os"
2322
"path/filepath"
24-
"strconv"
2523
"strings"
26-
"syscall"
2724
"testing"
2825

2926
"github.com/Microsoft/go-winio/vhd"
3027
"github.com/Microsoft/hcsshim/computestorage"
31-
"github.com/Microsoft/hcsshim/osversion"
3228
"golang.org/x/sys/windows"
33-
"golang.org/x/sys/windows/registry"
3429
)
3530

36-
// Getting build number via osversion.Build() requires the program to be properly manifested, which
37-
// requires importing `github.com/Microsoft/hcsshim/test/functional/manifest`, which is a dependency
38-
// we want to avoid here. Instead, since this is just test code, simply parse the build number from
39-
// the registry.
40-
func getWindowsBuildNumber() (int, error) {
41-
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
42-
if err != nil {
43-
return 0, fmt.Errorf("read CurrentVersion reg key: %s", err)
44-
}
45-
defer k.Close()
46-
buildNumStr, _, err := k.GetStringValue("CurrentBuild")
47-
if err != nil {
48-
return 0, fmt.Errorf("read CurrentBuild reg value: %s", err)
49-
}
50-
buildNum, err := strconv.Atoi(buildNumStr)
51-
if err != nil {
52-
return 0, err
53-
}
54-
return buildNum, nil
55-
}
56-
5731
func makeSymlink(t *testing.T, oldName string, newName string) {
5832
if err := os.Symlink(oldName, newName); err != nil {
5933
t.Fatalf("creating symlink: %s", err)
@@ -73,57 +47,11 @@ func getVolumeGUIDPath(t *testing.T, path string) string {
7347
return final
7448
}
7549

76-
func openDisk(path string) (windows.Handle, error) {
77-
u16, err := windows.UTF16PtrFromString(path)
78-
if err != nil {
79-
return 0, err
80-
}
81-
h, err := windows.CreateFile(
82-
u16,
83-
windows.GENERIC_READ|windows.GENERIC_WRITE,
84-
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE,
85-
nil,
86-
windows.OPEN_EXISTING,
87-
windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_NO_BUFFERING,
88-
0)
89-
if err != nil {
90-
return 0, &os.PathError{
91-
Op: "CreateFile",
92-
Path: path,
93-
Err: err,
94-
}
95-
}
96-
return h, nil
97-
}
98-
9950
func formatVHD(vhdHandle windows.Handle) error {
100-
h := vhdHandle
101-
// Pre-19H1 HcsFormatWritableLayerVhd expects a disk handle.
102-
// On newer builds it expects a VHD handle instead.
103-
// Open a handle to the VHD's disk object if needed.
104-
build, err := getWindowsBuildNumber()
105-
if err != nil {
106-
return err
107-
}
108-
if build < osversion.V19H1 {
109-
diskPath, err := vhd.GetVirtualDiskPhysicalPath(syscall.Handle(h))
110-
if err != nil {
111-
return err
112-
}
113-
diskHandle, err := openDisk(diskPath)
114-
if err != nil {
115-
return err
116-
}
117-
defer windows.CloseHandle(diskHandle)
118-
h = diskHandle
119-
}
12051
// Formatting a disk directly in Windows is a pain, so we use FormatWritableLayerVhd to do it.
12152
// It has a side effect of creating a sandbox directory on the formatted volume, but it's safe
12253
// to just ignore that for our purposes here.
123-
if err := computestorage.FormatWritableLayerVhd(context.Background(), h); err != nil {
124-
return err
125-
}
126-
return nil
54+
return computestorage.FormatWritableLayerVhd(context.Background(), vhdHandle)
12755
}
12856

12957
// Creates a VHD with a NTFS volume. Returns the volume path.

0 commit comments

Comments
 (0)