blob: 5e89e63055e3e262e81620d7990a4c9668e57cac [file] [log] [blame]
Andrew Scull2c242332018-08-08 13:30:32 +01001#!/usr/bin/env python
Andrew Scull18834872018-10-12 11:48:09 +01002#
3# Copyright 2018 Google LLC
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
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()
32 parser.add_argument("--primary_vm", required=True)
33 parser.add_argument("--primary_vm_initrd")
34 parser.add_argument(
35 "--secondary_vm",
36 action="append",
37 nargs=4,
38 metavar=("MEMORY", "CORES", "NAME", "IMAGE"))
39 parser.add_argument("--staging", required=True)
40 parser.add_argument("--output", required=True)
41 args = parser.parse_args()
42 # Prepare the primary VM image.
43 staged_files = ["vmlinuz"]
44 shutil.copyfile(args.primary_vm, os.path.join(args.staging, "vmlinuz"))
45 # Prepare the primary VM's initrd. Currently, it just makes an empty one.
46 if args.primary_vm_initrd:
47 raise NotImplementedError(
48 "This doesn't copy the primary VM's initrd yet")
49 with open(os.path.join(args.staging, "initrd.img"), "w") as vms_txt:
50 staged_files.append("initrd.img")
51 # Prepare the secondary VMs.
52 with open(os.path.join(args.staging, "vms.txt"), "w") as vms_txt:
53 staged_files.append("vms.txt")
54 if args.secondary_vm:
55 for vm in args.secondary_vm:
56 (vm_memory, vm_cores, vm_name, vm_image) = vm
57 staged_files.append(vm_name)
58 shutil.copy(vm_image, os.path.join(args.staging, vm_name))
59 vms_txt.write("{} {} {}\n".format(vm_memory, vm_cores, vm_name))
60 # Package files into an initial RAM disk.
61 with open(args.output, "w") as initrd:
62 # Move into the staging directory so the file names taken by cpio don't
63 # include the path.
64 os.chdir(args.staging)
65 cpio = subprocess.Popen(
66 ["cpio", "--create"],
67 stdin=subprocess.PIPE,
68 stdout=initrd,
69 stderr=subprocess.PIPE)
70 cpio.communicate(input="\n".join(staged_files).encode("utf-8"))
71 return 0
72
Andrew Scull2c242332018-08-08 13:30:32 +010073
74if __name__ == "__main__":
Andrew Scull4b0a32e2018-08-08 16:38:17 +010075 sys.exit(Main())