/
tasks.py
341 lines (288 loc) · 11.6 KB
/
tasks.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2023 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
import logging
import os
import shutil
import subprocess
from looseversion import LooseVersion
from pyanaconda.core import util
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.modules.common.task import Task
from org_qubes_os_initial_setup.constants import TEMPLATES_RPM_PATH
from org_qubes_os_initial_setup.utils import get_template_name, get_template_rpm
log = logging.getLogger(__name__)
class BaseQubesTask(Task):
def run_command(self, command, stdin=None, ignore_failure=False):
process_error = None
# not really needed, but make static analysis happy
stdout = None
stderr = None
try:
sys_root = conf.target.system_root
cmd = util.startProgram(
command, stderr=subprocess.PIPE, stdin=stdin, root=sys_root
)
(stdout, stderr) = cmd.communicate()
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
if not ignore_failure and cmd.returncode != 0:
process_error = '{} failed:\nstdout: "{}"\nstderr: "{}"'.format(
command, stdout, stderr
)
except Exception as e:
process_error = str(e)
if process_error:
log.error(process_error)
raise Exception(process_error)
return (stdout, stderr)
class DefaultKernelTask(BaseQubesTask):
@property
def name(self):
return "Setup up default kernel"
def run(self):
installed_kernels = os.listdir("/var/lib/qubes/vm-kernels")
installed_kernels = [
LooseVersion(x) for x in installed_kernels if x[0].isdigit()
]
default_kernel = str(sorted(installed_kernels)[-1])
self.run_command(["/usr/bin/qubes-prefs", "default-kernel", default_kernel])
class DefaultPoolTask(BaseQubesTask):
def __init__(self, create_default_tpool, vg_tpool):
super().__init__()
self.create_default_tpool = create_default_tpool
self.vg_tpool = vg_tpool
@property
def name(self):
return "Setup up default storage pool"
def run(self):
# At this stage:
# 1) on default LVM install, '(qubes_dom0, vm-pool)' is not available yet
# 2) on non-default LVM install, we assume that user *should* have
# use custom thin pool to use
# 3) in all the cases, we propose to create 'vm-pool' in appropriate
# volume group
if self.create_default_tpool:
# should be pre-filled based on rootfs's volume group
# see utils.get_default_tpool() called from QubesInitialSetup()
if self.vg_tpool is None:
raise Exception("Cannot find default LVM volume group")
self.run_command(
[
"/usr/sbin/lvcreate",
"-l",
"90%FREE",
"--thinpool",
self.vg_tpool[1],
self.vg_tpool[0],
],
)
if self.vg_tpool:
volume_group, thin_pool = self.vg_tpool
sys_root = conf.target.system_root
cmd = util.startProgram(
["qvm-pool", "info", thin_pool],
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
root=sys_root,
)
cmd.wait()
if cmd.returncode != 0:
# create only if it doesn't exist already
self.run_command(
[
"/usr/bin/qvm-pool",
"--add",
thin_pool,
"lvm_thin",
"-o",
"volume_group={volume_group},thin_pool={thin_pool},revisions_to_keep=2".format(
volume_group=volume_group, thin_pool=thin_pool
),
]
)
self.run_command(["/usr/bin/qubes-prefs", "default-pool", thin_pool])
class InstallTemplateTask(BaseQubesTask):
def __init__(self, template):
super().__init__()
self.template = template
@property
def name(self):
return "Install template " + self.template
def run(self):
template = self.template
template_name = get_template_name(template)
self.report_progress("Installing TemplateVM %s" % template_name)
rpm = get_template_rpm(template)
self.run_command(["/usr/bin/qvm-template", "install", "--nogpgcheck", rpm])
class CleanTemplatePkgsTask(BaseQubesTask):
@property
def name(self):
return "Setting up default pool"
def run(self):
# Clean RPM after install of selected ones
if os.path.exists(TEMPLATES_RPM_PATH):
shutil.rmtree(TEMPLATES_RPM_PATH)
class ConfigureDom0Task(BaseQubesTask):
@property
def name(self):
return "Setup up administration VM (dom0)"
def run(self):
for service in ["rdisc", "kdump", "libvirt-guests", "salt-minion"]:
self.run_command(
["systemctl", "disable", "{}.service".format(service)],
ignore_failure=True,
)
self.run_command(
["systemctl", "stop", "{}.service".format(service)], ignore_failure=True
)
class SetDefaultTemplateTask(BaseQubesTask):
def __init__(self, default_template):
super().__init__()
self.default_template = default_template
@property
def name(self):
return "Setup default template"
def run(self):
if self.default_template:
self.run_command(
["/usr/bin/qubes-prefs", "default-template", self.default_template]
)
class ConfigureDefaultQubesTask(BaseQubesTask):
def __init__(
self,
system_vms,
usbvm,
usbvm_with_netvm,
disp_firewallvm_and_usbvm,
allow_usb_keyboard,
allow_usb_mouse,
whonix_default,
whonix_vms,
default_vms,
disp_netvm,
):
super().__init__()
self.system_vms = system_vms
self.usbvm = usbvm
self.usbvm_with_netvm = usbvm_with_netvm
self.disp_firewallvm_and_usbvm = disp_firewallvm_and_usbvm
self.allow_usb_keyboard = allow_usb_keyboard
self.allow_usb_mouse = allow_usb_mouse
self.whonix_default = whonix_default
self.whonix_vms = whonix_vms
self.default_vms = default_vms
self.disp_netvm = disp_netvm
@property
def name(self):
return "Executing qubes configuration"
def run(self):
states = []
if self.system_vms:
states.extend(("qvm.sys-net", "qvm.sys-firewall", "qvm.default-dispvm"))
if self.disp_firewallvm_and_usbvm:
states.extend(
("pillar.qvm.disposable-sys-firewall", "pillar.qvm.disposable-sys-usb")
)
if self.disp_netvm:
states.append("pillar.qvm.disposable-sys-net")
if self.default_vms:
states.extend(("qvm.personal", "qvm.work", "qvm.untrusted", "qvm.vault"))
if self.whonix_vms:
states.extend(("qvm.sys-whonix", "qvm.anon-whonix"))
if self.whonix_default:
states.append("qvm.updates-via-whonix")
if self.usbvm:
states.append("qvm.sys-usb")
if self.usbvm_with_netvm:
states.append("pillar.qvm.sys-net-as-usbvm")
if self.allow_usb_mouse:
states.append("pillar.qvm.sys-usb-allow-mouse")
if self.allow_usb_keyboard:
states.append("pillar.qvm.usb-keyboard")
try:
# get rid of initial entries (from package installation time)
os.rename("/var/log/salt/minion", "/var/log/salt/minion.install")
except OSError:
pass
# Refresh minion configuration to make sure all installed formulas are included
self.run_command(["qubesctl", "saltutil.clear_cache"])
self.run_command(["qubesctl", "saltutil.sync_all"])
for state in states:
print("Setting up state: {}".format(state))
if state.startswith("pillar."):
self.run_command(
["qubesctl", "top.enable", state[len("pillar.") :], "pillar=True"]
)
else:
self.run_command(["qubesctl", "top.enable", state])
try:
self.run_command(["qubesctl", "--all", "state.highstate"])
# After successful call disable all the states to not leave them
# enabled, to not interfere with later user changes (like assigning
# additional PCI devices)
for state in states:
if not state.startswith("pillar."):
self.run_command(["qubesctl", "top.disable", state])
except Exception:
raise Exception(
(
"Qubes initial configuration failed. Login to the system and "
+ "check /var/log/salt/minion for details. "
+ "You can retry configuration by calling "
+ "'sudo qubesctl --all state.highstate' in dom0 (you will get "
+ "detailed state there)."
)
)
class CreateDefaultDVMTask(BaseQubesTask):
def __init__(self, default_template):
super().__init__()
self.default_template = default_template
@property
def name(self):
return "Create default DisposableVM"
def run(self):
if self.default_template:
dispvm_name = "default-dvm"
self.run_command(["/usr/bin/qubes-prefs", "default-dispvm", dispvm_name])
class ConfigureNetworkTask(BaseQubesTask):
def __init__(self, whonix_default, start_usb, start_whonix):
super().__init__()
self.whonix_default = whonix_default
# should sys-usb be started too?
self.start_usb = start_usb
self.start_whonix = start_whonix
@property
def name(self):
return "Setup networking"
def run(self):
default_netvm = "sys-firewall"
updatevm = default_netvm
if self.whonix_default:
updatevm = "sys-whonix"
self.run_command(["/usr/bin/qvm-prefs", "sys-firewall", "netvm", "sys-net"])
self.run_command(["/usr/bin/qubes-prefs", "default-netvm", default_netvm])
self.run_command(["/usr/bin/qubes-prefs", "updatevm", updatevm])
self.run_command(["/usr/bin/qubes-prefs", "clockvm", "sys-net"])
self.run_command(["/usr/bin/qvm-start", default_netvm])
if self.start_usb:
# Workaround for #1464 (so qvm.start from salt can't be used)
self.run_command(["systemctl", "start", "qubes-vm@sys-usb.service"])
if self.start_whonix:
# Workaround for #1464 (so qvm.start from salt can't be used)
self.run_command(["systemctl", "start", "qubes-vm@sys-whonix.service"])