|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package vm |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 15 | + |
| 16 | + "github.com/google/go-cmp/cmp" |
| 17 | + |
| 18 | + runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" |
| 19 | + lcowbuilder "github.com/Microsoft/hcsshim/internal/builder/vm/lcow" |
| 20 | + hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2" |
| 21 | + "github.com/Microsoft/hcsshim/internal/oci" |
| 22 | + "github.com/Microsoft/hcsshim/internal/uvm" |
| 23 | + "github.com/Microsoft/hcsshim/internal/vm/vmutils" |
| 24 | + "github.com/Microsoft/hcsshim/osversion" |
| 25 | + vm "github.com/Microsoft/hcsshim/sandbox-spec/vm/v2" |
| 26 | +) |
| 27 | + |
| 28 | +// buildLegacyLCOWDocument creates the HCS document for an LCOW VM using the |
| 29 | +// legacy shim pipeline. It runs the same sequence as createInternal → createPod |
| 30 | +// → CreateLCOW: annotation processing, spec conversion, option verification, |
| 31 | +// and document generation. |
| 32 | +func buildLegacyLCOWDocument( |
| 33 | + ctx context.Context, |
| 34 | + spec specs.Spec, |
| 35 | + shimOpts *runhcsopts.Options, |
| 36 | + bundle string, |
| 37 | +) (*hcsschema.ComputeSystem, *uvm.OptionsLCOW, error) { |
| 38 | + // Step 1: Merge shim options into the OCI spec annotations. |
| 39 | + spec = oci.UpdateSpecFromOptions(spec, shimOpts) |
| 40 | + |
| 41 | + // Step 2: Expand annotation groups (e.g., security toggles). |
| 42 | + if err := oci.ProcessAnnotations(ctx, spec.Annotations); err != nil { |
| 43 | + return nil, nil, fmt.Errorf("failed to expand OCI annotations: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Step 3: Convert OCI spec + annotations into OptionsLCOW. |
| 47 | + rawOpts, err := oci.SpecToUVMCreateOpts(ctx, &spec, "test-parity@vm", "test-owner") |
| 48 | + if err != nil { |
| 49 | + return nil, nil, fmt.Errorf("failed to convert OCI spec to UVM create options: %w", err) |
| 50 | + } |
| 51 | + opts := rawOpts.(*uvm.OptionsLCOW) |
| 52 | + opts.BundleDirectory = bundle |
| 53 | + |
| 54 | + // Step 4: Verify options constraints (same as CreateLCOW). |
| 55 | + if err := uvm.VerifyOptions(ctx, opts); err != nil { |
| 56 | + return nil, nil, fmt.Errorf("option verification failed: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Step 5: Build the temporary UtilityVM with fields that MakeLCOWDoc reads. |
| 60 | + scsiCount := opts.SCSIControllerCount |
| 61 | + if osversion.Build() >= osversion.RS5 && opts.VPMemDeviceCount == 0 { |
| 62 | + scsiCount = 4 |
| 63 | + } |
| 64 | + tempUVM := uvm.NewUtilityVMForDoc( |
| 65 | + opts.ID, opts.Owner, |
| 66 | + scsiCount, opts.VPMemDeviceCount, opts.VPMemSizeBytes, |
| 67 | + !opts.VPMemNoMultiMapping, |
| 68 | + ) |
| 69 | + |
| 70 | + // Step 6: Generate the HCS document. |
| 71 | + doc, err := uvm.MakeLCOWDoc(ctx, opts, tempUVM) |
| 72 | + if err != nil { |
| 73 | + return nil, nil, fmt.Errorf("failed to generate legacy LCOW HCS document: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return doc, opts, nil |
| 77 | +} |
| 78 | + |
| 79 | +// buildV2LCOWDocument creates the HCS document and sandbox options from the |
| 80 | +// provided VM spec and runhcs options using the v2 modular builder. |
| 81 | +// The returned document can be used to create a VM directly via HCS. |
| 82 | +func buildV2LCOWDocument( |
| 83 | + ctx context.Context, |
| 84 | + shimOpts *runhcsopts.Options, |
| 85 | + spec *vm.Spec, |
| 86 | + bundle string, |
| 87 | +) (*hcsschema.ComputeSystem, *lcowbuilder.SandboxOptions, error) { |
| 88 | + return lcowbuilder.BuildSandboxConfig(ctx, "test-owner", bundle, shimOpts, spec) |
| 89 | +} |
| 90 | + |
| 91 | +// setupBootFiles creates a temporary directory containing the kernel and rootfs |
| 92 | +// files that both document builders probe during boot configuration resolution. |
| 93 | +func setupBootFiles(t *testing.T) string { |
| 94 | + t.Helper() |
| 95 | + dir := t.TempDir() |
| 96 | + for _, name := range []string{ |
| 97 | + vmutils.KernelFile, |
| 98 | + vmutils.UncompressedKernelFile, |
| 99 | + vmutils.InitrdFile, |
| 100 | + vmutils.VhdFile, |
| 101 | + } { |
| 102 | + if err := os.WriteFile(filepath.Join(dir, name), []byte("test"), 0644); err != nil { |
| 103 | + t.Fatalf("failed to create boot file %s: %v", name, err) |
| 104 | + } |
| 105 | + } |
| 106 | + return dir |
| 107 | +} |
| 108 | + |
| 109 | +// jsonToString serializes v to indented JSON for test log output. |
| 110 | +func jsonToString(v interface{}) string { |
| 111 | + b, err := json.MarshalIndent(v, "", " ") |
| 112 | + if err != nil { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | + return string(b) |
| 116 | +} |
| 117 | + |
| 118 | +// normalizeKernelCmdLine trims leading/trailing whitespace from the kernel |
| 119 | +// command line in the document. The legacy builder has a minor quirk that |
| 120 | +// produces a leading space for initrd+KernelDirect boot. The v2 builder |
| 121 | +// does not. Since HCS trims whitespace from kernel args, this difference |
| 122 | +// is harmless and we normalize it away. |
| 123 | +func normalizeKernelCmdLine(doc *hcsschema.ComputeSystem) { |
| 124 | + if doc == nil || doc.VirtualMachine == nil || doc.VirtualMachine.Chipset == nil { |
| 125 | + return |
| 126 | + } |
| 127 | + if kd := doc.VirtualMachine.Chipset.LinuxKernelDirect; kd != nil { |
| 128 | + kd.KernelCmdLine = strings.TrimSpace(kd.KernelCmdLine) |
| 129 | + } |
| 130 | + if uefi := doc.VirtualMachine.Chipset.Uefi; uefi != nil && uefi.BootThis != nil { |
| 131 | + uefi.BootThis.OptionalData = strings.TrimSpace(uefi.BootThis.OptionalData) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// isOnlyKernelCmdLineWhitespaceDiff returns true if the only difference between |
| 136 | +// two documents is leading/trailing whitespace in the kernel command line. |
| 137 | +// This is a known legacy quirk where initrd+KernelDirect boot produces a |
| 138 | +// leading space that v2 correctly omits. |
| 139 | +func isOnlyKernelCmdLineWhitespaceDiff(legacy, v2 *hcsschema.ComputeSystem) bool { |
| 140 | + // Deep copy and normalize, then re-compare. |
| 141 | + legacyCopy := *legacy |
| 142 | + v2Copy := *v2 |
| 143 | + // Shallow copy the VM and chipset to avoid mutating originals. |
| 144 | + if legacyCopy.VirtualMachine != nil { |
| 145 | + vmCopy := *legacyCopy.VirtualMachine |
| 146 | + legacyCopy.VirtualMachine = &vmCopy |
| 147 | + if vmCopy.Chipset != nil { |
| 148 | + chipCopy := *vmCopy.Chipset |
| 149 | + legacyCopy.VirtualMachine.Chipset = &chipCopy |
| 150 | + if chipCopy.LinuxKernelDirect != nil { |
| 151 | + lkdCopy := *chipCopy.LinuxKernelDirect |
| 152 | + legacyCopy.VirtualMachine.Chipset.LinuxKernelDirect = &lkdCopy |
| 153 | + } |
| 154 | + if chipCopy.Uefi != nil { |
| 155 | + uefiCopy := *chipCopy.Uefi |
| 156 | + legacyCopy.VirtualMachine.Chipset.Uefi = &uefiCopy |
| 157 | + if uefiCopy.BootThis != nil { |
| 158 | + btCopy := *uefiCopy.BootThis |
| 159 | + legacyCopy.VirtualMachine.Chipset.Uefi.BootThis = &btCopy |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + if v2Copy.VirtualMachine != nil { |
| 165 | + vmCopy := *v2Copy.VirtualMachine |
| 166 | + v2Copy.VirtualMachine = &vmCopy |
| 167 | + if vmCopy.Chipset != nil { |
| 168 | + chipCopy := *vmCopy.Chipset |
| 169 | + v2Copy.VirtualMachine.Chipset = &chipCopy |
| 170 | + if chipCopy.LinuxKernelDirect != nil { |
| 171 | + lkdCopy := *chipCopy.LinuxKernelDirect |
| 172 | + v2Copy.VirtualMachine.Chipset.LinuxKernelDirect = &lkdCopy |
| 173 | + } |
| 174 | + if chipCopy.Uefi != nil { |
| 175 | + uefiCopy := *chipCopy.Uefi |
| 176 | + v2Copy.VirtualMachine.Chipset.Uefi = &uefiCopy |
| 177 | + if uefiCopy.BootThis != nil { |
| 178 | + btCopy := *uefiCopy.BootThis |
| 179 | + v2Copy.VirtualMachine.Chipset.Uefi.BootThis = &btCopy |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + normalizeKernelCmdLine(&legacyCopy) |
| 185 | + normalizeKernelCmdLine(&v2Copy) |
| 186 | + return cmp.Diff(&legacyCopy, &v2Copy) == "" |
| 187 | +} |
0 commit comments