Move mount to core/mount

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2024-01-17 09:52:12 -08:00
parent 1a1e0e8c81
commit 6e5408dcec
93 changed files with 74 additions and 74 deletions

60
core/mount/temp_unix.go Normal file
View File

@@ -0,0 +1,60 @@
//go:build !windows && !darwin
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mount
import (
"os"
"sort"
"github.com/moby/sys/mountinfo"
)
// SetTempMountLocation sets the temporary mount location
func SetTempMountLocation(root string) error {
err := os.MkdirAll(root, 0700)
if err != nil {
return err
}
// We need to pass canonicalized path to mountinfo.PrefixFilter in CleanupTempMounts
tempMountLocation, err = CanonicalizePath(root)
return err
}
// CleanupTempMounts all temp mounts and remove the directories
func CleanupTempMounts(flags int) (warnings []error, err error) {
mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(tempMountLocation))
if err != nil {
return nil, err
}
// Make the deepest mount be first
sort.Slice(mounts, func(i, j int) bool {
return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint)
})
for _, mount := range mounts {
if err := UnmountAll(mount.Mountpoint, flags); err != nil {
warnings = append(warnings, err)
continue
}
if err := os.Remove(mount.Mountpoint); err != nil {
warnings = append(warnings, err)
}
}
return warnings, nil
}