@@ -8,8 +8,10 @@ import (
88 "fmt"
99 "os"
1010 "path/filepath"
11+ "strings"
1112 "testing"
1213
14+ "github.com/google/go-cmp/cmp"
1315 "github.com/opencontainers/runtime-spec/specs-go"
1416
1517 runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
@@ -111,3 +113,70 @@ func jsonToString(v interface{}) string {
111113 }
112114 return string (b )
113115}
116+
117+ // normalizeKernelCmdLine trims leading/trailing whitespace from the kernel
118+ // command line in the document. The legacy builder has a minor quirk that
119+ // produces a leading space for initrd+KernelDirect boot. The v2 builder
120+ // does not. Since HCS trims whitespace from kernel args, this difference
121+ // is harmless and we normalize it away.
122+ func normalizeKernelCmdLine (doc * hcsschema.ComputeSystem ) {
123+ if doc == nil || doc .VirtualMachine == nil || doc .VirtualMachine .Chipset == nil {
124+ return
125+ }
126+ if kd := doc .VirtualMachine .Chipset .LinuxKernelDirect ; kd != nil {
127+ kd .KernelCmdLine = strings .TrimSpace (kd .KernelCmdLine )
128+ }
129+ if uefi := doc .VirtualMachine .Chipset .Uefi ; uefi != nil && uefi .BootThis != nil {
130+ uefi .BootThis .OptionalData = strings .TrimSpace (uefi .BootThis .OptionalData )
131+ }
132+ }
133+
134+ // isOnlyKernelCmdLineWhitespaceDiff returns true if the only difference between
135+ // two documents is leading/trailing whitespace in the kernel command line.
136+ func isOnlyKernelCmdLineWhitespaceDiff (legacy , v2 * hcsschema.ComputeSystem ) bool {
137+ legacyCopy := * legacy
138+ v2Copy := * v2
139+ if legacyCopy .VirtualMachine != nil {
140+ vmCopy := * legacyCopy .VirtualMachine
141+ legacyCopy .VirtualMachine = & vmCopy
142+ if vmCopy .Chipset != nil {
143+ chipCopy := * vmCopy .Chipset
144+ legacyCopy .VirtualMachine .Chipset = & chipCopy
145+ if chipCopy .LinuxKernelDirect != nil {
146+ lkdCopy := * chipCopy .LinuxKernelDirect
147+ legacyCopy .VirtualMachine .Chipset .LinuxKernelDirect = & lkdCopy
148+ }
149+ if chipCopy .Uefi != nil {
150+ uefiCopy := * chipCopy .Uefi
151+ legacyCopy .VirtualMachine .Chipset .Uefi = & uefiCopy
152+ if uefiCopy .BootThis != nil {
153+ btCopy := * uefiCopy .BootThis
154+ legacyCopy .VirtualMachine .Chipset .Uefi .BootThis = & btCopy
155+ }
156+ }
157+ }
158+ }
159+ if v2Copy .VirtualMachine != nil {
160+ vmCopy := * v2Copy .VirtualMachine
161+ v2Copy .VirtualMachine = & vmCopy
162+ if vmCopy .Chipset != nil {
163+ chipCopy := * vmCopy .Chipset
164+ v2Copy .VirtualMachine .Chipset = & chipCopy
165+ if chipCopy .LinuxKernelDirect != nil {
166+ lkdCopy := * chipCopy .LinuxKernelDirect
167+ v2Copy .VirtualMachine .Chipset .LinuxKernelDirect = & lkdCopy
168+ }
169+ if chipCopy .Uefi != nil {
170+ uefiCopy := * chipCopy .Uefi
171+ v2Copy .VirtualMachine .Chipset .Uefi = & uefiCopy
172+ if uefiCopy .BootThis != nil {
173+ btCopy := * uefiCopy .BootThis
174+ v2Copy .VirtualMachine .Chipset .Uefi .BootThis = & btCopy
175+ }
176+ }
177+ }
178+ }
179+ normalizeKernelCmdLine (& legacyCopy )
180+ normalizeKernelCmdLine (& v2Copy )
181+ return cmp .Diff (& legacyCopy , & v2Copy ) == ""
182+ }
0 commit comments