-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add podman machine cp
subcommand
#25331
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
//go:build amd64 || arm64 | ||
|
||
package machine | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
|
||
"github.com/containers/podman/v5/cmd/podman/registry" | ||
"github.com/containers/podman/v5/libpod/events" | ||
"github.com/containers/podman/v5/pkg/copy" | ||
"github.com/containers/podman/v5/pkg/machine" | ||
"github.com/containers/podman/v5/pkg/machine/define" | ||
"github.com/containers/podman/v5/pkg/machine/env" | ||
"github.com/containers/podman/v5/pkg/machine/vmconfigs" | ||
"github.com/containers/podman/v5/pkg/specgen" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type cpOptions struct { | ||
Quiet bool | ||
Machine *vmconfigs.MachineConfig | ||
IsSrc bool | ||
SrcPath string | ||
DestPath string | ||
} | ||
|
||
var ( | ||
cpCmd = &cobra.Command{ | ||
Use: "cp [options] SRC_PATH DEST_PATH", | ||
Short: "Securely copy contents between the virtual machine", | ||
Long: "Securely copy files or directories between the virtual machine and your host", | ||
PersistentPreRunE: machinePreRunE, | ||
RunE: cp, | ||
Args: cobra.ExactArgs(2), | ||
Example: `podman machine cp ~/ca.crt podman-machine-default:/etc/containers/certs.d/ca.crt`, | ||
ValidArgsFunction: autocompleteMachineCp, | ||
} | ||
|
||
cpOpts = cpOptions{} | ||
) | ||
|
||
func init() { | ||
registry.Commands = append(registry.Commands, registry.CliCommand{ | ||
Command: cpCmd, | ||
Parent: machineCmd, | ||
}) | ||
|
||
flags := cpCmd.Flags() | ||
quietFlagName := "quiet" | ||
flags.BoolVarP(&cpOpts.Quiet, quietFlagName, "q", false, "Suppress copy status output") | ||
} | ||
|
||
func cp(_ *cobra.Command, args []string) error { | ||
var err error | ||
|
||
srcMachine, srcPath, destMachine, destPath, err := copy.ParseSourceAndDestination(args[0], args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Passing an absolute windows path of the format <volume>:\<path> will cause | ||
// `copy.ParseSourceAndDestination` to think the volume is a Machine. Check | ||
// if the raw cmdline argument is a Windows host path. | ||
if runtime.GOOS == "windows" { | ||
if specgen.IsHostWinPath(args[0]) { | ||
jakecorrenti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
srcMachine = "" | ||
srcPath = args[0] | ||
} | ||
|
||
if specgen.IsHostWinPath(args[1]) { | ||
destMachine = "" | ||
destPath = args[1] | ||
} | ||
Comment on lines
+70
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also isn't this breaking for machine names with one letter? i.e. I think it runs into the same issue as shown in #25323 We do allow a machine called |
||
} | ||
|
||
mc, err := resolveMachine(srcMachine, destMachine) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
state, err := provider.State(mc, false) | ||
if err != nil { | ||
return err | ||
} | ||
if state != define.Running { | ||
return fmt.Errorf("vm %q is not running", mc.Name) | ||
} | ||
|
||
cpOpts.Machine = mc | ||
cpOpts.SrcPath = srcPath | ||
cpOpts.DestPath = destPath | ||
|
||
err = secureCopy(&cpOpts) | ||
if err != nil { | ||
return fmt.Errorf("copy failed: %s", err.Error()) | ||
} | ||
|
||
fmt.Println("Copy successful") | ||
newMachineEvent(events.Copy, events.Event{Name: mc.Name}) | ||
return nil | ||
} | ||
|
||
func secureCopy(opts *cpOptions) error { | ||
srcPath := opts.SrcPath | ||
destPath := opts.DestPath | ||
sshConfig := opts.Machine.SSH | ||
|
||
username := sshConfig.RemoteUsername | ||
if cpOpts.Machine.HostUser.Rootful { | ||
username = "root" | ||
} | ||
username += "@localhost:" | ||
|
||
if opts.IsSrc { | ||
srcPath = username + srcPath | ||
} else { | ||
destPath = username + destPath | ||
} | ||
|
||
args := []string{"-r", "-i", sshConfig.IdentityPath, "-P", strconv.Itoa(sshConfig.Port)} | ||
args = append(args, machine.CommonSSHArgs()...) | ||
args = append(args, []string{srcPath, destPath}...) | ||
|
||
cmd := exec.Command("scp", args...) | ||
if !opts.Quiet { | ||
cmd.Stdout = os.Stdout | ||
} | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func resolveMachine(srcMachine, destMachine string) (*vmconfigs.MachineConfig, error) { | ||
if len(srcMachine) > 0 && len(destMachine) > 0 { | ||
return nil, errors.New("copying between two machines is unsupported") | ||
} | ||
|
||
if len(srcMachine) == 0 && len(destMachine) == 0 { | ||
return nil, errors.New("a machine name must prefix either the source path or destination path") | ||
} | ||
|
||
dirs, err := env.GetMachineDirs(provider.VMType()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
name := destMachine | ||
if len(srcMachine) > 0 { | ||
cpOpts.IsSrc = true | ||
name = srcMachine | ||
} | ||
|
||
return vmconfigs.LoadMachineByName(name, dirs) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
% podman-machine-cp 1 | ||
|
||
## NAME | ||
podman\-machine\-cp - Securely copy contents between the host and the virtual machine | ||
|
||
## SYNOPSIS | ||
**podman machine cp** [*options*] *src_path* *dest_path* | ||
|
||
## DESCRIPTION | ||
|
||
Use secure copy (scp) to copy files or directories between the virtual machine | ||
and your host machine. | ||
|
||
`podman machine cp` does not support copying between two virtual machines, | ||
which would require two machines running simultaneously. | ||
|
||
Additionally, `podman machine cp` will automatically do a recursive copy of | ||
files and directories. | ||
|
||
## OPTIONS | ||
|
||
#### **--help** | ||
|
||
Print usage statement. | ||
|
||
#### **--quiet**, **-q** | ||
|
||
Suppress copy status output. | ||
|
||
## EXAMPLES | ||
Copy a file from your host to the running Podman Machine. | ||
``` | ||
$ podman machine cp ~/configuration.txt podman-machine-default:~/configuration.txt | ||
... | ||
Copy Successful | ||
``` | ||
|
||
Copy a file from the running Podman Machine to your host. | ||
``` | ||
$ podman machine cp podman-machine-default:~/logs/log.txt ~/logs/podman-machine-default.txt | ||
... | ||
Copy Successful | ||
``` | ||
|
||
Copy a directory from your host to the running Podman Machine. | ||
``` | ||
$ podman machine cp ~/.config podman-machine-default:~/.config | ||
... | ||
Copy Successful | ||
``` | ||
|
||
Copy a directory from the running Podman Machine to your host. | ||
``` | ||
$ podman machine cp podman-machine-default:~/.config ~/podman-machine-default.config | ||
... | ||
Copy Successful | ||
``` | ||
|
||
## SEE ALSO | ||
**[podman(1)](podman.1.md)**, **[podman-machine(1)](podman-machine.1.md)** | ||
|
||
## HISTORY | ||
February 2025, Originally compiled by Jake Correnti <[email protected]> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package e2e_test | ||
|
||
type cpMachine struct { | ||
quiet bool | ||
src string | ||
dest string | ||
|
||
cmd []string | ||
} | ||
|
||
func (c *cpMachine) buildCmd(m *machineTestBuilder) []string { | ||
cmd := []string{"machine", "cp"} | ||
|
||
if c.quiet { | ||
cmd = append(cmd, "--quiet") | ||
} | ||
|
||
cmd = append(cmd, c.src, c.dest) | ||
|
||
c.cmd = cmd | ||
return cmd | ||
} | ||
|
||
func (c *cpMachine) withQuiet() *cpMachine { | ||
c.quiet = true | ||
return c | ||
} | ||
|
||
func (c *cpMachine) withSrc(src string) *cpMachine { | ||
c.src = src | ||
return c | ||
} | ||
|
||
func (c *cpMachine) withDest(dest string) *cpMachine { | ||
c.dest = dest | ||
return c | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just thinking about it does
podman cp
not have the exact same issue, i.e. should this fix be moved into ParseSourceAndDestination()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that result in the same issue mentioned below having to do with single name containers?