-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
64 lines (52 loc) · 1.89 KB
/
common.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
import os.path
import json
import pool
import luks
settings_filepath = str()
settings = None
def get_settings():
return settings
def read_settings(filepath):
global settings_filepath
global settings
with open(filepath) as json_file:
settings = json.load(json_file)
settings_filepath = filepath
def save_settings():
with open(settings_filepath,'w') as json_file:
json.dump(settings, json_file, indent=3)
def open_luks_and_import_pool(disk, print_depth):
name = disk["zpool"]
ident = disk["id"]
luksname = disk["luks"]
luks_keyfile = disk["luks-keyfile"]
luks_path = "/dev/mapper/" + luksname
partpath = "/dev/disk/by-id/" + ident
if not os.path.exists(luks_path):
print(" "*print_depth + "Opening LUKS container: " + luks_path)
retval,errormsg = luks.luksopen(partpath, luksname, luks_keyfile)
if retval != 0:
raise Exception(errormsg)
else:
print(" "*print_depth + "LUKS container already open: " + luks_path)
if not pool.pool_is_imported(name):
print(" "*print_depth + "Importing pool: " + name)
pool.import_pool(name)
else:
print(" "*print_depth + "Pool already imported: " + name)
def export_pool_and_close_luks(disk, print_depth):
name = disk["zpool"]
luksname = disk["luks"]
if pool.pool_is_imported(name):
print(" "*print_depth + "Exporting pool: " + name)
pool.export_pool(name)
else:
print(" "*print_depth + "Pool already exported: " + name)
luks_path = "/dev/mapper/" + luksname
if os.path.exists(luks_path):
print(" "*print_depth + "Closing LUKS container: " + luks_path)
retval,errormsg = luks.luksclose(luksname)
if retval != 0:
raise Exception(errormsg)
else:
print(" "*print_depth + "LUKS container already closed: " + luks_path)