mirror of
https://github.com/containers/podman.git
synced 2025-06-21 01:19:15 +08:00
cmd: support rootless mode for cp command
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/buildah/util"
|
"github.com/containers/buildah/util"
|
||||||
@ -10,6 +12,7 @@ import (
|
|||||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||||
"github.com/containers/libpod/libpod"
|
"github.com/containers/libpod/libpod"
|
||||||
"github.com/containers/libpod/pkg/chrootuser"
|
"github.com/containers/libpod/pkg/chrootuser"
|
||||||
|
"github.com/containers/libpod/pkg/rootless"
|
||||||
"github.com/containers/storage"
|
"github.com/containers/storage"
|
||||||
"github.com/containers/storage/pkg/archive"
|
"github.com/containers/storage/pkg/archive"
|
||||||
"github.com/containers/storage/pkg/chrootarchive"
|
"github.com/containers/storage/pkg/chrootarchive"
|
||||||
@ -48,6 +51,9 @@ func cpCmd(c *cliconfig.CpValues) error {
|
|||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
return errors.Errorf("you must provide a source path and a destination path")
|
return errors.Errorf("you must provide a source path and a destination path")
|
||||||
}
|
}
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
rootless.SetSkipStorageSetup(true)
|
||||||
|
}
|
||||||
|
|
||||||
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
|
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -76,6 +82,34 @@ func copyBetweenHostAndContainer(runtime *libpod.Runtime, src string, dest strin
|
|||||||
ctr = destCtr
|
ctr = destCtr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
s, err := ctr.State()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var became bool
|
||||||
|
var ret int
|
||||||
|
if s == libpod.ContainerStateRunning || s == libpod.ContainerStatePaused {
|
||||||
|
data, err := ioutil.ReadFile(ctr.Config().ConmonPidFile)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "cannot read conmon PID file %q", ctr.Config().ConmonPidFile)
|
||||||
|
}
|
||||||
|
conmonPid, err := strconv.Atoi(string(data))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "cannot parse PID %q", data)
|
||||||
|
}
|
||||||
|
became, ret, err = rootless.JoinDirectUserAndMountNS(uint(conmonPid))
|
||||||
|
} else {
|
||||||
|
became, ret, err = rootless.BecomeRootInUserNS()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if became {
|
||||||
|
os.Exit(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mountPoint, err := ctr.Mount()
|
mountPoint, err := ctr.Mount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -61,6 +61,7 @@ var cmdsNotRequiringRootless = map[*cobra.Command]bool{
|
|||||||
_versionCommand: true,
|
_versionCommand: true,
|
||||||
_createCommand: true,
|
_createCommand: true,
|
||||||
_execCommand: true,
|
_execCommand: true,
|
||||||
|
_cpCommand: true,
|
||||||
_exportCommand: true,
|
_exportCommand: true,
|
||||||
//// `info` must be executed in an user namespace.
|
//// `info` must be executed in an user namespace.
|
||||||
//// If this change, please also update libpod.refreshRootless()
|
//// If this change, please also update libpod.refreshRootless()
|
||||||
|
Reference in New Issue
Block a user