This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvfio-kata-osbuilder.sh.template
More file actions
269 lines (203 loc) · 6.89 KB
/
vfio-kata-osbuilder.sh.template
File metadata and controls
269 lines (203 loc) · 6.89 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/bash
set -o errexit
set -o nounset
set -o pipefail
[ -n "${DEBUG:-}" ] && set -o xtrace
readonly IMAGE_TOPDIR="%KATAPREFIX%/var/cache/kata-containers"
readonly KERNEL_SYMLINK="${IMAGE_TOPDIR}/vmlinuz.container"
readonly SCRIPTNAME="$0"
readonly DRACUT_ROOTFS=`mktemp --directory -t kata-dracut-rootfs-XXXXXX`
readonly DRACUT_IMAGES=`mktemp --directory -t kata-dracut-images-XXXXXX`
trap exit_handler EXIT
readonly GENERATED_IMAGE="${DRACUT_IMAGES}/kata-containers.img"
readonly GENERATED_INITRD="${DRACUT_IMAGES}/kata-containers-initrd.img"
KVERSION=`uname -r`
KERNEL_PATH=""
COMMAND=""
OSBUILDER_DIR="%OSBUILDER%"
GENERATE_IMAGE=""
# rpm %check sets this to run the script without overwriting host
# content, and not requiring root
TEST_MODE="${TEST_MODE:-}"
die()
{
error "$*"
exit 1
}
error()
{
echo "ERROR: ${SCRIPTNAME}: $*" >&2
}
info()
{
echo "${SCRIPTNAME}: $*"
}
exit_handler()
{
rm -rf "${DRACUT_ROOTFS}" "${DRACUT_IMAGES}"
}
usage()
{
cat <<EOT
Usage: ${SCRIPTNAME} [options]
This script builds the kata appliance initrd and image and adds
stable symlink paths in ${IMAGE_TOPDIR}
This script is called at kata-osbuilder at RPM install %post time and
via kata-osbuilder-generate.service
Options:
-h Show this help message
-c Check if an initrd is already generated for the current
kernel, and if so, simply exit
-i Also generate media for kata image= option. kata
image= doesn't work out of the box with Fedora/RHEL
distro kernels so media is not generated by default.
-k Manually specify kernel version to use. Default comes
from uname. Default: ${KVERSION}
-o DIRNAME Use the passed directory for osbuilder code. Point
To a git checkout if you want to use upstream osbuilder.
Default: ${OSBUILDER_DIR}
EOT
exit $1
}
parse_args()
{
while getopts "chik:o:" opt
do
case $opt in
c) COMMAND="check" ;;
h) usage 0 ;;
i) GENERATE_IMAGE=1 ;;
o) OSBUILDER_DIR="${OPTARG}" ;;
k) KVERSION="${OPTARG}" ;;
*) usage 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ -n "$*" ]; then
error "Unhandled options: '$*'"
usage 1
fi
}
find_host_kernel_path()
{
local modpath="/lib/modules/$KVERSION/"
[ ! -e "$modpath" ] && die "version=$KVERSION path=$modpath does not exist"
local vmname
for vmname in vmlinuz vmlinux; do
local trypath="$modpath/$vmname"
if [ -e "$trypath" ] ; then
KERNEL_PATH="$trypath"
break
fi
done
[ -z "$KERNEL_PATH" ] && die "Didn't find vmlinu* path in $modpath"
if [ "$COMMAND" = "check" ]; then
local linked_kernel=$(readlink -n "${KERNEL_SYMLINK}" || :)
if [ "${KERNEL_PATH}" = "${linked_kernel}" ] ; then
info "symlink=${KERNEL_SYMLINK} already points to host kernel=${KERNEL_PATH}"
info "Nothing to generate. Exiting."
exit 0
fi
fi
}
generate_modules_load_conf()
{
# Write the modules-load file from all driver .ko.* files in the initrd
local loadfile="${DRACUT_ROOTFS}/etc/modules-load.d/kata-modules.conf"
mkdir -p $(dirname $loadfile)
local modpath
for modpath in `find ${DRACUT_ROOTFS} -path \*lib/modules/\*\.ko\*`; do
local name=$(echo $(basename ${modpath}) | cut -d '.' -f 1)
echo "${name}" >> $loadfile
done
}
generate_rootfs()
{
# To generate the rootfs, we build an initrd with dracut, extract
# the initrd content, and then discard the initrd. We then rebuild
# the initrd using the osbuilder native scripts.
#
# This is a bit wasteful, but it's the easiest way to work around
# obuilder script inflexibility for now, which expect that some rootfs.sh
# code is called on a fully populated distro root.
local agent_dir="%AGENT_TREE%"
if [ -n "${TEST_MODE}" ] ; then
nsdax_bin="${OSBUILDER_DIR}/nsdax"
fi
local agent_source_bin="${agent_dir}/usr/bin/kata-agent"
local osbuilder_version="fedora-osbuilder-version-unknown"
local dracut_conf_dir="./dracut/dracut.conf.d"
local tmp_initrd=`mktemp --tmpdir=${DRACUT_IMAGES}`
unlink "$tmp_initrd"
# Build the initrd
echo -e "+ Building dracut initrd"
dracut \
--confdir "${dracut_conf_dir}" \
--no-compress \
--conf /dev/null \
${tmp_initrd} ${KVERSION}
# Extract the generated rootfs
echo "+ Extracting dracut initrd rootfs"
cat ${tmp_initrd} | \
cpio --extract --preserve-modification-time --make-directories --directory=${DRACUT_ROOTFS}
# Using the busybox dracut module sets /sbin/init -> busybox
# We don't want that. Reset it to systemd
ln -sf ../lib/systemd/systemd ${DRACUT_ROOTFS}/usr/sbin/init
echo "+ Copying agent directory tree into place"
cp -ar ${agent_dir}/* ${DRACUT_ROOTFS}
# Make kata specific adjustments to our rootfs
echo "Calling osbuilder rootfs.sh on extracted rootfs"
AGENT_SOURCE_BIN="${agent_source_bin}" \
./rootfs-builder/rootfs.sh \
-o ${osbuilder_version} \
-r ${DRACUT_ROOTFS}
# Generate modules-load.d file
generate_modules_load_conf
}
move_images()
{
# Move images into place
local image_osbuilder_dir="${IMAGE_TOPDIR}/osbuilder-images"
local image_dir="${image_osbuilder_dir}/$KVERSION"
local initrd_dest_path="${image_dir}/fedora-kata-${KVERSION}.initrd"
local image_dest_path="${image_dir}/fedora-kata-${KVERSION}.img"
local image_dest_link="${IMAGE_TOPDIR}/kata-containers.img"
# This blows away the entire osbuilder-images/ dir, deleting any
# previously cached content
rm -rf "${image_osbuilder_dir}"
mkdir -p "${image_dir}"
ln -sf ${KERNEL_PATH} ${KERNEL_SYMLINK}
mv -Z ${GENERATED_INITRD} ${initrd_dest_path}
ln -sf ${initrd_dest_path} ${IMAGE_TOPDIR}/kata-containers-initrd.img
if [ -n "${GENERATE_IMAGE}" ]; then
mv -Z ${GENERATED_IMAGE} ${image_dest_path}
ln -sf ${image_dest_path} ${image_dest_link}
else
rm -f ${image_dest_link}
fi
}
main()
{
parse_args $*
find_host_kernel_path
cd "${OSBUILDER_DIR}"
# Generate the rootfs using dracut
generate_rootfs
if [ -n "${TEST_MODE}" ]; then
echo "+ Exiting TEST_MODE successfully"
return
fi
# Build the initrd
echo "+ Calling osbuilder initrd_builder.sh"
./initrd-builder/initrd_builder.sh -o ${GENERATED_INITRD} ${DRACUT_ROOTFS}
if [ -n "${GENERATE_IMAGE}" ]; then
# Build the FS image
local nsdax_bin="/usr/libexec/kata-containers/osbuilder/nsdax"
echo "+ Calling osbuilder image_builder.sh"
NSDAX_BIN="${nsdax_bin}" \
./image-builder/image_builder.sh \
-o ${GENERATED_IMAGE} ${DRACUT_ROOTFS}
fi
move_images
}
main $*