Skip to content

Commit 9c71939

Browse files
committed
refactor common constants into vmutils package
Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
1 parent b7d75a1 commit 9c71939

17 files changed

Lines changed: 164 additions & 134 deletions

internal/devices/assigned_devices.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/Microsoft/hcsshim/internal/cmd"
1313
"github.com/Microsoft/hcsshim/internal/log"
1414
"github.com/Microsoft/hcsshim/internal/uvm"
15+
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
1516
"github.com/pkg/errors"
1617
)
1718

@@ -40,7 +41,7 @@ func AddDevice(ctx context.Context, vm *uvm.UtilityVM, idType, deviceID string,
4041
}
4142
}()
4243

43-
if uvm.IsValidDeviceType(idType) {
44+
if vmutils.IsValidDeviceType(idType) {
4445
vpci, err = vm.AssignDevice(ctx, deviceID, index, "")
4546
if err != nil {
4647
return vpci, nil, errors.Wrapf(err, "failed to assign device %s of type %s to pod %s", deviceID, idType, vm.ID())

internal/hcsoci/devices.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/Microsoft/hcsshim/internal/oci"
1919
"github.com/Microsoft/hcsshim/internal/resources"
2020
"github.com/Microsoft/hcsshim/internal/uvm"
21+
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
2122
"github.com/Microsoft/hcsshim/osversion"
2223
"github.com/Microsoft/hcsshim/pkg/annotations"
2324
)
@@ -174,7 +175,7 @@ func handleAssignedDevicesLCOW(
174175

175176
// assign device into UVM and create corresponding spec windows devices
176177
for _, d := range specDevs {
177-
if !uvm.IsValidDeviceType(d.IDType) {
178+
if !vmutils.IsValidDeviceType(d.IDType) {
178179
return resultDevs, closers, errors.Errorf("specified device %s has unsupported type %s", d.ID, d.IDType)
179180
}
180181

internal/oci/uvm.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111

1212
runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
13+
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
1314
"github.com/Microsoft/hcsshim/pkg/annotations"
1415
"github.com/opencontainers/runtime-spec/specs-go"
1516
"github.com/sirupsen/logrus"
@@ -156,7 +157,7 @@ func handleAnnotationBootFilesPath(ctx context.Context, a map[string]string, lop
156157
func handleAnnotationKernelDirectBoot(ctx context.Context, a map[string]string, lopts *uvm.OptionsLCOW) {
157158
lopts.KernelDirect = ParseAnnotationsBool(ctx, a, annotations.KernelDirectBoot, lopts.KernelDirect)
158159
if !lopts.KernelDirect {
159-
lopts.KernelFile = uvm.KernelFile
160+
lopts.KernelFile = vmutils.KernelFile
160161
}
161162
}
162163

@@ -166,9 +167,9 @@ func handleAnnotationPreferredRootFSType(ctx context.Context, a map[string]strin
166167
lopts.PreferredRootFSType = parseAnnotationsPreferredRootFSType(ctx, a, annotations.PreferredRootFSType, lopts.PreferredRootFSType)
167168
switch lopts.PreferredRootFSType {
168169
case uvm.PreferredRootFSTypeInitRd:
169-
lopts.RootFSFile = uvm.InitrdFile
170+
lopts.RootFSFile = vmutils.InitrdFile
170171
case uvm.PreferredRootFSTypeVHD:
171-
lopts.RootFSFile = uvm.VhdFile
172+
lopts.RootFSFile = vmutils.VhdFile
172173
}
173174
}
174175

@@ -206,7 +207,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
206207
// VPMem not supported by the enlightened kernel for SNP so set count to zero.
207208
lopts.VPMemDeviceCount = 0
208209
// set the default GuestState filename.
209-
lopts.GuestStateFilePath = uvm.GuestStateFile
210+
lopts.GuestStateFilePath = vmutils.DefaultGuestStateFile
210211
lopts.KernelBootOptions = ""
211212
lopts.AllowOvercommit = false
212213
lopts.SecurityPolicyEnabled = true
@@ -219,7 +220,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
219220
// The default behavior is to use kernel.vmgs and a rootfs-verity.vhd file with Merkle tree appended to ext4 filesystem.
220221
lopts.PreferredRootFSType = uvm.PreferredRootFSTypeNA
221222
lopts.RootFSFile = ""
222-
lopts.DmVerityRootFsVhd = uvm.DefaultDmVerityRootfsVhd
223+
lopts.DmVerityRootFsVhd = vmutils.DefaultDmVerityRootfsVhd
223224
lopts.DmVerityMode = true
224225
}
225226

@@ -286,7 +287,7 @@ func parseDevices(ctx context.Context, specWindows *specs.Windows) []uvm.VPCIDev
286287
extraDevices := []uvm.VPCIDeviceID{}
287288
for _, d := range specWindows.Devices {
288289
pciID, index := devices.GetDeviceInfoFromPath(d.ID)
289-
if uvm.IsValidDeviceType(d.IDType) {
290+
if vmutils.IsValidDeviceType(d.IDType) {
290291
key := uvm.NewVPCIDeviceID(pciID, index)
291292
extraDevices = append(extraDevices, key)
292293
} else {

internal/tools/uvmboot/lcow.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
197197
}
198198
if c.IsSet(kernelFileArgName) {
199199
switch strings.ToLower(c.String(kernelFileArgName)) {
200-
case uvm.KernelFile:
201-
options.KernelFile = uvm.KernelFile
202-
case uvm.UncompressedKernelFile:
203-
options.KernelFile = uvm.UncompressedKernelFile
200+
case vmutils.KernelFile:
201+
options.KernelFile = vmutils.KernelFile
202+
case vmutils.UncompressedKernelFile:
203+
options.KernelFile = vmutils.UncompressedKernelFile
204204
default:
205205
return nil, unrecognizedError(c.String(kernelFileArgName), kernelFileArgName)
206206
}
@@ -213,10 +213,10 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
213213
if c.IsSet(rootFSTypeArgName) {
214214
switch strings.ToLower(c.String(rootFSTypeArgName)) {
215215
case "initrd":
216-
options.RootFSFile = uvm.InitrdFile
216+
options.RootFSFile = vmutils.InitrdFile
217217
options.PreferredRootFSType = uvm.PreferredRootFSTypeInitRd
218218
case "vhd":
219-
options.RootFSFile = uvm.VhdFile
219+
options.RootFSFile = vmutils.VhdFile
220220
options.PreferredRootFSType = uvm.PreferredRootFSTypeVHD
221221
case "none":
222222
options.RootFSFile = ""
@@ -275,7 +275,7 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
275275
options.SecurityPolicyEnforcer = c.String(securityPolicyEnforcerArgName)
276276
}
277277
if c.IsSet(securityHardwareFlag) {
278-
options.GuestStateFilePath = uvm.GuestStateFile
278+
options.GuestStateFilePath = vmutils.DefaultGuestStateFile
279279
options.SecurityPolicyEnabled = true
280280
options.AllowOvercommit = false
281281
}

internal/uvm/constants.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,9 @@ package uvm
55
import (
66
"errors"
77

8-
"github.com/Microsoft/hcsshim/internal/memory"
98
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
109
)
1110

12-
const (
13-
// MaxVPMEMCount is the maximum number of VPMem devices that may be added to an LCOW
14-
// utility VM
15-
MaxVPMEMCount = 128
16-
17-
// DefaultVPMEMCount is the default number of VPMem devices that may be added to an LCOW
18-
// utility VM if the create request doesn't specify how many.
19-
DefaultVPMEMCount = 64
20-
21-
// DefaultVPMemSizeBytes is the default size of a VPMem device if the create request
22-
// doesn't specify.
23-
DefaultVPMemSizeBytes = 4 * memory.GiB // 4GB
24-
)
25-
2611
var (
2712
errNotSupported = errors.New("not supported")
2813
errBadUVMOpts = errors.New("UVM options incorrect")

internal/uvm/cpugroups.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import (
1010
"github.com/Microsoft/hcsshim/internal/cpugroup"
1111
"github.com/Microsoft/hcsshim/internal/hcs/resourcepaths"
1212
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
13-
"github.com/Microsoft/hcsshim/osversion"
1413
)
1514

16-
var errCPUGroupCreateNotSupported = fmt.Errorf("cpu group assignment on create requires a build of %d or higher", osversion.V21H1)
17-
1815
// ReleaseCPUGroup unsets the cpugroup from the VM
1916
func (uvm *UtilityVM) ReleaseCPUGroup(ctx context.Context) error {
2017
if err := uvm.unsetCPUGroup(ctx); err != nil {

internal/uvm/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func verifyOptions(_ context.Context, options interface{}) error {
169169
if opts.SCSIControllerCount > MaxSCSIControllers {
170170
return fmt.Errorf("SCSI controller count can't be more than %d", MaxSCSIControllers)
171171
}
172-
if opts.VPMemDeviceCount > MaxVPMEMCount {
173-
return fmt.Errorf("VPMem device count cannot be greater than %d", MaxVPMEMCount)
172+
if opts.VPMemDeviceCount > vmutils.MaxVPMEMCount {
173+
return fmt.Errorf("VPMem device count cannot be greater than %d", vmutils.MaxVPMEMCount)
174174
}
175175
if opts.VPMemDeviceCount > 0 {
176176
if opts.VPMemSizeBytes%4096 != 0 {

internal/uvm/create_lcow.go

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,6 @@ const (
6060
PreferredRootFSTypeInitRd PreferredRootFSType = iota
6161
PreferredRootFSTypeVHD
6262
PreferredRootFSTypeNA
63-
64-
entropyVsockPort = 1
65-
linuxLogVsockPort = 109
66-
)
67-
68-
const (
69-
// InitrdFile is the default file name for an initrd.img used to boot LCOW.
70-
InitrdFile = "initrd.img"
71-
// VhdFile is the default file name for a rootfs.vhd used to boot LCOW.
72-
VhdFile = "rootfs.vhd"
73-
// DefaultDmVerityRootfsVhd is the default file name for a dmverity_rootfs.vhd,
74-
// which is mounted by the GuestStateFile during boot and used as the root file
75-
// system when booting in the SNP case. Similar to layer VHDs, the Merkle tree
76-
// is appended after ext4 filesystem ends.
77-
DefaultDmVerityRootfsVhd = "rootfs.vhd"
78-
// KernelFile is the default file name for a kernel used to boot LCOW.
79-
KernelFile = "kernel"
80-
// UncompressedKernelFile is the default file name for an uncompressed
81-
// kernel used to boot LCOW with KernelDirect.
82-
UncompressedKernelFile = "vmlinux"
83-
// GuestStateFile is the default file name for a vmgs (VM Guest State) file
84-
// which contains the kernel and kernel command which mounts DmVerityVhdFile
85-
// when booting in the SNP case.
86-
GuestStateFile = "kernel.vmgs"
87-
// UVMReferenceInfoFile is the default file name for a COSE_Sign1
88-
// reference UVM info, which can be made available to workload containers
89-
// and can be used for validation purposes.
90-
UVMReferenceInfoFile = "reference_info.cose"
9163
)
9264

9365
type ConfidentialLCOWOptions struct {
@@ -143,17 +115,17 @@ func NewDefaultOptionsLCOW(id, owner string) *OptionsLCOW {
143115
kernelDirectSupported := osversion.Build() >= 18286
144116
opts := &OptionsLCOW{
145117
Options: newDefaultOptions(id, owner),
146-
KernelFile: KernelFile,
118+
KernelFile: vmutils.KernelFile,
147119
KernelDirect: kernelDirectSupported,
148-
RootFSFile: InitrdFile,
120+
RootFSFile: vmutils.InitrdFile,
149121
KernelBootOptions: "",
150122
UseGuestConnection: true,
151123
ExecCommandLine: fmt.Sprintf("/bin/gcs -v4 -log-format json -loglevel %s", logrus.StandardLogger().Level.String()),
152124
ForwardStdout: false,
153125
ForwardStderr: true,
154126
OutputHandlerCreator: vmutils.ParseGCSLogrus,
155-
VPMemDeviceCount: DefaultVPMEMCount,
156-
VPMemSizeBytes: DefaultVPMemSizeBytes,
127+
VPMemDeviceCount: vmutils.DefaultVPMEMCount,
128+
VPMemSizeBytes: vmutils.DefaultVPMemSizeBytes,
157129
VPMemNoMultiMapping: osversion.Get().Build < osversion.V19H1,
158130
PreferredRootFSType: PreferredRootFSTypeInitRd,
159131
EnableColdDiscardHint: false,
@@ -163,7 +135,7 @@ func NewDefaultOptionsLCOW(id, owner string) *OptionsLCOW {
163135
ConfidentialLCOWOptions: &ConfidentialLCOWOptions{
164136
ConfidentialCommonOptions: &ConfidentialCommonOptions{
165137
SecurityPolicyEnabled: false,
166-
UVMReferenceInfoFile: UVMReferenceInfoFile,
138+
UVMReferenceInfoFile: vmutils.DefaultUVMReferenceInfoFile,
167139
},
168140
},
169141
}
@@ -196,29 +168,29 @@ func (opts *OptionsLCOW) UpdateBootFilesPath(ctx context.Context, path string) {
196168

197169
opts.BootFilesPath = path
198170

199-
if _, err := os.Stat(filepath.Join(opts.BootFilesPath, VhdFile)); err == nil {
171+
if _, err := os.Stat(filepath.Join(opts.BootFilesPath, vmutils.VhdFile)); err == nil {
200172
// We have a rootfs.vhd in the boot files path. Use it over an initrd.img
201-
opts.RootFSFile = VhdFile
173+
opts.RootFSFile = vmutils.VhdFile
202174
opts.PreferredRootFSType = PreferredRootFSTypeVHD
203175

204176
log.G(ctx).WithFields(logrus.Fields{
205177
logfields.UVMID: opts.ID,
206-
VhdFile: filepath.Join(opts.BootFilesPath, VhdFile),
207-
}).Debug("updated LCOW root filesystem to " + VhdFile)
178+
vmutils.VhdFile: filepath.Join(opts.BootFilesPath, vmutils.VhdFile),
179+
}).Debug("updated LCOW root filesystem to " + vmutils.VhdFile)
208180
}
209181

210182
if opts.KernelDirect {
211183
// KernelDirect supports uncompressed kernel if the kernel is present.
212184
// Default to uncompressed if on box. NOTE: If `kernel` is already
213185
// uncompressed and simply named 'kernel' it will still be used
214186
// uncompressed automatically.
215-
if _, err := os.Stat(filepath.Join(opts.BootFilesPath, UncompressedKernelFile)); err == nil {
216-
opts.KernelFile = UncompressedKernelFile
187+
if _, err := os.Stat(filepath.Join(opts.BootFilesPath, vmutils.UncompressedKernelFile)); err == nil {
188+
opts.KernelFile = vmutils.UncompressedKernelFile
217189

218190
log.G(ctx).WithFields(logrus.Fields{
219-
logfields.UVMID: opts.ID,
220-
UncompressedKernelFile: filepath.Join(opts.BootFilesPath, UncompressedKernelFile),
221-
}).Debug("updated LCOW kernel file to " + UncompressedKernelFile)
191+
logfields.UVMID: opts.ID,
192+
vmutils.UncompressedKernelFile: filepath.Join(opts.BootFilesPath, vmutils.UncompressedKernelFile),
193+
}).Debug("updated LCOW kernel file to " + vmutils.UncompressedKernelFile)
222194
}
223195
}
224196
}
@@ -242,7 +214,7 @@ func fetchProcessor(ctx context.Context, opts *OptionsLCOW, uvm *UtilityVM) (*hc
242214
// We can set a cpu group for the VM at creation time in recent builds.
243215
if opts.CPUGroupID != "" {
244216
if osversion.Build() < osversion.V21H1 {
245-
return nil, errCPUGroupCreateNotSupported
217+
return nil, vmutils.ErrCPUGroupCreateNotSupported
246218
}
247219
processor.CpuGroup = &hcsschema.CpuGroup{Id: opts.CPUGroupID}
248220
}
@@ -363,7 +335,7 @@ func makeLCOWVMGSDoc(ctx context.Context, opts *OptionsLCOW, uvm *UtilityVM) (_
363335
}
364336
}()
365337

366-
dmVerityRootFsFullPath := filepath.Join(opts.BundleDirectory, DefaultDmVerityRootfsVhd)
338+
dmVerityRootFsFullPath := filepath.Join(opts.BundleDirectory, vmutils.DefaultDmVerityRootfsVhd)
367339
if err := copyfile.CopyFile(ctx, dmVerityRootfsTemplatePath, dmVerityRootFsFullPath, true); err != nil {
368340
return nil, fmt.Errorf("failed to copy DM Verity rootfs template file: %w", err)
369341
}
@@ -425,7 +397,7 @@ func makeLCOWVMGSDoc(ctx context.Context, opts *OptionsLCOW, uvm *UtilityVM) (_
425397
// entropyVsockPort - 1 is the entropy port,
426398
// linuxLogVsockPort - 109 used by vsockexec to log stdout/stderr logging,
427399
// 0x40000000 + 1 (LinuxGcsVsockPort + 1) is the bridge (see guestconnectiuon.go)
428-
hvSockets := []uint32{entropyVsockPort, linuxLogVsockPort, prot.LinuxGcsVsockPort, prot.LinuxGcsVsockPort + 1}
400+
hvSockets := []uint32{vmutils.LinuxEntropyVsockPort, vmutils.LinuxLogVsockPort, prot.LinuxGcsVsockPort, prot.LinuxGcsVsockPort + 1}
429401
hvSockets = append(hvSockets, opts.ExtraVSockPorts...)
430402
for _, whichSocket := range hvSockets {
431403
key := winio.VsockServiceID(whichSocket).String()
@@ -836,18 +808,18 @@ func makeLCOWDoc(ctx context.Context, opts *OptionsLCOW, uvm *UtilityVM) (_ *hcs
836808
}
837809

838810
// Inject initial entropy over vsock during init launch.
839-
entropyArgs := fmt.Sprintf("-e %d", entropyVsockPort)
811+
entropyArgs := fmt.Sprintf("-e %d", vmutils.LinuxEntropyVsockPort)
840812

841813
// With default options, run GCS with stderr pointing to the vsock port
842814
// created below in order to forward guest logs to logrus.
843815
execCmdArgs := "/bin/vsockexec"
844816

845817
if opts.ForwardStdout {
846-
execCmdArgs += fmt.Sprintf(" -o %d", linuxLogVsockPort)
818+
execCmdArgs += fmt.Sprintf(" -o %d", vmutils.LinuxLogVsockPort)
847819
}
848820

849821
if opts.ForwardStderr {
850-
execCmdArgs += fmt.Sprintf(" -e %d", linuxLogVsockPort)
822+
execCmdArgs += fmt.Sprintf(" -e %d", vmutils.LinuxLogVsockPort)
851823
}
852824

853825
if opts.DisableTimeSyncService {
@@ -994,7 +966,7 @@ func CreateLCOW(ctx context.Context, opts *OptionsLCOW) (_ *UtilityVM, err error
994966
}
995967

996968
// Create a socket to inject entropy during boot.
997-
uvm.entropyListener, err = uvm.listenVsock(entropyVsockPort)
969+
uvm.entropyListener, err = uvm.listenVsock(vmutils.LinuxEntropyVsockPort)
998970
if err != nil {
999971
return nil, err
1000972
}
@@ -1004,7 +976,7 @@ func CreateLCOW(ctx context.Context, opts *OptionsLCOW) (_ *UtilityVM, err error
1004976
if opts.ForwardStdout || opts.ForwardStderr {
1005977
uvm.outputHandler = opts.OutputHandlerCreator(opts.ID)
1006978
uvm.outputProcessingDone = make(chan struct{})
1007-
uvm.outputListener, err = uvm.listenVsock(linuxLogVsockPort)
979+
uvm.outputListener, err = uvm.listenVsock(vmutils.LinuxLogVsockPort)
1008980
if err != nil {
1009981
return nil, err
1010982
}

internal/uvm/create_wcow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func prepareCommonConfigDoc(ctx context.Context, uvm *UtilityVM, opts *OptionsWC
238238
// We can set a cpu group for the VM at creation time in recent builds.
239239
if opts.CPUGroupID != "" {
240240
if osversion.Build() < osversion.V21H1 {
241-
return nil, errCPUGroupCreateNotSupported
241+
return nil, vmutils.ErrCPUGroupCreateNotSupported
242242
}
243243
processor.CpuGroup = &hcsschema.CpuGroup{Id: opts.CPUGroupID}
244244
}

internal/uvm/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type UtilityVM struct {
8383
vpmemMaxCount uint32 // The max number of VPMem devices.
8484
vpmemMaxSizeBytes uint64 // The max size of the layer in bytes per vPMem device.
8585
vpmemMultiMapping bool // Enable mapping multiple VHDs onto a single VPMem device
86-
vpmemDevicesDefault [MaxVPMEMCount]*vPMemInfoDefault
87-
vpmemDevicesMultiMapped [MaxVPMEMCount]*vPMemInfoMulti
86+
vpmemDevicesDefault [vmutils.MaxVPMEMCount]*vPMemInfoDefault
87+
vpmemDevicesMultiMapped [vmutils.MaxVPMEMCount]*vPMemInfoMulti
8888

8989
// SCSI devices that are mapped into a Windows or Linux utility VM
9090
SCSIManager *scsi.Manager

0 commit comments

Comments
 (0)