-
Notifications
You must be signed in to change notification settings - Fork 521
/
rpm2img
executable file
·361 lines (312 loc) · 13.3 KB
/
rpm2img
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env bash
set -eu -o pipefail
shopt -qs failglob
# import the partition helper functions
# shellcheck source=partyplanner
. "${0%/*}/partyplanner"
OUTPUT_FMT="raw"
BUILDER_ARCH="$(uname -m)"
for opt in "$@"; do
optarg="$(expr "${opt}" : '[^=]*=\(.*\)')"
case "${opt}" in
--package-dir=*) PACKAGE_DIR="${optarg}" ;;
--output-dir=*) OUTPUT_DIR="${optarg}" ;;
--output-fmt=*) OUTPUT_FMT="${optarg}" ;;
--os-image-size-gib=*) OS_IMAGE_SIZE_GIB="${optarg}" ;;
--data-image-size-gib=*) DATA_IMAGE_SIZE_GIB="${optarg}" ;;
--partition-plan=*) PARTITION_PLAN="${optarg}" ;;
esac
done
case "${OUTPUT_FMT}" in
raw|qcow2|vmdk) ;;
*)
echo "unexpected image output format '${OUTPUT_FMT}'" >&2
exit 1
;;
esac
case "${PARTITION_PLAN}" in
split|unified) ;;
*)
echo "unexpected partition plan '${PARTITION_PLAN}'" >&2
exit 1
;;
esac
mkdir -p "${OUTPUT_DIR}"
FILENAME_PREFIX="${IMAGE_NAME}-${VARIANT}-${ARCH}-${VERSION_ID}-${BUILD_ID}"
OS_IMAGE_BASENAME="${FILENAME_PREFIX}"
DATA_IMAGE_BASENAME="${FILENAME_PREFIX}-data"
BOOT_IMAGE_NAME="${FILENAME_PREFIX}-boot.ext4.lz4"
VERITY_IMAGE_NAME="${FILENAME_PREFIX}-root.verity.lz4"
ROOT_IMAGE_NAME="${FILENAME_PREFIX}-root.ext4.lz4"
OS_IMAGE="$(mktemp)"
BOOT_IMAGE="$(mktemp)"
VERITY_IMAGE="$(mktemp)"
ROOT_IMAGE="$(mktemp)"
DATA_IMAGE="$(mktemp)"
EFI_IMAGE="$(mktemp)"
PRIVATE_IMAGE="$(mktemp)"
BOTTLEROCKET_DATA="$(mktemp)"
ROOT_MOUNT="$(mktemp -d)"
BOOT_MOUNT="$(mktemp -d)"
DATA_MOUNT="$(mktemp -d)"
EFI_MOUNT="$(mktemp -d)"
PRIVATE_MOUNT="$(mktemp -d)"
SELINUX_ROOT="/etc/selinux"
SELINUX_POLICY="fortified"
SELINUX_FILE_CONTEXTS="${ROOT_MOUNT}/${SELINUX_ROOT}/${SELINUX_POLICY}/contexts/files/file_contexts"
VERITY_VERSION=1
VERITY_HASH_ALGORITHM=sha256
VERITY_DATA_BLOCK_SIZE=4096
VERITY_HASH_BLOCK_SIZE=4096
case "${PARTITION_PLAN}" in
split)
truncate -s "${OS_IMAGE_SIZE_GIB}G" "${OS_IMAGE}"
truncate -s "${DATA_IMAGE_SIZE_GIB}G" "${DATA_IMAGE}"
;;
unified)
truncate -s "$((OS_IMAGE_SIZE_GIB + DATA_IMAGE_SIZE_GIB))G" "${OS_IMAGE}"
;;
esac
declare -A partlabel parttype partsize partoff
set_partition_sizes \
"${OS_IMAGE_SIZE_GIB}" "${DATA_IMAGE_SIZE_GIB}" "${PARTITION_PLAN}" \
partsize partoff
set_partition_labels partlabel
set_partition_types parttype
declare -a partargs
for part in \
BIOS \
EFI-A BOOT-A ROOT-A HASH-A RESERVED-A \
EFI-B BOOT-B ROOT-B HASH-B RESERVED-B \
PRIVATE DATA ;
do
# We only append the data partition if we're using the unified layout.
if [ "${part}" == "DATA" ] && [ "${PARTITION_PLAN}" != "unified" ] ; then
continue
fi
# Each partition is aligned to a 1 MiB boundary, and extends to the sector
# before the next partition starts. Specify the end point in sectors so we
# can subtract a sector to fix the off-by-one error that comes from adding
# start and size together. (1 MiB contains 2048 512-byte sectors.)
part_start="${partoff[${part}]}"
part_end="$((part_start + partsize[${part}]))"
part_end="$((part_end * 2048 - 1))"
partargs+=(-n "0:${part_start}M:${part_end}")
partargs+=(-c "0:${partlabel[${part}]}")
partargs+=(-t "0:${parttype[${part}]}")
# Boot partition attributes:
# 48 = gptprio priority bit
# 56 = gptprio successful bit
case "${part}" in
BOOT-A) partargs+=(-A 0:"set":48 -A 0:"set":56) ;;
BOOT-B) partargs+=(-A 0:"clear":48 -A 0:"clear":56) ;;
esac
done
sgdisk --clear "${partargs[@]}" --sort --print "${OS_IMAGE}"
# Partition the separate data disk, if we're using the split layout.
if [ "${PARTITION_PLAN}" == "split" ] ; then
data_start="${partoff[DATA]}"
data_end=$((data_start + partsize[DATA]))
data_end=$((data_end * 2048 - 1))
sgdisk --clear \
-n "0:${data_start}M:${data_end}" \
-c "0:${partlabel[DATA]}" \
-t "0:${parttype[DATA]}" \
--sort --print "${DATA_IMAGE}"
fi
INSTALL_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
rpm -iv --root "${ROOT_MOUNT}" "${PACKAGE_DIR}"/*.rpm
# inventory installed packages
INVENTORY_QUERY="\{\"Name\":\"%{NAME}\"\
,\"Publisher\":\"Bottlerocket\"\
,\"Version\":\"${VERSION_ID}\"\
,\"Release\":\"${BUILD_ID}\"\
,\"InstalledTime\":\"${INSTALL_TIME}\"\
,\"ApplicationType\":\"%{GROUP}\"\
,\"Architecture\":\"%{ARCH}\"\
,\"Url\":\"%{URL}\"\
,\"Summary\":\"%{Summary}\"\}\n"
mapfile -t installed_rpms <<< "$(rpm -qa --root "${ROOT_MOUNT}" \
--queryformat "${INVENTORY_QUERY}")"
# wrap installed_rpms mapfile into json
INVENTORY_DATA="$(jq --raw-output . <<< "${installed_rpms[@]}")"
# replace the package architecture with the target architecture (for cross-compiled builds)
if [[ "${BUILDER_ARCH}" != "${ARCH}" ]]; then
INVENTORY_DATA="$(jq --arg BUILDER_ARCH "${BUILDER_ARCH}" --arg TARGET_ARCH "${ARCH}" \
'(.Architecture) |= sub($BUILDER_ARCH; $TARGET_ARCH)' <<< "${INVENTORY_DATA}")"
fi
# remove the 'bottlerocket-<arch>-' prefix from package names
INVENTORY_DATA="$(jq --arg PKG_PREFIX "bottlerocket-${ARCH}-" \
'(.Name) |= sub($PKG_PREFIX; "")' <<< "${INVENTORY_DATA}")"
# sort by package name and add 'Content' as top-level
INVENTORY_DATA="$(jq --slurp 'sort_by(.Name)' <<< "${INVENTORY_DATA}" | jq '{"Content": .}')"
printf "%s\n" "${INVENTORY_DATA}" > "${ROOT_MOUNT}/usr/share/bottlerocket/application-inventory.json"
# install licenses
install -p -m 0644 /host/{COPYRIGHT,LICENSE-APACHE,LICENSE-MIT} "${ROOT_MOUNT}"/usr/share/licenses/
mksquashfs \
"${ROOT_MOUNT}"/usr/share/licenses \
"${ROOT_MOUNT}"/usr/share/bottlerocket/licenses.squashfs \
-no-exports -all-root -comp zstd
rm -rf "${ROOT_MOUNT}"/var/lib "${ROOT_MOUNT}"/usr/share/licenses/*
if [[ "${ARCH}" == "x86_64" ]]; then
# MBR and BIOS-BOOT
echo "(hd0) ${OS_IMAGE}" > "${ROOT_MOUNT}/boot/grub/device.map"
"${ROOT_MOUNT}/sbin/grub-bios-setup" \
--directory="${ROOT_MOUNT}/boot/grub" \
--device-map="${ROOT_MOUNT}/boot/grub/device.map" \
--root="hd0" \
--skip-fs-probe \
"${OS_IMAGE}"
rm -vf "${ROOT_MOUNT}"/boot/grub/* "${ROOT_MOUNT}"/sbin/grub*
fi
# We also need an EFI partition, formatted FAT32 with the
# EFI binary at the correct path, eg /efi/boot. The grub
# package has placed the image in /boot/efi/EFI/BOOT.
mv "${ROOT_MOUNT}/boot/efi"/* "${EFI_MOUNT}"
dd if=/dev/zero of="${EFI_IMAGE}" bs=1M count="${partsize[EFI-A]}"
mkfs.vfat -I -S 512 "${EFI_IMAGE}" $((partsize[EFI-A] * 2048))
mmd -i "${EFI_IMAGE}" ::/EFI
mmd -i "${EFI_IMAGE}" ::/EFI/BOOT
mcopy -i "${EFI_IMAGE}" "${EFI_MOUNT}/EFI/BOOT"/*.efi ::/EFI/BOOT
dd if="${EFI_IMAGE}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[EFI-A]}"
# Ensure that the grub directory exists.
mkdir -p "${ROOT_MOUNT}/boot/grub"
# Now that we're done messing with /, move /boot out of it
mv "${ROOT_MOUNT}/boot"/* "${BOOT_MOUNT}"
# Set the Bottlerocket variant, version, and build-id
SYS_ROOT="${ARCH}-bottlerocket-linux-gnu/sys-root"
VERSION="${VERSION_ID} (${VARIANT})"
cat <<EOF >> "${ROOT_MOUNT}/${SYS_ROOT}/usr/lib/os-release"
VERSION="${VERSION}"
PRETTY_NAME="${PRETTY_NAME} ${VERSION}"
VARIANT_ID=${VARIANT}
VERSION_ID=${VERSION_ID}
BUILD_ID=${BUILD_ID}
HOME_URL="https://github.com/bottlerocket-os/bottlerocket"
SUPPORT_URL="https://github.com/bottlerocket-os/bottlerocket/discussions"
BUG_REPORT_URL="https://github.com/bottlerocket-os/bottlerocket/issues"
EOF
# BOTTLEROCKET-ROOT-A
mkdir -p "${ROOT_MOUNT}/lost+found"
ROOT_LABELS=$(setfiles -n -d -F -m -r "${ROOT_MOUNT}" \
"${SELINUX_FILE_CONTEXTS}" "${ROOT_MOUNT}" \
| awk -v root="${ROOT_MOUNT}" '{gsub(root"/","/"); gsub(root,"/"); print "ea_set", $1, "security.selinux", $4}')
mkfs.ext4 -O ^has_journal -b "${VERITY_DATA_BLOCK_SIZE}" -d "${ROOT_MOUNT}" "${ROOT_IMAGE}" "${partsize[ROOT-A]}M"
echo "${ROOT_LABELS}" | debugfs -w -f - "${ROOT_IMAGE}"
resize2fs -M "${ROOT_IMAGE}"
dd if="${ROOT_IMAGE}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[ROOT-A]}"
# BOTTLEROCKET-VERITY-A
veritypart_mib="${partsize[HASH-A]}"
truncate -s "${veritypart_mib}M" "${VERITY_IMAGE}"
veritysetup_output="$(veritysetup format \
--format "$VERITY_VERSION" \
--hash "$VERITY_HASH_ALGORITHM" \
--data-block-size "$VERITY_DATA_BLOCK_SIZE" \
--hash-block-size "$VERITY_HASH_BLOCK_SIZE" \
"${ROOT_IMAGE}" "${VERITY_IMAGE}" \
| tee /dev/stderr)"
verityimage_size="$(stat -c %s "${VERITY_IMAGE}")"
veritypart_bytes="$((veritypart_mib * 1024 * 1024))"
if [ "${verityimage_size}" -gt "${veritypart_bytes}" ] ; then
echo "verity content is larger than partition (${veritypart_mib}M)"
exit 1
fi
VERITY_DATA_4K_BLOCKS="$(grep '^Data blocks:' <<<"${veritysetup_output}" | awk '{ print $NF }')"
VERITY_DATA_512B_BLOCKS="$((VERITY_DATA_4K_BLOCKS * 8))"
VERITY_ROOT_HASH="$(grep '^Root hash:' <<<"${veritysetup_output}" | awk '{ print $NF }')"
VERITY_SALT="$(grep '^Salt:' <<<"${veritysetup_output}" | awk '{ print $NF }')"
veritysetup verify "${ROOT_IMAGE}" "${VERITY_IMAGE}" "${VERITY_ROOT_HASH}"
dd if="${VERITY_IMAGE}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[HASH-A]}"
# write GRUB config
GRUB_FEATURES_ARRAY=("${GRUB_FEATURES}")
# If GRUB_SET_PRIVATE_VAR is set, include the parameters that support Boot Config
if printf '%s\n' "${GRUB_FEATURES_ARRAY[@]}" | grep -Fxq 'GRUB_SET_PRIVATE_VAR' ; then
BOOTCONFIG='bootconfig'
INITRD='initrd ($private)/bootconfig.data'
else
BOOTCONFIG=""
INITRD=""
fi
cat <<EOF > "${BOOT_MOUNT}/grub/grub.cfg"
set default="0"
set timeout="0"
menuentry "${PRETTY_NAME} ${VERSION_ID}" {
linux (\$root)/vmlinuz \\
${KERNEL_PARAMETERS} \\
${BOOTCONFIG} \\
root=/dev/dm-0 rootwait ro \\
raid=noautodetect \\
random.trust_cpu=on selinux=1 enforcing=1 \\
dm_verity.max_bios=-1 dm_verity.dev_wait=1 \\
dm-mod.create="root,,,ro,0 $VERITY_DATA_512B_BLOCKS verity $VERITY_VERSION PARTUUID=\$boot_uuid/PARTNROFF=1 PARTUUID=\$boot_uuid/PARTNROFF=2 \\
$VERITY_DATA_BLOCK_SIZE $VERITY_HASH_BLOCK_SIZE $VERITY_DATA_4K_BLOCKS 1 $VERITY_HASH_ALGORITHM $VERITY_ROOT_HASH $VERITY_SALT \\
2 restart_on_corruption ignore_zero_blocks" \\
-- \\
systemd.log_target=journal-or-kmsg systemd.log_color=0 systemd.show_status=true
${INITRD}
}
EOF
# BOTTLEROCKET-BOOT-A
mkdir -p "${BOOT_MOUNT}/lost+found"
chmod -R go-rwx "${BOOT_MOUNT}"
BOOT_LABELS=$(setfiles -n -d -F -m -r "${BOOT_MOUNT}" \
"${SELINUX_FILE_CONTEXTS}" "${BOOT_MOUNT}" \
| awk -v root="${BOOT_MOUNT}" '{gsub(root"/","/"); gsub(root,"/"); print "ea_set", $1, "security.selinux", $4}')
mkfs.ext4 -O ^has_journal -d "${BOOT_MOUNT}" "${BOOT_IMAGE}" "${partsize[BOOT-A]}M"
echo "${BOOT_LABELS}" | debugfs -w -f - "${BOOT_IMAGE}"
resize2fs -M "${BOOT_IMAGE}"
dd if="${BOOT_IMAGE}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[BOOT-A]}"
# BOTTLEROCKET-PRIVATE
# Copy the empty bootconfig file into the image so grub doesn't pause and print
# an error that the file doesn't exist
cp /host/tools/bootconfig/empty-bootconfig.data "${PRIVATE_MOUNT}/bootconfig.data"
# Targeted toward the current API server implementation.
# Relative to the ext4 defaults, we:
# - adjust the inode ratio since we expect lots of small files
# - retain the inode size to allow most settings to be stored inline
# - retain the block size to handle worse-case alignment for hardware
mkfs.ext4 -b 4096 -i 4096 -I 256 -d "${PRIVATE_MOUNT}" "${PRIVATE_IMAGE}" "${partsize[PRIVATE]}M"
dd if="${PRIVATE_IMAGE}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[PRIVATE]}"
# BOTTLEROCKET-DATA
# If we build on a host with SELinux enabled, we could end up with labels that
# do not match our policy. Since we allow replacing the data volume at runtime,
# we can't count on these labels being correct in any case, and it's better to
# remove them all.
UNLABELED=$(find "${DATA_MOUNT}" \
| awk -v root="${DATA_MOUNT}" '{gsub(root"/","/"); gsub(root,"/"); print "ea_rm", $1, "security.selinux"}')
mkfs.ext4 -d "${DATA_MOUNT}" "${BOTTLEROCKET_DATA}" "${partsize[DATA]}M"
echo "${UNLABELED}" | debugfs -w -f - "${BOTTLEROCKET_DATA}"
case "${PARTITION_PLAN}" in
split)
dd if="${BOTTLEROCKET_DATA}" of="${DATA_IMAGE}" conv=notrunc bs=1M seek="${partoff[DATA]}"
;;
unified)
dd if="${BOTTLEROCKET_DATA}" of="${OS_IMAGE}" conv=notrunc bs=1M seek="${partoff[DATA]}"
;;
esac
sgdisk -v "${OS_IMAGE}"
[ -s "${DATA_IMAGE}" ] && sgdisk -v "${DATA_IMAGE}"
if [[ ${OUTPUT_FMT} == "raw" ]]; then
lz4 -vc "${OS_IMAGE}" >"${OUTPUT_DIR}/${OS_IMAGE_BASENAME}.img.lz4"
if [ -s "${DATA_IMAGE}" ] ; then
lz4 -vc "${DATA_IMAGE}" >"${OUTPUT_DIR}/${DATA_IMAGE_BASENAME}.img.lz4"
fi
elif [[ ${OUTPUT_FMT} == "qcow2" ]]; then
qemu-img convert -f raw -O qcow2 "${OS_IMAGE}" "${OUTPUT_DIR}/${OS_IMAGE_BASENAME}.qcow2"
if [ -s "${DATA_IMAGE}" ] ; then
qemu-img convert -f raw -O qcow2 "${DATA_IMAGE}" "${OUTPUT_DIR}/${DATA_IMAGE_BASENAME}.qcow2"
fi
elif [[ ${OUTPUT_FMT} == "vmdk" ]]; then
# Stream optimization is required for creating an Open Virtual Appliance (OVA)
qemu-img convert -f raw -O vmdk -o subformat=streamOptimized "${OS_IMAGE}" "${OUTPUT_DIR}/${OS_IMAGE_BASENAME}.vmdk"
if [ -s "${DATA_IMAGE}" ] ; then
qemu-img convert -f raw -O vmdk -o subformat=streamOptimized "${DATA_IMAGE}" "${OUTPUT_DIR}/${DATA_IMAGE_BASENAME}.vmdk"
fi
fi
lz4 -9vc "${BOOT_IMAGE}" >"${OUTPUT_DIR}/${BOOT_IMAGE_NAME}"
lz4 -9vc "${VERITY_IMAGE}" >"${OUTPUT_DIR}/${VERITY_IMAGE_NAME}"
lz4 -9vc "${ROOT_IMAGE}" >"${OUTPUT_DIR}/${ROOT_IMAGE_NAME}"
find "${OUTPUT_DIR}" -type f -print -exec chown 1000:1000 {} \;
# Clean up temporary files to reduce size of layer.
rm -f "${PACKAGE_DIR}"/*.rpm
rm -rf /tmp/*