diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-01-07 16:52:26 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-01-07 16:52:26 +0000 |
commit | fd092fc2d40640dd53ae66d931f95ad265f4dab6 (patch) | |
tree | 8d6c209fb3d1cd0e133d4891fce20da6a1d1a651 /v-i | |
parent | a697783d63d8dc410ae0b104f1cf37e4b4d886cf (diff) | |
parent | 04224dc0b688abfbb67f796fd9fa89330bc5bf49 (diff) | |
download | v-i-fd092fc2d40640dd53ae66d931f95ad265f4dab6.tar.gz |
Merge branch 'tidy' into 'main'
chore: shfmt
See merge request larswirzenius/v-i!6
Diffstat (limited to 'v-i')
-rwxr-xr-x | v-i | 108 |
1 files changed, 71 insertions, 37 deletions
@@ -10,8 +10,12 @@ import yaml from subprocess import run +verbose = False + + def log(msg): - print("INSTALLER:", msg) + if verbose: + print("INSTALLER:", msg) def physical_volumes(): @@ -89,25 +93,25 @@ def clean_up_disks(): for lv in lvs: for m in find_mount_points(mounts, lv["path"]): log(f"unmount {m['mount']}") - run(["umount", m["mount"]]) + run(["umount", m["mount"]], capture_output=True) for vg in vgs: log(f"remove volume group {vg}") - run(["vgremove", "--yes", vg], check=True) + run(["vgremove", "--yes", vg], check=True, capture_output=True) for pv in pvs: log(f"remove physical volume {pv}") - run(["pvremove", pv["pv"]], check=True) + run(["pvremove", pv["pv"]], check=True, capture_output=True) if is_luks(pv["pv"]): - run(["cryptsetup", "close", pv["pv"]], check=True) + run(["cryptsetup", "close", pv["pv"]], check=True, capture_output=True) for mapping in glob.glob("/dev/mapper/*"): if not mapping.endswith("/control"): log(f"open LUKS volume {mapping} (just in case it is one)") - run(["cryptsetup", "close", mapping], check=False) + run(["cryptsetup", "close", mapping], check=False, capture_output=True) -def vmdb_spec(cryptsetup_password, playbook): +def vmdb_spec(cryptsetup_password, playbook, extra_vars): device = "{{ image }}" spec = { "steps": [ @@ -190,16 +194,17 @@ def vmdb_spec(cryptsetup_password, playbook): { "mount": "root", }, - {"mount": "boot", "dirname": "/boot", "mount-on": "root"}, + { + "mount": "boot", + "dirname": "/boot", + "mount-on": "root", + }, { "mount": "efi", "dirname": "/boot/efi", "mount-on": "boot", }, { - "virtual-filesystems": "root", - }, - { "unpack-rootfs": "root", }, { @@ -209,35 +214,46 @@ def vmdb_spec(cryptsetup_password, playbook): "unless": "rootfs_unpacked", }, { + "apt": "install", + "packages": [ + "console-setup", + "dosfstools", + "ifupdown", + "linux-image-amd64", + "locales-all", + "lvm2", + "psmisc", + "python3", + "ssh", + "strace", + ], + "tag": "root", + "unless": "rootfs_unpacked", + }, + { "cache-rootfs": "root", "unless": "rootfs_unpacked", }, { - "fstab": "root", + # This MUST be after the debootstrap step. + "virtual-filesystems": "root", }, { - "apt": "install", - "packages": ["linux-image-amd64"], - "tag": "root", + "fstab": "root", }, { + # These MUST come after the fstab step so that they add the + # crypttab in the initramfs. "apt": "install", "packages": [ - "console-setup", "cryptsetup", "cryptsetup-initramfs", - "dosfstools", - "ifupdown", - "locales-all", - "lvm2", - "psmisc", - "python3", - "ssh", - "strace", ], "tag": "root", }, { + # This also MUST come outside the rootfs caching, as it install + # things outside the file systems. "grub": "uefi", "tag": "root", "efi": "efi", @@ -249,7 +265,9 @@ def vmdb_spec(cryptsetup_password, playbook): # If a playbook has been specified, add an ansible step. if playbook: - spec["steps"].append({"ansible": "root", "playbook": playbook}) + spec["steps"].append( + {"ansible": "root", "playbook": playbook, "extra_vars": extra_vars} + ) return spec @@ -260,13 +278,23 @@ def main(): p.add_argument("--log", default="install.log") p.add_argument("--cache", default="cache.tar.gz") p.add_argument("--playbook") + p.add_argument("--vars") p.add_argument("--luks") p.add_argument("device") args = p.parse_args() + if args.verbose: + global verbose + verbose = args.verbose + + extra_vars = {} + if args.vars: + with open(args.vars) as f: + extra_vars = yaml.safe_load(f) + clean_up_disks() - spec = vmdb_spec(args.luks, args.playbook) + spec = vmdb_spec(args.luks, args.playbook, extra_vars) tmp = tempfile.mkdtemp() specfile = os.path.join(tmp, "spec.yaml") if args.verbose: @@ -275,22 +303,28 @@ def main(): yaml.dump(spec, stream=f, indent=4) log(f"run vmdb2 to install on {args.device}") - run( - [ - "vmdb2", - "--verbose", - f"--rootfs-tarball={args.cache}", - f"--log={args.log}", - f"--image={args.device}", - specfile, - ], - check=True, - ) + env = dict(os.environ) + env["ANSIBLE_STDOUT_CALLBACK"] = "yaml" + env["ANSIBLE_NOCOWS"] = "1" + env["ANSIBLE_LOG_PATH"] = "ansible.log" + + argv = [ + "vmdb2", + f"--rootfs-tarball={args.cache}", + f"--log={args.log}", + f"--image={args.device}", + specfile, + ] + if verbose: + argv.append("--verbose") + + run(argv, check=True, capture_output=True) log("cleanup") shutil.rmtree(tmp) log("OK, done") + print("OK, done") main() |