start spice in background
This commit is contained in:
@@ -3,9 +3,11 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from threading import Condition, Thread
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@@ -53,7 +55,7 @@ def qemu_command(
|
|||||||
"-kernel", f'{nixos_config["toplevel"]}/kernel',
|
"-kernel", f'{nixos_config["toplevel"]}/kernel',
|
||||||
"-initrd", nixos_config["initrd"],
|
"-initrd", nixos_config["initrd"],
|
||||||
"-append", " ".join(kernel_cmdline),
|
"-append", " ".join(kernel_cmdline),
|
||||||
] # fmt: on
|
] # fmt: on
|
||||||
|
|
||||||
if vm.graphics:
|
if vm.graphics:
|
||||||
# fmt: off
|
# fmt: off
|
||||||
@@ -84,6 +86,21 @@ def qemu_command(
|
|||||||
return command
|
return command
|
||||||
|
|
||||||
|
|
||||||
|
def start_spicy(spice_socket: Path, stop_condition: Condition) -> None:
|
||||||
|
while not spice_socket.exists():
|
||||||
|
with stop_condition:
|
||||||
|
if stop_condition.wait(0.1):
|
||||||
|
return
|
||||||
|
|
||||||
|
spicy = nix_shell(["spice-gtk"], ["spicy", f"--uri=spice+unix://{spice_socket}"])
|
||||||
|
proc = subprocess.Popen(spicy)
|
||||||
|
while proc.poll() is None:
|
||||||
|
with stop_condition:
|
||||||
|
if stop_condition.wait(0.1):
|
||||||
|
proc.terminate()
|
||||||
|
proc.wait()
|
||||||
|
|
||||||
|
|
||||||
class BuildVmTask(BaseTask):
|
class BuildVmTask(BaseTask):
|
||||||
def __init__(self, uuid: UUID, vm: VmConfig, nix_options: list[str] = []) -> None:
|
def __init__(self, uuid: UUID, vm: VmConfig, nix_options: list[str] = []) -> None:
|
||||||
super().__init__(uuid, num_cmds=7)
|
super().__init__(uuid, num_cmds=7)
|
||||||
@@ -212,12 +229,19 @@ class BuildVmTask(BaseTask):
|
|||||||
disk_img=disk_img,
|
disk_img=disk_img,
|
||||||
spice_socket=spice_socket,
|
spice_socket=spice_socket,
|
||||||
)
|
)
|
||||||
print(
|
stop_condition = Condition()
|
||||||
f"nix shell nixpkgs#spice-gtk -c spicy --uri=spice+unix://{spice_socket} --spice-shared-dir $HOME"
|
spice_thread = Thread(
|
||||||
|
target=start_spicy, args=(spice_socket, stop_condition), name="qemu"
|
||||||
)
|
)
|
||||||
|
spice_thread.start()
|
||||||
|
|
||||||
print("$ " + shlex.join(qemu_cmd))
|
print("$ " + shlex.join(qemu_cmd))
|
||||||
cmd.run(nix_shell(["qemu"], qemu_cmd), name="qemu")
|
try:
|
||||||
|
cmd.run(nix_shell(["qemu"], qemu_cmd), name="qemu")
|
||||||
|
finally:
|
||||||
|
with stop_condition:
|
||||||
|
stop_condition.notify()
|
||||||
|
spice_thread.join()
|
||||||
|
|
||||||
|
|
||||||
def create_vm(vm: VmConfig, nix_options: list[str] = []) -> BuildVmTask:
|
def create_vm(vm: VmConfig, nix_options: list[str] = []) -> BuildVmTask:
|
||||||
|
|||||||
Reference in New Issue
Block a user