aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Cholewiński <harry666t@gmail.com>2016-02-16 20:19:11 +0100
committerKamil Cholewiński <harry666t@gmail.com>2016-02-16 20:19:11 +0100
commit3f82c78684005b721ff12eb3afd62415c41e2576 (patch)
tree11f746280c8c82b4d009cbaed69c59212e693078
parent3150219036b860660b21b4d3cdad4360e5828da8 (diff)
downloadcookbook-3f82c78684005b721ff12eb3afd62415c41e2576.zip
cookbook-3f82c78684005b721ff12eb3afd62415c41e2576.tar.gz
cookbook-3f82c78684005b721ff12eb3afd62415c41e2576.tar.bz2
Various fixes in parsing /etc/fstab
-rwxr-xr-xfiles/remount36
1 files changed, 32 insertions, 4 deletions
diff --git a/files/remount b/files/remount
index 6a52752..d199580 100755
--- a/files/remount
+++ b/files/remount
@@ -1,19 +1,44 @@
#!/usr/bin/env python
+from __future__ import print_function
+
import os
+import re
import subprocess
import sys
+FSTAB_LINE = r"""
+^
+\s* (?P<spec>\S+)
+\s+ (?P<file>\S+)
+\s+ (?P<vfstype>\S+)
+\s+ (?P<mntopts>\S+)
+(?:\s+ (?P<freq>\d+)
+ (?:\s+ (?P<passno>\d+))?)?
+\s*
+$
+"""
+
+
+def try_int(x):
+ try:
+ return int(x)
+ except (TypeError, ValueError):
+ return None
+
+
def read_fstab(fname="/etc/fstab"):
with open(fname) as f:
lines = f.readlines()
for line in lines:
- line = line[:-1]
if not line or line.startswith("#"):
continue
- spec, file, vfstype, mntops, freq, passno = line.split()
+ m = re.match(FSTAB_LINE, line, re.X)
+ if not m:
+ raise ValueError("Malformed line: {!r}".format(line))
+ spec, file, vfstype, mntops, freq, passno = m.groups()
mntops = mntops.split(",")
- yield spec, file, vfstype, mntops, freq, passno
+ yield spec, file, vfstype, mntops, try_int(freq), try_int(passno)
return
@@ -30,8 +55,11 @@ def mount(file, mode):
def main():
mode = sys.argv[1]
- assert mode in ["ro", "rw"]
+ assert mode in ["ro", "rw", "show"]
for spec, file, vfstype, mntops, freq, passno in read_fstab():
+ if mode == "show":
+ print(spec, file, vfstype, mntops, freq, passno)
+ continue
if "ro" in mntops:
cmd = mount(file, mode)
subprocess.check_call(cmd)