-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-target-vm.py
executable file
·62 lines (48 loc) · 2.04 KB
/
create-target-vm.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
#!/usr/bin/env python3
import argparse
import subprocess
from pathlib import Path
def parse_cli_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--console-output", action="store_true")
parser.add_argument("--installer-iso", type=Path, default="/srv/iso/ubuntu-24.04.1-live-server-amd64.iso")
parser.add_argument("--nvme-volume-name", default="nvme-target.qcow2")
return parser.parse_args()
def find_nvme_target_drive(nvme_volume_name: str, *, create=False) -> Path:
if create:
subprocess.run(["virsh", "--connect", "qemu:///session",
"vol-create-as",
"--pool", "ubuntu-nvmeotcp-poc",
"--name", nvme_volume_name,
"--capacity", "8G",
"--format", "qcow2"])
cmd = ["virsh", "--connect", "qemu:///session",
"vol-path",
"--pool", "ubuntu-nvmeotcp-poc",
"--vol", nvme_volume_name]
return Path(subprocess.check_output(cmd, text=True).rstrip())
def main() -> None:
args = parse_cli_args()
extra_args = ["autoinstall"]
if args.console_output:
extra_args.append("console=tty0")
nvme_target_file = find_nvme_target_drive(args.nvme_volume_name, create=True)
cmd = [
"virt-install",
"--autoconsole", "graphical",
"--noreboot",
"--connect", "qemu:///session",
"--name", "ubuntu-nvmeotcp-poc-target",
"--disk", "size=10,pool=ubuntu-nvmeotcp-poc",
"--memory", "1024",
"--virt-type", "kvm",
"--location", str(args.installer_iso),
"--cloud-init", "user-data=resources/cc-nvmet.yaml",
"--extra-args", " ".join(extra_args),
f"--qemu-commandline=-drive file={nvme_target_file},if=none,id=nvm",
"--qemu-commandline=-device nvme,drive=nvm,serial=nvme-1,addr=0x10",
"--network", "network=poc-network",
]
subprocess.run(cmd)
if __name__ == "__main__":
main()