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