2017-08-03 22:24:02 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
2020-07-16 06:56:20 +00:00
|
|
|
"io/ioutil"
|
2017-08-03 22:24:02 +00:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/mount"
|
2020-07-16 06:56:20 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-03 22:24:02 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 06:56:20 +00:00
|
|
|
func (lm *localMounter) Mount() (string, error) {
|
|
|
|
lm.mu.Lock()
|
|
|
|
defer lm.mu.Unlock()
|
|
|
|
|
|
|
|
if lm.mounts == nil {
|
|
|
|
mounts, release, err := lm.mountable.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
lm.mounts = mounts
|
|
|
|
lm.release = release
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lm.mounts) == 1 && (lm.mounts[0].Type == "bind" || lm.mounts[0].Type == "rbind") {
|
|
|
|
ro := false
|
|
|
|
for _, opt := range lm.mounts[0].Options {
|
|
|
|
if opt == "ro" {
|
|
|
|
ro = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ro {
|
|
|
|
return lm.mounts[0].Source, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "buildkit-mount")
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to create temp dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.All(lm.mounts, dir); err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
return "", errors.Wrapf(err, "failed to mount %s: %+v", dir, lm.mounts)
|
|
|
|
}
|
|
|
|
lm.target = dir
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 22:24:02 +00:00
|
|
|
func (lm *localMounter) Unmount() error {
|
|
|
|
lm.mu.Lock()
|
|
|
|
defer lm.mu.Unlock()
|
|
|
|
|
|
|
|
if lm.target != "" {
|
|
|
|
if err := mount.Unmount(lm.target, syscall.MNT_DETACH); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.RemoveAll(lm.target)
|
|
|
|
lm.target = ""
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:42:03 +00:00
|
|
|
if lm.release != nil {
|
|
|
|
return lm.release()
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 22:24:02 +00:00
|
|
|
return nil
|
|
|
|
}
|