Skip to content

Commit

Permalink
Rename podman machine create to init and clean up
Browse files Browse the repository at this point in the history
Rename podman machine create to init because we're initing a VM, not
really creating it
Wire up CPUs flag
Suppress QEMU GUI from popping up when not in debug mode

[NO TESTS NEEDED]
Signed-off-by: Ashley Cui <[email protected]>
  • Loading branch information
ashley-cui committed Mar 25, 2021
1 parent db35674 commit f663857
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 125 deletions.
99 changes: 0 additions & 99 deletions cmd/podman/machine/create.go

This file was deleted.

99 changes: 99 additions & 0 deletions cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// +build amd64,linux amd64,darwin arm64,darwin

package machine

import (
"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/spf13/cobra"
)

var (
initCmd = &cobra.Command{
Use: "init [options] [NAME]",
Short: "initialize a vm",
Long: "initialize a virtual machine for Podman to run on. Virtual machines are used to run Podman.",
RunE: initMachine,
Args: cobra.MaximumNArgs(1),
Example: `podman machine init myvm`,
ValidArgsFunction: completion.AutocompleteNone,
}
)

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

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

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: initCmd,
Parent: machineCmd,
})
flags := initCmd.Flags()

cpusFlagName := "cpus"
flags.Uint64Var(
&initOpts.CPUS,
cpusFlagName, 1,
"Number of CPUs. The default is 1.",
)
_ = initCmd.RegisterFlagCompletionFunc(cpusFlagName, completion.AutocompleteNone)

memoryFlagName := "memory"
flags.Uint64VarP(
&initOpts.Memory,
memoryFlagName, "m", 2048,
"Memory (in MB)",
)
_ = initCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone)

ImagePathFlagName := "image-path"
flags.StringVar(&initOpts.ImagePath, ImagePathFlagName, "", "Path to qcow image")
_ = initCmd.RegisterFlagCompletionFunc(ImagePathFlagName, completion.AutocompleteDefault)

IgnitionPathFlagName := "ignition-path"
flags.StringVar(&initOpts.IgnitionPath, IgnitionPathFlagName, "", "Path to ignition file")
_ = initCmd.RegisterFlagCompletionFunc(IgnitionPathFlagName, completion.AutocompleteDefault)
}

// 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
)
switch vmType {
default: // qemu is the default
vm, err = qemu.NewMachine(vmOpts)
}
if err != nil {
return err
}
return vm.Init(vmOpts)
}
2 changes: 1 addition & 1 deletion docs/source/machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Machine
======


:doc:`create <markdown/podman-machine-create.1>` Create a new virtual 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:`ssh <markdown/podman-machine-ssh.1>` SSH into a virtual machine
:doc:`start <markdown/podman-machine-start.1>` Start a virtual machine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
% podman-machine-create(1)
% podman-machine-init(1)

## NAME
podman\-machine\-create - Create a new virtual machine
podman\-machine\-init - Initialize a new virtual machine

## SYNOPSIS
**podman machine create** [*options*] [*name*]
**podman machine init** [*options*] [*name*]

## DESCRIPTION

Creates a new virtual machine for Podman.
Initialize a new virtual machine for Podman.

Podman on MacOS requires a virtual machine. This is because containers are Linux -
containers do not run on any other OS because containers' core functionality are
tied to the Linux kernel.

**podman machine create** creates a new Linux virtual machine where containers are run.
**podman machine init** initializes a new Linux virtual machine where containers are run.

## OPTIONS

Expand All @@ -41,9 +41,9 @@ Print usage statement.
## EXAMPLES

```
$ podman machine create myvm
$ podman machine create --device=/dev/xvdc:rw myvm
$ podman machine create --memory=1024 myvm
$ podman machine init myvm
$ podman machine init --device=/dev/xvdc:rw myvm
$ podman machine init --memory=1024 myvm
```

## SEE ALSO
Expand Down
14 changes: 7 additions & 7 deletions docs/source/markdown/podman-machine.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ podman\-machine - Manage Podman's virtual machine

## SUBCOMMANDS

| Command | Man Page | Description |
| ------- | ------------------------------------------------------- | ----------------------------- |
| create | [podman-machine-create(1)](podman-machine-create.1.md) | Create a new virtual machine |
| remove | [podman-machine-destroy(1)](podman-machine-remove.1.md)| Remove a virtual machine |
| ssh | [podman-machine-ssh.1.md(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 |
| 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 |

## SEE ALSO
podman(1)
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/containers/storage/pkg/homedir"
)

type CreateOptions struct {
type InitOptions struct {
Name string
CPUS uint64
Memory uint64
Expand Down Expand Up @@ -58,7 +58,7 @@ type RemoveOptions struct {
}

type VM interface {
Create(opts CreateOptions) error
Init(opts InitOptions) error
Remove(name string, opts RemoveOptions) (string, func() error, error)
SSH(name string, opts SSHOptions) error
Start(name string, opts StartOptions) error
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/libvirt/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package libvirt

import "github.com/containers/podman/v3/pkg/machine"

func (v *MachineVM) Create(name string, opts machine.CreateOptions) error {
func (v *MachineVM) Init(name string, opts machine.InitOptions) error {
return nil
}

Expand Down
22 changes: 15 additions & 7 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var (
//qemuCommon = []string{"-cpu", "host", "-qmp", "tcp:localhost:4444,server,nowait"}
)

// NewMachine creates an instance of a virtual machine based on the qemu
// NewMachine initializes an instance of a virtual machine based on the qemu
// virtualization.
func NewMachine(opts machine.CreateOptions) (machine.VM, error) {
func NewMachine(opts machine.InitOptions) (machine.VM, error) {
vmConfigDir, err := machine.GetConfDir(vmtype)
if err != nil {
return nil, err
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewMachine(opts machine.CreateOptions) (machine.VM, error) {
// Add memory
cmd = append(cmd, []string{"-m", strconv.Itoa(int(vm.Memory))}...)
// Add cpus
// TODO
cmd = append(cmd, []string{"-smp", strconv.Itoa(int(vm.CPUs))}...)
// Add ignition file
cmd = append(cmd, []string{"-fw_cfg", "name=opt/com.coreos/config,file=" + vm.IgnitionFilePath}...)
// Add qmp socket
Expand All @@ -88,8 +88,8 @@ func NewMachine(opts machine.CreateOptions) (machine.VM, error) {

// Add network
cmd = append(cmd, "-nic", "user,model=virtio,hostfwd=tcp::"+strconv.Itoa(vm.Port)+"-:22")

vm.CmdLine = cmd
fmt.Println("///")
return vm, nil
}

Expand All @@ -111,9 +111,9 @@ func LoadVMByName(name string) (machine.VM, error) {
return vm, err
}

// Create writes the json configuration file to the filesystem for
// Init writes the json configuration file to the filesystem for
// other verbs (start, stop)
func (v *MachineVM) Create(opts machine.CreateOptions) error {
func (v *MachineVM) Init(opts machine.InitOptions) error {
sshDir := filepath.Join(homedir.Get(), ".ssh")
// GetConfDir creates the directory so no need to check for
// its existence
Expand Down Expand Up @@ -172,7 +172,15 @@ func (v *MachineVM) Start(name string, _ machine.StartOptions) error {
files := []*os.File{os.Stdin, os.Stdout, os.Stderr}
attr.Files = files
logrus.Debug(v.CmdLine)
_, err = os.StartProcess(v.CmdLine[0], v.CmdLine, attr)
cmd := v.CmdLine

// Disable graphic window when not in debug mode
// Done in start, so we're not suck with the debug level we used on init
if logrus.GetLevel() != logrus.DebugLevel {
cmd = append(cmd, "-display", "none")
}

_, err = os.StartProcess(v.CmdLine[0], cmd, attr)
return err
}

Expand Down

0 comments on commit f663857

Please sign in to comment.