mirror of https://github.com/RPi-Distro/pi-gen
Mark Spatz
11 months ago
committed by
GitHub
13 changed files with 78 additions and 675 deletions
@ -1,17 +1,13 @@
|
||||
#!/bin/bash -e |
||||
|
||||
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" |
||||
ROOT_PARTUUID="${IMGID}-02" |
||||
|
||||
BOOT_PARTUUID="${IMGID}-01" |
||||
ROOT_PARTUUID="${IMGID}-02" |
||||
|
||||
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}/boot/firmware/cmdline.txt" |
||||
fi |
||||
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}/boot/firmware/cmdline.txt" |
||||
|
@ -1,68 +1,66 @@
|
||||
#!/bin/bash -e |
||||
|
||||
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}" |
||||
mkdir -p "${ROOTFS_DIR}" |
||||
rm -rf "${ROOTFS_DIR}" |
||||
mkdir -p "${ROOTFS_DIR}" |
||||
|
||||
BOOT_SIZE="$((512 * 1024 * 1024))" |
||||
ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot/firmware --block-size=1 | cut -f 1) |
||||
BOOT_SIZE="$((512 * 1024 * 1024))" |
||||
ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot/firmware --block-size=1 | cut -f 1) |
||||
|
||||
# All partition sizes and starts will be aligned to this size |
||||
ALIGN="$((4 * 1024 * 1024))" |
||||
# Add this much space to the calculated file size. This allows for |
||||
# some overhead (since actual space usage is usually rounded up to the |
||||
# filesystem block size) and gives some free space on the resulting |
||||
# image. |
||||
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)" |
||||
# All partition sizes and starts will be aligned to this size |
||||
ALIGN="$((4 * 1024 * 1024))" |
||||
# Add this much space to the calculated file size. This allows for |
||||
# some overhead (since actual space usage is usually rounded up to the |
||||
# filesystem block size) and gives some free space on the resulting |
||||
# image. |
||||
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)" |
||||
|
||||
BOOT_PART_START=$((ALIGN)) |
||||
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN)) |
||||
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE)) |
||||
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN)) |
||||
IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE)) |
||||
BOOT_PART_START=$((ALIGN)) |
||||
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN)) |
||||
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE)) |
||||
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN)) |
||||
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}" 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}" 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 ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))" |
||||
|
||||
echo "Creating loop device..." |
||||
cnt=0 |
||||
until ensure_next_loopdev && LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do |
||||
if [ $cnt -lt 5 ]; then |
||||
cnt=$((cnt + 1)) |
||||
echo "Error in losetup. Retrying..." |
||||
sleep 5 |
||||
else |
||||
echo "ERROR: losetup failed; exiting" |
||||
exit 1 |
||||
fi |
||||
done |
||||
|
||||
ensure_loopdev_partitions "$LOOP_DEV" |
||||
BOOT_DEV="${LOOP_DEV}p1" |
||||
ROOT_DEV="${LOOP_DEV}p2" |
||||
|
||||
ROOT_FEATURES="^huge_file" |
||||
for FEATURE in 64bit; do |
||||
if grep -q "$FEATURE" /etc/mke2fs.conf; then |
||||
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES" |
||||
echo "Creating loop device..." |
||||
cnt=0 |
||||
until ensure_next_loopdev && LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do |
||||
if [ $cnt -lt 5 ]; then |
||||
cnt=$((cnt + 1)) |
||||
echo "Error in losetup. Retrying..." |
||||
sleep 5 |
||||
else |
||||
echo "ERROR: losetup failed; exiting" |
||||
exit 1 |
||||
fi |
||||
done |
||||
mkdosfs -n bootfs -F 32 -s 4 -v "$BOOT_DEV" > /dev/null |
||||
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null |
||||
done |
||||
|
||||
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4 |
||||
mkdir -p "${ROOTFS_DIR}/boot/firmware" |
||||
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot/firmware" -t vfat |
||||
ensure_loopdev_partitions "$LOOP_DEV" |
||||
BOOT_DEV="${LOOP_DEV}p1" |
||||
ROOT_DEV="${LOOP_DEV}p2" |
||||
|
||||
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot/firmware "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/" |
||||
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/firmware/" "${ROOTFS_DIR}/boot/firmware/" |
||||
ROOT_FEATURES="^huge_file" |
||||
for FEATURE in 64bit; do |
||||
if grep -q "$FEATURE" /etc/mke2fs.conf; then |
||||
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES" |
||||
fi |
||||
done |
||||
mkdosfs -n bootfs -F 32 -s 4 -v "$BOOT_DEV" > /dev/null |
||||
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null |
||||
|
||||
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4 |
||||
mkdir -p "${ROOTFS_DIR}/boot/firmware" |
||||
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot/firmware" -t vfat |
||||
|
||||
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot/firmware "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/" |
||||
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/firmware/" "${ROOTFS_DIR}/boot/firmware/" |
||||
|
@ -1,114 +0,0 @@
|
||||
#!/bin/bash |
||||
|
||||
if [ "$(id -u)" != "0" ]; then |
||||
echo "Please run as root" 1>&2 |
||||
exit 1 |
||||
fi |
||||
|
||||
progname=$(basename $0) |
||||
|
||||
function usage() |
||||
{ |
||||
cat << HEREDOC |
||||
|
||||
Usage: |
||||
Mount Image : $progname [--mount] [--image-name <path to qcow2 image>] [--mount-point <mount point>] |
||||
Umount Image: $progname [--umount] [--mount-point <mount point>] |
||||
Cleanup NBD : $progname [--cleanup] |
||||
|
||||
arguments: |
||||
-h, --help show this help message and exit |
||||
-c, --cleanup cleanup orphaned device mappings |
||||
-m, --mount mount image |
||||
-u, --umount umount image |
||||
-i, --image-name path to qcow2 image |
||||
-p, --mount-point mount point for image |
||||
|
||||
This tool will use /dev/nbd1 as default for mounting an image. If you want to use another device, execute like this: |
||||
NBD_DEV=/dev/nbd2 ./$progname --mount --image-name <your image> --mount-point <your path> |
||||
|
||||
HEREDOC |
||||
} |
||||
|
||||
MOUNT=0 |
||||
UMOUNT=0 |
||||
IMAGE="" |
||||
MOUNTPOINT="" |
||||
|
||||
nbd_cleanup() { |
||||
DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)" |
||||
if [ ! -z "${DEVS}" ]; then |
||||
for d in $DEVS; do |
||||
if [ ! -z "${d}" ]; then |
||||
QDEV="$(ps xa | grep $d | grep -v grep)" |
||||
if [ -z "${QDEV}" ]; then |
||||
kpartx -d /dev/$d && echo "Unconnected device map removed: /dev/$d" |
||||
fi |
||||
fi |
||||
done |
||||
fi |
||||
} |
||||
|
||||
# As long as there is at least one more argument, keep looping |
||||
while [[ $# -gt 0 ]]; do |
||||
key="$1" |
||||
case "$key" in |
||||
-h|--help) |
||||
usage |
||||
exit |
||||
;; |
||||
-c|--cleanup) |
||||
nbd_cleanup |
||||
;; |
||||
-m|--mount) |
||||
MOUNT=1 |
||||
;; |
||||
-u|--umount) |
||||
UMOUNT=1 |
||||
;; |
||||
-i|--image-name) |
||||
shift |
||||
IMAGE="$1" |
||||
;; |
||||
-p|--mount-point) |
||||
shift |
||||
MOUNTPOINT="$1" |
||||
;; |
||||
*) |
||||
echo "Unknown option '$key'" |
||||
usage |
||||
exit |
||||
;; |
||||
esac |
||||
# Shift after checking all the cases to get the next option |
||||
shift |
||||
done |
||||
|
||||
if [ "${MOUNT}" = "1" ] && [ "${UMOUNT}" = "1" ]; then |
||||
usage |
||||
echo "Concurrent mount options not possible." |
||||
exit |
||||
fi |
||||
|
||||
if [ "${MOUNT}" = "1" ] && ([ -z "${IMAGE}" ] || [ -z "${MOUNTPOINT}" ]); then |
||||
usage |
||||
echo "Can not mount image. Image path and/or mount point missing." |
||||
exit |
||||
fi |
||||
|
||||
if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then |
||||
usage |
||||
echo "Can not umount. Mount point parameter missing." |
||||
exit |
||||
fi |
||||
|
||||
export NBD_DEV="${NBD_DEV:-/dev/nbd1}" |
||||
export MAP_BOOT_DEV=/dev/mapper/nbd1p1 |
||||
export MAP_ROOT_DEV=/dev/mapper/nbd1p2 |
||||
source scripts/qcow2_handling |
||||
|
||||
if [ "${MOUNT}" = "1" ]; then |
||||
mount_qimage "${IMAGE}" "${MOUNTPOINT}" |
||||
elif [ "${UMOUNT}" = "1" ]; then |
||||
umount_qimage "${MOUNTPOINT}" |
||||
fi |
@ -1,256 +0,0 @@
|
||||
#!/bin/bash |
||||
|
||||
# QCOW2 Routines |
||||
|
||||
export CURRENT_IMAGE |
||||
export CURRENT_MOUNTPOINT |
||||
|
||||
export NBD_DEV |
||||
export MAP_BOOT_DEV |
||||
export MAP_ROOT_DEV |
||||
|
||||
# set in build.sh |
||||
# should be fairly enough for the beginning |
||||
# overwrite here by uncommenting following lines |
||||
# BASE_QCOW2_SIZE=12G |
||||
|
||||
# find and initialize free block device nodes |
||||
init_nbd() { |
||||
modprobe nbd max_part=16 |
||||
if [ -z "${NBD_DEV}" ]; then |
||||
for x in /sys/class/block/nbd* ; do |
||||
S=`cat $x/size` |
||||
if [ "$S" == "0" ] ; then |
||||
NBD_DEV=/dev/$(basename $x) |
||||
MAP_BOOT_DEV=/dev/mapper/$(basename $x)p1 |
||||
MAP_ROOT_DEV=/dev/mapper/$(basename $x)p2 |
||||
break |
||||
fi |
||||
done |
||||
fi |
||||
} |
||||
export -f init_nbd |
||||
|
||||
# connect image to block device |
||||
connect_blkdev() { |
||||
init_nbd |
||||
qemu-nbd --discard=unmap -c $NBD_DEV "$1" |
||||
sync |
||||
kpartx -as $NBD_DEV |
||||
sync |
||||
CURRENT_IMAGE="$1" |
||||
} |
||||
export -f connect_blkdev |
||||
|
||||
# disconnect image from block device |
||||
disconnect_blkdev() { |
||||
kpartx -d $NBD_DEV |
||||
qemu-nbd -d $NBD_DEV |
||||
NBD_DEV= |
||||
MAP_BOOT_DEV= |
||||
MAP_ROOT_DEV= |
||||
CURRENT_IMAGE= |
||||
} |
||||
export -f disconnect_blkdev |
||||
|
||||
# mount qcow2 image: mount_image <image file> <mountpoint> |
||||
mount_qimage() { |
||||
connect_blkdev "$1" |
||||
mount -v -t ext4 $MAP_ROOT_DEV "$2" |
||||
mkdir -p "${ROOTFS_DIR}/boot" |
||||
mount -v -t vfat $MAP_BOOT_DEV "$2/boot" |
||||
CURRENT_MOUNTPOINT="$2" |
||||
} |
||||
export -f mount_qimage |
||||
|
||||
# umount qcow2 image: umount_image <current mountpoint> |
||||
umount_qimage() { |
||||
sync |
||||
#umount "$1/boot" |
||||
while mount | grep -q "$1"; do |
||||
local LOCS |
||||
LOCS=$(mount | grep "$1" | cut -f 3 -d ' ' | sort -r) |
||||
for loc in $LOCS; do |
||||
echo "$loc" |
||||
while mountpoint -q "$loc" && ! umount "$loc"; do |
||||
sleep 0.1 |
||||
done |
||||
done |
||||
done |
||||
CURRENT_MOUNTPOINT= |
||||
disconnect_blkdev |
||||
} |
||||
export -f umount_qimage |
||||
|
||||
# create base image / backing image / mount image |
||||
load_qimage() { |
||||
if [ -z "${CURRENT_MOUNTPOINT}" ]; then |
||||
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 [ ! -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then |
||||
pushd ${WORK_DIR} > /dev/null |
||||
init_nbd |
||||
if [ -z "${PREV_STAGE}" ]; then |
||||
echo "Creating base image: image-${STAGE}.qcow2" |
||||
# -o preallocation=falloc |
||||
qemu-img create -f qcow2 image-${STAGE}.qcow2 $BASE_QCOW2_SIZE |
||||
sync |
||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2 |
||||
sync |
||||
sfdisk $NBD_DEV << EOF |
||||
4MiB,250MiB,c,* |
||||
254MiB,,83; |
||||
EOF |
||||
sync |
||||
kpartx -as $NBD_DEV |
||||
mkdosfs -n boot -F 32 -s 4 -v $MAP_BOOT_DEV |
||||
mkfs.ext4 -L rootfs -O "^huge_file,^64bit" $MAP_ROOT_DEV |
||||
sync |
||||
else |
||||
if [ ! -f "${WORK_DIR}/image-${PREV_STAGE}.qcow2" ]; then |
||||
exit 1; |
||||
fi |
||||
echo "Creating backing image: image-${STAGE}.qcow2 <- ${WORK_DIR}/image-${PREV_STAGE}.qcow2" |
||||
qemu-img create -f qcow2 \ |
||||
-o backing_file=${WORK_DIR}/image-${PREV_STAGE}.qcow2 \ |
||||
${WORK_DIR}/image-${STAGE}.qcow2 |
||||
sync |
||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2 |
||||
sync |
||||
kpartx -as $NBD_DEV |
||||
fi |
||||
|
||||
mount -v -t ext4 $MAP_ROOT_DEV "${ROOTFS_DIR}" |
||||
mkdir -p "${ROOTFS_DIR}/boot" |
||||
mount -v -t vfat $MAP_BOOT_DEV "${ROOTFS_DIR}/boot" |
||||
CURRENT_IMAGE=${WORK_DIR}/image-${STAGE}.qcow2 |
||||
CURRENT_MOUNTPOINT=${ROOTFS_DIR} |
||||
popd > /dev/null |
||||
else |
||||
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 |
||||
|
||||
# umount current image and refresh mount point env var |
||||
unload_qimage() { |
||||
if [ ! -z "${CURRENT_MOUNTPOINT}" ]; then |
||||
fstrim -v "${CURRENT_MOUNTPOINT}" || true |
||||
umount_qimage "${CURRENT_MOUNTPOINT}" |
||||
fi |
||||
} |
||||
export -f unload_qimage |
||||
|
||||
# based on: https://github.com/SirLagz/RaspberryPi-ImgAutoSizer |
||||
# helper function for make_bootable_image, do not call directly |
||||
function resize_qcow2() { |
||||
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=$((1*1024*1024)) |
||||
PARTED_OUT=`parted -s -m "$NBD_DEV" unit B print` |
||||
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 |
||||
|
||||
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=${BLOCK_SIZE// /} |
||||
|
||||
let DATA_SIZE=$DATA_SIZE+$ROOT_MARGIN/$BLOCK_SIZE |
||||
resize2fs -p $MAP_ROOT_DEV $DATA_SIZE |
||||
sleep 1 |
||||
|
||||
let PART_NEW_SIZE=$DATA_SIZE*$BLOCK_SIZE |
||||
let PART_NEW_END=$PART_START+$PART_NEW_SIZE |
||||
ACT1=`parted -s "$NBD_DEV" rm 2` |
||||
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) } '` |
||||
} |
||||
export -f resize_qcow2 |
||||
|
||||
# create raw img from qcow2: make_bootable_image <in.qcow2> <out.img> |
||||
function make_bootable_image() { |
||||
|
||||
EXPORT_QCOW2="$1" |
||||
EXPORT_IMAGE="$2" |
||||
|
||||
echo "Connect block device to source qcow2" |
||||
connect_blkdev "${EXPORT_QCOW2}" |
||||
|
||||
echo "Resize fs and partition" |
||||
CALL_FROM_MBI=1 |
||||
resize_qcow2 |
||||
sync |
||||
CALL_FROM_MBI= |
||||
|
||||
echo "Disconnect block device" |
||||
disconnect_blkdev |
||||
|
||||
if [ -z "$NEW_IMG_SIZE" ]; then |
||||
echo "NEW_IMG_SIZE could not be calculated, cannot process image. Exit." |
||||
exit 1 |
||||
fi |
||||
|
||||
echo "Shrinking qcow2 image" |
||||
qemu-img resize --shrink "${EXPORT_QCOW2}" $NEW_IMG_SIZE |
||||
sync |
||||
|
||||
echo "Convert qcow2 to raw image" |
||||
qemu-img convert -f qcow2 -O raw "${EXPORT_QCOW2}" "${EXPORT_IMAGE}" |
||||
sync |
||||
|
||||
echo "Get PARTUUIDs from image" |
||||
IMGID="$(blkid -o value -s PTUUID "${EXPORT_IMAGE}")" |
||||
|
||||
BOOT_PARTUUID="${IMGID}-01" |
||||
echo "Boot: $BOOT_PARTUUID" |
||||
ROOT_PARTUUID="${IMGID}-02" |
||||
echo "Root: $ROOT_PARTUUID" |
||||
|
||||
echo "Mount image" |
||||
MOUNTROOT=${WORK_DIR}/tmpimage |
||||
mkdir -p $MOUNTROOT |
||||
|
||||
MOUNTPT=$MOUNTROOT |
||||
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 |
||||
|
||||
MOUNTPT=$MOUNTROOT/boot |
||||
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 |
||||
|
||||
if [ ! -d "${MOUNTROOT}/root" ]; then |
||||
echo "Image damaged or not mounted. Exit." |
||||
exit 1 |
||||
fi |
||||
|
||||
echo "Setup PARTUUIDs" |
||||
if [ ! -z "$BOOT_PARTUUID" ] && [ ! -z "$ROOT_PARTUUID" ]; then |
||||
echo "Set UUIDs to make it bootable" |
||||
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}/boot/cmdline.txt" |
||||
fi |
||||
|
||||
echo "Umount image" |
||||
sync |
||||
umount "${MOUNTROOT}/boot" || exit 1 |
||||
umount "${MOUNTROOT}" || exit 1 |
||||
|
||||
echo "Remove qcow2 export image" |
||||
rm -f "${EXPORT_QCOW2}" |
||||
} |
||||
export -f make_bootable_image |
Loading…
Reference in new issue