diff options
Diffstat (limited to 'cmd/podman/machine/create.go')
-rw-r--r-- | cmd/podman/machine/create.go | 119 |
1 files changed, 47 insertions, 72 deletions
diff --git a/cmd/podman/machine/create.go b/cmd/podman/machine/create.go index 35f0677ad..04c5e9e65 100644 --- a/cmd/podman/machine/create.go +++ b/cmd/podman/machine/create.go @@ -1,12 +1,11 @@ package machine import ( - "fmt" - "strings" - "github.com/containers/common/pkg/completion" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/machine" + "github.com/containers/podman/v3/pkg/machine/qemu" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -17,17 +16,19 @@ var ( Short: "Create a vm", Long: "Create a virtual machine for Podman to run on. Virtual machines are used to run Podman on Macs. ", RunE: create, - Args: cobra.ExactArgs(1), + Args: cobra.NoArgs, Example: `podman machine create myvm`, ValidArgsFunction: completion.AutocompleteNone, } ) type CreateCLIOptions struct { - CPUS uint64 - Memory uint64 - KernelPath string - Devices []string + CPUS uint64 + Memory uint64 + Devices []string + ImagePath string + IgnitionPath string + Name string } var ( @@ -45,7 +46,7 @@ func init() { cpusFlagName := "cpus" flags.Uint64Var( &createOpts.CPUS, - cpusFlagName, 0, + cpusFlagName, 1, "Number of CPUs. The default is 0.000 which means no limit", ) _ = createCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone) @@ -53,80 +54,54 @@ func init() { memoryFlagName := "memory" flags.Uint64VarP( &createOpts.Memory, - memoryFlagName, "m", 0, + memoryFlagName, "m", 2048, "Memory (in MB)", ) _ = createCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone) - kernelPathFlagName := "kernel-path" + deviceFlagName := "name" flags.StringVar( - &createOpts.KernelPath, - kernelPathFlagName, "", - "Kernel path", - ) - _ = createCmd.RegisterFlagCompletionFunc(kernelPathFlagName, completion.AutocompleteNone) - - deviceFlagName := "device" - flags.StringSliceVar( - &createOpts.Devices, - deviceFlagName, []string{}, - "Add a device", + &createOpts.Name, + deviceFlagName, "", + "set vm name", ) _ = createCmd.RegisterFlagCompletionFunc(deviceFlagName, completion.AutocompleteDefault) + + ImagePathFlagName := "image-path" + flags.StringVar(&createOpts.ImagePath, ImagePathFlagName, "", "Path to qcow image") + _ = createCmd.RegisterFlagCompletionFunc(ImagePathFlagName, completion.AutocompleteDefault) + + IgnitionPathFlagName := "ignition-path" + flags.StringVar(&createOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file") + _ = createCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault) } +// TODO should we allow for a users to append to the qemu cmdline? func create(cmd *cobra.Command, args []string) error { - vmOpts := CreateOptions{ - CPUS: createOpts.CPUS, - Memory: createOpts.Memory, - KernelPath: createOpts.KernelPath, + // TODO add ability to create default, not name required + if len(createOpts.Name) < 1 { + return errors.New("required --name not provided") } - - if cmd.Flags().Changed("device") { - devices, err := cmd.Flags().GetStringSlice("device") - if err != nil { - return err - } - vmOpts.Devices, err = parseDevices(devices) - if err != nil { - return err - } + vmOpts := machine.CreateOptions{ + CPUS: createOpts.CPUS, + Memory: createOpts.Memory, + IgnitionPath: createOpts.IgnitionPath, + ImagePath: createOpts.ImagePath, + Name: createOpts.Name, } - - test := new(TestVM) - test.Create(args[0], vmOpts) - - return nil -} - -func parseDevices(devices []string) ([]VMDevices, error) { - vmDevices := make([]VMDevices, 0, len(devices)) - - for _, dev := range devices { - split := strings.Split(dev, ":") - - if len(split) == 1 { - vmDevices = append(vmDevices, VMDevices{ - Path: split[0], - ReadOnly: false, - }) - } else if len(split) == 2 { - var readonly bool - - switch split[1] { - case "ro", "readonly": - readonly = true - default: - return nil, errors.New(fmt.Sprintf("Invalid readonly value: %s", dev)) - } - - vmDevices = append(vmDevices, VMDevices{ - Path: split[0], - ReadOnly: readonly, - }) - } else { - return nil, errors.New(fmt.Sprintf("Invalid device format: %s", dev)) - } + var ( + vm machine.VM + vmType string + err error + ) + switch vmType { + case "foobar": + // do nothing + default: // qemu is the default + vm, err = qemu.NewMachine(vmOpts) + } + if err != nil { + return err } - return vmDevices, nil + return vm.Create(vmOpts) } |