mirror of https://github.com/containers/podman.git
Merge pull request #9820 from ashley-cui/machineinit
[NO TESTS NEEDED] Rename podman machine create to init and clean up
This commit is contained in:
commit
25525ff834
|
@ -1,99 +0,0 @@
|
|||
// +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 (
|
||||
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.",
|
||||
RunE: create,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Example: `podman machine create myvm`,
|
||||
ValidArgsFunction: completion.AutocompleteNone,
|
||||
}
|
||||
)
|
||||
|
||||
type CreateCLIOptions struct {
|
||||
CPUS uint64
|
||||
Memory uint64
|
||||
Devices []string
|
||||
ImagePath string
|
||||
IgnitionPath string
|
||||
Name string
|
||||
}
|
||||
|
||||
var (
|
||||
createOpts = CreateCLIOptions{}
|
||||
defaultMachineName string = "podman-machine-default"
|
||||
)
|
||||
|
||||
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, 1,
|
||||
"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", 2048,
|
||||
"Memory (in MB)",
|
||||
)
|
||||
_ = createCmd.RegisterFlagCompletionFunc(memoryFlagName, completion.AutocompleteNone)
|
||||
|
||||
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 {
|
||||
createOpts.Name = defaultMachineName
|
||||
if len(args) > 0 {
|
||||
createOpts.Name = args[0]
|
||||
}
|
||||
vmOpts := machine.CreateOptions{
|
||||
CPUS: createOpts.CPUS,
|
||||
Memory: createOpts.Memory,
|
||||
IgnitionPath: createOpts.IgnitionPath,
|
||||
ImagePath: createOpts.ImagePath,
|
||||
Name: createOpts.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.Create(vmOpts)
|
||||
}
|
|
@ -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,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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -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
|
|
@ -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)
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/containers/storage/pkg/homedir"
|
||||
)
|
||||
|
||||
type CreateOptions struct {
|
||||
type InitOptions struct {
|
||||
Name string
|
||||
CPUS uint64
|
||||
Memory uint64
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue