Add support for template with partition table on root.img

When TEMPLATE_ROOT_WITH_PARTITIONS is set, include (GPT) partition table
and install actual OS on the first one. This allows installing grub, or
other bootloader.

Fixes QubesOS/qubes-issues#2577
This commit is contained in:
Marek Marczykowski-Górecki 2017-06-14 10:53:22 +02:00
parent a97fe13c1b
commit 9b2078d346
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
3 changed files with 49 additions and 16 deletions

View File

@ -1,30 +1,24 @@
#!/bin/sh
export IMG=$1
export INSTALLDIR=$1
. ./builder_setup
set -e
if ! [ $# -eq 1 ]; then
echo "usage $0 <img_file_name>"
echo "usage $0 <mount_point>"
exit 1
fi
if ! [ -f $IMG ]; then
echo $IMG does not exist
if ! [ -d $INSTALLDIR ]; then
echo $INSTALLDIR does not exist
exit 1
fi
ls -als $IMG
mount -o loop $IMG mnt || exit 1
export INSTALLDIR=`pwd`/mnt/
echo "--> Cleaning up image file..."
$SCRIPTSDIR/09_cleanup.sh
echo "--> Compacting image file..."
fstrim mnt/
umount mnt
ls -als $IMG
fstrim -v "$INSTALLDIR"

View File

@ -45,15 +45,41 @@ echo "-> Preparing instalation of ${DIST} template..."
# ------------------------------------------------------------------------------
if [ -f "${IMG}" ]; then
echo "-> Image file already exists, assuming *update*..."
if [ "0$TEMPLATE_ROOT_WITH_PARTITIONS" -eq 1 ]; then
IMG_LOOP=$(losetup -P -f --show "$IMG")
IMG_DEV=${IMG_LOOP}p1
else
IMG_LOOP=$(losetup -f --show "$IMG")
IMG_DEV=${IMG_LOOP}
fi
else
echo "-> Initializing empty image..."
truncate -s 10G "${IMG}" || exit 1
if [ "0$TEMPLATE_ROOT_WITH_PARTITIONS" -eq 1 ]; then
echo "-> Creating partition table"
# have static UUIDs to make partition table reproducible
sfdisk "$IMG" <<EOF || exit 1
label: gpt
label-id: f4796a2a-e377-45bd-b539-d6d49e569055
size=10000MiB, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=693244e6-3e07-47bf-ad79-acade4293fe7, name="Root filesystem"
size=200MiB, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=fa4d6529-56da-47c7-ae88-e2dfecb72621, name="EFI System"
size=2MiB, type=21686148-6449-6E6F-744E-656564454649, uuid=1e6c9db4-1e91-46c4-846a-2030dcb13b8c, name="BIOS boot partition"
EOF
IMG_LOOP=$(losetup -P -f --show "$IMG")
IMG_DEV=${IMG_LOOP}p1
else
IMG_LOOP=$(losetup -f --show "$IMG")
IMG_DEV=${IMG_LOOP}
fi
echo "-> Creating filesystem..."
mkfs.ext4 -q -F "${IMG}" || exit 1
mkfs.ext4 -q -F "${IMG_DEV}" || exit 1
fi
mount -o loop "${IMG}" "${INSTALLDIR}" || exit 1
mount "${IMG_DEV}" "${INSTALLDIR}" || exit 1
trap "umount_kill $(readlink -m ${INSTALLDIR})" EXIT
"${SCRIPTSDIR}/01_install_core.sh"
@ -70,5 +96,6 @@ trap - EXIT
echo "-> Unmounting prepared_image..."
umount_kill "$(readlink -m ${INSTALLDIR})" || true
losetup -d ${IMG_LOOP}
exit ${RETCODE}

View File

@ -50,6 +50,7 @@ function cleanup() {
trap - ERR
trap
umount_kill "$PWD/mnt" || true
losetup -d ${IMG_LOOP}
exit $errval
}
trap cleanup ERR
@ -65,7 +66,14 @@ cp "$CLEANIMG" "$IMG" || exit 1
echo "--> Mounting $IMG"
mkdir -p mnt
mount -o loop "$IMG" mnt || exit 1
if [ "0$TEMPLATE_ROOT_WITH_PARTITIONS" -eq 1 ]; then
IMG_LOOP=$(losetup -P -f --show "$IMG")
IMG_DEV=${IMG_LOOP}p1
else
IMG_LOOP=$(losetup -f --show "$IMG")
IMG_DEV=${IMG_LOOP}
fi
mount "$IMG_DEV" mnt || exit 1
export INSTALLDIR=mnt
# ------------------------------------------------------------------------------
@ -100,14 +108,18 @@ echo "--> Linking /usr/local to /rw/usrlocal..."
mv mnt/usr/local mnt/usr/local.orig
ln -sf /rw/usrlocal mnt/usr/local
echo "Reducing image size (calling cleanup_image)..."
ls -als $IMG
./cleanup_image "$INSTALLDIR"
ls -als $IMG
# ------------------------------------------------------------------------------
# Finsh - unmount image
# ------------------------------------------------------------------------------
echo "--> Unmounting $IMG"
umount_kill "$PWD/mnt" || true
losetup -d ${IMG_LOOP}
echo "Qubeized image stored at: $IMG"
echo "Reducing image size (calling cleanup_image)..."
./cleanup_image "$IMG"
chown -R --reference=. qubeized_images/$NAME