forked from coreos/coreos-assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunvm-osbuild
executable file
·103 lines (87 loc) · 3.2 KB
/
runvm-osbuild
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
#!/bin/bash
set -eux -o pipefail
usage() {
cat <<EOC
${0} creates a supermin virtual machine that then
runs OSBuild to build a CoreOS style disk image.
Options:
--config: JSON-formatted image.yaml
--help: show this help
--mpp: the path to the OSBuild mpp.yaml file
--filepath: where to write the created image file
You probably don't want to run this script by hand. This script is
run as part of 'coreos-assembler build'.
EOC
}
# Parse the passed config JSON and extract a mandatory value
getconfig() {
k=$1
jq -re .\""$k"\" < "${config}"
}
# Return a configuration value, or default if not set
getconfig_def() {
k=$1
shift
default=$1
shift
jq -re .\""$k"\"//\""${default}"\" < "${config}"
}
while [ $# -gt 0 ];
do
flag="${1}"; shift;
case "${flag}" in
--config) config="${1}"; shift;;
--help) usage; exit;;
--mpp) mppyaml="${1}"; shift;;
--filepath) filepath="${1}"; shift;;
*) echo "${flag} is not understood."; usage; exit 10;;
esac;
done
# Get the base filename of the desired file output path
filename=$(basename "$filepath")
ostree_repo=$(getconfig_def "ostree-repo" "")
ostree_ref=$(getconfig_def "ostree-ref" "")
osname=$(getconfig "osname")
platform=$(getconfig "image-type")
deploy_via_container=$(getconfig_def "deploy-via-container" "")
metal_image_size_mb=$(getconfig "metal-image-size")
cloud_image_size_mb=$(getconfig "cloud-image-size")
container_imgref=$(getconfig "container-imgref")
# If we are deploying via container let's go ahead and pull
# the oci archive path from the config
ostree_container=""
if [ -n "${deploy_via_container}" ]; then
ostree_container=$(getconfig "ostree-container")
fi
# Since it doesn't exist create loop-control
[ ! -e /dev/loop-control ] && mknod /dev/loop-control c 10 237
# Put the store and the output dir on the cache. At the end we'll mv
# out the created artifact from the output dir to the place it's supposed
# to go.
outdir=cache/osbuild/out
storedir=cache/osbuild/store
processed_json=$(mktemp -t osbuild-XXXX.json)
# Run through the preprocessor
# Note: don't quote the size arguements since they are numbers, not strings
osbuild-mpp \
-D ref=\""${ostree_ref}"\" \
-D repourl=\""file://${ostree_repo}"\" \
-D filename=\""${filename}"\" \
-D ociarchive=\""${ostree_container}"\" \
-D osname=\""${osname}"\" \
-D container_imgref=\""${container_imgref}"\" \
-D metal_image_size_mb="${metal_image_size_mb}" \
-D cloud_image_size_mb="${cloud_image_size_mb}" \
"${mppyaml}" "${processed_json}"
# Build the image
osbuild \
--out "$outdir" \
--store "$storedir" \
--cache-max-size 9GiB \
--checkpoint tree \
--checkpoint raw-image \
--export "$platform" "${processed_json}"
# Copy it out to the specified location. Use mv here so we remove it
# from the cache qcow2 so we don't cache it.
mv "${outdir}/${platform}/${filename}" "${filepath}"
rm -f "${processed_json}"