Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
Signed-off-by: Manu Gupta <[email protected]>
  • Loading branch information
manugupt1 committed Feb 13, 2024
1 parent ef037f8 commit 4dcfc58
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
103 changes: 103 additions & 0 deletions pkg/mountutil/mountutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,109 @@ func ProcessFlagV(s string, volStore volumestore.VolumeStore, createDir bool) (*
return res, nil
}

func processMount(s string, volStore volumestore.VolumeStore, createDir bool) (*Processed, error) {
var (
res *Processed
volSpec volumeSpec
src, dst string
options []string
)

split, err := splitVolumeSpec(s)
if err != nil {
return nil, fmt.Errorf("failed to split volume mount specification: %v", err)
}

switch len(split) {
case 1:
// validate destination
dst = split[0]
if _, err := validateAnonymousVolumeDestination(dst); err != nil {
return nil, err
}

// create anonymous volume
volSpec, err = handleAnonymousVolumes(dst, volStore)
if err != nil {
return nil, err
}

src = volSpec.Source
res = &Processed{
Type: volSpec.Type,
AnonymousVolume: volSpec.AnonymousVolume,
}
case 2, 3:
// Vaildate destination
dst = split[1]
dst = strings.TrimLeft(dst, ":")
if _, err := isValidPath(dst); err != nil {
return nil, err
}

// Get volume spec
src = split[0]
volSpec, err = handleVolumeToMount(src, dst, volStore, createDir)
if err != nil {
return nil, err
}

src = volSpec.Source
res = &Processed{
Type: volSpec.Type,
Name: volSpec.Name,
AnonymousVolume: volSpec.AnonymousVolume,
}

// Parse volume options
if len(split) == 3 {
res.Mode = split[2]

rawOpts := res.Mode

options, res.Opts, err = getVolumeOptions(src, res.Type, rawOpts)
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("failed to parse %q", s)
}

fstype := DefaultMountType
if runtime.GOOS != "freebsd" {
found := false
for _, opt := range options {
switch opt {
case "rbind", "bind":
fstype = "bind"
found = true
}
if found {
break
}
}
if !found {
options = append(options, "rbind")
}
}
res.Mount = specs.Mount{
Type: fstype,
Source: cleanMount(src),
Destination: cleanMount(dst),
Options: options,
}
if userns.RunningInUserNS() {
unpriv, err := UnprivilegedMountFlags(src)
if err != nil {
return nil, fmt.Errorf("failed to get unprivileged mount flags for %q: %w", src, err)
}
res.Mount.Options = strutil.DedupeStrSlice(append(res.Mount.Options, unpriv...))
}

return res, nil
}

func handleBindMounts(source string, createDir bool) (volumeSpec, error) {
var res volumeSpec
res.Type = Bind
Expand Down
2 changes: 1 addition & 1 deletion pkg/mountutil/mountutil_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func ProcessFlagMount(s string, volStore volumestore.VolumeStore) (*Processed, e
return ProcessFlagTmpfs(fieldsStr)
case Volume, Bind:
// createDir=false for --mount option to disallow creating directories on host if not found
return ProcessFlagV(fieldsStr, volStore, false)
return processMount(fieldsStr, volStore, false)
}
return nil, fmt.Errorf("invalid mount type '%s' must be a volume/bind/tmpfs", mountType)
}
Expand Down

0 comments on commit 4dcfc58

Please sign in to comment.