-
Notifications
You must be signed in to change notification settings - Fork 5
/
overlay.go
72 lines (61 loc) · 1.93 KB
/
overlay.go
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
package scan
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/prometheus/procfs"
)
type Overlay struct {
// UpperDir is the path to the upper directory of the overlay filesystem.
UpperDir string
// WorkDir is the path to the work directory of the overlay filesystem.
WorkDir string
// MergedDir is the path to the merged directory of the overlay filesystem.
MergedDir string
// LowerDirs is the path to the lower directory of the overlay filesystem.
LowerDirs []string
}
func GetOverlayLayer(path string, pid uint32) string {
process, err := procfs.NewProc(int(pid))
if err != nil {
log.Infof("Error creating procfs for PID %d: %s\n", pid, err)
return ""
}
// Get the overlay mount points for the process.
overlay, err := getOverlayMountPoints(&process)
if err != nil {
log.Errorf("Error getting overlay mount points for PID %d: %s\n", pid, err)
return ""
}
// Check if the path is in one of the overlay mount points.
for _, lowerDir := range overlay.LowerDirs {
if strings.HasPrefix(path, lowerDir) {
return "lower"
}
}
if strings.HasPrefix(path, overlay.UpperDir) {
return "upper"
} else if strings.HasPrefix(path, overlay.WorkDir) {
return "work"
} else if strings.HasPrefix(path, overlay.MergedDir) {
return "merged"
}
return ""
}
func getOverlayMountPoints(process *procfs.Proc) (Overlay, error) {
// Read the mount info for the process, and find the overlay mount point. (There should only be one?).
if mounts, err := process.MountInfo(); err == nil {
for _, mount := range mounts {
if mount.FSType == "overlay" {
// Return the merged directory.
return Overlay{
mount.SuperOptions["upperdir"],
mount.SuperOptions["workdir"],
strings.Replace(mount.SuperOptions["upperdir"], "diff", "merged", 1),
strings.Split(mount.SuperOptions["lowerdir"], ":"),
}, nil
}
}
}
return Overlay{}, fmt.Errorf("failed to get mount point for pid %d", process.PID)
}