windvn/path/make-image

74 lines
1.4 KiB
Plaintext
Raw Normal View History

2022-05-10 10:50:10 +00:00
#!/usr/bin/env bash
2022-05-10 20:50:49 +00:00
set -e
2022-05-20 16:02:59 +00:00
if
export DISK_IMAGE="$2"
export ROOT_MOUNT="$OPUS_OUTPUT/mount"
# Get the approximate size required for the image
export DISK_SIZE=$(du -sb "$1" | cut -f1)
# Add 256MB for SWAP, and 700MB for overhead
export DISK_SIZE=$(( $DISK_SIZE + ( 256 * 1000000 ) + ( 700 * 1000000 ) ))
# Create and mount disk
fallocate -l $DISK_SIZE "$DISK_IMAGE"
export DISK_LOOP="$(losetup --show -f -P "$DISK_IMAGE")"
# Partition disk
(
echo n
echo e
echo
echo
echo +256M
echo t
echo swap
echo n
echo p
echo
echo
echo
echo t
echo
echo linux
echo w
echo
echo
) | fdisk "$DISK_LOOP"
# Format partions
mkswap "${DISK_LOOP}p1"
mkfs.ext4 "${DISK_LOOP}p2"
# Mount root
mkdir "$ROOT_MOUNT"
mount "${DISK_LOOP}p2" "$ROOT_MOUNT"
# Add files
dupe "$1/." "$ROOT_MOUNT"
# Make fstab
echo "UUID=$(blkid -o value -s UUID "${DISK_LOOP}p1") none swap sw 0 0" >"$ROOT_MOUNT/etc/fstab"
echo "UUID=$(blkid -o value -s UUID "${DISK_LOOP}p2") / ext4 errors=remount-ro 0 1" >>"$ROOT_MOUNT/etc/fstab"
# Set up GRUB and misc
fchroot "$ROOT_MOUNT" "/setupdisk"
# Clear logs
find "$ROOT_MOUNT/var/log" -type f -delete
# Unmount root
umount -l "$ROOT_MOUNT"
rmdir "$ROOT_MOUNT"
losetup -d "$DISK_LOOP"
then
set +e
else
set +e
echo
echo Script execution ended due to error. Cleaning up...
umount -l "$ROOT_MOUNT"
rmdir "$ROOT_MOUNT"
losetup -d "$DISK_LOOP"
echo Script execution ended due to error.
exit 1
fi