@@ -18,42 +18,16 @@ package os
1818
1919import (
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-
5731func 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-
9950func 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