-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-benchmark-rootfs.py
executable file
·161 lines (132 loc) · 4.87 KB
/
gen-benchmark-rootfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import shutil
import time
from contextlib import contextmanager
import errno
import sys
from pathlib import Path
sys.path.append("firemarshal-symlink")
from wlutil.wlutil import mountImg
BUILD_DIR = 'build'
MOUNT_POINT = BUILD_DIR + "/mount_point"
EXT_TYPE = "ext2"
INIT_SCRIPT_NAME = '/etc/init.d/S99run'
init_script_head = """#!/bin/sh
#
SYSLOGD_ARGS=-n
KLOGD_ARGS=-n
start() {
"""
init_script_tail = """
sync
poweroff -f
}
case "$1" in
start)
start
;;
stop)
#stop
;;
restart|reload)
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit
"""
def copy_base_rootfs(base_rootfs, dest):
try:
os.makedirs(BUILD_DIR)
except OSError:
pass
print("Copying base rootfs {} to {}".format(base_rootfs, dest))
shutil.copy2(base_rootfs, dest)
def cp_target(src, target_dest):
print("Copying src: {} to {} in target filesystem.".format(src, target_dest))
if not os.path.isdir(target_dest):
dirname = os.path.dirname(target_dest)
else:
dirname = target_dest
subprocess.check_output(["mkdir", "-p", MOUNT_POINT + "/" + dirname])
rc = subprocess.check_output(["cp", "-dpR", src, "-T", MOUNT_POINT + "/" + target_dest])
def chmod_target(permissions, target_dest):
subprocess.check_output(["chmod", permissions, MOUNT_POINT + "/" + target_dest])
def copy_files_to_mounted_fs(overlay, deliver_dir, files):
for f in files:
src = overlay + "/" + f
target_local_dest = "/" + deliver_dir + "/" + f
cp_target(src, target_local_dest)
def generate_init_script(command):
print("Creating init script with command:\n " + command)
init_script_body = init_script_head + " " + command + init_script_tail
temp_script = BUILD_DIR + "/temp"
with open(temp_script, 'w') as f:
f.write(init_script_body)
cp_target(temp_script, INIT_SCRIPT_NAME)
chmod_target('755', INIT_SCRIPT_NAME)
class Workload:
def __init__(self, name, deliver_dir, files, command, args, outputs):
self.name = name
self.deliver_dir = deliver_dir
self.files = files
self.command = command
for arg in args:
self.command += " " + arg
self.outputs = outputs
def generate_rootfs(self, base_rootfs, overlay, gen_init, output_dir):
print("\nGenerating a Rootfs image for " + self.name)
dest_rootfs = output_dir + "/" + self.name + "." + EXT_TYPE
copy_base_rootfs(base_rootfs, dest_rootfs)
os.makedirs(MOUNT_POINT, exist_ok=True)
with mountImg(Path(dest_rootfs), Path(MOUNT_POINT)):
copy_files_to_mounted_fs(overlay, self.deliver_dir, self.files)
if gen_init:
generate_init_script(self.command)
def __str__(self):
return("""
Name: {}
Files : {}
Command: {}
""".format(self.name, str(self.files), self.command))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='FireSim Benchmark RootFS Generator.')
parser.add_argument('-w', '--benchmark', type=str, help='Benchmark JSON.')
parser.add_argument('-s', '--overlay-dir', type=str, dest='overlay_dir',
help='Overlay location that files specified in the json can be found.')
parser.add_argument('-o', '--output-dir',
dest='output_dir',
type=str,
help='Location to store filesystem images. Default: ./<benchmark-name>/')
parser.add_argument('-r', '--run-on-init',
dest='init',
action='store_true',
help='Emits an init script to run the workload and power off the instance.')
parser.add_argument('-b', '--base', type=str, help='The base filesystem image')
args = parser.parse_args()
with open(args.benchmark) as jsonf:
benchmark_config = json.load(jsonf)
benchmark_name = benchmark_config["benchmark_name"]
common_args = benchmark_config["common_args"]
common_files = benchmark_config["common_files"]
common_outputs = benchmark_config["common_outputs"]
overlay_dir = args.overlay_dir
deliver_dir = benchmark_config["deliver_dir"]
output_dir = args.output_dir if args.output_dir is not None else benchmark_name
workloads = []
for workload_def in benchmark_config["workloads"]:
workloads.append(Workload(workload_def["name"],
deliver_dir,
workload_def["files"] + common_files,
workload_def["command"],
common_args,
workload_def["outputs"] + common_outputs))
subprocess.check_output(["mkdir", "-p", output_dir])
for workload in workloads:
workload.generate_rootfs(args.base, overlay_dir, args.init, output_dir)