Skip to content

Commit

Permalink
Merge pull request containers#9836 from baude/vmcreateresize
Browse files Browse the repository at this point in the history
Podman machine enhancements
  • Loading branch information
openshift-merge-robot authored Mar 28, 2021
2 parents 4831d41 + 7a79f70 commit b2e7a3e
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 83 deletions.
43 changes: 20 additions & 23 deletions cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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"
)

Expand All @@ -23,17 +24,8 @@ var (
}
)

type InitCLIOptions struct {
CPUS uint64
Memory uint64
Devices []string
ImagePath string
IgnitionPath string
Name string
}

var (
initOpts = InitCLIOptions{}
initOpts = machine.InitOptions{}
defaultMachineName string = "podman-machine-default"
)

Expand All @@ -53,6 +45,15 @@ func init() {
)
_ = initCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone)

diskSizeFlagName := "disk-size"
flags.Uint64Var(
&initOpts.DiskSize,
diskSizeFlagName, 10,
"Disk size in GB",
)

_ = initCmd.RegisterFlagCompletionFunc(diskSizeFlagName, completion.AutocompleteNone)

memoryFlagName := "memory"
flags.Uint64VarP(
&initOpts.Memory,
Expand All @@ -72,28 +73,24 @@ func init() {

// TODO should we allow for a users to append to the qemu cmdline?
func initMachine(cmd *cobra.Command, args []string) error {
initOpts.Name = defaultMachineName
if len(args) > 0 {
initOpts.Name = args[0]
}
vmOpts := machine.InitOptions{
CPUS: initOpts.CPUS,
Memory: initOpts.Memory,
IgnitionPath: initOpts.IgnitionPath,
ImagePath: initOpts.ImagePath,
Name: initOpts.Name,
}
var (
vm machine.VM
vmType string
err error
)
initOpts.Name = defaultMachineName
if len(args) > 0 {
initOpts.Name = args[0]
}
switch vmType {
default: // qemu is the default
vm, err = qemu.NewMachine(vmOpts)
if _, err := qemu.LoadVMByName(initOpts.Name); err == nil {
return errors.Wrap(machine.ErrVMAlreadyExists, initOpts.Name)
}
vm, err = qemu.NewMachine(initOpts)
}
if err != nil {
return err
}
return vm.Init(vmOpts)
return vm.Init(initOpts)
}
28 changes: 16 additions & 12 deletions cmd/podman/machine/remove.go → cmd/podman/machine/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
)

var (
removeCmd = &cobra.Command{
Use: "remove [options] NAME",
rmCmd = &cobra.Command{
Use: "rm [options] [NAME]",
Short: "Remove an existing machine",
Long: "Remove an existing machine ",
RunE: remove,
Args: cobra.ExactArgs(1),
Example: `podman machine remove myvm`,
RunE: rm,
Args: cobra.MaximumNArgs(1),
Example: `podman machine rm myvm`,
ValidArgsFunction: completion.AutocompleteNone,
}
)
Expand All @@ -35,13 +35,13 @@ var (
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: removeCmd,
Command: rmCmd,
Parent: machineCmd,
})

flags := removeCmd.Flags()
flags := rmCmd.Flags()
formatFlagName := "force"
flags.BoolVar(&destoryOptions.Force, formatFlagName, false, "Do not prompt before removeing")
flags.BoolVar(&destoryOptions.Force, formatFlagName, false, "Do not prompt before rming")

keysFlagName := "save-keys"
flags.BoolVar(&destoryOptions.SaveKeys, keysFlagName, false, "Do not delete SSH keys")
Expand All @@ -53,20 +53,24 @@ func init() {
flags.BoolVar(&destoryOptions.SaveImage, imageFlagName, false, "Do not delete the image file")
}

func remove(cmd *cobra.Command, args []string) error {
func rm(cmd *cobra.Command, args []string) error {
var (
err error
vm machine.VM
vmType string
)
vmName := defaultMachineName
if len(args) > 0 && len(args[0]) > 0 {
vmName = args[0]
}
switch vmType {
default:
vm, err = qemu.LoadVMByName(args[0])
vm, err = qemu.LoadVMByName(vmName)
}
if err != nil {
return err
}
confirmationMessage, doIt, err := vm.Remove(args[0], machine.RemoveOptions{})
confirmationMessage, remove, err := vm.Remove(vmName, machine.RemoveOptions{})
if err != nil {
return err
}
Expand All @@ -84,5 +88,5 @@ func remove(cmd *cobra.Command, args []string) error {
return nil
}
}
return doIt()
return remove()
}
12 changes: 8 additions & 4 deletions cmd/podman/machine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

var (
sshCmd = &cobra.Command{
Use: "ssh [options] NAME [COMMAND [ARG ...]]",
Use: "ssh [options] [NAME] [COMMAND [ARG ...]]",
Short: "SSH into a virtual machine",
Long: "SSH into a virtual machine ",
RunE: ssh,
Args: cobra.MinimumNArgs(1),
Args: cobra.MaximumNArgs(1),
Example: `podman machine ssh myvm
podman machine ssh -e myvm echo hello`,

Expand Down Expand Up @@ -48,6 +48,10 @@ func ssh(cmd *cobra.Command, args []string) error {
vm machine.VM
vmType string
)
vmName := defaultMachineName
if len(args) > 0 && len(args[0]) > 1 {
vmName = args[0]
}
sshOpts.Args = args[1:]

// Error if no execute but args given
Expand All @@ -61,10 +65,10 @@ func ssh(cmd *cobra.Command, args []string) error {

switch vmType {
default:
vm, err = qemu.LoadVMByName(args[0])
vm, err = qemu.LoadVMByName(vmName)
}
if err != nil {
return errors.Wrapf(err, "vm %s not found", args[0])
}
return vm.SSH(args[0], sshOpts)
return vm.SSH(vmName, sshOpts)
}
12 changes: 8 additions & 4 deletions cmd/podman/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

var (
startCmd = &cobra.Command{
Use: "start NAME",
Use: "start [NAME]",
Short: "Start an existing machine",
Long: "Start an existing machine ",
RunE: start,
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Example: `podman machine start myvm`,
ValidArgsFunction: completion.AutocompleteNone,
}
Expand All @@ -37,12 +37,16 @@ func start(cmd *cobra.Command, args []string) error {
vm machine.VM
vmType string
)
vmName := defaultMachineName
if len(args) > 0 && len(args[0]) > 0 {
vmName = args[0]
}
switch vmType {
default:
vm, err = qemu.LoadVMByName(args[0])
vm, err = qemu.LoadVMByName(vmName)
}
if err != nil {
return err
}
return vm.Start(args[0], machine.StartOptions{})
return vm.Start(vmName, machine.StartOptions{})
}
12 changes: 8 additions & 4 deletions cmd/podman/machine/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

var (
stopCmd = &cobra.Command{
Use: "stop NAME",
Use: "stop [NAME]",
Short: "Stop an existing machine",
Long: "Stop an existing machine ",
RunE: stop,
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Example: `podman machine stop myvm`,
ValidArgsFunction: completion.AutocompleteNone,
}
Expand All @@ -38,12 +38,16 @@ func stop(cmd *cobra.Command, args []string) error {
vm machine.VM
vmType string
)
vmName := defaultMachineName
if len(args) > 0 && len(args[0]) > 0 {
vmName = args[0]
}
switch vmType {
default:
vm, err = qemu.LoadVMByName(args[0])
vm, err = qemu.LoadVMByName(vmName)
}
if err != nil {
return err
}
return vm.Stop(args[0], machine.StopOptions{})
return vm.Stop(vmName, machine.StopOptions{})
}
2 changes: 1 addition & 1 deletion docs/source/machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Machine


:doc:`init <markdown/podman-machine-init.1>` Initialize a new virtual machine
:doc:`remove <markdown/podman-machine-remove.1>` Remove a virtual machine
:doc:`rm <markdown/podman-machine-rm.1>` Remove a virtual machine
:doc:`ssh <markdown/podman-machine-ssh.1>` SSH into a virtual machine
:doc:`start <markdown/podman-machine-start.1>` Start a virtual machine
:doc:`stop <markdown/podman-machine-stop.1>` Stop a virtual machine
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-machine-init.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ tied to the Linux kernel.

Number of CPUs.

#### **--disk-size**=*number*

Size of the disk for the guest VM in GB.

#### **--ignition-path**

Fully qualified path of the ignition file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
% podman-machine-remove(1)
% podman-machine-rm(1)

## NAME
podman\-machine\-remove - Remove a virtual machine
podman\-machine\-rm - Remove a virtual machine

## SYNOPSIS
**podman machine remove** [*options*] *name*
**podman machine rm** [*options*] [*name*]

## DESCRIPTION

Remove a virtual machine and its related files. What is actually deleted
depends on the virtual machine type. For all virtual machines, the generated
SSH keys and the podman system connection are deleted. The ignition files
generated for that VM are also removeed as is its image file on the filesystem.
generated for that VM are also removed as is its image file on the filesystem.

Users get a display of what will be deleted and are required to confirm unless the option `--force`
is used.
Expand Down Expand Up @@ -45,7 +45,7 @@ deleted.
Remove a VM named "test1"

```
$ podman machine remove test1
$ podman machine rm test1
The following files will be deleted:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-machine-ssh.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
podman\-machine\-ssh - SSH into a virtual machine

## SYNOPSIS
**podman machine ssh** [*options*] *name* [*command* [*arg* ...]]
**podman machine ssh** [*options*] [*name*] [*command* [*arg* ...]]

## DESCRIPTION

Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-machine-start.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
podman\-machine\-start - Start a virtual machine

## SYNOPSIS
**podman machine start** *name*
**podman machine start** [*name*]

## DESCRIPTION

Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-machine-stop.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
podman\-machine\-stop - Stop a virtual machine

## SYNOPSIS
**podman machine stop** *name*
**podman machine stop** [*name*]

## DESCRIPTION

Expand Down
8 changes: 4 additions & 4 deletions docs/source/markdown/podman-machine.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ podman\-machine - Manage Podman's virtual machine
| Command | Man Page | Description |
| ------- | ------------------------------------------------------- | --------------------------------- |
| init | [podman-machine-init(1)](podman-machine-init.1.md) | Initialize a new virtual machine |
| remove | [podman-machine-remove(1)](podman-machine-remove.1.md) | Remove a virtual machine |
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |
| rm | [podman-machine-rm(1)](podman-machine-rm.1.md)| Remove a virtual machine |
| ssh | [podman-machine-ssh(1)](podman-machine-ssh.1.md) | SSH into a virtual machine |
| start | [podman-machine-start(1)](podman-machine-start.1.md) | Start a virtual machine |
| stop | [podman-machine-stop(1)](podman-machine-stop.1.md) | Stop a virtual machine |

## SEE ALSO
podman(1)
Expand Down
14 changes: 8 additions & 6 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ import (
"path/filepath"

"github.com/containers/storage/pkg/homedir"
"github.com/pkg/errors"
)

type InitOptions struct {
Name string
CPUS uint64
Memory uint64
DiskSize uint64
IgnitionPath string
ImagePath string
Username string
URI url.URL
IsDefault bool
//KernelPath string
//Devices []VMDevices
Memory uint64
Name string
URI url.URL
Username string
}

type RemoteConnectionType string

var (
SSHRemoteConnection RemoteConnectionType = "ssh"
DefaultIgnitionUserName = "core"
ErrNoSuchVM = errors.New("VM does not exist")
ErrVMAlreadyExists = errors.New("VM already exists")
)

type Download struct {
Expand Down
Loading

0 comments on commit b2e7a3e

Please sign in to comment.