Skip to content

Commit 719caa8

Browse files
committed
Meta: Add a script for building x86-64 UEFI GRUB disk images
This script is mostly based on build-image-grub.sh. Unlike that script, it uses the EFIPrekernel instead of the multiboot prekernel.
1 parent 9ba55f9 commit 719caa8

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ if("${SERENITY_ARCH}" STREQUAL "x86_64")
8383
BYPRODUCTS ${CMAKE_BINARY_DIR}/grub_disk_image
8484
USES_TERMINAL
8585
)
86+
add_custom_target(grub-uefi-image
87+
COMMAND "${CMAKE_COMMAND}" -E env "SERENITY_SOURCE_DIR=${SerenityOS_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" "SERENITY_TOOLCHAIN=${CMAKE_CXX_COMPILER_ID}" "LLVM_VERSION=${CMAKE_CXX_COMPILER_VERSION}" "${SerenityOS_SOURCE_DIR}/Meta/build-image-grub-uefi.sh"
88+
BYPRODUCTS ${CMAKE_BINARY_DIR}/grub_uefi_disk_image
89+
USES_TERMINAL
90+
)
8691
add_custom_target(limine-image
8792
COMMAND "${CMAKE_COMMAND}" -E env "SERENITY_SOURCE_DIR=${SerenityOS_SOURCE_DIR}" "SERENITY_ARCH=${SERENITY_ARCH}" "SERENITY_TOOLCHAIN=${CMAKE_CXX_COMPILER_ID}" "LLVM_VERSION=${CMAKE_CXX_COMPILER_VERSION}" "${SerenityOS_SOURCE_DIR}/Meta/build-image-limine.sh"
8893
BYPRODUCTS ${CMAKE_BINARY_DIR}/limine_disk_image

Documentation/AdvancedBuildInstructions.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ following build targets cannot be accessed through the script and have to be use
4141
directory to `Build/<architecture>` and then running `ninja <target>`:
4242

4343
- `ninja limine-image`: Builds an x86-64 disk image (`limine_disk_image`) with Limine
44-
- `ninja grub-image`: Builds an x86-64 disk image (`grub_disk_image`) with GRUB
44+
- `ninja grub-image`: Builds an x86-64 disk image (`grub_disk_image`) with GRUB for legacy BIOS systems
45+
- `ninja grub-uefi-image`: Builds an x86-64 disk image (`grub_uefi_disk_image`) with GRUB for UEFI systems
4546
- `ninja extlinux-image`: Builds an x86-64 disk image (`extlinux_disk_image`) with extlinux
4647
- `ninja check-style`: Runs the same linters the CI does to verify project style on changed files
4748
- `ninja install-ports`: Copies the entire ports tree into the installed rootfs for building ports in Serenity

Meta/build-image-grub-uefi.sh

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)