#!/usr/bin/sudo /bin/sh
set -x
if [ -z "$BUILD_HOSTNAME" ]; then
. ./rpi2-settings.sh
fi
export DATE="$(date +%Y-%m-%d)"
dd if=/dev/zero of="${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img" bs=1024 count=7340032
cat rpi2-create-fdisk-keys_with_swap | fdisk "${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img"
VFAT_LOOP="$(losetup -o 1M --sizelimit 64M -f --show ${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img)"
mkfs.vfat "${VFAT_LOOP}"
EXT4_LOOP="$(losetup -o 65M --sizelimit 8000000000 -f --show ${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img)"
mkfs.ext4 "${EXT4_LOOP}"
MOUNTDIR="${BUILDDIR}/mount"
mkdir -p "${MOUNTDIR}"
mount "${EXT4_LOOP}" "${MOUNTDIR}"
mkdir -p "${MOUNTDIR}/boot/firmware"
mount "${VFAT_LOOP}" "${MOUNTDIR}/boot/firmware"
rsync -a "$R/" "${MOUNTDIR}/"
df -h "${EXT4_LOOP}"
umount "${MOUNTDIR}/boot/firmware"
umount "${MOUNTDIR}"
losetup -d "${EXT4_LOOP}"
losetup -d "${VFAT_LOOP}"
if which bmaptool; then
bmaptool create -o "${BASEDIR}/${DATE}-ubuntu-${RELEASE}.bmap" "${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img"
fi
ls -lh "${BASEDIR}"
set +x
echo ""
echo "Done. Image created:"
echo "p" | fdisk -u "${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img"
echo ""
echo "Now you can use:"
echo ""
echo "sudo bmaptool copy --bmap \"${BASEDIR}/${DATE}-ubuntu-${RELEASE}.bmap\" \"${BASEDIR}/${DATE}-ubuntu-${RELEASE}.img\" /dev/mmcblk0"
echo ""
echo "... to create the card. If the card is greater then 8GB, then you shold use"
echo ""
echo "cat rpi2-resize-ext4-fdisk-keys_with_swap | sudo fdisk /dev/mmcblk0"
echo ""
echo "and"
echo "resize2fs /dev/mmcblk0p3"
echo "fsck.ext4 -p -y /dev/mmcblk0p3"
echo ""
echo "to resize the linux-partition."
echo ""
# Done!