David Brazdil | 6c63a26 | 2019-12-23 13:23:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2019 The Hafnium Authors. |
| 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 Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 8 | |
| 9 | """Generate an initial RAM disk for a Linux VM.""" |
| 10 | |
| 11 | import argparse |
| 12 | import os |
| 13 | import shutil |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | def Main(): |
| 18 | parser = argparse.ArgumentParser() |
| 19 | parser.add_argument("--staging", required=True) |
| 20 | parser.add_argument("--output", required=True) |
| 21 | args = parser.parse_args() |
| 22 | # Package files into an initial RAM disk. |
| 23 | with open(args.output, "w") as initrd: |
| 24 | # Move into the staging directory so the file names taken by cpio don't |
| 25 | # include the path. |
| 26 | os.chdir(args.staging) |
| 27 | staged_files = [os.path.join(root, filename) |
| 28 | for (root, dirs, files) in os.walk(".") for filename in files + dirs] |
| 29 | cpio = subprocess.Popen( |
| 30 | ["cpio", "--create", "--format=newc"], |
| 31 | stdin=subprocess.PIPE, |
| 32 | stdout=initrd, |
| 33 | stderr=subprocess.PIPE) |
| 34 | cpio.communicate(input="\n".join(staged_files).encode("utf-8")) |
| 35 | return 0 |
| 36 | |
| 37 | |
| 38 | if __name__ == "__main__": |
| 39 | sys.exit(Main()) |