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