File
Blob: dev/vm/smartos/__init__.py
| 1 | """SmartOS boot media and a persistent native zone for illumos tests.""" |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import secrets |
| 7 | import shlex |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | import urllib.request |
| 11 | import uuid |
| 12 | |
| 13 | |
| 14 | GOOS = "illumos" |
| 15 | SSH_USER = "root" |
| 16 | PLATFORM = "20261023T001807Z" |
| 17 | ISO_URL = ( |
| 18 | "https://us-central.manta.mnx.io/Joyent_Dev/public/SmartOS/" |
| 19 | f"{PLATFORM}/smartos-{PLATFORM}.iso" |
| 20 | ) |
| 21 | ISO_SHA256 = "64c06b2ae527f323c4e0bb6cb6e97529c3e32e29038c1a9a0114d854639d25d5" |
| 22 | IMAGE_UUID = "b933df4b-b8f4-4bef-ad1e-236a20304496" |
| 23 | IMAGE_URL = f"https://images.smartos.org/images/{IMAGE_UUID}" |
| 24 | IMAGE_SHA1 = "e6d4bc669b25a1cfc2d8020ee8945300c752e8a0" |
| 25 | HERE = Path(__file__).resolve().parent |
| 26 | GUEST_ROOT = "/var/lib/specter-native/work" |
| 27 | |
| 28 | |
| 29 | def prepare(ctx): |
| 30 | backend = ctx.state.setdefault("backend", {}) |
| 31 | backend.setdefault("bootstrap_password", secrets.token_urlsafe(32)) |
| 32 | backend.setdefault("zone_uuid", str(uuid.uuid4())) |
| 33 | ctx.save_state() |
| 34 | source = ctx.fetch(ISO_URL, ISO_SHA256) |
| 35 | media = ctx.state_dir / "smartos-autoinstall.iso" |
| 36 | if not media.exists(): |
| 37 | seed = ctx.state_dir / "smartos-seed" |
| 38 | seed.mkdir(mode=0o700, exist_ok=True) |
| 39 | answers = { |
| 40 | "config_console": True, |
| 41 | "skip_instructions": True, |
| 42 | "simple_headers": True, |
| 43 | "admin_mac": ctx.mac, |
| 44 | "admin_ip": "dhcp", |
| 45 | "headnode_default_gateway": "none", |
| 46 | "dns_resolver1": ctx.network["dns"], |
| 47 | "dns_resolver2": ctx.network["dns"], |
| 48 | "dns_search": "native.invalid", |
| 49 | "ntp_host": "0.smartos.pool.ntp.org", |
| 50 | "skip_ntp_check": True, |
| 51 | "zpool_layout": "default", |
| 52 | "zpool_confirm_layout": "yes", |
| 53 | "bootpool": "none", |
| 54 | "install_pkgsrc": "no", |
| 55 | "root_password": backend["bootstrap_password"], |
| 56 | "hostname": ctx.name, |
| 57 | "skip_final_confirm": True, |
| 58 | } |
| 59 | answer_path = seed / "answers.json" |
| 60 | answer_path.write_text(json.dumps(answers) + "\n") |
| 61 | answer_path.chmod(0o600) |
| 62 | shutil.copyfile(HERE / "loader.conf.local", seed / "loader.conf.local") |
| 63 | _remaster(source, seed, media) |
| 64 | return [ |
| 65 | {"role": "boot", "source": media, "media": True, "boot": 1}, |
| 66 | { |
| 67 | "role": "system", |
| 68 | "source": None, |
| 69 | "size_gib": ctx.settings["disk_gib"], |
| 70 | "media": False, |
| 71 | "boot": 2, |
| 72 | }, |
| 73 | ] |
| 74 | |
| 75 | |
| 76 | def _remaster(source, seed, media): |
| 77 | partial = media.with_suffix(".partial.iso") |
| 78 | partial.unlink(missing_ok=True) |
| 79 | command = shutil.which("xorriso") |
| 80 | if command: |
| 81 | prefix = [command] |
| 82 | original, files, output = str(source), str(seed), str(partial) |
| 83 | else: |
| 84 | if not shutil.which("docker"): |
| 85 | raise RuntimeError("SmartOS media preparation needs xorriso or Docker") |
| 86 | image = "specter-smartos-media:local" |
| 87 | subprocess.run( |
| 88 | ["docker", "build", "--tag", image, str(HERE)], check=True |
| 89 | ) |
| 90 | prefix = [ |
| 91 | "docker", "run", "--rm", "--network=none", |
| 92 | "--user", f"{os.getuid()}:{os.getgid()}", |
| 93 | "--volume", f"{source}:/original.iso:ro", |
| 94 | "--volume", f"{seed}:/seed:ro", |
| 95 | "--volume", f"{media.parent}:/output", |
| 96 | image, |
| 97 | ] |
| 98 | original, files, output = "/original.iso", "/seed", f"/output/{partial.name}" |
| 99 | subprocess.run( |
| 100 | prefix + [ |
| 101 | "-indev", original, |
| 102 | "-outdev", output, |
| 103 | "-map", f"{files}/answers.json", "/private/answers.json", |
| 104 | "-map", f"{files}/loader.conf.local", "/boot/loader.conf.local", |
| 105 | "-boot_image", "any", "replay", |
| 106 | "-commit", "-end", |
| 107 | ], |
| 108 | check=True, |
| 109 | ) |
| 110 | partial.chmod(0o600) |
| 111 | partial.replace(media) |
| 112 | |
| 113 | |
| 114 | def provision(ctx): |
| 115 | backend = ctx.state["backend"] |
| 116 | # OpenSSH tries the persistent key first, then the first-boot password. |
| 117 | # This also recovers an interrupted key install or a guest still rebooting. |
| 118 | password = backend["bootstrap_password"] |
| 119 | ctx.wait_ssh(password=password, timeout=900) |
| 120 | ctx.ssh( |
| 121 | (HERE / "configure-ssh.sh").read_text(), |
| 122 | password=password, |
| 123 | input=ctx.public_key.strip() + "\n", |
| 124 | ) |
| 125 | ctx.wait_ssh(timeout=60) |
| 126 | |
| 127 | # The isolated bridge intentionally does not forward traffic, but Specter's |
| 128 | # outbound-address selection still needs a local default route. |
| 129 | gateway = ctx.network["gateway"] |
| 130 | ctx.ssh( |
| 131 | "set -eu; umask 077; sed " |
| 132 | + shlex.quote(f"s/^headnode_default_gateway=.*/headnode_default_gateway={gateway}/") |
| 133 | + " /usbkey/config > /usbkey/config.new; " |
| 134 | "mv /usbkey/config.new /usbkey/config; " |
| 135 | f"route -n add default {shlex.quote(gateway)} >/dev/null 2>&1 || " |
| 136 | f"route -n change default {shlex.quote(gateway)}" |
| 137 | ) |
| 138 | |
| 139 | staging = "/var/tmp/specter-vm" |
| 140 | ctx.ssh(f"umask 077; mkdir -p {staging}") |
| 141 | if ctx.ssh(f"imgadm get {IMAGE_UUID} >/dev/null 2>&1", check=False).returncode: |
| 142 | with urllib.request.urlopen(IMAGE_URL, timeout=60) as response: |
| 143 | manifest = json.load(response) |
| 144 | if ( |
| 145 | manifest.get("uuid") != IMAGE_UUID |
| 146 | or manifest.get("name") != "base-64-lts" |
| 147 | or manifest.get("version") != "25.4.0" |
| 148 | or manifest.get("files", [{}])[0].get("sha1") != IMAGE_SHA1 |
| 149 | ): |
| 150 | raise RuntimeError("SmartOS zone manifest differs from the pinned image") |
| 151 | manifest_path = ctx.state_dir / "smartos-image.json" |
| 152 | manifest_path.write_text(json.dumps(manifest) + "\n") |
| 153 | archive = ctx.fetch(IMAGE_URL + "/file", IMAGE_SHA1, algorithm="sha1") |
| 154 | ctx.upload(manifest_path, f"{staging}/image.json") |
| 155 | ctx.upload(archive, f"{staging}/image.zfs.gz") |
| 156 | |
| 157 | zone = { |
| 158 | "uuid": backend["zone_uuid"], |
| 159 | "brand": "joyent", |
| 160 | "image_uuid": IMAGE_UUID, |
| 161 | "alias": "specter-native", |
| 162 | "hostname": "specter-native", |
| 163 | "autoboot": True, |
| 164 | "max_physical_memory": 2048, |
| 165 | "quota": 8, |
| 166 | "nics": [{"nic_tag": "admin", "ips": ["dhcp"], "primary": True}], |
| 167 | } |
| 168 | zone_path = ctx.state_dir / "smartos-zone.json" |
| 169 | zone_path.write_text(json.dumps(zone) + "\n") |
| 170 | ctx.upload(zone_path, f"{staging}/zone.json") |
| 171 | script = (HERE / "provision-zone.sh").read_text() |
| 172 | ctx.ssh( |
| 173 | f"/bin/sh -c {shlex.quote(script)} provision-zone " |
| 174 | f"{shlex.quote(backend['zone_uuid'])} {IMAGE_UUID} {shlex.quote(gateway)}", |
| 175 | timeout=900, |
| 176 | ) |
| 177 | |
| 178 | |
| 179 | def workspace(ctx): |
| 180 | return f"/zones/{ctx.state['backend']['zone_uuid']}/root{GUEST_ROOT}", GUEST_ROOT |
| 181 | |
| 182 | |
| 183 | def prepare_workspace(ctx): |
| 184 | zone_uuid = ctx.state["backend"]["zone_uuid"] |
| 185 | ctx.ssh(f"zlogin {shlex.quote(zone_uuid)} chown -R specter:specter {GUEST_ROOT}") |
| 186 | |
| 187 | |
| 188 | def command(ctx, script): |
| 189 | zone_uuid = ctx.state["backend"]["zone_uuid"] |
| 190 | script = "export TMPDIR=/var/lib/specter-native/tmp; " + script |
| 191 | # zlogin reconstructs its command through a login shell. Pass the script on |
| 192 | # stdin so that an extra shell does not consume quotes or regex anchors. |
| 193 | return ( |
| 194 | f"printf '%s\\n' {shlex.quote(script)} | " |
| 195 | f"zlogin -l specter {shlex.quote(zone_uuid)} /bin/sh -s" |
| 196 | ) |