solve incorrect unmount

1. add WithTempMount for better unmount and remove
2. solve incorrect unmount for
   diff.DiffMounts,
   diff.Apply,
   oci.WithUsername,
   oci.WithUserID,
   remapRootFS

Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
This commit is contained in:
yanxuean
2018-01-05 22:43:24 +08:00
parent 1a560540b9
commit cb58bb885a
4 changed files with 200 additions and 229 deletions

View File

@@ -1,5 +1,13 @@
package mount
import (
"io/ioutil"
"os"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"
)
// Mount is the lingua franca of containerd. A mount represents a
// serialized mount syscall. Components either emit or consume mounts.
type Mount struct {
@@ -22,3 +30,44 @@ func All(mounts []Mount, target string) error {
}
return nil
}
// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
// The mounts are valid during the call to the f.
// Finally we will unmount and remove the temp dir regardless of the result of f.
func WithTempMount(mounts []Mount, f func(root string) error) (err error) {
root, uerr := ioutil.TempDir("", "containerd-WithTempMount")
if uerr != nil {
return errors.Wrapf(uerr, "failed to create temp dir for %v", mounts)
}
// We use Remove here instead of RemoveAll.
// The RemoveAll will delete the temp dir and all children it contains.
// When the Unmount fails, if we use RemoveAll, We will incorrectly delete data from mounted dir.
// if we use Remove,even though we won't successfully delete the temp dir,
// but we only leak a temp dir, we don't loss data from mounted dir.
// For details, please refer to #1868 #1785.
defer func() {
if uerr = os.Remove(root); uerr != nil {
log.L.Errorf("Failed to remove the temp dir %s: %v", root, uerr)
}
}()
// We should do defer first, if not we will not do Unmount when only a part of Mounts are failed.
defer func() {
if uerr = UnmountAll(root, 0); uerr != nil {
uerr = errors.Wrapf(uerr, "failed to unmount %s", root)
if err == nil {
err = uerr
} else {
err = errors.Wrap(err, uerr.Error())
}
}
}()
if uerr = All(mounts, root); uerr != nil {
return errors.Wrapf(uerr, "failed to mount %s", root)
}
if uerr = f(root); uerr != nil {
return errors.Wrapf(uerr, "failed to f(%s)", root)
}
return nil
}