blob: 4d8f28186b3ee6b76e3230b0048362000ae24351 [file] [log] [blame]
Andrew Scull2c242332018-08-08 13:30:32 +01001#!/usr/bin/env python
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#
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 Scull2c242332018-08-08 13:30:32 +010017"""Generate an initial RAM disk for the hypervisor.
18
19Packages the VMs, initrds for the VMs and the list of secondary VMs (vms.txt)
20into an initial RAM disk image.
21"""
22
23import argparse
24import os
25import shutil
26import subprocess
27import sys
28
Andrew Scull4b0a32e2018-08-08 16:38:17 +010029
Andrew Scull2c242332018-08-08 13:30:32 +010030def Main():
Andrew Scull4b0a32e2018-08-08 16:38:17 +010031 parser = argparse.ArgumentParser()
Andrew Scull72b43c02019-09-18 13:53:45 +010032 parser.add_argument("--primary_name", required=True)
Andrew Scull4b0a32e2018-08-08 16:38:17 +010033 parser.add_argument("--primary_vm", required=True)
34 parser.add_argument("--primary_vm_initrd")
35 parser.add_argument(
36 "--secondary_vm",
37 action="append",
David Brazdil7a462ec2019-08-15 12:27:47 +010038 nargs=2,
39 metavar=("NAME", "IMAGE"))
Andrew Scull4b0a32e2018-08-08 16:38:17 +010040 parser.add_argument("--staging", required=True)
41 parser.add_argument("--output", required=True)
42 args = parser.parse_args()
Andrew Scull72b43c02019-09-18 13:53:45 +010043 staged_files = [args.primary_name, "initrd.img"]
David Brazdil7a462ec2019-08-15 12:27:47 +010044
45 # Create staging folder if needed.
46 if not os.path.isdir(args.staging):
47 os.makedirs(args.staging)
48
Andrew Scull4b0a32e2018-08-08 16:38:17 +010049 # Prepare the primary VM image.
Andrew Scull72b43c02019-09-18 13:53:45 +010050 shutil.copyfile(args.primary_vm,
51 os.path.join(args.staging, args.primary_name))
Andrew Walbranbc342d42019-02-05 16:56:02 +000052 # Prepare the primary VM's initrd.
Andrew Scull4b0a32e2018-08-08 16:38:17 +010053 if args.primary_vm_initrd:
Andrew Scull72b43c02019-09-18 13:53:45 +010054 shutil.copyfile(args.primary_vm_initrd,
55 os.path.join(args.staging, "initrd.img"))
Andrew Walbranbc342d42019-02-05 16:56:02 +000056 else:
57 open(os.path.join(args.staging, "initrd.img"), "w").close()
Andrew Scull4b0a32e2018-08-08 16:38:17 +010058 # Prepare the secondary VMs.
David Brazdil7a462ec2019-08-15 12:27:47 +010059 if args.secondary_vm:
60 for vm in args.secondary_vm:
61 (vm_name, vm_image) = vm
62 staged_files.append(vm_name)
63 shutil.copy(vm_image, os.path.join(args.staging, vm_name))
Andrew Scull4b0a32e2018-08-08 16:38:17 +010064 # Package files into an initial RAM disk.
65 with open(args.output, "w") as initrd:
66 # Move into the staging directory so the file names taken by cpio don't
67 # include the path.
68 os.chdir(args.staging)
69 cpio = subprocess.Popen(
70 ["cpio", "--create"],
71 stdin=subprocess.PIPE,
72 stdout=initrd,
73 stderr=subprocess.PIPE)
74 cpio.communicate(input="\n".join(staged_files).encode("utf-8"))
75 return 0
76
Andrew Scull2c242332018-08-08 13:30:32 +010077
78if __name__ == "__main__":
Andrew Scull4b0a32e2018-08-08 16:38:17 +010079 sys.exit(Main())