Cleanup and changes to image parition and sizing, see: https://github.com/RPi-Distro/pi-gen/pull/349#issuecomment-623850785
This commit is contained in:
parent
f917a310b3
commit
5e22be6440
171
build.sh
171
build.sh
|
@ -14,7 +14,7 @@ $(cat "${i}-debconf")
|
||||||
SELEOF
|
SELEOF
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
log "End ${SUB_STAGE_DIR}/${i}-debconf"
|
log "End ${SUB_STAGE_DIR}/${i}-debconf"
|
||||||
fi
|
fi
|
||||||
if [ -f "${i}-packages-nr" ]; then
|
if [ -f "${i}-packages-nr" ]; then
|
||||||
log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
|
log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
|
||||||
|
@ -23,11 +23,11 @@ EOF
|
||||||
on_chroot << EOF
|
on_chroot << EOF
|
||||||
apt-get -o APT::Acquire::Retries=3 install --no-install-recommends -y $PACKAGES
|
apt-get -o APT::Acquire::Retries=3 install --no-install-recommends -y $PACKAGES
|
||||||
EOF
|
EOF
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
on_chroot << EOF
|
on_chroot << EOF
|
||||||
apt-get clean
|
apt-get clean
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
|
log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
|
||||||
fi
|
fi
|
||||||
|
@ -38,11 +38,11 @@ EOF
|
||||||
on_chroot << EOF
|
on_chroot << EOF
|
||||||
apt-get -o APT::Acquire::Retries=3 install -y $PACKAGES
|
apt-get -o APT::Acquire::Retries=3 install -y $PACKAGES
|
||||||
EOF
|
EOF
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
on_chroot << EOF
|
on_chroot << EOF
|
||||||
apt-get clean
|
apt-get clean
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
log "End ${SUB_STAGE_DIR}/${i}-packages"
|
log "End ${SUB_STAGE_DIR}/${i}-packages"
|
||||||
fi
|
fi
|
||||||
|
@ -100,14 +100,14 @@ run_stage(){
|
||||||
ROOTFS_DIR="${STAGE_WORK_DIR}"/rootfs
|
ROOTFS_DIR="${STAGE_WORK_DIR}"/rootfs
|
||||||
|
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
if [ ! -f SKIP ]; then
|
if [ ! -f SKIP ]; then
|
||||||
load_qimage
|
load_qimage
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# make sure we are not umounting during export-image stage
|
# make sure we are not umounting during export-image stage
|
||||||
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
||||||
unmount "${WORK_DIR}/${STAGE}"
|
unmount "${WORK_DIR}/${STAGE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f SKIP_IMAGES ]; then
|
if [ ! -f SKIP_IMAGES ]; then
|
||||||
|
@ -127,8 +127,7 @@ run_stage(){
|
||||||
log "End ${STAGE_DIR}/prerun.sh"
|
log "End ${STAGE_DIR}/prerun.sh"
|
||||||
fi
|
fi
|
||||||
for SUB_STAGE_DIR in "${STAGE_DIR}"/*; do
|
for SUB_STAGE_DIR in "${STAGE_DIR}"/*; do
|
||||||
if [ -d "${SUB_STAGE_DIR}" ] &&
|
if [ -d "${SUB_STAGE_DIR}" ] && [ ! -f "${SUB_STAGE_DIR}/SKIP" ]; then
|
||||||
[ ! -f "${SUB_STAGE_DIR}/SKIP" ]; then
|
|
||||||
run_sub_stage
|
run_sub_stage
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
@ -137,10 +136,10 @@ run_stage(){
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
unload_qimage
|
unload_qimage
|
||||||
else
|
else
|
||||||
# make sure we are not umounting during export-image stage
|
# make sure we are not umounting during export-image stage
|
||||||
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
||||||
unmount "${WORK_DIR}/${STAGE}"
|
unmount "${WORK_DIR}/${STAGE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PREV_STAGE="${STAGE}"
|
PREV_STAGE="${STAGE}"
|
||||||
|
@ -177,10 +176,10 @@ do
|
||||||
done
|
done
|
||||||
|
|
||||||
term() {
|
term() {
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
log "Unloading image"
|
log "Unloading image"
|
||||||
unload_qimage
|
unload_qimage
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
trap term EXIT INT TERM
|
trap term EXIT INT TERM
|
||||||
|
@ -255,9 +254,9 @@ export USE_QCOW2="${USE_QCOW2:-1}"
|
||||||
export BASE_QCOW2_SIZE=${BASE_QCOW2_SIZE:-12G}
|
export BASE_QCOW2_SIZE=${BASE_QCOW2_SIZE:-12G}
|
||||||
source "${SCRIPT_DIR}/qcow2_handling"
|
source "${SCRIPT_DIR}/qcow2_handling"
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
NO_PRERUN_QCOW2=1
|
NO_PRERUN_QCOW2=1
|
||||||
else
|
else
|
||||||
NO_PRERUN_QCOW2=0
|
NO_PRERUN_QCOW2=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export NO_PRERUN_QCOW2="${NO_PRERUN_QCOW2:-1}"
|
export NO_PRERUN_QCOW2="${NO_PRERUN_QCOW2:-1}"
|
||||||
|
@ -297,71 +296,71 @@ for EXPORT_DIR in ${EXPORT_DIRS}; do
|
||||||
source "${EXPORT_DIR}/EXPORT_IMAGE"
|
source "${EXPORT_DIR}/EXPORT_IMAGE"
|
||||||
EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename "${EXPORT_DIR}")/rootfs
|
EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename "${EXPORT_DIR}")/rootfs
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
USE_QCOW2=0
|
USE_QCOW2=0
|
||||||
EXPORT_NAME="${IMG_FILENAME}${IMG_SUFFIX}"
|
EXPORT_NAME="${IMG_FILENAME}${IMG_SUFFIX}"
|
||||||
echo "------------------------------------------------------------------------"
|
echo "------------------------------------------------------------------------"
|
||||||
echo "Running export stage for ${EXPORT_NAME}"
|
echo "Running export stage for ${EXPORT_NAME}"
|
||||||
rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.img" || true
|
rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.img" || true
|
||||||
rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" || true
|
rm -f "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" || true
|
||||||
rm -f "${WORK_DIR}/${EXPORT_NAME}.img" || true
|
rm -f "${WORK_DIR}/${EXPORT_NAME}.img" || true
|
||||||
rm -f "${WORK_DIR}/${EXPORT_NAME}.qcow2" || true
|
rm -f "${WORK_DIR}/${EXPORT_NAME}.qcow2" || true
|
||||||
EXPORT_STAGE=$(basename "${EXPORT_DIR}")
|
EXPORT_STAGE=$(basename "${EXPORT_DIR}")
|
||||||
for s in $STAGE_LIST; do
|
for s in $STAGE_LIST; do
|
||||||
TMP_LIST=${TMP_LIST:+$TMP_LIST }$(basename "${s}")
|
TMP_LIST=${TMP_LIST:+$TMP_LIST }$(basename "${s}")
|
||||||
done
|
done
|
||||||
FIRST_STAGE=${TMP_LIST%% *}
|
FIRST_STAGE=${TMP_LIST%% *}
|
||||||
FIRST_IMAGE="image-${FIRST_STAGE}.qcow2"
|
FIRST_IMAGE="image-${FIRST_STAGE}.qcow2"
|
||||||
|
|
||||||
pushd "${WORK_DIR}" > /dev/null
|
pushd "${WORK_DIR}" > /dev/null
|
||||||
echo "Creating new base "${EXPORT_NAME}.qcow2" from ${FIRST_IMAGE}"
|
echo "Creating new base "${EXPORT_NAME}.qcow2" from ${FIRST_IMAGE}"
|
||||||
cp "./${FIRST_IMAGE}" "${EXPORT_NAME}.qcow2"
|
cp "./${FIRST_IMAGE}" "${EXPORT_NAME}.qcow2"
|
||||||
|
|
||||||
ARR=($TMP_LIST)
|
ARR=($TMP_LIST)
|
||||||
# rebase stage images to new export base
|
# rebase stage images to new export base
|
||||||
for CURR_STAGE in "${ARR[@]}"; do
|
for CURR_STAGE in "${ARR[@]}"; do
|
||||||
if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
|
if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
|
||||||
PREV_IMG="${EXPORT_NAME}"
|
PREV_IMG="${EXPORT_NAME}"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
echo "Rebasing image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
|
echo "Rebasing image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
|
||||||
qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2
|
qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2
|
||||||
if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
|
if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
PREV_IMG="image-${CURR_STAGE}"
|
PREV_IMG="image-${CURR_STAGE}"
|
||||||
done
|
done
|
||||||
|
|
||||||
# commit current export stage into base export image
|
# commit current export stage into base export image
|
||||||
echo "Committing image-${EXPORT_STAGE}.qcow2 to ${EXPORT_NAME}.qcow2"
|
echo "Committing image-${EXPORT_STAGE}.qcow2 to ${EXPORT_NAME}.qcow2"
|
||||||
qemu-img commit -f qcow2 -p -b "${EXPORT_NAME}.qcow2" image-${EXPORT_STAGE}.qcow2
|
qemu-img commit -f qcow2 -p -b "${EXPORT_NAME}.qcow2" image-${EXPORT_STAGE}.qcow2
|
||||||
|
|
||||||
# rebase stage images back to original first stage for easy re-run
|
# rebase stage images back to original first stage for easy re-run
|
||||||
for CURR_STAGE in "${ARR[@]}"; do
|
for CURR_STAGE in "${ARR[@]}"; do
|
||||||
if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
|
if [ "${CURR_STAGE}" = "${FIRST_STAGE}" ]; then
|
||||||
PREV_IMG="image-${CURR_STAGE}"
|
PREV_IMG="image-${CURR_STAGE}"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
echo "Rebasing back image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
|
echo "Rebasing back image-${CURR_STAGE}.qcow2 onto ${PREV_IMG}.qcow2"
|
||||||
qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2
|
qemu-img rebase -f qcow2 -u -b ${PREV_IMG}.qcow2 image-${CURR_STAGE}.qcow2
|
||||||
if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
|
if [ "${CURR_STAGE}" = "${EXPORT_STAGE}" ]; then
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
PREV_IMG="image-${CURR_STAGE}"
|
PREV_IMG="image-${CURR_STAGE}"
|
||||||
done
|
done
|
||||||
popd > /dev/null
|
popd > /dev/null
|
||||||
|
|
||||||
mkdir -p "${WORK_DIR}/export-image/rootfs"
|
mkdir -p "${WORK_DIR}/export-image/rootfs"
|
||||||
mv "${WORK_DIR}/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/"
|
mv "${WORK_DIR}/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/"
|
||||||
echo "Mounting image ${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2 to rootfs ${WORK_DIR}/export-image/rootfs"
|
echo "Mounting image ${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2 to rootfs ${WORK_DIR}/export-image/rootfs"
|
||||||
mount_qimage "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/rootfs"
|
mount_qimage "${WORK_DIR}/export-image/${EXPORT_NAME}.qcow2" "${WORK_DIR}/export-image/rootfs"
|
||||||
|
|
||||||
CLEAN=0
|
CLEAN=0
|
||||||
run_stage
|
run_stage
|
||||||
CLEAN=1
|
CLEAN=1
|
||||||
USE_QCOW2=1
|
USE_QCOW2=1
|
||||||
|
|
||||||
else
|
else
|
||||||
run_stage
|
run_stage
|
||||||
fi
|
fi
|
||||||
if [ "${USE_QEMU}" != "1" ]; then
|
if [ "${USE_QEMU}" != "1" ]; then
|
||||||
if [ -e "${EXPORT_DIR}/EXPORT_NOOBS" ]; then
|
if [ -e "${EXPORT_DIR}/EXPORT_NOOBS" ]; then
|
||||||
|
@ -369,11 +368,11 @@ for EXPORT_DIR in ${EXPORT_DIRS}; do
|
||||||
source "${EXPORT_DIR}/EXPORT_NOOBS"
|
source "${EXPORT_DIR}/EXPORT_NOOBS"
|
||||||
STAGE_DIR="${BASE_DIR}/export-noobs"
|
STAGE_DIR="${BASE_DIR}/export-noobs"
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
USE_QCOW2=0
|
USE_QCOW2=0
|
||||||
run_stage
|
run_stage
|
||||||
USE_QCOW2=1
|
USE_QCOW2=1
|
||||||
else
|
else
|
||||||
run_stage
|
run_stage
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
@ -387,7 +386,7 @@ if [ -x postrun.sh ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
unload_qimage
|
unload_qimage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "End ${BASE_DIR}"
|
log "End ${BASE_DIR}"
|
||||||
|
|
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
||||||
|
|
||||||
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
||||||
|
|
||||||
IMGID="$(dd if="${IMG_FILE}" skip=440 bs=1 count=4 2>/dev/null | xxd -e | cut -f 2 -d' ')"
|
IMGID="$(dd if="${IMG_FILE}" skip=440 bs=1 count=4 2>/dev/null | xxd -e | cut -f 2 -d' ')"
|
||||||
|
|
||||||
BOOT_PARTUUID="${IMGID}-01"
|
BOOT_PARTUUID="${IMGID}-01"
|
||||||
ROOT_PARTUUID="${IMGID}-02"
|
ROOT_PARTUUID="${IMGID}-02"
|
||||||
|
|
||||||
sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
|
sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
|
||||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
|
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
|
||||||
|
|
||||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/boot/cmdline.txt"
|
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/boot/cmdline.txt"
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -85,15 +85,15 @@ rm -f "${DEPLOY_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
||||||
mv "$INFO_FILE" "$DEPLOY_DIR/"
|
mv "$INFO_FILE" "$DEPLOY_DIR/"
|
||||||
|
|
||||||
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
||||||
ROOT_DEV="$(mount | grep "${ROOTFS_DIR} " | cut -f1 -d' ')"
|
ROOT_DEV="$(mount | grep "${ROOTFS_DIR} " | cut -f1 -d' ')"
|
||||||
|
|
||||||
unmount "${ROOTFS_DIR}"
|
unmount "${ROOTFS_DIR}"
|
||||||
zerofree "${ROOT_DEV}"
|
zerofree "${ROOT_DEV}"
|
||||||
|
|
||||||
unmount_image "${IMG_FILE}"
|
unmount_image "${IMG_FILE}"
|
||||||
else
|
else
|
||||||
unload_qimage
|
unload_qimage
|
||||||
make_bootable_image "${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.qcow2" "$IMG_FILE"
|
make_bootable_image "${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.qcow2" "$IMG_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${DEPLOY_ZIP}" == "1" ]; then
|
if [ "${DEPLOY_ZIP}" == "1" ]; then
|
||||||
|
|
|
@ -1,63 +1,63 @@
|
||||||
#!/bin/bash -e
|
#!/bin/bash -e
|
||||||
|
|
||||||
if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
|
||||||
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
||||||
|
|
||||||
unmount_image "${IMG_FILE}"
|
unmount_image "${IMG_FILE}"
|
||||||
|
|
||||||
rm -f "${IMG_FILE}"
|
rm -f "${IMG_FILE}"
|
||||||
|
|
||||||
rm -rf "${ROOTFS_DIR}"
|
rm -rf "${ROOTFS_DIR}"
|
||||||
mkdir -p "${ROOTFS_DIR}"
|
mkdir -p "${ROOTFS_DIR}"
|
||||||
|
|
||||||
BOOT_SIZE="$((256 * 1024 * 1024))"
|
BOOT_SIZE="$((256 * 1024 * 1024))"
|
||||||
ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot --block-size=1 | cut -f 1)
|
ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot --block-size=1 | cut -f 1)
|
||||||
|
|
||||||
# All partition sizes and starts will be aligned to this size
|
# All partition sizes and starts will be aligned to this size
|
||||||
ALIGN="$((4 * 1024 * 1024))"
|
ALIGN="$((4 * 1024 * 1024))"
|
||||||
# Add this much space to the calculated file size. This allows for
|
# Add this much space to the calculated file size. This allows for
|
||||||
# some overhead (since actual space usage is usually rounded up to the
|
# some overhead (since actual space usage is usually rounded up to the
|
||||||
# filesystem block size) and gives some free space on the resulting
|
# filesystem block size) and gives some free space on the resulting
|
||||||
# image.
|
# image.
|
||||||
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
|
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
|
||||||
|
|
||||||
BOOT_PART_START=$((ALIGN))
|
BOOT_PART_START=$((ALIGN))
|
||||||
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
|
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
|
||||||
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
|
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
|
||||||
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN))
|
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN))
|
||||||
IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE))
|
IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE))
|
||||||
|
|
||||||
truncate -s "${IMG_SIZE}" "${IMG_FILE}"
|
truncate -s "${IMG_SIZE}" "${IMG_FILE}"
|
||||||
|
|
||||||
parted --script "${IMG_FILE}" mklabel msdos
|
parted --script "${IMG_FILE}" mklabel msdos
|
||||||
parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
|
parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
|
||||||
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
|
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
|
||||||
|
|
||||||
PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
|
PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
|
||||||
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
|
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
|
||||||
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
|
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
|
||||||
|
|
||||||
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
|
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
|
||||||
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
|
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
|
||||||
|
|
||||||
BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
|
BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
|
||||||
ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
|
ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
|
||||||
echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
|
echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
|
||||||
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
|
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
|
||||||
|
|
||||||
ROOT_FEATURES="^huge_file"
|
ROOT_FEATURES="^huge_file"
|
||||||
for FEATURE in metadata_csum 64bit; do
|
for FEATURE in metadata_csum 64bit; do
|
||||||
if grep -q "$FEATURE" /etc/mke2fs.conf; then
|
if grep -q "$FEATURE" /etc/mke2fs.conf; then
|
||||||
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES"
|
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
mkdosfs -n boot -F 32 -v "$BOOT_DEV" > /dev/null
|
mkdosfs -n boot -F 32 -v "$BOOT_DEV" > /dev/null
|
||||||
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null
|
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null
|
||||||
|
|
||||||
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4
|
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4
|
||||||
mkdir -p "${ROOTFS_DIR}/boot"
|
mkdir -p "${ROOTFS_DIR}/boot"
|
||||||
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat
|
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat
|
||||||
|
|
||||||
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
|
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
|
||||||
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"
|
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -41,7 +41,7 @@ sed "${NOOBS_DIR}/os.json" -i -e "s|RELEASE|${RELEASE}|"
|
||||||
sed "${NOOBS_DIR}/release_notes.txt" -i -e "s|UNRELEASED|${IMG_DATE}|"
|
sed "${NOOBS_DIR}/release_notes.txt" -i -e "s|UNRELEASED|${IMG_DATE}|"
|
||||||
|
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
mv "${NOOBS_DIR}" "${DEPLOY_DIR}/"
|
mv "${NOOBS_DIR}" "${DEPLOY_DIR}/"
|
||||||
else
|
else
|
||||||
cp -a "${NOOBS_DIR}" "${DEPLOY_DIR}/"
|
cp -a "${NOOBS_DIR}" "${DEPLOY_DIR}/"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -4,9 +4,9 @@ NOOBS_DIR="${STAGE_WORK_DIR}/${IMG_DATE}-${IMG_NAME}${IMG_SUFFIX}"
|
||||||
mkdir -p "${STAGE_WORK_DIR}"
|
mkdir -p "${STAGE_WORK_DIR}"
|
||||||
|
|
||||||
if [ "${DEPLOY_ZIP}" == "1" ]; then
|
if [ "${DEPLOY_ZIP}" == "1" ]; then
|
||||||
IMG_FILE="${WORK_DIR}/export-image/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
IMG_FILE="${WORK_DIR}/export-image/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
||||||
else
|
else
|
||||||
IMG_FILE="${DEPLOY_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
IMG_FILE="${DEPLOY_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unmount_image "${IMG_FILE}"
|
unmount_image "${IMG_FILE}"
|
||||||
|
@ -38,7 +38,7 @@ umount "${STAGE_WORK_DIR}/rootfs/boot"
|
||||||
bsdtar --numeric-owner --format gnutar -C "${STAGE_WORK_DIR}/rootfs" --one-file-system -cpf - . | xz -T0 > "${NOOBS_DIR}/root.tar.xz"
|
bsdtar --numeric-owner --format gnutar -C "${STAGE_WORK_DIR}/rootfs" --one-file-system -cpf - . | xz -T0 > "${NOOBS_DIR}/root.tar.xz"
|
||||||
|
|
||||||
if [ "${USE_QCOW2}" = "1" ]; then
|
if [ "${USE_QCOW2}" = "1" ]; then
|
||||||
rm "$ROOTFS_DIR/etc/systemd/system/multi-user.target.wants/apply_noobs_os_config.service"
|
rm "$ROOTFS_DIR/etc/systemd/system/multi-user.target.wants/apply_noobs_os_config.service"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unmount_image "${IMG_FILE}"
|
unmount_image "${IMG_FILE}"
|
||||||
|
|
112
imagetool.sh
112
imagetool.sh
|
@ -1,15 +1,15 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
if [ "$(id -u)" != "0" ]; then
|
if [ "$(id -u)" != "0" ]; then
|
||||||
echo "Please run as root" 1>&2
|
echo "Please run as root" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
progname=$(basename $0)
|
progname=$(basename $0)
|
||||||
|
|
||||||
function usage()
|
function usage()
|
||||||
{
|
{
|
||||||
cat << HEREDOC
|
cat << HEREDOC
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
Mount Image : $progname [--mount] [--image-name <path to qcow2 image>] [--mount-point <mount point>]
|
Mount Image : $progname [--mount] [--image-name <path to qcow2 image>] [--mount-point <mount point>]
|
||||||
|
@ -36,77 +36,77 @@ IMAGE=""
|
||||||
MOUNTPOINT=""
|
MOUNTPOINT=""
|
||||||
|
|
||||||
nbd_cleanup() {
|
nbd_cleanup() {
|
||||||
DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)"
|
DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)"
|
||||||
if [ ! -z "${DEVS}" ]; then
|
if [ ! -z "${DEVS}" ]; then
|
||||||
for d in $DEVS; do
|
for d in $DEVS; do
|
||||||
if [ ! -z "${d}" ]; then
|
if [ ! -z "${d}" ]; then
|
||||||
QDEV="$(ps xa | grep $d | grep -v grep)"
|
QDEV="$(ps xa | grep $d | grep -v grep)"
|
||||||
if [ -z "${QDEV}" ]; then
|
if [ -z "${QDEV}" ]; then
|
||||||
kpartx -d /dev/$d && echo "Unconnected device map removed: /dev/$d"
|
kpartx -d /dev/$d && echo "Unconnected device map removed: /dev/$d"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# As long as there is at least one more argument, keep looping
|
# As long as there is at least one more argument, keep looping
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
key="$1"
|
key="$1"
|
||||||
case "$key" in
|
case "$key" in
|
||||||
-h|--help)
|
-h|--help)
|
||||||
usage
|
usage
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
-c|--cleanup)
|
-c|--cleanup)
|
||||||
nbd_cleanup
|
nbd_cleanup
|
||||||
;;
|
;;
|
||||||
-m|--mount)
|
-m|--mount)
|
||||||
MOUNT=1
|
MOUNT=1
|
||||||
;;
|
;;
|
||||||
-u|--umount)
|
-u|--umount)
|
||||||
UMOUNT=1
|
UMOUNT=1
|
||||||
;;
|
;;
|
||||||
-i|--image-name)
|
-i|--image-name)
|
||||||
shift
|
shift
|
||||||
IMAGE="$1"
|
IMAGE="$1"
|
||||||
;;
|
;;
|
||||||
-p|--mount-point)
|
-p|--mount-point)
|
||||||
shift
|
shift
|
||||||
MOUNTPOINT="$1"
|
MOUNTPOINT="$1"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unknown option '$key'"
|
echo "Unknown option '$key'"
|
||||||
usage
|
usage
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
# Shift after checking all the cases to get the next option
|
# Shift after checking all the cases to get the next option
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "${MOUNT}" = "1" ] && [ "${UMOUNT}" = "1" ]; then
|
if [ "${MOUNT}" = "1" ] && [ "${UMOUNT}" = "1" ]; then
|
||||||
usage
|
usage
|
||||||
echo "Concurrent mount options not possible."
|
echo "Concurrent mount options not possible."
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${MOUNT}" = "1" ] && ([ -z "${IMAGE}" ] || [ -z "${MOUNTPOINT}" ]); then
|
if [ "${MOUNT}" = "1" ] && ([ -z "${IMAGE}" ] || [ -z "${MOUNTPOINT}" ]); then
|
||||||
usage
|
usage
|
||||||
echo "Can not mount image. Image path and/or mount point missing."
|
echo "Can not mount image. Image path and/or mount point missing."
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then
|
if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then
|
||||||
usage
|
usage
|
||||||
echo "Can not umount. Mount point parameter missing."
|
echo "Can not umount. Mount point parameter missing."
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export NBD_DEV="${NBD_DEV:-/dev/nbd1}"
|
export NBD_DEV="${NBD_DEV:-/dev/nbd1}"
|
||||||
source scripts/qcow2_handling
|
source scripts/qcow2_handling
|
||||||
|
|
||||||
if [ "${MOUNT}" = "1" ]; then
|
if [ "${MOUNT}" = "1" ]; then
|
||||||
mount_qimage "${MOUNTPOINT}" "${IMAGE}"
|
mount_qimage "${MOUNTPOINT}" "${IMAGE}"
|
||||||
elif [ "${UMOUNT}" = "1" ]; then
|
elif [ "${UMOUNT}" = "1" ]; then
|
||||||
umount_qimage "${MOUNTPOINT}"
|
umount_qimage "${MOUNTPOINT}"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -16,230 +16,241 @@ export MAP_ROOT_DEV
|
||||||
|
|
||||||
# find and initialize free block device nodes
|
# find and initialize free block device nodes
|
||||||
init_nbd() {
|
init_nbd() {
|
||||||
modprobe nbd max_part=16
|
modprobe nbd max_part=16
|
||||||
if [ -z "${NBD_DEV}" ]; then
|
if [ -z "${NBD_DEV}" ]; then
|
||||||
for x in /sys/class/block/nbd* ; do
|
for x in /sys/class/block/nbd* ; do
|
||||||
S=`cat $x/size`
|
S=`cat $x/size`
|
||||||
if [ "$S" == "0" ] ; then
|
if [ "$S" == "0" ] ; then
|
||||||
NBD_DEV=/dev/$(basename $x)
|
NBD_DEV=/dev/$(basename $x)
|
||||||
MAP_BOOT_DEV=/dev/mapper/$(basename $x)p1
|
MAP_BOOT_DEV=/dev/mapper/$(basename $x)p1
|
||||||
MAP_ROOT_DEV=/dev/mapper/$(basename $x)p2
|
MAP_ROOT_DEV=/dev/mapper/$(basename $x)p2
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
export -f init_nbd
|
export -f init_nbd
|
||||||
|
|
||||||
# connect image to block device
|
# connect image to block device
|
||||||
connect_blkdev() {
|
connect_blkdev() {
|
||||||
init_nbd
|
init_nbd
|
||||||
qemu-nbd --discard=unmap -c $NBD_DEV "$1"
|
qemu-nbd --discard=unmap -c $NBD_DEV "$1"
|
||||||
sync
|
sync
|
||||||
kpartx -a $NBD_DEV
|
kpartx -a $NBD_DEV
|
||||||
sync
|
sync
|
||||||
CURRENT_IMAGE="$1"
|
CURRENT_IMAGE="$1"
|
||||||
}
|
}
|
||||||
export -f connect_blkdev
|
export -f connect_blkdev
|
||||||
|
|
||||||
# disconnect image from block device
|
# disconnect image from block device
|
||||||
disconnect_blkdev() {
|
disconnect_blkdev() {
|
||||||
kpartx -d $NBD_DEV
|
kpartx -d $NBD_DEV
|
||||||
qemu-nbd -d $NBD_DEV
|
qemu-nbd -d $NBD_DEV
|
||||||
NBD_DEV=
|
NBD_DEV=
|
||||||
MAP_BOOT_DEV=
|
MAP_BOOT_DEV=
|
||||||
MAP_ROOT_DEV=
|
MAP_ROOT_DEV=
|
||||||
CURRENT_IMAGE=
|
CURRENT_IMAGE=
|
||||||
}
|
}
|
||||||
export -f disconnect_blkdev
|
export -f disconnect_blkdev
|
||||||
|
|
||||||
# mount qcow2 image: mount_image <image file> <mountpoint>
|
# mount qcow2 image: mount_image <image file> <mountpoint>
|
||||||
mount_qimage() {
|
mount_qimage() {
|
||||||
connect_blkdev "$1"
|
connect_blkdev "$1"
|
||||||
mount -v -t ext4 $MAP_ROOT_DEV "$2"
|
mount -v -t ext4 $MAP_ROOT_DEV "$2"
|
||||||
mkdir -p "${ROOTFS_DIR}/boot"
|
mkdir -p "${ROOTFS_DIR}/boot"
|
||||||
mount -v -t vfat $MAP_BOOT_DEV "$2/boot"
|
mount -v -t vfat $MAP_BOOT_DEV "$2/boot"
|
||||||
CURRENT_MOUNTPOINT="$2"
|
CURRENT_MOUNTPOINT="$2"
|
||||||
}
|
}
|
||||||
export -f mount_qimage
|
export -f mount_qimage
|
||||||
|
|
||||||
# umount qcow2 image: umount_image <current mountpoint>
|
# umount qcow2 image: umount_image <current mountpoint>
|
||||||
umount_qimage() {
|
umount_qimage() {
|
||||||
sync
|
sync
|
||||||
#umount "$1/boot"
|
#umount "$1/boot"
|
||||||
while mount | grep -q "$1"; do
|
while mount | grep -q "$1"; do
|
||||||
local LOCS
|
local LOCS
|
||||||
LOCS=$(mount | grep "$1" | cut -f 3 -d ' ' | sort -r)
|
LOCS=$(mount | grep "$1" | cut -f 3 -d ' ' | sort -r)
|
||||||
for loc in $LOCS; do
|
for loc in $LOCS; do
|
||||||
echo "$loc"
|
echo "$loc"
|
||||||
while mountpoint -q "$loc" && ! umount "$loc"; do
|
while mountpoint -q "$loc" && ! umount "$loc"; do
|
||||||
sleep 0.1
|
sleep 0.1
|
||||||
|
done
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
done
|
CURRENT_MOUNTPOINT=
|
||||||
CURRENT_MOUNTPOINT=
|
disconnect_blkdev
|
||||||
disconnect_blkdev
|
|
||||||
}
|
}
|
||||||
export -f umount_qimage
|
export -f umount_qimage
|
||||||
|
|
||||||
# create base image / backing image / mount image
|
# create base image / backing image / mount image
|
||||||
load_qimage() {
|
load_qimage() {
|
||||||
if [ -z "${CURRENT_MOUNTPOINT}" ]; then
|
if [ -z "${CURRENT_MOUNTPOINT}" ]; then
|
||||||
if [ ! -d "${ROOTFS_DIR}" ]; then mkdir -p "${ROOTFS_DIR}"; fi
|
if [ ! -d "${ROOTFS_DIR}" ]; then
|
||||||
|
mkdir -p "${ROOTFS_DIR}";
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "${CLEAN}" = "1" ] && [ -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then rm -f "${WORK_DIR}/image-${STAGE}.qcow2"; fi
|
if [ "${CLEAN}" = "1" ] && [ -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then
|
||||||
|
rm -f "${WORK_DIR}/image-${STAGE}.qcow2";
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then
|
if [ ! -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then
|
||||||
pushd ${WORK_DIR} > /dev/null
|
pushd ${WORK_DIR} > /dev/null
|
||||||
init_nbd
|
init_nbd
|
||||||
if [ -z "${PREV_STAGE}" ]; then
|
if [ -z "${PREV_STAGE}" ]; then
|
||||||
echo "Creating base image: image-${STAGE}.qcow2"
|
echo "Creating base image: image-${STAGE}.qcow2"
|
||||||
# -o preallocation=falloc
|
# -o preallocation=falloc
|
||||||
qemu-img create -f qcow2 image-${STAGE}.qcow2 $BASE_QCOW2_SIZE
|
qemu-img create -f qcow2 image-${STAGE}.qcow2 $BASE_QCOW2_SIZE
|
||||||
sync
|
sync
|
||||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
||||||
sync
|
sync
|
||||||
sfdisk $NBD_DEV << EOF
|
sfdisk $NBD_DEV << EOF
|
||||||
,250MiB,b
|
4,250MiB,b,*
|
||||||
,,83;
|
,,83;
|
||||||
EOF
|
EOF
|
||||||
sync
|
sync
|
||||||
kpartx -a $NBD_DEV
|
kpartx -a $NBD_DEV
|
||||||
mkdosfs -n boot -F 32 -v $MAP_BOOT_DEV
|
mkdosfs -n boot -F 32 -v $MAP_BOOT_DEV
|
||||||
mkfs.ext4 -L rootfs -O "^huge_file,^metadata_csum,^64bit" $MAP_ROOT_DEV
|
mkfs.ext4 -L rootfs -O "^huge_file,^metadata_csum,^64bit" $MAP_ROOT_DEV
|
||||||
sync
|
sync
|
||||||
else
|
else
|
||||||
if [ ! -f "${WORK_DIR}/image-${PREV_STAGE}.qcow2" ]; then exit 1; fi
|
if [ ! -f "${WORK_DIR}/image-${PREV_STAGE}.qcow2" ]; then
|
||||||
echo "Creating backing image: image-${STAGE}.qcow2 <- ${WORK_DIR}/image-${PREV_STAGE}.qcow2"
|
exit 1;
|
||||||
qemu-img create -f qcow2 \
|
fi
|
||||||
-o backing_file=${WORK_DIR}/image-${PREV_STAGE}.qcow2 \
|
echo "Creating backing image: image-${STAGE}.qcow2 <- ${WORK_DIR}/image-${PREV_STAGE}.qcow2"
|
||||||
${WORK_DIR}/image-${STAGE}.qcow2
|
qemu-img create -f qcow2 \
|
||||||
sync
|
-o backing_file=${WORK_DIR}/image-${PREV_STAGE}.qcow2 \
|
||||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
${WORK_DIR}/image-${STAGE}.qcow2
|
||||||
sync
|
sync
|
||||||
kpartx -a $NBD_DEV
|
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
||||||
fi
|
sync
|
||||||
mount -v -t ext4 $MAP_ROOT_DEV "${ROOTFS_DIR}"
|
kpartx -a $NBD_DEV
|
||||||
mkdir -p "${ROOTFS_DIR}/boot"
|
fi
|
||||||
mount -v -t vfat $MAP_BOOT_DEV "${ROOTFS_DIR}/boot"
|
|
||||||
CURRENT_IMAGE=${WORK_DIR}/image-${STAGE}.qcow2
|
mount -v -t ext4 $MAP_ROOT_DEV "${ROOTFS_DIR}"
|
||||||
CURRENT_MOUNTPOINT=${ROOTFS_DIR}
|
mkdir -p "${ROOTFS_DIR}/boot"
|
||||||
popd > /dev/null
|
mount -v -t vfat $MAP_BOOT_DEV "${ROOTFS_DIR}/boot"
|
||||||
else
|
CURRENT_IMAGE=${WORK_DIR}/image-${STAGE}.qcow2
|
||||||
mount_qimage "${WORK_DIR}/image-${STAGE}.qcow2" "${ROOTFS_DIR}"
|
CURRENT_MOUNTPOINT=${ROOTFS_DIR}
|
||||||
fi
|
popd > /dev/null
|
||||||
echo "Current image in use: ${CURRENT_IMAGE} (MP: ${CURRENT_MOUNTPOINT})"
|
else
|
||||||
fi
|
mount_qimage "${WORK_DIR}/image-${STAGE}.qcow2" "${ROOTFS_DIR}"
|
||||||
|
fi
|
||||||
|
echo "Current image in use: ${CURRENT_IMAGE} (MP: ${CURRENT_MOUNTPOINT})"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
export -f load_qimage
|
export -f load_qimage
|
||||||
|
|
||||||
# umount current image and refresh mount point env var
|
# umount current image and refresh mount point env var
|
||||||
unload_qimage() {
|
unload_qimage() {
|
||||||
if [ ! -z "${CURRENT_MOUNTPOINT}" ]; then
|
if [ ! -z "${CURRENT_MOUNTPOINT}" ]; then
|
||||||
fstrim -v "${CURRENT_MOUNTPOINT}" || true
|
fstrim -v "${CURRENT_MOUNTPOINT}" || true
|
||||||
umount_qimage "${CURRENT_MOUNTPOINT}"
|
umount_qimage "${CURRENT_MOUNTPOINT}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
export -f unload_qimage
|
export -f unload_qimage
|
||||||
|
|
||||||
# based on: https://github.com/SirLagz/RaspberryPi-ImgAutoSizer
|
# based on: https://github.com/SirLagz/RaspberryPi-ImgAutoSizer
|
||||||
# helper function for make_bootable_image, do not call directly
|
# helper function for make_bootable_image, do not call directly
|
||||||
function resize_qcow2() {
|
function resize_qcow2() {
|
||||||
if [ -z "$CALL_FROM_MBI" ]; then echo "resize_qcow2: cannot be called directly, use make_bootable_image instead"; return 1; fi
|
if [ -z "$CALL_FROM_MBI" ]; then
|
||||||
|
echo "resize_qcow2: cannot be called directly, use make_bootable_image instead"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
ROOT_MARGIN=$((800*1024*1024))
|
# ROOT_MARGIN=$((800*1024*1024))
|
||||||
PARTED_OUT=`parted -s -m "$NBD_DEV" unit B print`
|
ROOT_MARGIN=$((1*1024*1024))
|
||||||
PART_NO=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print $1 } '`
|
PARTED_OUT=`parted -s -m "$NBD_DEV" unit B print`
|
||||||
PART_START=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
PART_NO=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print $1 } '`
|
||||||
|
PART_START=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
||||||
|
|
||||||
e2fsck -y -f $MAP_ROOT_DEV || true
|
e2fsck -y -f $MAP_ROOT_DEV || true
|
||||||
|
|
||||||
DATA_SIZE=`resize2fs -P $MAP_ROOT_DEV | awk -F': ' ' { print $2 } '`
|
DATA_SIZE=`resize2fs -P $MAP_ROOT_DEV | awk -F': ' ' { print $2 } '`
|
||||||
BLOCK_SIZE=$(dumpe2fs -h $MAP_ROOT_DEV | grep 'Block size' | awk -F': ' ' { print $2 }')
|
BLOCK_SIZE=$(dumpe2fs -h $MAP_ROOT_DEV | grep 'Block size' | awk -F': ' ' { print $2 }')
|
||||||
BLOCK_SIZE=${BLOCK_SIZE// /}
|
BLOCK_SIZE=${BLOCK_SIZE// /}
|
||||||
|
|
||||||
let DATA_SIZE=$DATA_SIZE+$ROOT_MARGIN/$BLOCK_SIZE
|
let DATA_SIZE=$DATA_SIZE+$ROOT_MARGIN/$BLOCK_SIZE
|
||||||
resize2fs -p $MAP_ROOT_DEV $DATA_SIZE
|
resize2fs -p $MAP_ROOT_DEV $DATA_SIZE
|
||||||
sleep 1
|
sleep 1
|
||||||
|
|
||||||
let PART_NEW_SIZE=$DATA_SIZE*$BLOCK_SIZE
|
let PART_NEW_SIZE=$DATA_SIZE*$BLOCK_SIZE
|
||||||
let PART_NEW_END=$PART_START+$PART_NEW_SIZE
|
let PART_NEW_END=$PART_START+$PART_NEW_SIZE
|
||||||
ACT1=`parted -s "$NBD_DEV" rm 2`
|
ACT1=`parted -s "$NBD_DEV" rm 2`
|
||||||
ACT2=`parted -s "$NBD_DEV" unit B mkpart primary $PART_START $PART_NEW_END`
|
ACT2=`parted -s "$NBD_DEV" unit B mkpart primary $PART_START $PART_NEW_END`
|
||||||
NEW_IMG_SIZE=`parted -s -m "$NBD_DEV" unit B print free | tail -1 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
NEW_IMG_SIZE=`parted -s -m "$NBD_DEV" unit B print free | tail -1 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
||||||
}
|
}
|
||||||
export -f resize_qcow2
|
export -f resize_qcow2
|
||||||
|
|
||||||
# create raw img from qcow2: make_bootable_image <in.qcow2> <out.img>
|
# create raw img from qcow2: make_bootable_image <in.qcow2> <out.img>
|
||||||
function make_bootable_image() {
|
function make_bootable_image() {
|
||||||
|
|
||||||
EXPORT_QCOW2="$1"
|
EXPORT_QCOW2="$1"
|
||||||
EXPORT_IMAGE="$2"
|
EXPORT_IMAGE="$2"
|
||||||
|
|
||||||
echo "Connect block device to source qcow2"
|
echo "Connect block device to source qcow2"
|
||||||
connect_blkdev "${EXPORT_QCOW2}"
|
connect_blkdev "${EXPORT_QCOW2}"
|
||||||
|
|
||||||
echo "Resize fs and partition"
|
echo "Resize fs and partition"
|
||||||
CALL_FROM_MBI=1
|
CALL_FROM_MBI=1
|
||||||
resize_qcow2
|
resize_qcow2
|
||||||
sync
|
sync
|
||||||
CALL_FROM_MBI=
|
CALL_FROM_MBI=
|
||||||
|
|
||||||
echo "Disconnect block device"
|
echo "Disconnect block device"
|
||||||
disconnect_blkdev
|
disconnect_blkdev
|
||||||
|
|
||||||
if [ -z "$NEW_IMG_SIZE" ]; then
|
if [ -z "$NEW_IMG_SIZE" ]; then
|
||||||
echo "NEW_IMG_SIZE could not be calculated, cannot process image. Exit."
|
echo "NEW_IMG_SIZE could not be calculated, cannot process image. Exit."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Shrinking qcow2 image"
|
echo "Shrinking qcow2 image"
|
||||||
qemu-img resize --shrink "${EXPORT_QCOW2}" $NEW_IMG_SIZE
|
qemu-img resize --shrink "${EXPORT_QCOW2}" $NEW_IMG_SIZE
|
||||||
sync
|
sync
|
||||||
|
|
||||||
echo "Convert qcow2 to raw image"
|
echo "Convert qcow2 to raw image"
|
||||||
qemu-img convert -f qcow2 -O raw "${EXPORT_QCOW2}" "${EXPORT_IMAGE}"
|
qemu-img convert -f qcow2 -O raw "${EXPORT_QCOW2}" "${EXPORT_IMAGE}"
|
||||||
sync
|
sync
|
||||||
|
|
||||||
echo "Get PARTUUIDs from image"
|
echo "Get PARTUUIDs from image"
|
||||||
IMGID="$(blkid -o value -s PTUUID "${EXPORT_IMAGE}")"
|
IMGID="$(blkid -o value -s PTUUID "${EXPORT_IMAGE}")"
|
||||||
|
|
||||||
BOOT_PARTUUID="${IMGID}-01"
|
BOOT_PARTUUID="${IMGID}-01"
|
||||||
echo "Boot: $BOOT_PARTUUID"
|
echo "Boot: $BOOT_PARTUUID"
|
||||||
ROOT_PARTUUID="${IMGID}-02"
|
ROOT_PARTUUID="${IMGID}-02"
|
||||||
echo "Root: $ROOT_PARTUUID"
|
echo "Root: $ROOT_PARTUUID"
|
||||||
|
|
||||||
echo "Mount image"
|
echo "Mount image"
|
||||||
MOUNTROOT=${WORK_DIR}/tmpimage
|
MOUNTROOT=${WORK_DIR}/tmpimage
|
||||||
mkdir -p $MOUNTROOT
|
mkdir -p $MOUNTROOT
|
||||||
|
|
||||||
MOUNTPT=$MOUNTROOT
|
MOUNTPT=$MOUNTROOT
|
||||||
PARTITION=2
|
PARTITION=2
|
||||||
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
||||||
|
|
||||||
MOUNTPT=$MOUNTROOT/boot
|
MOUNTPT=$MOUNTROOT/boot
|
||||||
PARTITION=1
|
PARTITION=1
|
||||||
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
||||||
|
|
||||||
if [ ! -d "${MOUNTROOT}/root" ]; then
|
if [ ! -d "${MOUNTROOT}/root" ]; then
|
||||||
echo "Image damaged or not mounted. Exit."
|
echo "Image damaged or not mounted. Exit."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Setup PARTUUIDs"
|
echo "Setup PARTUUIDs"
|
||||||
if [ ! -z "$BOOT_PARTUUID" ] && [ ! -z "$ROOT_PARTUUID" ]; then
|
if [ ! -z "$BOOT_PARTUUID" ] && [ ! -z "$ROOT_PARTUUID" ]; then
|
||||||
echo "Set UUIDs to make it bootable"
|
echo "Set UUIDs to make it bootable"
|
||||||
sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
||||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
||||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/boot/cmdline.txt"
|
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/boot/cmdline.txt"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Umount image"
|
echo "Umount image"
|
||||||
sync
|
sync
|
||||||
umount "${MOUNTROOT}/boot" || exit 1
|
umount "${MOUNTROOT}/boot" || exit 1
|
||||||
umount "${MOUNTROOT}" || exit 1
|
umount "${MOUNTROOT}" || exit 1
|
||||||
|
|
||||||
echo "Remove qcow2 export image"
|
echo "Remove qcow2 export image"
|
||||||
rm -f "${EXPORT_QCOW2}"
|
rm -f "${EXPORT_QCOW2}"
|
||||||
}
|
}
|
||||||
export -f make_bootable_image
|
export -f make_bootable_image
|
||||||
|
|
Loading…
Reference in New Issue
Block a user