Skip to content

Commit 2249d38

Browse files
committed
Consistent preload template gathering
- Easier copy-paste when code duplication is necessary; - Only fires event if the qube should be really preloaded, which is more difficult to do considering the global feature. The prior behavior didn't error out, just a warning log; - Add tests For: QubesOS/qubes-issues#1512
1 parent a932795 commit 2249d38

5 files changed

Lines changed: 332 additions & 98 deletions

File tree

linux/aux-tools/preload-dispvm

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,43 @@ import concurrent.futures
1212
import qubesadmin
1313

1414

15-
def get_max(qube):
16-
return int(qube.features.get("preload-dispvm-max", 0) or 0)
15+
def get_preload_max(qube) -> int | None:
16+
value = qube.features.get("preload-dispvm-max", None)
17+
return int(value) if value else value
1718

1819

1920
async def main():
2021
app = qubesadmin.Qubes()
2122
domains = app.domains
2223
default_dispvm = getattr(app, "default_dispvm", None)
24+
global_max = get_preload_max(domains["dom0"])
2325
appvms = [
2426
qube
2527
for qube in domains
26-
if get_max(qube) > 0
27-
and (
28-
(
29-
qube.klass == "AppVM"
30-
and getattr(qube, "template_for_dispvms", False)
28+
if (
29+
qube.klass == "AppVM"
30+
and getattr(qube, "template_for_dispvms", False)
31+
and (
32+
(qube != default_dispvm and get_preload_max(qube))
33+
or (
34+
(qube == default_dispvm and global_max)
35+
or (global_max is None and get_preload_max(qube))
36+
)
3137
)
32-
or (qube.name == "dom0" and default_dispvm)
3338
)
3439
]
3540
method = "admin.vm.CreateDisposable"
3641
loop = asyncio.get_running_loop()
3742
tasks = []
38-
if "dom0" in appvms and default_dispvm in appvms:
39-
appvms.remove(default_dispvm)
4043
with concurrent.futures.ThreadPoolExecutor() as executor:
4144
for qube in appvms:
42-
maximum = get_max(qube)
43-
msg = f"{qube}:{maximum}"
44-
if qube.name == "dom0":
45-
qube = default_dispvm
46-
msg = "global:" + msg
47-
print(msg)
45+
if qube == default_dispvm and global_max is not None:
46+
maximum = global_max
47+
msg = f"global:{qube}:{maximum}"
48+
else:
49+
maximum = get_preload_max(qube)
50+
msg = f"{qube}:{maximum}"
51+
print(repr(msg))
4852
exec_args = qube.qubesd_call, qube.name, method, "preload-autostart"
4953
future = loop.run_in_executor(executor, *exec_args)
5054
tasks.append(future)

0 commit comments

Comments
 (0)