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
|
2020-06-20 00:55:39 +00:00
|
|
|
exec sudo -E -- "$0" "$@" || die "this script needs to run as root"
|
2020-06-20 03:20:50 +00:00
|
|
|
else
|
|
|
|
: "${SUDO_UID:=0}" "${SUDO_GID:=0}"
|
2019-06-04 12:16:30 +00:00
|
|
|
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}"
|
|
|
|
|
2020-07-28 16:20:06 +00:00
|
|
|
disk_usage() {
|
2020-07-28 16:47:04 +00:00
|
|
|
du -sm "$1" | cut -f1
|
2020-07-28 16:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DISK_SIZE=$(($(disk_usage "$SERENITY_ROOT/Base") + $(disk_usage Root) + 300))
|
|
|
|
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "setting up disk image..."
|
2020-12-26 12:16:45 +00:00
|
|
|
if [ "$1" = "ebr" ]; then
|
|
|
|
DISK_SIZE=
|
|
|
|
fi
|
2020-05-28 08:08:38 +00:00
|
|
|
dd if=/dev/zero of=grub_disk_image bs=1M count="${DISK_SIZE:-800}" status=none || die "couldn't create disk image"
|
|
|
|
chown "$SUDO_UID":"$SUDO_GID" grub_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... "
|
2020-05-28 08:08:38 +00:00
|
|
|
dev=$(losetup --find --partscan --show grub_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 )
|
2020-05-06 15:40:06 +00:00
|
|
|
rmdir mnt
|
2019-06-04 12:16:30 +00:00
|
|
|
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... "
|
2020-02-01 23:12:49 +00:00
|
|
|
if [ "$1" = "mbr" ]; then
|
|
|
|
parted -s "${dev}" mklabel msdos mkpart primary ext2 32k 100% -a minimal set 1 boot on || die "couldn't partition disk"
|
|
|
|
partition_number="p1"
|
|
|
|
partition_scheme="mbr"
|
|
|
|
elif [ "$1" = "gpt" ]; then
|
2020-12-25 23:41:37 +00:00
|
|
|
parted -s "${dev}" mklabel gpt mkpart BIOSBOOT ext3 1MiB 8MiB mkpart OS ext2 8MiB 290MiB set 1 bios_grub || die "couldn't partition disk"
|
2020-02-01 23:12:49 +00:00
|
|
|
partition_number="p2"
|
|
|
|
partition_scheme="gpt"
|
|
|
|
elif [ "$1" = "ebr" ]; then
|
|
|
|
parted -s "${dev}" mklabel msdos mkpart primary 32k 200MiB mkpart primary 200MiB 201MiB mkpart primary 201MiB 202MiB mkpart extended 250MiB 739MiB mkpart logical 372MiB 739MiB -a minimal set 1 boot on || die "couldn't partition disk"
|
|
|
|
partition_number="p5"
|
|
|
|
partition_scheme="ebr"
|
|
|
|
else
|
|
|
|
parted -s "${dev}" mklabel msdos mkpart primary ext2 32k 100% -a minimal set 1 boot on || die "couldn't partition disk"
|
|
|
|
partition_number="p1"
|
|
|
|
partition_scheme="mbr"
|
|
|
|
fi
|
|
|
|
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 16:34:54 +00:00
|
|
|
printf "destroying old filesystem... "
|
2020-02-01 23:12:49 +00:00
|
|
|
dd if=/dev/zero of="${dev}${partition_number}" 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... "
|
2020-02-01 23:12:49 +00:00
|
|
|
mke2fs -q -I 128 "${dev}${partition_number}" || 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
|
2020-02-01 23:12:49 +00:00
|
|
|
mount "${dev}${partition_number}" mnt/ || die "couldn't mount filesystem"
|
2019-06-04 12:16:30 +00:00
|
|
|
echo "done"
|
|
|
|
|
2020-05-06 15:40:06 +00:00
|
|
|
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
|
|
|
|
"$script_path/build-root-filesystem.sh"
|
2019-06-04 12:16:30 +00:00
|
|
|
|
|
|
|
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
|
2020-05-26 21:52:16 +00:00
|
|
|
cp "$SERENITY_ROOT"/Meta/grub-"${partition_scheme}".cfg mnt/boot/grub2/grub.cfg
|
2019-06-04 12:16:30 +00:00
|
|
|
else
|
2020-05-26 21:52:16 +00:00
|
|
|
cp "$SERENITY_ROOT"/Meta/grub-"${partition_scheme}".cfg mnt/boot/grub/grub.cfg
|
2019-06-04 12:16:30 +00:00
|
|
|
fi
|
|
|
|
echo "done"
|