-
Notifications
You must be signed in to change notification settings - Fork 238
/
wcow.go
106 lines (87 loc) · 3.01 KB
/
wcow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//go:build windows
package uvm
import (
"context"
"testing"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
shimlayers "github.com/Microsoft/hcsshim/internal/layers"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/test/internal/layers"
"github.com/Microsoft/hcsshim/test/pkg/images"
)
// DefaultWCOWOptions returns default options for a bootable WCOW uVM.
//
// See [uvm.NewDefaultOptionsWCOW] for more information.
func DefaultWCOWOptions(_ context.Context, tb testing.TB, id, owner string) *uvm.OptionsWCOW {
// mostly here to match [DefaultLCOWOptions]
tb.Helper()
opts := uvm.NewDefaultOptionsWCOW(id, owner)
return opts
}
// CreateWCOWUVM creates a WCOW utility VM with all default options. Returns the
// UtilityVM object; folder used as its scratch.
//
// Deprecated: use [CreateWCOW] and [layers.WCOWScratchDir].
func CreateWCOWUVM(ctx context.Context, tb testing.TB, id, image string) (*uvm.UtilityVM, []string, string) {
tb.Helper()
return CreateWCOWUVMFromOptsWithImage(ctx, tb, uvm.NewDefaultOptionsWCOW(id, ""), image)
}
// CreateWCOW creates a WCOW utility VM with the passed opts.
func CreateWCOW(ctx context.Context, tb testing.TB, opts *uvm.OptionsWCOW) (*uvm.UtilityVM, CleanupFn) {
tb.Helper()
if opts == nil || opts.BootFiles == nil {
tb.Fatalf("opts must bet set with BootFiles")
}
vm, err := uvm.CreateWCOW(ctx, opts)
if err != nil {
tb.Fatalf("could not create WCOW UVM: %v", err)
}
return vm, newCleanupFn(ctx, tb, vm)
}
// CreateWCOWUVMFromOptsWithImage creates a WCOW utility VM with the passed opts
// builds the LayerFolders based on `image`. Returns the UtilityVM object;
// folder used as its scratch.
//
// Deprecated: use [CreateWCOW] and [layers.WCOWScratchDir].
//
//nolint:staticcheck // SA5011: staticcheck thinks `opts` may be nil, even though we fail if it is
func CreateWCOWUVMFromOptsWithImage(
ctx context.Context,
tb testing.TB,
opts *uvm.OptionsWCOW,
image string,
) (*uvm.UtilityVM, []string, string) {
tb.Helper()
if opts == nil {
tb.Fatal("opts must be set")
}
img := layers.LazyImageLayers{Image: image, Platform: images.PlatformWindows}
tb.Cleanup(func() {
if err := img.Close(ctx); err != nil {
tb.Errorf("could not close image %s: %v", image, err)
}
})
uvmLayers := img.Layers(ctx, tb)
scratchDir := tb.TempDir()
bootFiles, err := shimlayers.GetWCOWUVMBootFilesFromLayers(ctx, nil, append(uvmLayers, scratchDir))
if err != nil {
tb.Fatalf("failed to parse UVM boot layers: %s", err)
}
opts.BootFiles = bootFiles
vm, cleanup := CreateWCOW(ctx, tb, opts)
tb.Cleanup(func() { cleanup(ctx) })
return vm, uvmLayers, scratchDir
}
func AddVSMB(ctx context.Context, tb testing.TB, vm *uvm.UtilityVM, path string, options *hcsschema.VirtualSmbShareOptions) *uvm.VSMBShare {
tb.Helper()
s, err := vm.AddVSMB(ctx, path, options)
if err != nil {
tb.Fatalf("failed to add vSMB share: %v", err)
}
tb.Cleanup(func() {
if err := s.Release(ctx); err != nil {
tb.Fatalf("failed to remove vSMB share: %v", err)
}
})
return s
}