Support mount with target folder in box exec

Also mount bind before pivotroot, this fixes bind mounting in general
This commit is contained in:
Ettore Di Giacinto
2020-04-21 17:30:27 +02:00
parent 4cee27da7f
commit 9321a310a5
2 changed files with 18 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"github.com/pkg/errors"
@@ -63,15 +64,25 @@ func (b *DefaultBox) Exec() error {
if err := mountDev(b.Root); err != nil {
return errors.Wrap(err, "Failed mounting dev on rootfs")
}
if err := PivotRoot(b.Root); err != nil {
return errors.Wrap(err, "Failed switching pivot on rootfs")
}
for _, hostMount := range b.HostMounts {
if err := mountBind(hostMount, b.Root); err != nil {
target := hostMount
if strings.Contains(hostMount, ":") {
dest := strings.Split(hostMount, ":")
if len(dest) != 2 {
return errors.New("Invalid arguments for mount, it can be: fullpath, or source:target")
}
hostMount = dest[0]
target = dest[1]
}
if err := mountBind(hostMount, b.Root, target); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed mounting %s on rootfs", hostMount))
}
}
if err := PivotRoot(b.Root); err != nil {
return errors.Wrap(err, "Failed switching pivot on rootfs")
}
cmd := exec.Command(b.Cmd, b.Args...)
if b.Stdin {

View File

@@ -75,9 +75,9 @@ func mountProc(newroot string) error {
return nil
}
func mountBind(hostfolder, newroot string) error {
func mountBind(hostfolder, newroot, dst string) error {
source := hostfolder
target := filepath.Join(newroot, hostfolder)
target := filepath.Join(newroot, dst)
fstype := "bind"
data := ""
@@ -90,5 +90,5 @@ func mountBind(hostfolder, newroot string) error {
}
func mountDev(newroot string) error {
return mountBind("/dev", newroot)
return mountBind("/dev", newroot, "/dev")
}