forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.go
More file actions
203 lines (155 loc) · 4.06 KB
/
Copy pathall.go
File metadata and controls
203 lines (155 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mount
import (
"bufio"
"context"
"log"
"os"
"sort"
"strings"
"time"
)
// UnmountAll attempts to unmount all the mounted filesystems via "self" mountinfo.
//
//nolint:gocyclo
func UnmountAll() error {
// timeout in seconds
const timeout = 10
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for iteration := range timeout {
mounts, err := readMountInfo()
if err != nil {
return err
}
failedUnmounts := 0
var failedMountPoints []string
for _, mountInfo := range mounts {
if mountInfo.MountPoint == "" {
continue
}
if strings.HasPrefix(mountInfo.MountSource, "/dev/") {
err = SafeUnmount(context.Background(), log.Printf, mountInfo.MountPoint, true, false)
if err == nil {
log.Printf("unmounted %s (%s)", mountInfo.MountPoint, mountInfo.MountSource)
} else {
log.Printf("failed unmounting %s: %s", mountInfo.MountPoint, err)
failedUnmounts++
failedMountPoints = append(failedMountPoints, mountInfo.MountPoint)
}
}
}
if failedUnmounts == 0 {
break
}
// Log mount users on first and last failure to help diagnose busy mounts.
if iteration == 0 || iteration == timeout-1 {
for _, mp := range failedMountPoints {
logMountUsers(log.Printf, mp)
}
}
log.Printf("retrying %d unmount operations...", failedUnmounts)
<-ticker.C
}
return nil
}
type mountInfo struct {
MountPoint string
MountSource string
MountType string
MountOptions map[string]string
}
func readMountInfo() ([]mountInfo, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return nil, err
}
defer f.Close() //nolint:errcheck
var mounts []mountInfo
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " - ", 2)
if len(parts) < 2 {
continue
}
var mntInfo mountInfo
pre := strings.Fields(parts[0])
post := strings.Fields(parts[1])
if len(pre) >= 5 {
mntInfo.MountPoint = pre[4]
}
if len(post) >= 1 {
mntInfo.MountType = post[0]
}
if len(post) >= 2 {
mntInfo.MountSource = post[1]
}
if len(post) >= 3 {
mntInfo.MountOptions = make(map[string]string)
for option := range strings.SplitSeq(post[2], ",") {
k, v, ok := strings.Cut(option, "=")
if ok {
mntInfo.MountOptions[k] = v
} else {
mntInfo.MountOptions[option] = ""
}
}
}
mounts = append(mounts, mntInfo)
}
return mounts, scanner.Err()
}
func getSubmounts(target string) ([]string, error) {
mounts, err := readMountInfo()
if err != nil {
return nil, err
}
var submounts []string
seen := make(map[string]struct{})
add := func(mountPoint string) {
if _, ok := seen[mountPoint]; ok {
return
}
seen[mountPoint] = struct{}{}
submounts = append(submounts, mountPoint)
}
for _, mnt := range mounts {
if mnt.MountPoint == target {
continue
}
// mounts nested under the target keep it busy.
if strings.HasPrefix(mnt.MountPoint, target+"/") {
add(mnt.MountPoint)
continue
}
// overlays mounted elsewhere still pin the target if any of their
// backing directories (upper/work/lower) live under it.
if mnt.MountType == "overlay" && overlayReferences(mnt, target) {
add(mnt.MountPoint)
}
}
sort.Slice(submounts, func(i, j int) bool {
return len(submounts[i]) > len(submounts[j])
})
return submounts, nil
}
// overlayReferences reports whether an overlay mount has any of its backing
// directories (upperdir, workdir or one of the lowerdirs) located under target.
func overlayReferences(mnt mountInfo, target string) bool {
prefix := target + "/"
if strings.HasPrefix(mnt.MountOptions["upperdir"], prefix) {
return true
}
if strings.HasPrefix(mnt.MountOptions["workdir"], prefix) {
return true
}
for lowerdir := range strings.SplitSeq(mnt.MountOptions["lowerdir"], ":") {
if strings.HasPrefix(lowerdir, prefix) {
return true
}
}
return false
}