Skip to content

Commit f81d4a8

Browse files
authored
Merge pull request #672 from q384566678/add-generate-option
Add generate option
2 parents 51e5a89 + 3fc5fcb commit f81d4a8

File tree

5 files changed

+220
-1
lines changed

5 files changed

+220
-1
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ var generateFlags = []cli.Flag{
126126
cli.StringFlag{Name: "solaris-max-shm-memory", Usage: "Specifies the maximum amount of shared memory"},
127127
cli.StringFlag{Name: "solaris-milestone", Usage: "Specifies the SMF FMRI"},
128128
cli.StringFlag{Name: "template", Usage: "base template to use for creating the configuration"},
129+
cli.StringFlag{Name: "vm-hypervisor-parameters", Usage: "specifies an array of parameters to pass to the hypervisor"},
130+
cli.StringFlag{Name: "vm-hypervisor-path", Usage: "specifies the path to the hypervisor binary that manages the container virtual machine"},
131+
cli.StringFlag{Name: "vm-image-format", Usage: "set the format of the container virtual machine root image"},
132+
cli.StringFlag{Name: "vm-image-path", Usage: "set path to the container virtual machine root image"},
133+
cli.StringFlag{Name: "vm-kernel-initrd", Usage: "set path to an initial ramdisk to be used by the container virtual machine"},
134+
cli.StringFlag{Name: "vm-kernel-parameters", Usage: "specifies an array of parameters to pass to the kernel"},
135+
cli.StringFlag{Name: "vm-kernel-path", Usage: "set path to the kernel used to boot the container virtual machine"},
136+
cli.StringSliceFlag{Name: "windows-devices", Usage: "specifies a list of devices to be mapped into the container"},
129137
cli.StringFlag{Name: "windows-hyperv-utilityVMPath", Usage: "specifies the path to the image used for the utility VM"},
130138
cli.BoolFlag{Name: "windows-ignore-flushes-during-boot", Usage: "ignore flushes during boot"},
131139
cli.StringSliceFlag{Name: "windows-layer-folders", Usage: "specifies a list of layer folders the container image relies on"},
@@ -862,6 +870,44 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
862870
g.SetSolarisMilestone(context.String("solaris-milestone"))
863871
}
864872

873+
if context.IsSet("vm-hypervisor-path") {
874+
if err := g.SetVMHypervisorPath(context.String("vm-hypervisor-path")); err != nil {
875+
return err
876+
}
877+
}
878+
879+
if context.IsSet("vm-hypervisor-parameters") {
880+
g.SetVMHypervisorParameters(context.String("vm-hypervisor-parameters"))
881+
}
882+
883+
if context.IsSet("vm-kernel-path") {
884+
if err := g.SetVMKernelPath(context.String("vm-kernel-path")); err != nil {
885+
return err
886+
}
887+
}
888+
889+
if context.IsSet("vm-kernel-parameters") {
890+
g.SetVMKernelParameters(context.String("vm-kernel-parameters"))
891+
}
892+
893+
if context.IsSet("vm-kernel-initrd") {
894+
if err := g.SetVMKernelInitRD(context.String("vm-kernel-initrd")); err != nil {
895+
return err
896+
}
897+
}
898+
899+
if context.IsSet("vm-image-path") {
900+
if err := g.SetVMImagePath(context.String("vm-image-path")); err != nil {
901+
return err
902+
}
903+
}
904+
905+
if context.IsSet("vm-image-format") {
906+
if err := g.SetVMImageFormat(context.String("vm-image-format")); err != nil {
907+
return err
908+
}
909+
}
910+
865911
if context.IsSet("windows-hyperv-utilityVMPath") {
866912
g.SetWindowsHypervUntilityVMPath(context.String("windows-hyperv-utilityVMPath"))
867913
}
@@ -877,6 +923,19 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
877923
}
878924
}
879925

926+
if context.IsSet("windows-devices") {
927+
devices := context.StringSlice("windows-devices")
928+
for _, device := range devices {
929+
id, idType, err := parseWindowsDevices(device)
930+
if err != nil {
931+
return err
932+
}
933+
if err := g.AddWindowsDevices(id, idType); err != nil {
934+
return err
935+
}
936+
}
937+
}
938+
880939
if context.IsSet("windows-network") {
881940
network := context.String("windows-network")
882941
tmpNetwork := rspec.WindowsNetwork{}
@@ -1028,6 +1087,17 @@ func parseNamespace(ns string) (string, string, error) {
10281087
return nsType, nsPath, nil
10291088
}
10301089

1090+
func parseWindowsDevices(device string) (string, string, error) {
1091+
parts := strings.Split(device, ":")
1092+
if len(parts) != 2 {
1093+
return "", "", fmt.Errorf("invalid windows device value: %s", device)
1094+
}
1095+
1096+
id := parts[0]
1097+
idType := parts[1]
1098+
return id, idType, nil
1099+
}
1100+
10311101
var deviceType = map[string]bool{
10321102
"b": true, // a block (buffered) special file
10331103
"c": true, // a character special file

completions/bash/oci-runtime-tool

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,16 @@ _oci-runtime-tool_generate() {
396396
--solaris-capped-memory-swap
397397
--solaris-limitpriv1
398398
--solaris-max-shm-memory
399-
--solaris-milestone
399+
--solaris-milestone
400400
--template
401+
--vm-hypervisor-parameters
402+
--vm-hypervisor-path
403+
--vm-image-format
404+
--vm-image-path
405+
--vm-kernel-initrd
406+
--vm-kernel-parameters
407+
--vm-kernel-path
408+
--windows-devices
401409
--windows-hyperv-utilityVMPath
402410
--windows-layer-folders
403411
--windows-network

generate/config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,31 @@ func (g *Generator) initConfigWindowsResourcesMemory() {
178178
g.Config.Windows.Resources.Memory = &rspec.WindowsMemoryResources{}
179179
}
180180
}
181+
182+
func (g *Generator) initConfigVM() {
183+
g.initConfig()
184+
if g.Config.VM == nil {
185+
g.Config.VM = &rspec.VM{}
186+
}
187+
}
188+
189+
func (g *Generator) initConfigVMHypervisor() {
190+
g.initConfigVM()
191+
if &g.Config.VM.Hypervisor == nil {
192+
g.Config.VM.Hypervisor = rspec.VMHypervisor{}
193+
}
194+
}
195+
196+
func (g *Generator) initConfigVMKernel() {
197+
g.initConfigVM()
198+
if &g.Config.VM.Kernel == nil {
199+
g.Config.VM.Kernel = rspec.VMKernel{}
200+
}
201+
}
202+
203+
func (g *Generator) initConfigVMImage() {
204+
g.initConfigVM()
205+
if &g.Config.VM.Image == nil {
206+
g.Config.VM.Image = rspec.VMImage{}
207+
}
208+
}

generate/generate.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,74 @@ func (g *Generator) SetSolarisMilestone(milestone string) {
15331533
g.Config.Solaris.Milestone = milestone
15341534
}
15351535

1536+
// SetVMHypervisorPath sets g.Config.VM.Hypervisor.Path
1537+
func (g *Generator) SetVMHypervisorPath(path string) error {
1538+
if !strings.HasPrefix(path, "/") {
1539+
return fmt.Errorf("hypervisorPath %v is not an absolute path", path)
1540+
}
1541+
g.initConfigVMHypervisor()
1542+
g.Config.VM.Hypervisor.Path = path
1543+
return nil
1544+
}
1545+
1546+
// SetVMHypervisorParameters sets g.Config.VM.Hypervisor.Parameters
1547+
func (g *Generator) SetVMHypervisorParameters(parameters string) {
1548+
g.initConfigVMHypervisor()
1549+
g.Config.VM.Hypervisor.Parameters = parameters
1550+
}
1551+
1552+
// SetVMKernelPath sets g.Config.VM.Kernel.Path
1553+
func (g *Generator) SetVMKernelPath(path string) error {
1554+
if !strings.HasPrefix(path, "/") {
1555+
return fmt.Errorf("kernelPath %v is not an absolute path", path)
1556+
}
1557+
g.initConfigVMKernel()
1558+
g.Config.VM.Kernel.Path = path
1559+
return nil
1560+
}
1561+
1562+
// SetVMKernelParameters sets g.Config.VM.Kernel.Parameters
1563+
func (g *Generator) SetVMKernelParameters(parameters string) {
1564+
g.initConfigVMKernel()
1565+
g.Config.VM.Kernel.Parameters = parameters
1566+
}
1567+
1568+
// SetVMKernelInitRD sets g.Config.VM.Kernel.InitRD
1569+
func (g *Generator) SetVMKernelInitRD(initrd string) error {
1570+
if !strings.HasPrefix(initrd, "/") {
1571+
return fmt.Errorf("kernelInitrd %v is not an absolute path", initrd)
1572+
}
1573+
g.initConfigVMKernel()
1574+
g.Config.VM.Kernel.InitRD = initrd
1575+
return nil
1576+
}
1577+
1578+
// SetVMImagePath sets g.Config.VM.Image.Path
1579+
func (g *Generator) SetVMImagePath(path string) error {
1580+
if !strings.HasPrefix(path, "/") {
1581+
return fmt.Errorf("imagePath %v is not an absolute path", path)
1582+
}
1583+
g.initConfigVMImage()
1584+
g.Config.VM.Image.Path = path
1585+
return nil
1586+
}
1587+
1588+
// SetVMImageFormat sets g.Config.VM.Image.Format
1589+
func (g *Generator) SetVMImageFormat(format string) error {
1590+
switch format {
1591+
case "raw":
1592+
case "qcow2":
1593+
case "vdi":
1594+
case "vmdk":
1595+
case "vhd":
1596+
default:
1597+
return fmt.Errorf("Commonly supported formats are: raw, qcow2, vdi, vmdk, vhd")
1598+
}
1599+
g.initConfigVMImage()
1600+
g.Config.VM.Image.Format = format
1601+
return nil
1602+
}
1603+
15361604
// SetWindowsHypervUntilityVMPath sets g.Config.Windows.HyperV.UtilityVMPath.
15371605
func (g *Generator) SetWindowsHypervUntilityVMPath(path string) {
15381606
g.initConfigWindowsHyperV()
@@ -1551,6 +1619,27 @@ func (g *Generator) AddWindowsLayerFolders(folder string) {
15511619
g.Config.Windows.LayerFolders = append(g.Config.Windows.LayerFolders, folder)
15521620
}
15531621

1622+
// AddWindowsDevices adds or sets g.Config.Windwos.Devices
1623+
func (g *Generator) AddWindowsDevices(id, idType string) error {
1624+
if idType != "class" {
1625+
return fmt.Errorf("Invalid idType value: %s. Windows only supports a value of class", idType)
1626+
}
1627+
device := rspec.WindowsDevice{
1628+
ID: id,
1629+
IDType: idType,
1630+
}
1631+
1632+
g.initConfigWindows()
1633+
for i, device := range g.Config.Windows.Devices {
1634+
if device.ID == id {
1635+
g.Config.Windows.Devices[i].IDType = idType
1636+
return nil
1637+
}
1638+
}
1639+
g.Config.Windows.Devices = append(g.Config.Windows.Devices, device)
1640+
return nil
1641+
}
1642+
15541643
// SetWindowsNetwork sets g.Config.Windows.Network.
15551644
func (g *Generator) SetWindowsNetwork(network rspec.WindowsNetwork) {
15561645
g.initConfigWindows()

man/oci-runtime-tool-generate.1.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,30 @@ read the configuration from `config.json`.
505505
Additional options will only adjust the relevant portions of your template.
506506
Templates are not validated for correctness, so the user should ensure that they are correct.
507507

508+
**--vm-hypervisor-parameters**=""
509+
Specifies an array of parameters to pass to the hypervisor.
510+
511+
**--vm-hypervisor-path**=PATH
512+
Specifies the path to the hypervisor binary that manages the container virtual machine.
513+
514+
**--vm-image-format**=""
515+
Set the format of the container virtual machine root image.
516+
517+
**--vm-image-path**=PATH
518+
Set path to the container virtual machine root image.
519+
520+
**--vm-kernel-initrd**=""
521+
Set path to an initial ramdisk to be used by the container virtual machine.
522+
523+
**--vm-kernel-parameters**=""
524+
Specifies an array of parameters to pass to the kernel.
525+
526+
**--vm-kernel-path**=PATH
527+
Set path to the kernel used to boot the container virtual machine.
528+
529+
**--windows-devices**=[]
530+
Specifies devices that MUST be available in the container.
531+
508532
**--windows-hyperv-utilityVMPath**=PATH
509533
Specifies the path to the image used for the utility VM.
510534

0 commit comments

Comments
 (0)