summaryrefslogtreecommitdiff
path: root/v-i
blob: 49721e4ee4a0d42a67a1cf9977af3c590b47deec (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/python3

import argparse
import glob
import os
import shutil
import sys
import tempfile
import yaml
from subprocess import run


def log(msg):
    print("INSTALLER:", msg)


def physical_volumes():
    log("list physical volumes")
    p = run(["pvdisplay", "-C", "--noheadings"], capture_output=True, check=True)
    lines = p.stdout.decode().splitlines()
    pvs = []
    for line in lines:
        words = line.split()
        pv = words[0]
        vg = words[1]
        pvs.append({"pv": pv, "vg": vg})
    return pvs


def logical_volumes():
    log("list logical volumes")
    p = run(["lvdisplay", "-C", "--noheadings"], capture_output=True, check=True)
    lines = p.stdout.decode().splitlines()
    lvs = []
    for line in lines:
        words = line.split()
        lv = words[0]
        vg = words[1]
        path = os.path.realpath(f"/dev/{vg}/{lv}")
        lvs.append({"lv": lv, "vg": vg, "path": path})
    return lvs


def volume_groups():
    log("list volume groups")
    p = run(["vgdisplay", "-C", "--noheadings"], capture_output=True, check=True)
    lines = p.stdout.decode().splitlines()
    return [line.split()[0] for line in lines if line.strip()]


def mount_points():
    log("find mount all points")
    mounts = {}
    for line in open("/proc/mounts").readlines():
        words = line.split()
        dev = os.path.realpath(words[0]) if words[0].startswith("/") else words[0]
        mounts[dev] = {
            "mount": words[1],
            "type": words[2],
        }
    return mounts


def find_mount_points(mounts, dev):
    log(f"find mount points using {dev}")
    res = []
    if dev in mounts:
        root = mounts[dev]["mount"]
        for m in mounts.values():
            if m["mount"].startswith(root):
                res.append(m)
    return res


def is_luks(path):
    log(f"is {path} a LUKS device?")
    p = run(["cryptsetup", "isLuks", path], check=False)
    return p.returncode == 0


def clean_up_disks():
    log("clean up disks from old installs")

    mounts = mount_points()
    pvs = physical_volumes()
    lvs = logical_volumes()
    vgs = volume_groups()

    for lv in lvs:
        for m in find_mount_points(mounts, lv["path"]):
            log(f"unmount {m['mount']}")
            run(["umount", m["mount"]])

    for vg in vgs:
        log(f"remove volume group {vg}")
        run(["vgremove", "--yes", vg], check=True)

    for pv in pvs:
        log(f"remove physical volume {pv}")
        run(["pvremove", pv["pv"]], check=True)
        if is_luks(pv["pv"]):
            run(["cryptsetup", "close", pv["pv"]], check=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)


def vmdb_spec(cryptsetup_password, playbook, extra_vars):
    device = "{{ image }}"
    spec = {
        "steps": [
            {
                "mklabel": "gpt",
                "device": device,
            },
            {
                "mkpart": "primary",
                "device": device,
                "start": "0%",
                "end": "500M",
                "tag": "efi",
            },
            {
                "mkpart": "primary",
                "device": device,
                "start": "500M",
                "end": "1G",
                "tag": "boot",
            },
        ]
    }

    # Set up pv0 for lvm2, either encrypted or cleartext.
    if cryptsetup_password:
        spec["steps"].extend(
            [
                {
                    "mkpart": "primary",
                    "device": device,
                    "start": "1G",
                    "end": "100%",
                    "tag": "cryptsetup0",
                },
                {
                    "cryptsetup": "cryptsetup0",
                    "password": cryptsetup_password,
                    "name": "pv0",
                },
            ]
        )
    else:
        spec["steps"].extend(
            [
                {
                    "mkpart": "primary",
                    "device": device,
                    "start": "1G",
                    "end": "100%",
                    "tag": "pv0",
                },
            ]
        )

    # Create file systems and install Debian.
    spec["steps"].extend(
        [
            {
                "mkfs": "vfat",
                "partition": "efi",
            },
            {
                "mkfs": "ext2",
                "partition": "boot",
            },
            {
                "vgcreate": "vg0",
                "physical": ["pv0"],
            },
            {
                "lvcreate": "vg0",
                "name": "root",
                "size": "10G",
            },
            {
                "mkfs": "ext4",
                "partition": "root",
            },
            {
                "mount": "root",
            },
            {
                "mount": "boot",
                "dirname": "/boot",
                "mount-on": "root",
            },
            {
                "mount": "efi",
                "dirname": "/boot/efi",
                "mount-on": "boot",
            },
            {
                "unpack-rootfs": "root",
            },
            {
                "debootstrap": "bullseye",
                "mirror": "http://deb.debian.org/debian",
                "target": "root",
                "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",
            },
            {
                # This MUST be after the debootstrap step.
                "virtual-filesystems": "root",
            },
            {
                "fstab": "root",
            },
            {
                # These MUST come after the fstab step so that they add the
                # crypttab in the initramfs.
                "apt": "install",
                "packages": [
                    "cryptsetup",
                    "cryptsetup-initramfs",
                ],
                "tag": "root",
            },
            {
                # This also MUST come outside the rootfs caching, as it install
                # things outside the file systems.
                "grub": "uefi",
                "tag": "root",
                "efi": "efi",
                "quiet": True,
                "image-dev": device,
            },
        ]
    )

    # If a playbook has been specified, add an ansible step.
    if playbook:
        spec["steps"].append(
            {"ansible": "root", "playbook": playbook, "extra_vars": extra_vars}
        )

    return spec


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--verbose", action="store_true")
    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()

    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, extra_vars)
    tmp = tempfile.mkdtemp()
    specfile = os.path.join(tmp, "spec.yaml")
    if args.verbose:
        yaml.dump(spec, stream=sys.stdout, indent=4)
    with open(specfile, "w") as f:
        yaml.dump(spec, stream=f, indent=4)

    log(f"run vmdb2 to install on {args.device}")
    env = dict(os.environ)
    env["ANSIBLE_STDOUT_CALLBACK"] = "yaml"
    env["ANSIBLE_NOCOWS"] = "1"
    env["ANSIBLE_LOG_PATH"] = "ansible.log"
    run(
        [
            "vmdb2",
            "--verbose",
            f"--rootfs-tarball={args.cache}",
            f"--log={args.log}",
            f"--image={args.device}",
            specfile,
        ],
        check=True,
    )

    log("cleanup")
    shutil.rmtree(tmp)

    log("OK, done")


main()