blob: d5e94be2ccd7b890c0d38e0dfa84e6d44de9b86f [file] [log] [blame]
David Brazdil6c63a262019-12-23 13:23:46 +00001#!/usr/bin/env python3
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
Andrew Walbrane959ec12020-06-17 15:01:09 +01005# 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 Scull18834872018-10-12 11:48:09 +01008
Andrew Scull2c242332018-08-08 13:30:32 +01009"""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():
Andrew Scull4b0a32e2018-08-08 16:38:17 +010022 parser = argparse.ArgumentParser()
David Brazdile6f83222019-09-23 14:47:37 +010023 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 Scull4b0a32e2018-08-08 16:38:17 +010029 args = parser.parse_args()
David Brazdil7a462ec2019-08-15 12:27:47 +010030
31 # Create staging folder if needed.
32 if not os.path.isdir(args.staging):
33 os.makedirs(args.staging)
34
David Brazdile6f83222019-09-23 14:47:37 +010035 # 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 Scull4b0a32e2018-08-08 16:38:17 +010042 # 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 Scull2c242332018-08-08 13:30:32 +010055if __name__ == "__main__":
Andrew Scull4b0a32e2018-08-08 16:38:17 +010056 sys.exit(Main())