From a861f6fd3ebe4fe0b63a1b550e6b99d7525228c0 Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Thu, 4 Mar 2021 15:29:12 -0500 Subject: Podman machine CLI and interface stub Podman machine will be a mac-only command that manages the VM where containers are run. Currently, only the CLI is written and the interface function for the VM management is stub for future developement The podman machine cli is only built on mac builds. Signed-off-by: Ashley Cui --- cmd/podman/machine/create.go | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 cmd/podman/machine/create.go (limited to 'cmd/podman/machine/create.go') diff --git a/cmd/podman/machine/create.go b/cmd/podman/machine/create.go new file mode 100644 index 000000000..35f0677ad --- /dev/null +++ b/cmd/podman/machine/create.go @@ -0,0 +1,132 @@ +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/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + createCmd = &cobra.Command{ + Use: "create [options] NAME", + 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), + Example: `podman machine create myvm`, + ValidArgsFunction: completion.AutocompleteNone, + } +) + +type CreateCLIOptions struct { + CPUS uint64 + Memory uint64 + KernelPath string + Devices []string +} + +var ( + createOpts = CreateCLIOptions{} +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, + Command: createCmd, + Parent: machineCmd, + }) + flags := createCmd.Flags() + + cpusFlagName := "cpus" + flags.Uint64Var( + &createOpts.CPUS, + cpusFlagName, 0, + "Number of CPUs. The default is 0.000 which means no limit", + ) + _ = createCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone) + + memoryFlagName := "memory" + flags.Uint64VarP( + &createOpts.Memory, + memoryFlagName, "m", 0, + "Memory (in MB)", + ) + _ = createCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone) + + kernelPathFlagName := "kernel-path" + flags.StringVar( + &createOpts.KernelPath, + kernelPathFlagName, "", + "Kernel path", + ) + _ = createCmd.RegisterFlagCompletionFunc(kernelPathFlagName, completion.AutocompleteNone) + + deviceFlagName := "device" + flags.StringSliceVar( + &createOpts.Devices, + deviceFlagName, []string{}, + "Add a device", + ) + _ = createCmd.RegisterFlagCompletionFunc(deviceFlagName, completion.AutocompleteDefault) +} + +func create(cmd *cobra.Command, args []string) error { + vmOpts := CreateOptions{ + CPUS: createOpts.CPUS, + Memory: createOpts.Memory, + KernelPath: createOpts.KernelPath, + } + + 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 + } + } + + 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)) + } + } + return vmDevices, nil +} -- cgit v1.2.3-54-g00ecf