David Brazdil | 6c63a26 | 2019-12-23 13:23:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame^] | 5 | # Use of this source code is governed by a BSD-style |
| 6 | # license that can be found in the LICENSE file or at |
| 7 | # https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 8 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 9 | """Generate an initial RAM disk for the hypervisor. |
| 10 | |
| 11 | Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt) |
| 12 | into an initial RAM disk image. |
| 13 | """ |
| 14 | |
| 15 | import argparse |
| 16 | import os |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import sys |
| 20 | |
| 21 | def Main(): |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 22 | parser = argparse.ArgumentParser() |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 23 | parser.add_argument("-f", "--file", |
| 24 | action="append", nargs=2, |
| 25 | metavar=("NAME", "PATH"), |
| 26 | help="File at host location PATH to be added to the RAM disk as NAME") |
| 27 | parser.add_argument("-s", "--staging", required=True) |
| 28 | parser.add_argument("-o", "--output", required=True) |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 29 | args = parser.parse_args() |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 30 | |
| 31 | # Create staging folder if needed. |
| 32 | if not os.path.isdir(args.staging): |
| 33 | os.makedirs(args.staging) |
| 34 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 35 | # Copy files into the staging folder. |
| 36 | staged_files = [] |
| 37 | for name, path in args.file: |
| 38 | shutil.copyfile(path, os.path.join(args.staging, name)) |
| 39 | assert name not in staged_files |
| 40 | staged_files.append(name) |
| 41 | |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 42 | # Package files into an initial RAM disk. |
| 43 | with open(args.output, "w") as initrd: |
| 44 | # Move into the staging directory so the file names taken by cpio don't |
| 45 | # include the path. |
| 46 | os.chdir(args.staging) |
| 47 | cpio = subprocess.Popen( |
| 48 | ["cpio", "--create"], |
| 49 | stdin=subprocess.PIPE, |
| 50 | stdout=initrd, |
| 51 | stderr=subprocess.PIPE) |
| 52 | cpio.communicate(input="\n".join(staged_files).encode("utf-8")) |
| 53 | return 0 |
| 54 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 55 | if __name__ == "__main__": |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 56 | sys.exit(Main()) |