Skip to content

Commit 2374178

Browse files
committed
pkg/cri/server: optimizations in unmountRecursive()
Use a PrefixFilter() to get only the mounts we're interested in, which removes the need to manually filter mounts from the mountinfo results. Additional optimizations can be made, as: > ... there's a little known fact that `umount(MNT_DETACH)` is actually > recursive in Linux, IOW this function can be replaced with > `unix.Umount(target, unix.MNT_DETACH)` (or `mount.UnmountAll(target, unix.MNT_DETACH)` > (provided that target itself is a mount point). e8fb2c3#r535450446 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 7572919 commit 2374178

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

pkg/cri/server/helpers_linux.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,34 +166,23 @@ func openLogFile(path string) (*os.File, error) {
166166
// unmountRecursive unmounts the target and all mounts underneath, starting with
167167
// the deepest mount first.
168168
func unmountRecursive(ctx context.Context, target string) error {
169-
mounts, err := mountinfo.GetMounts(nil)
169+
toUnmount, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
170170
if err != nil {
171171
return err
172172
}
173173

174-
var toUnmount []string
175-
for _, m := range mounts {
176-
p, err := filepath.Rel(target, m.Mountpoint)
177-
if err != nil {
178-
return err
179-
}
180-
if !strings.HasPrefix(p, "..") {
181-
toUnmount = append(toUnmount, m.Mountpoint)
182-
}
183-
}
184-
185174
// Make the deepest mount be first
186175
sort.Slice(toUnmount, func(i, j int) bool {
187-
return len(toUnmount[i]) > len(toUnmount[j])
176+
return len(toUnmount[i].Mountpoint) > len(toUnmount[j].Mountpoint)
188177
})
189178

190-
for i, mountPath := range toUnmount {
191-
if err := mount.UnmountAll(mountPath, unix.MNT_DETACH); err != nil {
179+
for i, m := range toUnmount {
180+
if err := mount.UnmountAll(m.Mountpoint, unix.MNT_DETACH); err != nil {
192181
if i == len(toUnmount)-1 { // last mount
193182
return err
194183
}
195184
// This is some submount, we can ignore this error for now, the final unmount will fail if this is a real problem
196-
log.G(ctx).WithError(err).Debugf("failed to unmount submount %s", mountPath)
185+
log.G(ctx).WithError(err).Debugf("failed to unmount submount %s", m.Mountpoint)
197186
}
198187
}
199188
return nil

0 commit comments

Comments
 (0)