mirror of
https://github.com/containers/podman.git
synced 2025-06-24 11:28:24 +08:00
Merge pull request #9648 from jmguzik/unify-mount-consts
[NO TESTS NEEDED] Cleanup/unify mount consts
This commit is contained in:
@ -6,23 +6,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/parse"
|
"github.com/containers/common/pkg/parse"
|
||||||
|
"github.com/containers/podman/v3/libpod/define"
|
||||||
"github.com/containers/podman/v3/pkg/specgen"
|
"github.com/containers/podman/v3/pkg/specgen"
|
||||||
"github.com/containers/podman/v3/pkg/util"
|
"github.com/containers/podman/v3/pkg/util"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// TypeBind is the type for mounting host dir
|
|
||||||
TypeBind = "bind"
|
|
||||||
// TypeVolume is the type for named volumes
|
|
||||||
TypeVolume = "volume"
|
|
||||||
// TypeTmpfs is the type for mounting tmpfs
|
|
||||||
TypeTmpfs = "tmpfs"
|
|
||||||
// TypeDevpts is the type for creating a devpts
|
|
||||||
TypeDevpts = "devpts"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errDuplicateDest = errors.Errorf("duplicate mount destination")
|
errDuplicateDest = errors.Errorf("duplicate mount destination")
|
||||||
optionArgError = errors.Errorf("must provide an argument for option")
|
optionArgError = errors.Errorf("must provide an argument for option")
|
||||||
@ -90,7 +80,7 @@ func parseVolumes(volumeFlag, mountFlag, tmpfsFlag []string, addReadOnlyTmpfs bo
|
|||||||
}
|
}
|
||||||
unifiedMounts[dest] = spec.Mount{
|
unifiedMounts[dest] = spec.Mount{
|
||||||
Destination: dest,
|
Destination: dest,
|
||||||
Type: TypeTmpfs,
|
Type: define.TypeTmpfs,
|
||||||
Source: "tmpfs",
|
Source: "tmpfs",
|
||||||
Options: options,
|
Options: options,
|
||||||
}
|
}
|
||||||
@ -131,7 +121,7 @@ func parseVolumes(volumeFlag, mountFlag, tmpfsFlag []string, addReadOnlyTmpfs bo
|
|||||||
// Final step: maps to arrays
|
// Final step: maps to arrays
|
||||||
finalMounts := make([]spec.Mount, 0, len(unifiedMounts))
|
finalMounts := make([]spec.Mount, 0, len(unifiedMounts))
|
||||||
for _, mount := range unifiedMounts {
|
for _, mount := range unifiedMounts {
|
||||||
if mount.Type == TypeBind {
|
if mount.Type == define.TypeBind {
|
||||||
absSrc, err := filepath.Abs(mount.Source)
|
absSrc, err := filepath.Abs(mount.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, nil, errors.Wrapf(err, "error getting absolute path of %s", mount.Source)
|
return nil, nil, nil, nil, errors.Wrapf(err, "error getting absolute path of %s", mount.Source)
|
||||||
@ -194,7 +184,7 @@ func getMounts(mountFlag []string) (map[string]spec.Mount, map[string]*specgen.N
|
|||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
switch mountType {
|
switch mountType {
|
||||||
case TypeBind:
|
case define.TypeBind:
|
||||||
mount, err := getBindMount(tokens)
|
mount, err := getBindMount(tokens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
@ -203,7 +193,7 @@ func getMounts(mountFlag []string) (map[string]spec.Mount, map[string]*specgen.N
|
|||||||
return nil, nil, nil, errors.Wrapf(errDuplicateDest, mount.Destination)
|
return nil, nil, nil, errors.Wrapf(errDuplicateDest, mount.Destination)
|
||||||
}
|
}
|
||||||
finalMounts[mount.Destination] = mount
|
finalMounts[mount.Destination] = mount
|
||||||
case TypeTmpfs:
|
case define.TypeTmpfs:
|
||||||
mount, err := getTmpfsMount(tokens)
|
mount, err := getTmpfsMount(tokens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
@ -212,7 +202,7 @@ func getMounts(mountFlag []string) (map[string]spec.Mount, map[string]*specgen.N
|
|||||||
return nil, nil, nil, errors.Wrapf(errDuplicateDest, mount.Destination)
|
return nil, nil, nil, errors.Wrapf(errDuplicateDest, mount.Destination)
|
||||||
}
|
}
|
||||||
finalMounts[mount.Destination] = mount
|
finalMounts[mount.Destination] = mount
|
||||||
case TypeDevpts:
|
case define.TypeDevpts:
|
||||||
mount, err := getDevptsMount(tokens)
|
mount, err := getDevptsMount(tokens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
@ -250,7 +240,7 @@ func getMounts(mountFlag []string) (map[string]spec.Mount, map[string]*specgen.N
|
|||||||
// Parse a single bind mount entry from the --mount flag.
|
// Parse a single bind mount entry from the --mount flag.
|
||||||
func getBindMount(args []string) (spec.Mount, error) {
|
func getBindMount(args []string) (spec.Mount, error) {
|
||||||
newMount := spec.Mount{
|
newMount := spec.Mount{
|
||||||
Type: TypeBind,
|
Type: define.TypeBind,
|
||||||
}
|
}
|
||||||
|
|
||||||
var setSource, setDest, setRORW, setSuid, setDev, setExec, setRelabel bool
|
var setSource, setDest, setRORW, setSuid, setDev, setExec, setRelabel bool
|
||||||
@ -381,8 +371,8 @@ func getBindMount(args []string) (spec.Mount, error) {
|
|||||||
// Parse a single tmpfs mount entry from the --mount flag
|
// Parse a single tmpfs mount entry from the --mount flag
|
||||||
func getTmpfsMount(args []string) (spec.Mount, error) {
|
func getTmpfsMount(args []string) (spec.Mount, error) {
|
||||||
newMount := spec.Mount{
|
newMount := spec.Mount{
|
||||||
Type: TypeTmpfs,
|
Type: define.TypeTmpfs,
|
||||||
Source: TypeTmpfs,
|
Source: define.TypeTmpfs,
|
||||||
}
|
}
|
||||||
|
|
||||||
var setDest, setRORW, setSuid, setDev, setExec, setTmpcopyup bool
|
var setDest, setRORW, setSuid, setDev, setExec, setTmpcopyup bool
|
||||||
@ -460,8 +450,8 @@ func getTmpfsMount(args []string) (spec.Mount, error) {
|
|||||||
// Parse a single devpts mount entry from the --mount flag
|
// Parse a single devpts mount entry from the --mount flag
|
||||||
func getDevptsMount(args []string) (spec.Mount, error) {
|
func getDevptsMount(args []string) (spec.Mount, error) {
|
||||||
newMount := spec.Mount{
|
newMount := spec.Mount{
|
||||||
Type: TypeDevpts,
|
Type: define.TypeDevpts,
|
||||||
Source: TypeDevpts,
|
Source: define.TypeDevpts,
|
||||||
}
|
}
|
||||||
|
|
||||||
var setDest bool
|
var setDest bool
|
||||||
@ -630,9 +620,9 @@ func getTmpfsMounts(tmpfsFlag []string) (map[string]spec.Mount, error) {
|
|||||||
|
|
||||||
mount := spec.Mount{
|
mount := spec.Mount{
|
||||||
Destination: filepath.Clean(destPath),
|
Destination: filepath.Clean(destPath),
|
||||||
Type: string(TypeTmpfs),
|
Type: string(define.TypeTmpfs),
|
||||||
Options: options,
|
Options: options,
|
||||||
Source: string(TypeTmpfs),
|
Source: string(define.TypeTmpfs),
|
||||||
}
|
}
|
||||||
m[destPath] = mount
|
m[destPath] = mount
|
||||||
}
|
}
|
||||||
|
12
libpod/define/mount.go
Normal file
12
libpod/define/mount.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package define
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TypeBind is the type for mounting host dir
|
||||||
|
TypeBind = "bind"
|
||||||
|
// TypeVolume is the type for named volumes
|
||||||
|
TypeVolume = "volume"
|
||||||
|
// TypeTmpfs is the type for mounting tmpfs
|
||||||
|
TypeTmpfs = "tmpfs"
|
||||||
|
// TypeDevpts is the type for creating a devpts
|
||||||
|
TypeDevpts = "devpts"
|
||||||
|
)
|
@ -8,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containers/podman/v3/libpod/define"
|
||||||
"github.com/containers/podman/v3/pkg/rootless"
|
"github.com/containers/podman/v3/pkg/rootless"
|
||||||
"github.com/containers/podman/v3/pkg/util"
|
"github.com/containers/podman/v3/pkg/util"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
@ -37,7 +38,7 @@ func addPrivilegedDevices(g *generate.Generator) error {
|
|||||||
for _, d := range hostDevices {
|
for _, d := range hostDevices {
|
||||||
devMnt := spec.Mount{
|
devMnt := spec.Mount{
|
||||||
Destination: d.Path,
|
Destination: d.Path,
|
||||||
Type: TypeBind,
|
Type: define.TypeBind,
|
||||||
Source: d.Path,
|
Source: d.Path,
|
||||||
Options: []string{"slave", "nosuid", "noexec", "rw", "rbind"},
|
Options: []string{"slave", "nosuid", "noexec", "rw", "rbind"},
|
||||||
}
|
}
|
||||||
@ -259,7 +260,7 @@ func addDevice(g *generate.Generator, device string) error {
|
|||||||
}
|
}
|
||||||
devMnt := spec.Mount{
|
devMnt := spec.Mount{
|
||||||
Destination: dst,
|
Destination: dst,
|
||||||
Type: TypeBind,
|
Type: define.TypeBind,
|
||||||
Source: src,
|
Source: src,
|
||||||
Options: []string{"slave", "nosuid", "noexec", perm, "rbind"},
|
Options: []string{"slave", "nosuid", "noexec", perm, "rbind"},
|
||||||
}
|
}
|
||||||
|
@ -277,7 +277,7 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt
|
|||||||
g.RemoveMount("/proc")
|
g.RemoveMount("/proc")
|
||||||
procMount := spec.Mount{
|
procMount := spec.Mount{
|
||||||
Destination: "/proc",
|
Destination: "/proc",
|
||||||
Type: TypeBind,
|
Type: define.TypeBind,
|
||||||
Source: "/proc",
|
Source: "/proc",
|
||||||
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
|
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/containers/common/pkg/config"
|
"github.com/containers/common/pkg/config"
|
||||||
"github.com/containers/podman/v3/libpod"
|
"github.com/containers/podman/v3/libpod"
|
||||||
|
"github.com/containers/podman/v3/libpod/define"
|
||||||
"github.com/containers/podman/v3/libpod/image"
|
"github.com/containers/podman/v3/libpod/image"
|
||||||
"github.com/containers/podman/v3/pkg/specgen"
|
"github.com/containers/podman/v3/pkg/specgen"
|
||||||
"github.com/containers/podman/v3/pkg/util"
|
"github.com/containers/podman/v3/pkg/util"
|
||||||
@ -18,16 +19,6 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO unify this in one place - maybe libpod/define
|
|
||||||
const (
|
|
||||||
// TypeBind is the type for mounting host dir
|
|
||||||
TypeBind = "bind"
|
|
||||||
// TypeVolume is the type for named volumes
|
|
||||||
TypeVolume = "volume"
|
|
||||||
// TypeTmpfs is the type for mounting tmpfs
|
|
||||||
TypeTmpfs = "tmpfs"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errDuplicateDest = errors.Errorf("duplicate mount destination")
|
errDuplicateDest = errors.Errorf("duplicate mount destination")
|
||||||
)
|
)
|
||||||
@ -156,7 +147,7 @@ func finalizeMounts(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Ru
|
|||||||
// Final step: maps to arrays
|
// Final step: maps to arrays
|
||||||
finalMounts := make([]spec.Mount, 0, len(baseMounts))
|
finalMounts := make([]spec.Mount, 0, len(baseMounts))
|
||||||
for _, mount := range baseMounts {
|
for _, mount := range baseMounts {
|
||||||
if mount.Type == TypeBind {
|
if mount.Type == define.TypeBind {
|
||||||
absSrc, err := filepath.Abs(mount.Source)
|
absSrc, err := filepath.Abs(mount.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, errors.Wrapf(err, "error getting absolute path of %s", mount.Source)
|
return nil, nil, nil, errors.Wrapf(err, "error getting absolute path of %s", mount.Source)
|
||||||
@ -208,8 +199,8 @@ func getImageVolumes(ctx context.Context, img *image.Image, s *specgen.SpecGener
|
|||||||
case "tmpfs":
|
case "tmpfs":
|
||||||
mount := spec.Mount{
|
mount := spec.Mount{
|
||||||
Destination: cleanDest,
|
Destination: cleanDest,
|
||||||
Source: TypeTmpfs,
|
Source: define.TypeTmpfs,
|
||||||
Type: TypeTmpfs,
|
Type: define.TypeTmpfs,
|
||||||
Options: []string{"rprivate", "rw", "nodev", "exec"},
|
Options: []string{"rprivate", "rw", "nodev", "exec"},
|
||||||
}
|
}
|
||||||
mounts[cleanDest] = mount
|
mounts[cleanDest] = mount
|
||||||
@ -277,7 +268,7 @@ func getVolumesFrom(volumesFrom []string, runtime *libpod.Runtime) (map[string]s
|
|||||||
return nil, nil, errors.Errorf("error retrieving container %s spec for volumes-from", ctr.ID())
|
return nil, nil, errors.Errorf("error retrieving container %s spec for volumes-from", ctr.ID())
|
||||||
}
|
}
|
||||||
for _, mnt := range spec.Mounts {
|
for _, mnt := range spec.Mounts {
|
||||||
if mnt.Type != TypeBind {
|
if mnt.Type != define.TypeBind {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, exists := userVolumes[mnt.Destination]; exists {
|
if _, exists := userVolumes[mnt.Destination]; exists {
|
||||||
@ -338,9 +329,9 @@ func getVolumesFrom(volumesFrom []string, runtime *libpod.Runtime) (map[string]s
|
|||||||
func addContainerInitBinary(s *specgen.SpecGenerator, path string) (spec.Mount, error) {
|
func addContainerInitBinary(s *specgen.SpecGenerator, path string) (spec.Mount, error) {
|
||||||
mount := spec.Mount{
|
mount := spec.Mount{
|
||||||
Destination: "/dev/init",
|
Destination: "/dev/init",
|
||||||
Type: TypeBind,
|
Type: define.TypeBind,
|
||||||
Source: path,
|
Source: path,
|
||||||
Options: []string{TypeBind, "ro"},
|
Options: []string{define.TypeBind, "ro"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
if path == "" {
|
||||||
@ -393,13 +384,13 @@ func SupersedeUserMounts(mounts []spec.Mount, configMount []spec.Mount) []spec.M
|
|||||||
func InitFSMounts(mounts []spec.Mount) error {
|
func InitFSMounts(mounts []spec.Mount) error {
|
||||||
for i, m := range mounts {
|
for i, m := range mounts {
|
||||||
switch {
|
switch {
|
||||||
case m.Type == TypeBind:
|
case m.Type == define.TypeBind:
|
||||||
opts, err := util.ProcessOptions(m.Options, false, m.Source)
|
opts, err := util.ProcessOptions(m.Options, false, m.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mounts[i].Options = opts
|
mounts[i].Options = opts
|
||||||
case m.Type == TypeTmpfs && filepath.Clean(m.Destination) != "/dev":
|
case m.Type == define.TypeTmpfs && filepath.Clean(m.Destination) != "/dev":
|
||||||
opts, err := util.ProcessOptions(m.Options, true, "")
|
opts, err := util.ProcessOptions(m.Options, true, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user