forked from amir73il/unionmount-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_box.py
84 lines (74 loc) · 2.03 KB
/
tool_box.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# Tools for test scripts
#
import os, sys
class ArgumentError(Exception):
def __init__(self, msg):
self.__msg = msg
def __str__(self):
return self.__msg
class TestError(Exception):
def __init__(self, msg):
self.__msg = msg
def __str__(self):
return self.__msg
def exit_error(msg):
print(msg, file=sys.stderr)
sys.exit(1)
def system(command):
ret = os.system(command)
if ret != 0:
raise RuntimeError("Command failed: " + command)
return True
def read_file(path):
fd = open(path, "r")
data = fd.read()
del fd
return data
def write_file(path, data):
fd = open(path, "w")
fd.write(data)
del fd
def write_kmsg(data):
write_file("/dev/kmsg", data);
#
# Check for taint (kernel warnings and oopses)
#
current_taint = read_file("/proc/sys/kernel/tainted")
def check_not_tainted():
taint = read_file("/proc/sys/kernel/tainted")
if taint != current_taint:
raise RuntimeError("TAINTED " + current_taint + " -> ", taint)
#
# Check if boolean module param is enabled
#
# Return None if module param does not exist
#
def check_bool_modparam(param):
# If overlay is a module, make sure it is loaded before checking its params
try:
system("modprobe overlay 2>/dev/null")
except RuntimeError:
pass
try:
value = read_file("/sys/module/overlay/parameters/" + param)
except FileNotFoundError:
return None
return value.startswith("Y")
#
# Check if overlay feature is enabled by mount option
#
# Return 'default' if mount option was not provided
#
def check_bool_mntopt(feature, mntopts, default, onopt2=None, offopt2=None):
onopt = feature + "=on"
offopt = feature + "=off"
on = onopt in mntopts or (onopt2 and onopt2 in mntopts)
off = offopt in mntopts or (offopt2 and offopt2 in mntopts)
if on and off:
raise RuntimeError("Conflicting mount options w.r.t feature '" + feature + "': " + mntopts)
if on:
return True
if off:
return False
return default;