blob: 488745f25d3919aa3b17bcce6dcee05b2d237e8f [file] [log] [blame]
Andrew Scull2c242332018-08-08 13:30:32 +01001#!/usr/bin/env python
2
3"""Generate an initial RAM disk for the hypervisor.
4
5Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt)
6into an initial RAM disk image.
7"""
8
9import argparse
10import os
11import shutil
12import subprocess
13import sys
14
15def Main():
16 parser = argparse.ArgumentParser()
17 parser.add_argument("--primary_vm", required=True)
18 parser.add_argument("--primary_vm_initrd")
19 parser.add_argument("--secondary_vm", action="append", nargs=4, metavar=("MEMORY", "CORES", "NAME", "IMAGE"))
20 parser.add_argument("--staging", required=True)
21 parser.add_argument("--output", required=True)
22 args = parser.parse_args()
23 # Prepare the primary VM image.
24 staged_files = ["vmlinuz"]
25 shutil.copyfile(args.primary_vm, os.path.join(args.staging, "vmlinuz"))
26 # Prepare the primary VM's initrd. Currently, it just makes an empty one.
27 if args.primary_vm_initrd:
28 raise NotImplementedError("This doesn't copy the primary VM's initrd yet")
29 with open(os.path.join(args.staging, "initrd.img"), "w") as vms_txt:
30 staged_files.append("initrd.img")
31 # Prepare the secondary VMs.
32 with open(os.path.join(args.staging, "vms.txt"), "w") as vms_txt:
33 staged_files.append("vms.txt")
34 if args.secondary_vm:
35 for vm in args.secondary_vm:
36 (vm_memory, vm_cores, vm_name, vm_image) = vm
37 staged_files.append(vm_name)
38 shutil.copy(vm_image, os.path.join(args.staging, vm_name))
39 vms_txt.write("{} {} {}\n".format(vm_memory, vm_cores, vm_name))
40 # Package files into an initial RAM disk.
41 with open(args.output, "w") as initrd:
42 # Move into the staging directory so the file names taken by cpio don't
43 # include the path.
44 os.chdir(args.staging)
45 cpio = subprocess.Popen([
46 "cpio",
47 "--create"],
48 stdin=subprocess.PIPE,
49 stdout=initrd,
50 stderr=subprocess.PIPE)
51 cpio.communicate(input="\n".join(staged_files).encode("utf-8"))
52 return 0
53
54if __name__ == "__main__":
55 sys.exit(Main())