-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ro.py
executable file
·66 lines (47 loc) · 1.4 KB
/
check_ro.py
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
#!/usr/bin/env python
import re
import os
import argparse
import random
import string
import errno
def get_random_name(length=8):
alphabet = string.ascii_letters + string.digits
result = ''
for _ in range(length):
result += random.choice(alphabet)
return result
def check(filename):
has_error = False
with open(filename) as opened:
file_systems = re.findall(r'[ ]\/[\w\/-]*\s', opened.read())
file_systems = [fs.strip() for fs in file_systems]
for fs in file_systems:
path_to_file = os.path.join(fs, get_random_name())
try:
open(path_to_file, 'a').close()
except IOError as err:
has_error = True
if err.errno == errno.EROFS:
print('{0} is read-only filesystem'.format(fs))
else:
print('Unexpected error: {0}'.format(err))
else:
os.remove(path_to_file)
if not has_error:
print('ok')
def main():
parser = argparse.ArgumentParser(
description='Check whether the mounted filesystem readable or writable'
)
parser.add_argument('-f', '--file',
help='File with mounted filesystems to check. If not specified, ' \
'/proc/mounts will be used'
)
args = parser.parse_args()
if args.file:
check(args.file)
else:
check('/proc/mounts')
if __name__ == '__main__':
main()