Skip to content

Commit 57d158f

Browse files
nikita-dubrovskiijlebon
authored andcommitted
s390x: enable osbuild based secex build
1 parent 8f5e816 commit 57d158f

File tree

4 files changed

+95
-48
lines changed

4 files changed

+95
-48
lines changed

src/cmd-buildextend-metal

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,9 @@ disk_args=()
190190
qemu_args=()
191191
# SecureExecution extra stuff
192192
if [[ $secure_execution -eq "1" ]]; then
193-
ignition_pubkey=$(mktemp -p "${tmp_builddir}")
194-
disk_args+=("--with-secure-execution" "--write-ignition-pubkey-to" "${ignition_pubkey}")
195-
if [ -z "${hostkey}" ]; then
196-
if [ ! -f "${genprotimgvm}" ]; then
197-
fatal "No genprotimgvm provided at ${genprotimgvm}"
198-
fi
199-
genprotimg_img="${PWD}/secex-genprotimg.img"
200-
qemu-img create -f raw "${genprotimg_img}" 512M
201-
mkfs.ext4 "${genprotimg_img}"
202-
qemu_args+=("-drive" "if=none,id=genprotimg,format=raw,file=${genprotimg_img}" \
203-
"-device" "virtio-blk,serial=genprotimg,drive=genprotimg")
204-
else
205-
qemu_args+=("-drive" "if=none,id=hostkey,format=raw,file=$hostkey,readonly=on" \
206-
"-device" "virtio-blk,serial=hostkey,drive=hostkey")
193+
disk_args+=("--with-secure-execution")
194+
if [ ! -f "${genprotimgvm}" ]; then
195+
fatal "No genprotimgvm provided at ${genprotimgvm}"
207196
fi
208197
fi
209198

@@ -219,10 +208,12 @@ rootfs_size_mb="$(jq '."estimate-mb".final' "$PWD/tmp/ostree-size.json")"
219208
# the platforms require and we want a "default" disk size that has some
220209
# free space.
221210
nonroot_partition_sizes=513
222-
# In the Secure Execution case, we need to also include the sizes of the se and verity
223-
# partitions so that they don't "eat into" the 35% buffer (though note this is
224-
# all blown away on first boot anyway).
225-
if [[ $secure_execution -eq "1" ]]; then
211+
# On s390x there is one more build - Secure Execution case, which has
212+
# different image layout. We add the sizes of the se and verity
213+
# partitions so that they don't "eat into" the 35% buffer (though note
214+
# this is all blown away on first boot anyway). For 's390x.mpp.yaml'
215+
# simplicity all s390x images have same size (of secex image).
216+
if [[ $basearch == "s390x" ]]; then
226217
nonroot_partition_sizes=$((nonroot_partition_sizes + 200 + 128 + 256 + 1))
227218
fi
228219
metal_image_size_mb="$(( rootfs_size_mb + nonroot_partition_sizes ))"
@@ -274,10 +265,11 @@ platforms_json="${tmp_builddir}/platforms.json"
274265
yaml2json "${configdir}/platforms.yaml" "${platforms_json}"
275266

276267
# Currently we only support OSBuild for qemu and metal disk images
268+
osbuild_extra_args=()
277269
if [ "${image_type}" == "qemu" ] || [ "${image_type}" == "metal" ] || [ "${image_type}" == "metal4k" ]; then
278-
# Right now we don't have support fully fleshed out for secex on s390x
279-
if [[ $secure_execution -ne "1" ]]; then
280-
OSBUILD_SUPPORTED=1
270+
OSBUILD_SUPPORTED=1
271+
if [[ $secure_execution -eq "1" ]]; then
272+
osbuild_extra_args+=("--secex" "1")
281273
fi
282274
fi
283275

@@ -293,7 +285,7 @@ if [ "${OSBUILD_SUPPORTED:-}" != "" ] && [ "${COSA_USE_OSBUILD:-}" != "0" ]; the
293285
runvm_with_cache_snapshot "$snapshot" -- /usr/lib/coreos-assembler/runvm-osbuild \
294286
--config "${image_for_disk_json}" \
295287
--mpp "/usr/lib/coreos-assembler/osbuild-manifests/coreos.osbuild.${basearch}.mpp.yaml" \
296-
--filepath "${path}.tmp"
288+
--filepath "${path}.tmp" "${osbuild_extra_args[@]}"
297289
else
298290
runvm "${qemu_args[@]}" -- \
299291
/usr/lib/coreos-assembler/create_disk.sh \
@@ -309,9 +301,36 @@ else
309301
fi
310302
fi
311303

312-
if [[ $secure_execution -eq "1" && -z "${hostkey}" ]]; then
304+
if [[ $secure_execution -eq "1" ]]; then
305+
# SecureVM (holding Universal Key for all IBM Z Mainframes) requires scripts to execute genprotimg
306+
se_script_dir="/usr/lib/coreos-assembler/secex-genprotimgvm-scripts"
307+
genprotimg_img="${PWD}/secex-genprotimg.img"
308+
genprotimg_dir=$(mktemp -p "${tmp_builddir}" -d)
309+
cp "${se_script_dir}/genprotimg-script.sh" "${se_script_dir}/post-script.sh" "${genprotimg_dir}"
310+
# Extra kargs with dm-verity hashes
311+
secex_kargs="ignition.firstboot rootfs.roothash=$(<"${PWD}"/rootfs_hash) bootfs.roothash=$(<"${PWD}"/bootfs_hash)"
312+
echo "${secex_kargs}" > "${genprotimg_dir}/parmfile"
313+
virt-make-fs --format=raw --type=ext4 "${genprotimg_dir}" "${genprotimg_img}"
314+
rm -rf "${genprotimg_dir}"
315+
qemu_args+=("-drive" "if=none,id=genprotimg,format=raw,file=${genprotimg_img}" \
316+
"-device" "virtio-blk,serial=genprotimg,drive=genprotimg")
317+
318+
# GPG keys used for protecting Ignition config
319+
tmp_gpg_home=$(mktemp -p "${tmp_builddir}" -d)
320+
ignition_pubkey=$(mktemp -p "${tmp_builddir}")
321+
ignition_prikey=$(mktemp -p "${tmp_builddir}")
322+
gpg --homedir "${tmp_gpg_home}" --batch --passphrase '' --yes --quick-gen-key "Secure Execution (secex) ${build}" rsa4096 encr none
323+
gpg --homedir "${tmp_gpg_home}" --armor --export secex > "${ignition_pubkey}"
324+
gpg --homedir "${tmp_gpg_home}" --armor --export-secret-key secex > "${ignition_prikey}"
325+
exec 9<"${ignition_prikey}"
326+
rm -rf "${tmp_gpg_home}" "${ignition_prikey}"
327+
qemu_args+=("-add-fd" "fd=9,set=3" "-drive" "if=none,id=gpgkey,format=raw,file=/dev/fdset/3,readonly=on" \
328+
"-device" "virtio-blk,serial=gpgkey,drive=gpgkey")
329+
313330
/usr/lib/coreos-assembler/secex-genprotimgvm-scripts/runvm.sh \
314331
--genprotimgvm "${genprotimgvm}" -- "${qemu_args[@]}"
332+
rm -f "${genprotimg_img}"
333+
exec 9>&-
315334
fi
316335

317336
/usr/lib/coreos-assembler/finalize-artifact "${path}.tmp" "${path}"

src/secex-genprotimgvm-scripts/genprotimg-script.sh

100644100755
Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
#!bin/bash
2-
32
set -exuo pipefail
43

54
echo "Preparing for genprotimg-daemon"
65

7-
source="/build/genprotimg"
8-
destination="/genprotimg"
9-
pkey="usr/lib/coreos/ignition.asc"
6+
# CoreOS based secure-vm uses '/var' as prefix
7+
PREFIX=/
8+
if [ -e /var/genprotimg ]; then
9+
PREFIX=/var
10+
fi
11+
12+
destination="${PREFIX}/genprotimg"
13+
parmfile="${PREFIX}/build/parmfile"
1014

11-
trap "rm -f ${source}/${pkey}" EXIT
15+
# This is our coreos.qemu-secex.qcow2
16+
disk=$(realpath /dev/disk/by-id/virtio-target)
1217

18+
19+
# 'boot' labeled partition on target image
20+
disk_boot="${disk}3"
21+
boot_mnt=$(mktemp -d /tmp/boot-XXXXXX)
22+
mount -o ro "${disk_boot}" "${boot_mnt}"
23+
blsfile=$(ls "${boot_mnt}"/loader/entries/*.conf)
24+
kernel=$(grep linux "${blsfile}" | cut -d' ' -f2)
25+
initrd=$(grep initrd "${blsfile}" | cut -d' ' -f2)
1326
# Files need to be named correctly
14-
# genprotimg daemon can only see /genprotimg folder
15-
cp "${source}/vmlinuz" "${source}/initrd.img" "${source}/parmfile" "${destination}/"
27+
cp "${boot_mnt}/${kernel}" "${destination}/vmlinuz"
28+
cp "${boot_mnt}/${initrd}" "${destination}/initrd.img"
29+
# Generate full cmdline
30+
echo "$(grep options "${blsfile}" | cut -d' ' -f2-) $(<${parmfile})" > "${destination}/parmfile"
31+
umount "${boot_mnt}"
32+
rmdir "${boot_mnt}"
33+
1634

35+
# We pass Ignition gpg private key from COSA to the VM as virtual disk
36+
gpg_private_key=$(realpath /dev/disk/by-id/virtio-gpgkey)
37+
gpg_dir=$(mktemp -d)
38+
pkey="usr/lib/coreos/ignition.asc"
39+
mkdir -p "${gpg_dir}/usr/lib/coreos"
40+
cat "${gpg_private_key}" > "${gpg_dir}/${pkey}"
1741
# Append Ignition GPG private key to initramfs
18-
cd "${source}"
19-
echo "${pkey}" | cpio --quiet -H newc -o | gzip -9 -n >> "${destination}/initrd.img"
20-
rm "${pkey}"
42+
echo "${pkey}" | cpio -D "${gpg_dir}" --quiet -H newc -o | gzip -9 -n >> "${destination}/initrd.img"
43+
rm -rf "${gpg_dir}"
44+
2145

2246
# Signal daemon that it can run genprotimg
2347
touch "${destination}/signal.file"
2448

2549
# Wait for genprotimg execution
2650
while [ -e "$destination/signal.file" ] && [ ! -e "$destination/error" ]; do
27-
sleep 5
51+
sleep 1
2852
done
2953
if [ -e "$destination/error" ] || [ ! -e "${destination}/se.img" ]; then
3054
ls -lha $destination

src/secex-genprotimgvm-scripts/post-script.sh

100644100755
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ set -exuo pipefail
44

55
echo "Moving sdboot and executing zipl"
66

7-
workdir="/build"
8-
sdboot="/genprotimg/se.img"
9-
genprotimg_dir="${workdir}/genprotimg"
10-
se_boot=$(mktemp -d /tmp/se-XXXXXX)
7+
# CoreOS based secure-vm uses '/var' as prefix
8+
PREFIX=/
9+
if [ -e /var/genprotimg ]; then
10+
PREFIX=/var
11+
fi
1112

13+
# This is our coreos.qemu-secex.qcow2
1214
disk=$(realpath /dev/disk/by-id/virtio-target)
15+
16+
17+
# 'se' labeled partition on target image, holds 'sdboot' image
1318
disk_se="${disk}1"
19+
se_mnt=$(mktemp -d /tmp/se-XXXXXX)
20+
mount "${disk_se}" "${se_mnt}"
21+
cp "${PREFIX}/genprotimg/se.img" "${se_mnt}/sdboot"
22+
zipl -V -i "${se_mnt}/sdboot" -t "${se_mnt}"
23+
umount "${se_mnt}"
24+
rmdir "${se_mnt}"
1425

15-
mount "${disk_se}" "${se_boot}"
16-
cp "${sdboot}" "${se_boot}/sdboot"
17-
zipl -V -i ${se_boot}/sdboot -t ${se_boot}
1826

1927
# Disable debug output, the last message should be success
2028
set +x
2129
echo "Success, added sdboot to image and executed zipl"
22-
23-
umount "${se_boot}"
24-
rm -rf "${se_boot}"

src/secex-genprotimgvm-scripts/runvm.sh

100644100755
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ if ! "${kola_args[@]}" -- "${base_qemu_args[@]}" \
6262
exit 1
6363
fi
6464

65-
cat "${runvm_console}"
66-
6765
if ! grep -q "Success, added sdboot to image and executed zipl" "${runvm_console}"; then
68-
echo "Could not find success message, genprotimg failed."
69-
exit 1
66+
echo "Could not find success message, genprotimg failed."
67+
cat "${runvm_console}"
68+
exit 1
7069
fi
7170

7271
exit 0

0 commit comments

Comments
 (0)