Created
May 8, 2019 13:05
-
-
Save jrelo/caacaa1c58df199b53d57b0ac7b9a991 to your computer and use it in GitHub Desktop.
bind mounts pythong3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os.path, re | |
from collections import namedtuple | |
MountInfo = namedtuple('MountInfo', ['mountid', 'parentid', 'devid', 'root', 'mountpoint', 'mountoptions', 'extra', 'fstype', 'source', 'fsoptions']) | |
mounts = {} | |
def unescape(string): | |
return re.sub(r'\\([0-7]{3})', (lambda m: chr(int(m.group(1), 8))), string) | |
with open('/proc/self/mountinfo', 'r') as f: | |
for line in f: | |
# Parse line | |
mid, pid, devid, root, mp, mopt, *tail = line.rstrip().split(' ') | |
extra = [] | |
for item in tail: | |
if item != '-': | |
extra.append(item) | |
else: | |
break | |
fstype, src, fsopt = tail[len(extra)+1:] | |
# Save mount info | |
mount = MountInfo(int(mid), int(pid), devid, unescape(root), unescape(mp), mopt, extra, fstype, unescape(src), fsopt) | |
mounts.setdefault(devid, []).append(mount) | |
for devid, mnts in mounts.items(): | |
# Skip single mounts | |
if len(mnts) <= 1: | |
continue | |
# Sort list to get the first mount of the device's root dir (if still mounted) | |
mnts.sort(key=lambda x: x.root) | |
src, *binds = mnts | |
# Print bind mounts | |
for bindmount in binds: | |
if src.root == bindmount.root: | |
srcstring = src.mountpoint | |
else: | |
srcstring = src.mountpoint+':/'+os.path.relpath(bindmount.root, src.root) | |
print('{0} -> {1.mountpoint} ({1.mountoptions})'.format(srcstring, bindmount)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment