Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 2 | """Generate an initial RAM disk for the hypervisor. |
| 3 | |
| 4 | Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt) |
| 5 | into an initial RAM disk image. |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import os |
| 10 | import shutil |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame^] | 14 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 15 | def Main(): |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame^] | 16 | 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 Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 58 | |
| 59 | if __name__ == "__main__": |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame^] | 60 | sys.exit(Main()) |