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 | |
| 29 | def Main(): |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 30 | parser = argparse.ArgumentParser() |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame^] | 31 | parser.add_argument("-f", "--file", |
| 32 | action="append", nargs=2, |
| 33 | metavar=("NAME", "PATH"), |
| 34 | help="File at host location PATH to be added to the RAM disk as NAME") |
| 35 | parser.add_argument("-s", "--staging", required=True) |
| 36 | parser.add_argument("-o", "--output", required=True) |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 37 | args = parser.parse_args() |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 38 | |
| 39 | # Create staging folder if needed. |
| 40 | if not os.path.isdir(args.staging): |
| 41 | os.makedirs(args.staging) |
| 42 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame^] | 43 | # Copy files into the staging folder. |
| 44 | staged_files = [] |
| 45 | for name, path in args.file: |
| 46 | shutil.copyfile(path, os.path.join(args.staging, name)) |
| 47 | assert name not in staged_files |
| 48 | staged_files.append(name) |
| 49 | |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 50 | # Package files into an initial RAM disk. |
| 51 | with open(args.output, "w") as initrd: |
| 52 | # Move into the staging directory so the file names taken by cpio don't |
| 53 | # include the path. |
| 54 | os.chdir(args.staging) |
| 55 | cpio = subprocess.Popen( |
| 56 | ["cpio", "--create"], |
| 57 | stdin=subprocess.PIPE, |
| 58 | stdout=initrd, |
| 59 | stderr=subprocess.PIPE) |
| 60 | cpio.communicate(input="\n".join(staged_files).encode("utf-8")) |
| 61 | return 0 |
| 62 | |
Andrew Scull | 2c24233 | 2018-08-08 13:30:32 +0100 | [diff] [blame] | 63 | if __name__ == "__main__": |
Andrew Scull | 4b0a32e | 2018-08-08 16:38:17 +0100 | [diff] [blame] | 64 | sys.exit(Main()) |