|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P) |
| 6 | + |
| 7 | +. "${script_path}/shell_include.sh" |
| 8 | + |
| 9 | +if [ "$(id -u)" != 0 ]; then |
| 10 | + set +e |
| 11 | + ${SUDO} -- "${SHELL}" -c "\"$0\" $* || exit 42" |
| 12 | + case $? in |
| 13 | + 1) |
| 14 | + die "this script needs to run as root" |
| 15 | + ;; |
| 16 | + 42) |
| 17 | + exit 1 |
| 18 | + ;; |
| 19 | + *) |
| 20 | + exit 0 |
| 21 | + ;; |
| 22 | + esac |
| 23 | +else |
| 24 | + : "${SUDO_UID:=0}" "${SUDO_GID:=0}" |
| 25 | +fi |
| 26 | + |
| 27 | +grub=$(command -v grub-install 2>/dev/null) || true |
| 28 | +if [ -z "$grub" ]; then |
| 29 | + grub=$(command -v grub2-install 2>/dev/null) || true |
| 30 | +fi |
| 31 | +if [ -z "$grub" ]; then |
| 32 | + echo "can't find a grub-install or grub2-install binary" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | +echo "using grub-install at ${grub}" |
| 36 | + |
| 37 | +DISK_SIZE=$(($(disk_usage "$SERENITY_SOURCE_DIR/Base") + $(disk_usage Root) + 512 + 300)) |
| 38 | + |
| 39 | +printf "setting up disk image... " |
| 40 | +dd if=/dev/zero of=grub_uefi_disk_image bs=1M count="${DISK_SIZE}" status=none || die "couldn't create disk image" |
| 41 | +chown "$SUDO_UID":"$SUDO_GID" grub_uefi_disk_image || die "couldn't adjust permissions on disk image" |
| 42 | +echo "done" |
| 43 | + |
| 44 | +printf "creating loopback device... " |
| 45 | +dev=$(losetup --find --partscan --show grub_uefi_disk_image) |
| 46 | +if [ -z "$dev" ]; then |
| 47 | + die "couldn't mount loopback device" |
| 48 | +fi |
| 49 | +echo "loopback device is at ${dev}" |
| 50 | + |
| 51 | +cleanup() { |
| 52 | + if [ -d esp ]; then |
| 53 | + printf "unmounting EFI system partition... " |
| 54 | + umount esp || ( sleep 1 && sync && umount esp ) |
| 55 | + rmdir esp |
| 56 | + echo "done" |
| 57 | + fi |
| 58 | + |
| 59 | + if [ -d mnt ]; then |
| 60 | + printf "unmounting root filesystem... " |
| 61 | + umount mnt || ( sleep 1 && sync && umount mnt ) |
| 62 | + rmdir mnt |
| 63 | + echo "done" |
| 64 | + fi |
| 65 | + |
| 66 | + if [ -e "${dev}" ]; then |
| 67 | + printf "cleaning up loopback device... " |
| 68 | + losetup -d "${dev}" |
| 69 | + echo "done" |
| 70 | + fi |
| 71 | +} |
| 72 | +trap cleanup EXIT |
| 73 | + |
| 74 | +printf "creating partition table... " |
| 75 | +parted -s "${dev}" mklabel gpt mkpart EFI fat32 0% 512MB mkpart SerenityOS ext2 512MB 100% set 1 esp on || die "couldn't partition disk" |
| 76 | +echo "done" |
| 77 | + |
| 78 | +printf "creating new filesystems... " |
| 79 | +mkfs.vfat -F 32 -n EFI "${dev}p1" >/dev/null || die "couldn't create EFI system partition filesystem" |
| 80 | +mke2fs -q -L SerenityOS "${dev}p2" || die "couldn't create root filesystem" |
| 81 | +echo "done" |
| 82 | + |
| 83 | +printf "mounting filesystems... " |
| 84 | +mkdir -p esp |
| 85 | +mount "${dev}p1" esp || die "couldn't mount EFI system partition" |
| 86 | +mkdir -p mnt |
| 87 | +mount "${dev}p2" mnt || die "couldn't mount root filesystem" |
| 88 | +echo "done" |
| 89 | + |
| 90 | +"$script_path/build-root-filesystem.sh" |
| 91 | + |
| 92 | +echo "installing grub using $grub..." |
| 93 | +$grub --target=x86_64-efi --efi-directory=esp/ --boot-directory=esp/ --removable |
| 94 | +echo "done" |
| 95 | + |
| 96 | +esp_uuid=$(blkid -o export "${dev}p1" | grep ^UUID | cut -d= -f2) |
| 97 | +root_uuid=$(blkid -o export "${dev}p2" | grep ^PARTUUID | cut -d= -f2) |
| 98 | + |
| 99 | +cat <<EOF >esp/grub/grub.cfg |
| 100 | +timeout=1 |
| 101 | +
|
| 102 | +search --no-floppy --fs-uuid --set=root ${esp_uuid} |
| 103 | +
|
| 104 | +menuentry 'SerenityOS (normal)' { |
| 105 | + chainloader /Kernel.efi root=PARTUUID:${root_uuid} |
| 106 | +} |
| 107 | +
|
| 108 | +menuentry 'SerenityOS (text mode)' { |
| 109 | + chainloader /Kernel.efi graphics_subsystem_mode=off root=PARTUUID:${root_uuid} |
| 110 | +} |
| 111 | +
|
| 112 | +menuentry 'SerenityOS (No ACPI)' { |
| 113 | + chainloader /Kernel.efi root=PARTUUID:${root_uuid} acpi=off |
| 114 | +} |
| 115 | +
|
| 116 | +menuentry 'SerenityOS (with serial debug)' { |
| 117 | + chainloader /Kernel.efi serial_debug root=PARTUUID:${root_uuid} |
| 118 | +} |
| 119 | +EOF |
| 120 | + |
| 121 | +# FIXME: Mount the EFI system partition on /boot (both here and in serenity), so we don't need to move the Kernel.efi file to the ESP. |
| 122 | +mv mnt/boot/Kernel.efi esp/ |
0 commit comments