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
|
from collections import OrderedDict
import os.path
from ._path import AbsPath
# oct-unescape certain characters as they are in /proc/mountinfo
# see seq_path[_root]() in linux source
def _oct_unescape(b):
trans = {
br'\040' : b' ', br'\011' : b'\t',
br'\012' : b'\n', br'\0134' : b'\\',
}
for src, dst in trans.items():
b = b.replace(src, dst)
return b
class NotAbsRoot(Exception):
# This exception is raised for mount entries whose root is not
# a valid absolute path (this happens e.g. for persistent namespaces bound
# to a file). Such entries are skipped from MountInfo
pass
class _MountEntry:
mount_id = None
"mount ID, bytes"
parent_id = None
"parent ID, bytes"
devnum = None
"device number, int"
root = None
"root of the mount, AbsPath"
mount_point = None
"path where the fs is mounted, AbsPath"
mount_opts = None
"mount options bytes"
opt_fields = None
"optional fields, list of bytes"
fstype = None
"filesystem type, bytes or None"
source = None
"mount source, bytes or None"
super_opts = None
"superblock options, bytes or None"
raw_entry = None
"raw mountinfo line, bytes"
parent = None
"parent mountinfo entry, None if it does not exit"
def __init__(self, line):
self.raw_entry = line
items = line.split()
if len(items) < 6:
raise ValueError('Not enough items in a mountinfo line', line)
mount_id, parent_id, devnum, root, mount_point, mount_opts = items[:6]
self.mount_id = mount_id
self.parent_id = parent_id
major, minor = map(int, devnum.split(b':'))
# see Documentation/admin-guide/devices.txt in linux source
if major < 0 or major > 511 or minor < 0 or minor > 255:
raise ValueError('Invalid device number', devnum)
self.devnum = (major << 8) + minor
root = _oct_unescape(root)
if len(root) < 1 or not root.startswith(b'/'):
raise NotAbsRoot()
self.root = AbsPath(root)
self.mount_point = AbsPath(_oct_unescape(mount_point))
self.mount_opts = mount_opts
remainder = items[6:]
opt_fields = []
while len(remainder) > 0 and remainder[0] != b'-':
opt_fields.append(remainder.pop(0))
self.opt_fields = opt_fields
if len(remainder) > 0:
sep = remainder.pop(0)
if sep != b'-':
raise ValueError('Expected separator "-", got', sep)
if len(remainder) > 0:
self.fstype = remainder.pop(0)
if len(remainder) > 0:
self.source = _oct_unescape(remainder.pop(0))
if len(remainder) > 0:
self.super_opts = remainder.pop(0)
def __str__(self):
return '({major}:{minor}){src}{root}->{mp}'.format(
major = self.devnum >> 8, minor = self.devnum & 255,
src = ("{%s}" % self.source) if self.source else "",
root = "[%s]" % self.root, mp = self.mount_point)
class MountInfo:
"""
A wrapper around the contents of the Linux /proc/<pid>/mountinfo file.
"""
mounts = None
"OrderedDict of 'mount ID' -> _MountEntry"
def __init__(self, data):
mounts = OrderedDict()
for line in data.splitlines():
try:
e = _MountEntry(line)
except NotAbsRoot:
pass
if e.mount_id in mounts:
raise ValueError('Duplicate mount ID: %s' % str(e.mount_id))
mounts[e.mount_id] = e
self.mounts = mounts
for e in mounts.values():
try:
e.parent = mounts[e.parent_id]
except KeyError:
pass
def __str__(self):
return '%s(%d entries)' % (self.__class__.__name__, len(self.mounts))
def entries_for_mountpoint(self, mountpoint):
"""
Iterate over all mountinfo entries mounted at the given mountpoint.
"""
return filter(lambda entry: entry.mount_point == mountpoint, self.mounts.values())
def mountpoint_for_path(self, path):
"""
Find the longest mountpoint that is a parent of path.
"""
best_match = None
for e in self.mounts.values():
if (path in e.mount_point and
(best_match is None or len(best_match) < len(e.mount_point))):
best_match = e.mount_point
if best_match is None:
raise LookupError('No mountpoint for', path)
return best_match
|