Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 2 | # |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 17 | """Generate an initial RAM disk for the hypervisor. |
| 18 | |
| 19 | Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt) |
| 20 | into an initial RAM disk image. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import os |
| 25 | import shutil |
| 26 | import subprocess |
| 27 | import sys |
| 28 | |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 29 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 30 | def Main(): |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 31 | parser = argparse.ArgumentParser() |
| 32 | parser.add_argument("--primary_vm", required=True) |
| 33 | parser.add_argument("--primary_vm_initrd") |
| 34 | parser.add_argument( |
| 35 | "--secondary_vm", |
| 36 | action="append", |
| 37 | nargs=4, |
| 38 | metavar=("MEMORY", "CORES", "NAME", "IMAGE")) |
| 39 | parser.add_argument("--staging", required=True) |
| 40 | parser.add_argument("--output", required=True) |
| 41 | args = parser.parse_args() |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 42 | staged_files = ["vmlinuz", "initrd.img"] |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 43 | # Prepare the primary VM image. |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 44 | shutil.copyfile(args.primary_vm, os.path.join(args.staging, "vmlinuz")) |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 45 | # Prepare the primary VM's initrd. |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 46 | if args.primary_vm_initrd: |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 47 | shutil.copyfile(args.primary_vm_initrd, os.path.join(args.staging, "initrd.img")) |
| 48 | else: |
| 49 | open(os.path.join(args.staging, "initrd.img"), "w").close() |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 50 | # Prepare the secondary VMs. |
| 51 | with open(os.path.join(args.staging, "vms.txt"), "w") as vms_txt: |
| 52 | staged_files.append("vms.txt") |
| 53 | if args.secondary_vm: |
| 54 | for vm in args.secondary_vm: |
| 55 | (vm_memory, vm_cores, vm_name, vm_image) = vm |
| 56 | staged_files.append(vm_name) |
| 57 | shutil.copy(vm_image, os.path.join(args.staging, vm_name)) |
| 58 | vms_txt.write("{} {} {}\n".format(vm_memory, vm_cores, vm_name)) |
| 59 | # Package files into an initial RAM disk. |
| 60 | with open(args.output, "w") as initrd: |
| 61 | # Move into the staging directory so the file names taken by cpio don't |
| 62 | # include the path. |
| 63 | os.chdir(args.staging) |
| 64 | cpio = subprocess.Popen( |
| 65 | ["cpio", "--create"], |
| 66 | stdin=subprocess.PIPE, |
| 67 | stdout=initrd, |
| 68 | stderr=subprocess.PIPE) |
| 69 | cpio.communicate(input="\n".join(staged_files).encode("utf-8")) |
| 70 | return 0 |
| 71 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 72 | |
| 73 | if __name__ == "__main__": |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 74 | sys.exit(Main()) |