1#!/usr/bin/env python3 2# 3# Copyright 2018 The Hafnium Authors. 4# 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. 8 9"""Generate an initial RAM disk for the hypervisor. 10 11Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt) 12into an initial RAM disk image. 13""" 14 15import argparse 16import os 17import shutil 18import subprocess 19import sys 20 21def Main(): 22 parser = argparse.ArgumentParser() 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) 29 args = parser.parse_args() 30 31 # Create staging folder if needed. 32 if not os.path.isdir(args.staging): 33 os.makedirs(args.staging) 34 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 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 55if __name__ == "__main__": 56 sys.exit(Main()) 57