2017-08-03 22:24:02 +00:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
2020-07-16 06:56:20 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2017-08-03 22:24:02 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows can only mount a single mount at a given location.
|
|
|
|
// Parent layers are carried in Options, opaquely to localMounter.
|
|
|
|
if len(lm.mounts) != 1 {
|
|
|
|
return "", errors.Wrapf(errdefs.ErrNotImplemented, "request to mount %d layers, only 1 is supported", len(lm.mounts))
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:40:46 +00:00
|
|
|
m := lm.mounts[0]
|
|
|
|
|
|
|
|
if m.Type == "bind" || m.Type == "rbind" {
|
2020-07-23 13:04:24 +00:00
|
|
|
ro := false
|
2020-07-24 10:40:46 +00:00
|
|
|
for _, opt := range m.Options {
|
2020-07-23 13:04:24 +00:00
|
|
|
if opt == "ro" {
|
|
|
|
ro = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ro {
|
2020-07-24 10:40:46 +00:00
|
|
|
return m.Source, nil
|
2020-07-23 13:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:56:20 +00:00
|
|
|
// Windows mounts always activate in-place, so the target of the mount must be the source directory.
|
|
|
|
// See https://github.com/containerd/containerd/pull/2366
|
2020-07-24 10:40:46 +00:00
|
|
|
dir := m.Source
|
2020-07-16 06:56:20 +00:00
|
|
|
|
2020-07-24 10:40:46 +00:00
|
|
|
if err := m.Mount(dir); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to mount in-place: %v", m)
|
2020-07-16 06:56:20 +00:00
|
|
|
}
|
|
|
|
lm.target = dir
|
|
|
|
return lm.target, 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, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|