-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize_functions.py
174 lines (142 loc) · 5.85 KB
/
initialize_functions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import subprocess
import common
import luks
import disks
import pool
# initialize disk functions
def initialize(diskname, partname, poolname):
settings = common.get_settings()
backup_partitions = [i["id"] for i in settings["backup-disks"]]
backup_pools = [i["zpool"] for i in settings["backup-disks"]]
if partname in backup_partitions:
print("Backup disk '"+diskname+"' already exist. Aborting.")
sys.exit(1)
elif poolname in backup_pools:
print("Backup pool '"+poolname+"' already exist. Aborting.")
sys.exit(1)
else:
init_disk = {"zpool": poolname, "id": partname, "luks": "luks-"+partname, "luks-keyfile": "/keys/"+partname+".key"}
if initialize_disk(diskname, init_disk) == 0:
settings["backup-disks"].append(init_disk)
common.save_settings()
inp = input("Do you want to install the systemd service for automatic backup and scrub on disk attach? Type uppercase 'yes': ")
if inp == "YES":
enable_attach_script([init_disk], 0)
else:
print("Skipping.", flush=True)
def initialize_disk(diskname, diskrec):
diskpath = "/dev/disk/by-id/"+diskname
partpath = "/dev/disk/by-id/"+diskrec["id"]
partname = diskrec["id"]
lukspath = "/dev/mapper/"+diskrec["luks"]
luksname = diskrec["luks"]
keypath = diskrec["luks-keyfile"]
poolname = diskrec["zpool"]
print("Zapping disk")
retval,errormsg = disks.zap_disk(diskpath)
if retval != 0:
print(errormsg)
return 1
print("Creating partition")
retval,errormsg = disks.create_partition(diskpath, poolname)
if retval != 0:
print(errormsg)
return 1
print("Creating luks container")
retval,errormsg = luks.luksformat(partpath)
if retval != 0:
print(errormsg)
return 1
print("Creating keyfile")
retval,errormsg = luks.lukscreatekeyfile(keypath)
if retval != 0:
print(errormsg)
return 1
print("Adding keyfile")
retval,errormsg = luks.luksaddkey(partpath, keypath)
if retval != 0:
print(errormsg)
return 1
print("Opening luks container")
retval,errormsg = luks.luksopen(partpath, luksname, keypath)
if retval != 0:
print(errormsg)
return 1
print("Creating pool")
retval,errormsg = pool.create_pool(poolname, lukspath)
if retval != 0:
print(errormsg)
return 1
print("Closing")
try:
common.export_pool_and_close_luks(diskrec, 1)
except Exception as e:
print(e)
return 1
return 0
def enable_attach_script(disks, print_depth):
print(" "*print_depth + "Enabling attach script for selected disks.", flush=True)
if len(disks) == 0:
print(" "*print_depth + " No disks selected", flush=True)
for disk in disks:
diskid = disk["id"]
diskid_subst = diskid.replace("-", "\\x2d")
disk_device = "dev-disk-by\\x2did-" + diskid_subst + ".device"
servicefile_content = "[Unit]\nAfter=" + disk_device + "\n\n[Service]\nExecStart=" + os.path.dirname(os.path.realpath(__file__)) + "/ondiskattach.sh " + disk["zpool"] + "\n\n[Install]\nWantedBy=" + disk_device + "\n"
servicefile_name = disk["zpool"] + ".service"
servicefile_path = "/etc/systemd/system/" + servicefile_name
# Create service file
print(" "*print_depth + " Installing service file...", end="", flush=True)
try:
with open(servicefile_path, "w") as servicefile:
servicefile.write(servicefile_content)
print("success.", flush=True)
except Exception as e:
print("error.", flush=True)
print(e)
break
# Enable the service
print(" "*print_depth + " Enabling the service...", end="", flush=True)
cmd = "systemctl enable " + servicefile_name
cpinst = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if cpinst.returncode != 0:
print("error.", flush=True)
break
else:
print("success.", flush=True)
# Reload systemd
print(" "*print_depth + " systemctl daemon-reload...", end="", flush=True)
cmd = "systemctl daemon-reload"
cpinst = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if cpinst.returncode != 0:
print("error.", flush=True)
break
else:
print("success.", flush=True)
def remove_attach_script(disks, print_depth):
print(" "*print_depth + "Removing attach script for ", end="", flush=True)
print([disk["zpool"] for disk in disks], flush=True)
if len(disks) == 0:
print(" "*print_depth + " No disks selected", flush=True)
for disk in disks:
servicefile_name = disk["zpool"] + ".service"
servicefile_path = "/etc/systemd/system/" + servicefile_name
# Disable the service
print(" "*print_depth + " Disabling the service...", end="", flush=True)
cmd = "systemctl disable " + servicefile_name
cpinst = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if cpinst.returncode != 0:
print("error.", flush=True)
stderr = cpinst.stderr.decode("utf-8")
print(stderr, flush=True)
break
else:
print("success.", flush=True)
# Delete service file
print(" "*print_depth + " Removing service file...", end="", flush=True)
if os.path.exists(servicefile_path):
os.remove(servicefile_path)
print("success.", flush=True)
else:
print("file not found.", flush=True)