vms/create: use tmpdir again
This commit is contained in:
@@ -5,6 +5,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
@@ -76,103 +77,105 @@ class BuildVmTask(BaseTask):
|
|||||||
validate_path(clan_flakes_dir(), flake_dir)
|
validate_path(clan_flakes_dir(), flake_dir)
|
||||||
flake_dir.mkdir(exist_ok=True)
|
flake_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
xchg_dir = flake_dir / "xchg"
|
with tempfile.TemporaryDirectory() as tmpdir_:
|
||||||
xchg_dir.mkdir(exist_ok=True)
|
tmpdir = Path(tmpdir_)
|
||||||
secrets_dir = flake_dir / "secrets"
|
xchg_dir = tmpdir / "xchg"
|
||||||
secrets_dir.mkdir(exist_ok=True)
|
xchg_dir.mkdir(exist_ok=True)
|
||||||
disk_img = f"{flake_dir}/disk.img"
|
secrets_dir = tmpdir / "secrets"
|
||||||
|
secrets_dir.mkdir(exist_ok=True)
|
||||||
|
disk_img = tmpdir / "disk.img"
|
||||||
|
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env["CLAN_DIR"] = str(self.vm.flake_url)
|
env["CLAN_DIR"] = str(self.vm.flake_url)
|
||||||
|
|
||||||
env["PYTHONPATH"] = str(
|
env["PYTHONPATH"] = str(
|
||||||
":".join(sys.path)
|
":".join(sys.path)
|
||||||
) # TODO do this in the clanCore module
|
) # TODO do this in the clanCore module
|
||||||
env["SECRETS_DIR"] = str(secrets_dir)
|
env["SECRETS_DIR"] = str(secrets_dir)
|
||||||
|
|
||||||
res = is_path_or_url(str(self.vm.flake_url))
|
res = is_path_or_url(str(self.vm.flake_url))
|
||||||
if res is None:
|
if res is None:
|
||||||
raise ClanError(
|
raise ClanError(
|
||||||
f"flake_url must be a valid path or URL, got {self.vm.flake_url}"
|
f"flake_url must be a valid path or URL, got {self.vm.flake_url}"
|
||||||
)
|
|
||||||
elif res == "path": # Only generate secrets for local clans
|
|
||||||
cmd = next(cmds)
|
|
||||||
if Path(self.vm.flake_url).is_dir():
|
|
||||||
cmd.run(
|
|
||||||
[vm_config["generateSecrets"], clan_name],
|
|
||||||
env=env,
|
|
||||||
)
|
)
|
||||||
else:
|
elif res == "path": # Only generate secrets for local clans
|
||||||
self.log.warning("won't generate secrets for non local clan")
|
cmd = next(cmds)
|
||||||
|
if Path(self.vm.flake_url).is_dir():
|
||||||
|
cmd.run(
|
||||||
|
[vm_config["generateSecrets"], clan_name],
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.log.warning("won't generate secrets for non local clan")
|
||||||
|
|
||||||
cmd = next(cmds)
|
cmd = next(cmds)
|
||||||
cmd.run(
|
cmd.run(
|
||||||
[vm_config["uploadSecrets"], clan_name],
|
[vm_config["uploadSecrets"], clan_name],
|
||||||
env=env,
|
env=env,
|
||||||
)
|
|
||||||
|
|
||||||
cmd = next(cmds)
|
|
||||||
cmd.run(
|
|
||||||
nix_shell(
|
|
||||||
["qemu"],
|
|
||||||
[
|
|
||||||
"qemu-img",
|
|
||||||
"create",
|
|
||||||
"-f",
|
|
||||||
"raw",
|
|
||||||
disk_img,
|
|
||||||
"1024M",
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
cmd = next(cmds)
|
cmd = next(cmds)
|
||||||
cmd.run(
|
cmd.run(
|
||||||
nix_shell(
|
nix_shell(
|
||||||
["e2fsprogs"],
|
["qemu"],
|
||||||
[
|
[
|
||||||
"mkfs.ext4",
|
"qemu-img",
|
||||||
"-L",
|
"create",
|
||||||
"nixos",
|
"-f",
|
||||||
disk_img,
|
"raw",
|
||||||
],
|
str(disk_img),
|
||||||
|
"1024M",
|
||||||
|
],
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
cmd = next(cmds)
|
cmd = next(cmds)
|
||||||
cmdline = [
|
cmd.run(
|
||||||
(Path(vm_config["toplevel"]) / "kernel-params").read_text(),
|
nix_shell(
|
||||||
f'init={vm_config["toplevel"]}/init',
|
["e2fsprogs"],
|
||||||
f'regInfo={vm_config["regInfo"]}/registration',
|
[
|
||||||
"console=ttyS0,115200n8",
|
"mkfs.ext4",
|
||||||
"console=tty0",
|
"-L",
|
||||||
]
|
"nixos",
|
||||||
qemu_command = [
|
str(disk_img),
|
||||||
# fmt: off
|
],
|
||||||
"qemu-kvm",
|
)
|
||||||
"-name", machine,
|
)
|
||||||
"-m", f'{vm_config["memorySize"]}M',
|
|
||||||
"-smp", str(vm_config["cores"]),
|
cmd = next(cmds)
|
||||||
"-device", "virtio-rng-pci",
|
cmdline = [
|
||||||
"-net", "nic,netdev=user.0,model=virtio", "-netdev", "user,id=user.0",
|
(Path(vm_config["toplevel"]) / "kernel-params").read_text(),
|
||||||
"-virtfs", "local,path=/nix/store,security_model=none,mount_tag=nix-store",
|
f'init={vm_config["toplevel"]}/init',
|
||||||
"-virtfs", f"local,path={xchg_dir},security_model=none,mount_tag=shared",
|
f'regInfo={vm_config["regInfo"]}/registration',
|
||||||
"-virtfs", f"local,path={xchg_dir},security_model=none,mount_tag=xchg",
|
"console=ttyS0,115200n8",
|
||||||
"-virtfs", f"local,path={secrets_dir},security_model=none,mount_tag=secrets",
|
"console=tty0",
|
||||||
"-drive", f'cache=writeback,file={disk_img},format=raw,id=drive1,if=none,index=1,werror=report',
|
]
|
||||||
"-device", "virtio-blk-pci,bootindex=1,drive=drive1,serial=root",
|
qemu_command = [
|
||||||
"-device", "virtio-keyboard",
|
# fmt: off
|
||||||
"-usb",
|
"qemu-kvm",
|
||||||
"-device", "usb-tablet,bus=usb-bus.0",
|
"-name", machine,
|
||||||
"-kernel", f'{vm_config["toplevel"]}/kernel',
|
"-m", f'{vm_config["memorySize"]}M',
|
||||||
"-initrd", vm_config["initrd"],
|
"-smp", str(vm_config["cores"]),
|
||||||
"-append", " ".join(cmdline),
|
"-device", "virtio-rng-pci",
|
||||||
# fmt: on
|
"-net", "nic,netdev=user.0,model=virtio", "-netdev", "user,id=user.0",
|
||||||
]
|
"-virtfs", "local,path=/nix/store,security_model=none,mount_tag=nix-store",
|
||||||
if not self.vm.graphics:
|
"-virtfs", f"local,path={xchg_dir},security_model=none,mount_tag=shared",
|
||||||
qemu_command.append("-nographic")
|
"-virtfs", f"local,path={xchg_dir},security_model=none,mount_tag=xchg",
|
||||||
print("$ " + shlex.join(qemu_command))
|
"-virtfs", f"local,path={secrets_dir},security_model=none,mount_tag=secrets",
|
||||||
cmd.run(nix_shell(["qemu"], qemu_command))
|
"-drive", f'cache=writeback,file={disk_img},format=raw,id=drive1,if=none,index=1,werror=report',
|
||||||
|
"-device", "virtio-blk-pci,bootindex=1,drive=drive1,serial=root",
|
||||||
|
"-device", "virtio-keyboard",
|
||||||
|
"-usb",
|
||||||
|
"-device", "usb-tablet,bus=usb-bus.0",
|
||||||
|
"-kernel", f'{vm_config["toplevel"]}/kernel',
|
||||||
|
"-initrd", vm_config["initrd"],
|
||||||
|
"-append", " ".join(cmdline),
|
||||||
|
# fmt: on
|
||||||
|
]
|
||||||
|
if not self.vm.graphics:
|
||||||
|
qemu_command.append("-nographic")
|
||||||
|
print("$ " + shlex.join(qemu_command))
|
||||||
|
cmd.run(nix_shell(["qemu"], qemu_command))
|
||||||
|
|
||||||
|
|
||||||
def create_vm(vm: VmConfig) -> BuildVmTask:
|
def create_vm(vm: VmConfig) -> BuildVmTask:
|
||||||
|
|||||||
Reference in New Issue
Block a user