2019-11-02 16:34:54 +00:00
|
|
|
#!/bin/sh
|
2019-06-04 12:16:30 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
die() {
|
2019-11-02 16:34:54 +00:00
|
|
|
echo "die: $*"
|
2019-06-04 12:16:30 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
if [ "$(id -u)" != 0 ]; then
|
2019-06-04 12:16:30 +00:00
|
|
|
die "this script needs to run as root"
|
|
|
|
fi
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
grub=$(command -v grub-install 2>/dev/null) || true
|
|
|
|
if [ -z "$grub" ]; then
|
|
|
|
grub=$(command -v grub2-install 2>/dev/null) || true
|
2019-06-04 12:16:30 +00:00
|
|
|
fi
|
|
|
|
if [ -z "$grub" ]; then
|
|
|
|
echo "can't find a grub-install or grub2-install binary, oh no"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "using grub-install at ${grub}"
|
|
|
|
|
|
|
|
echo "setting up disk image..."
|
2019-11-26 11:54:15 +00:00
|
|
|
dd if=/dev/zero of=_disk_image bs=1M count="${DISK_SIZE:-600}" status=none || die "couldn't create disk image"
|
2020-01-15 20:45:39 +00:00
|
|
|
chown "$SUDO_UID":"$SUDO_GID" _disk_image || die "couldn't adjust permissions on disk image"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "creating loopback device... "
|
2019-06-04 12:16:30 +00:00
|
|
|
dev=$(losetup --find --partscan --show _disk_image)
|
2019-11-02 16:34:54 +00:00
|
|
|
if [ -z "$dev" ]; then
|
2019-06-04 12:16:30 +00:00
|
|
|
die "couldn't mount loopback device"
|
|
|
|
fi
|
|
|
|
echo "loopback device is at ${dev}"
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
if [ -d mnt ]; then
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "unmounting filesystem... "
|
2019-06-04 12:16:30 +00:00
|
|
|
umount mnt || ( sleep 1 && sync && umount mnt )
|
|
|
|
rm -rf mnt
|
|
|
|
echo "done"
|
|
|
|
fi
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
if [ -e "${dev}" ]; then
|
|
|
|
printf "cleaning up loopback device... "
|
|
|
|
losetup -d "${dev}"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "creating partition table... "
|
|
|
|
parted -s "${dev}" mklabel msdos mkpart primary ext2 32k 100% -a minimal set 1 boot on || die "couldn't partition disk"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "destroying old filesystem... "
|
|
|
|
dd if=/dev/zero of="${dev}"p1 bs=1M count=1 status=none || die "couldn't destroy old filesystem"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "creating new filesystem... "
|
|
|
|
mke2fs -q -I 128 "${dev}"p1 || die "couldn't create filesystem"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "mounting filesystem... "
|
2019-06-04 12:16:30 +00:00
|
|
|
mkdir -p mnt
|
2019-11-02 16:34:54 +00:00
|
|
|
mount "${dev}"p1 mnt/ || die "couldn't mount filesystem"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
|
|
|
./build-root-filesystem.sh
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "creating /boot... "
|
2019-06-04 12:16:30 +00:00
|
|
|
mkdir -p mnt/boot
|
|
|
|
echo "done"
|
|
|
|
|
|
|
|
echo "installing grub using $grub..."
|
2019-11-03 12:11:43 +00:00
|
|
|
$grub --boot-directory=mnt/boot --target=i386-pc --modules="ext2 part_msdos" "${dev}"
|
2019-06-04 12:16:30 +00:00
|
|
|
|
|
|
|
if [ -d mnt/boot/grub2 ]; then
|
|
|
|
cp grub.cfg mnt/boot/grub2/grub.cfg
|
|
|
|
else
|
|
|
|
cp grub.cfg mnt/boot/grub/grub.cfg
|
|
|
|
fi
|
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "installing kernel in /boot... "
|
2019-06-04 12:16:30 +00:00
|
|
|
cp kernel mnt/boot
|
|
|
|
echo "done"
|