Skip to content

facts: server.Mounts: fix whitespaces and escaped characters #1313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions pyinfra/facts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,34 +206,53 @@ class Mounts(FactBase[Dict[str, MountsDict]]):

@override
def command(self):
return "mount"
return "cat /proc/self/mountinfo"

@override
def process(self, output) -> dict[str, MountsDict]:
devices: dict[str, MountsDict] = {}

for line in output:
is_map = False
if line.startswith("map "):
line = line[4:]
is_map = True

device, _, path, other_bits = line.split(" ", 3)
def unescape_octal(match: re.Match) -> str:
s = match.group(0)[1:] # skip the backslash
return chr(int(s, base=8))

if is_map:
device = "map {0}".format(device)
def replace_octal(s: str) -> str:
"""
Unescape strings encoded by linux's string_escape_mem with ESCAPE_OCTAL flag.
"""
return re.sub(r"\\[0-7]{3}", unescape_octal, s)

if other_bits.startswith("type"):
_, type_, options = other_bits.split(" ", 2)
options = options.strip("()").split(",")
else:
options = other_bits.strip("()").split(",")
type_ = options.pop(0)

devices[path] = {
"device": device,
"type": type_,
"options": [option.strip() for option in options],
for line in output:
# ignore mount ID, parent ID, major:minor, root
_, _, _, _, mount_point, mount_options, line = line.split(sep=" ", maxsplit=6)

# ignore optional tags "shared", "master", "propagate_from" and "unbindable"
while True:
optional, line = line.split(sep=" ", maxsplit=1)
if optional == "-":
break

fs_type, mount_source, super_options = line.split(sep=" ")

mount_options = mount_options.split(sep=",")

# escaped: mount_point, mount_source, super_options
# these strings can contain characters encoded in octal, e.g. '\054' for ','
mount_point = replace_octal(mount_point)
mount_source = replace_octal(mount_source)

# mount_options will override ro/rw and can be different than the super block options
# filter them, so they don't appear twice
super_options = [
replace_octal(opt)
for opt in super_options.split(sep=",")
if opt not in ["ro", "rw"]
]

devices[mount_point] = {
"device": mount_source,
"type": fs_type,
"options": mount_options + super_options,
}

return devices
Expand Down
53 changes: 29 additions & 24 deletions tests/facts/server.Mounts/mounts.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
{
"command": "mount",
"command": "cat /proc/self/mountinfo",
"output": [
"/dev/sda1 on /boot type ext2 (rw,relatime,block_validity,barrier,user_xattr,acl)",
"/dev/disk1s4 on /private/var/vm (apfs, local, noexec, journaled, noatime, nobrowse)",
"map thing on / type something ()"
"2 1 0:26 / / ro,noatime - ext4 /dev/sdb1 rw",
"33 29 8:1 / /boot rw,relatime - vfat /dev/sda1 rw,lazytime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro",
"408 35 0:352 / /mnt/overlay/overlay,dir\\040with\\040spaces\\040and\\040\\134backslashes ro,relatime shared:27 - overlay none ro,lowerdir=lower\\054dir,upperdir=upper\\054dir,workdir=work\\054dir"
],
"fact": {
"/": {
"device": "/dev/sdb1",
"type": "ext4",
"options": [
"ro",
"noatime"
]
},
"/boot": {
"device": "/dev/sda1",
"type": "ext2",
"type": "vfat",
"options": [
"rw",
"relatime",
"block_validity",
"barrier",
"user_xattr",
"acl"
"lazytime",
"fmask=0022",
"dmask=0022",
"codepage=437",
"iocharset=iso8859-1",
"shortname=mixed",
"utf8",
"errors=remount-ro"
]
},
"/private/var/vm": {
"device": "/dev/disk1s4",
"type": "apfs",
"/mnt/overlay/overlay,dir with spaces and \\backslashes": {
"device": "none",
"type": "overlay",
"options": [
"local",
"noexec",
"journaled",
"noatime",
"nobrowse"
]
},
"/": {
"device": "map thing",
"type": "something",
"options": [
""
"ro",
"relatime",
"lowerdir=lower,dir",
"upperdir=upper,dir",
"workdir=work,dir"
]
}
}
Expand Down
Loading