-
Notifications
You must be signed in to change notification settings - Fork 997
/
build_image.sh
executable file
·194 lines (168 loc) · 4.5 KB
/
build_image.sh
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
[ -z "${DEBUG}" ] || set -x
set -o errexit
set -o nounset
set -o pipefail
readonly script_name="$(basename "${BASH_SOURCE[0]}")"
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly packaging_root_dir="$(cd "${script_dir}/../" && pwd)"
source "${packaging_root_dir}/scripts/lib.sh"
readonly osbuilder_dir="$(cd "${repo_root_dir}/tools/osbuilder" && pwd)"
export GOPATH=${GOPATH:-${HOME}/go}
ARCH=${ARCH:-$(uname -m)}
if [ $(uname -m) == "${ARCH}" ]; then
arch_target="$(uname -m)"
else
arch_target="${ARCH}"
fi
final_artifact_name="kata-containers"
image_initrd_extension=".img"
build_initrd() {
info "Build initrd"
info "initrd os: $os_name"
info "initrd os version: $os_version"
# ROOTFS_BUILD_DEST is a Make variable
# SNP will also use the SEV guest module
if [[ "${AA_KBC:-}" == "offline_sev_kbc" || "${AA_KBC:-}" == "online_sev_kbc" ]]; then
config_version=$(get_config_version)
kernel_version="$(get_from_kata_deps "assets.kernel.sev.version")"
kernel_version=${kernel_version#v}
module_dir="${repo_root_dir}/tools/packaging/kata-deploy/local-build/build/kernel-sev/builddir/kata-linux-${kernel_version}-${config_version}/lib/modules/${kernel_version}"
sudo -E PATH="$PATH" make rootfs AGENT_INIT=yes USE_DOCKER=1 ROOTFS_BUILD_DEST="${builddir}/initrd-image" KERNEL_MODULES_DIR="${module_dir}"
else
sudo -E PATH="$PATH" make rootfs AGENT_INIT=yes USE_DOCKER=1 ROOTFS_BUILD_DEST="${builddir}/initrd-image"
fi
if [ -n "${INCLUDE_ROOTFS:-}" ]; then
sudo cp -RL --preserve=mode "${INCLUDE_ROOTFS}/." "${builddir}/initrd-image/${initrd_distro}_rootfs/"
fi
sudo -E PATH="$PATH" make initrd \
DISTRO="$os_name" \
DEBUG="${DEBUG:-}" \
OS_VERSION="${os_version}" \
ROOTFS_BUILD_DEST="${builddir}/initrd-image" \
USE_DOCKER=1 \
AGENT_INIT="yes"
mv "kata-containers-initrd.img" "${install_dir}/${artifact_name}"
(
cd "${install_dir}"
ln -sf "${artifact_name}" "${final_artifact_name}${image_initrd_extension}"
)
}
build_image() {
set -x
info "Build image"
info "image os: $os_name"
info "image os version: $os_version"
sudo -E PATH="${PATH}" make image \
DISTRO="${os_name}" \
DEBUG="${DEBUG:-}" \
USE_DOCKER="1" \
IMG_OS_VERSION="${os_version}" \
ROOTFS_BUILD_DEST="${builddir}/rootfs-image"
mv -f "kata-containers.img" "${install_dir}/${artifact_name}"
if [ -e "root_hash.txt" ]; then
root_hash_suffix=${image_initrd_suffix}
[ -z "${image_initrd_suffix}" ] && root_hash_suffix=vanilla
mv root_hash.txt root_hash_${root_hash_suffix}.txt
fi
(
cd "${install_dir}"
ln -sf "${artifact_name}" "${final_artifact_name}${image_initrd_extension}"
)
}
usage() {
return_code=${1:-0}
cat <<EOF
Create image and initrd in a tarball for kata containers.
Use it to build an image to distribute kata.
Usage:
${script_name} [options]
Options:
--osname=${os_name}
--osversion=${os_version}
--imagetype=${image_type}
--prefix=${prefix}
--destdir=${destdir}
--image_initrd_suffix=${image_initrd_suffix}
EOF
exit "${return_code}"
}
main() {
image_type=image
destdir="$PWD"
prefix="/opt/kata"
image_suffix=""
image_initrd_suffix=""
builddir="${PWD}"
while getopts "h-:" opt; do
case "$opt" in
-)
case "${OPTARG}" in
osname=*)
os_name=${OPTARG#*=}
;;
osversion=*)
os_version=${OPTARG#*=}
;;
imagetype=image)
image_type=image
;;
imagetype=initrd)
image_type=initrd
;;
image_initrd_suffix=*)
image_initrd_suffix=${OPTARG#*=}
;;
root_hash_suffix=*)
root_hash_suffix=${OPTARG#*=}
;;
prefix=*)
prefix=${OPTARG#*=}
;;
destdir=*)
destdir=${OPTARG#*=}
;;
builddir=*)
builddir=${OPTARG#*=}
;;
*)
echo >&2 "ERROR: Invalid option -$opt${OPTARG}"
usage 1
;;
esac
;;
h) usage 0 ;;
*)
echo "Invalid option $opt"
usage 1
;;
esac
done
readonly destdir
readonly builddir
echo "build ${image_type}"
if [ "${image_type}" = "initrd" ]; then
final_artifact_name+="-initrd"
fi
if [ -n "${image_initrd_suffix}" ]; then
artifact_name="kata-${os_name}-${os_version}-${image_initrd_suffix}.${image_type}"
final_artifact_name+="-${image_initrd_suffix}"
else
artifact_name="kata-${os_name}-${os_version}.${image_type}"
fi
install_dir="${destdir}/${prefix}/share/kata-containers/"
readonly install_dir
mkdir -p "${install_dir}"
pushd "${osbuilder_dir}"
case "${image_type}" in
initrd) build_initrd ;;
image) build_image ;;
esac
popd
}
main $*