CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/extensions/grub.sh
Views: 3957
1
# This runs *after* user_config. Don't change anything not coming from other variables or meant to be configured by the user.
2
function extension_prepare_config__prepare_flash_kernel() {
3
# Extension configuration defaults.
4
export DISTRO_GENERIC_KERNEL=${DISTRO_GENERIC_KERNEL:-no} # if yes, does not build our own kernel, instead, uses generic one from distro
5
export UEFI_GRUB_TERMINAL="${UEFI_GRUB_TERMINAL:-serial console}" # 'serial' forces grub menu on serial console. empty to not include
6
export UEFI_GRUB_DISABLE_OS_PROBER="${UEFI_GRUB_DISABLE_OS_PROBER:-}" # 'true' will disable os-probing, useful for SD cards.
7
export UEFI_GRUB_DISTRO_NAME="${UEFI_GRUB_DISTRO_NAME:-Armbian}" # Will be used on grub menu display
8
export UEFI_GRUB_TIMEOUT=${UEFI_GRUB_TIMEOUT:-0} # Small timeout by default
9
export UEFI_ENABLE_BIOS_AMD64="${UEFI_ENABLE_BIOS_AMD64:-yes}" # Enable BIOS too if target is amd64
10
export UEFI_EXPORT_KERNEL_INITRD="${UEFI_EXPORT_KERNEL_INITRD:-no}" # Export kernel and initrd for direct kernel boot "kexec"
11
# User config overrides.
12
export BOOTCONFIG="none" # To try and convince lib/ to not build or install u-boot.
13
unset BOOTSOURCE # To try and convince lib/ to not build or install u-boot.
14
export IMAGE_PARTITION_TABLE="gpt" # GPT partition table is essential for many UEFI-like implementations, eg Apple+Intel stuff.
15
export UEFISIZE=256 # in MiB - grub EFI is tiny - but some EFI BIOSes ignore small too small EFI partitions
16
export BOOTSIZE=0 # No separate /boot when using UEFI.
17
export CLOUD_INIT_CONFIG_LOCATION="${CLOUD_INIT_CONFIG_LOCATION:-/boot/efi}" # use /boot/efi for cloud-init as default when using Grub.
18
export EXTRA_BSP_NAME="${EXTRA_BSP_NAME}-grub" # Unique bsp name.
19
export UEFI_GRUB_TARGET_BIOS="" # Target for BIOS GRUB install, set to i386-pc when UEFI_ENABLE_BIOS_AMD64=yes and target is amd64
20
local uefi_packages="efibootmgr efivar cloud-initramfs-growroot" # Use growroot, add some efi-related packages
21
uefi_packages="os-prober grub-efi-${ARCH}-bin ${uefi_packages}" # This works for Ubuntu and Debian, by sheer luck; common for EFI and BIOS
22
23
# BIOS-compatibility for amd64
24
if [[ "${ARCH}" == "amd64" ]]; then
25
export UEFI_GRUB_TARGET="x86_64-efi" # Default for x86_64
26
if [[ "${UEFI_ENABLE_BIOS_AMD64}" == "yes" ]]; then
27
export uefi_packages="${uefi_packages} grub-pc-bin grub-pc"
28
export UEFI_GRUB_TARGET_BIOS="i386-pc"
29
export BIOSSIZE=4 # 4 MiB BIOS partition
30
else
31
export uefi_packages="${uefi_packages} grub-efi-${ARCH}"
32
fi
33
fi
34
35
[[ "${ARCH}" == "arm64" ]] && export uefi_packages="${uefi_packages} grub-efi-${ARCH}"
36
[[ "${ARCH}" == "arm64" ]] && export UEFI_GRUB_TARGET="arm64-efi" # Default for arm64-efi
37
38
if [[ "${DISTRIBUTION}" == "Ubuntu" ]]; then
39
DISTRO_KERNEL_PACKAGES="linux-image-generic"
40
DISTRO_FIRMWARE_PACKAGES="linux-firmware"
41
elif [[ "${DISTRIBUTION}" == "Debian" ]]; then
42
DISTRO_KERNEL_PACKAGES="linux-image-${ARCH}"
43
DISTRO_FIRMWARE_PACKAGES="firmware-linux-free"
44
# Debian's prebuilt kernels dont support hvc0, hack.
45
if [[ "${SERIALCON}" == "hvc0" ]]; then
46
display_alert "Debian's kernels don't support hvc0, changing to ttyS0" "${DISTRIBUTION}" "wrn"
47
export SERIALCON="ttyS0"
48
fi
49
fi
50
51
if [[ "${DISTRO_GENERIC_KERNEL}" == "yes" ]]; then
52
export VER="generic"
53
unset KERNELSOURCE # This should make Armbian skip most stuff. At least, I hacked it to.
54
export INSTALL_ARMBIAN_FIRMWARE=no # Should skip build and install of Armbian-firmware.
55
else
56
export KERNELDIR="linux-uefi-${LINUXFAMILY}" # Avoid sharing a source tree with others, until we know it's safe.
57
# Don't install anything. Armbian handles everything.
58
DISTRO_KERNEL_PACKAGES=""
59
DISTRO_FIRMWARE_PACKAGES=""
60
fi
61
62
export PACKAGE_LIST_BOARD="${PACKAGE_LIST_BOARD} ${DISTRO_FIRMWARE_PACKAGES} ${DISTRO_KERNEL_PACKAGES} ${uefi_packages}"
63
64
display_alert "Activating" "GRUB with SERIALCON=${SERIALCON}; timeout ${UEFI_GRUB_TIMEOUT}; BIOS=${UEFI_GRUB_TARGET_BIOS}" ""
65
}
66
67
# @TODO: extract u-boot into an extension, so that core bsps don't have this stuff in there to begin with.
68
# @TODO: this code is duplicated in flash-kernel.sh extension, so another reason to refactor the root of the evil
69
post_family_tweaks_bsp__remove_uboot_grub() {
70
display_alert "Removing uboot from BSP" "${EXTENSION}" "info"
71
# Simply remove everything with 'uboot' or 'u-boot' in their filenames from the BSP package.
72
# shellcheck disable=SC2154 # $destination is the target dir of the bsp building function
73
find "$destination" -type f | grep -e "uboot" -e "u-boot" | xargs rm
74
}
75
76
pre_umount_final_image__remove_uboot_initramfs_hook_grub() {
77
# even if BSP still contained this (cached .deb), make sure by removing from ${MOUNT}
78
[[ -f "$MOUNT"/etc/initramfs/post-update.d/99-uboot ]] && rm -v "$MOUNT"/etc/initramfs/post-update.d/99-uboot
79
}
80
81
pre_umount_final_image__install_grub() {
82
configure_grub
83
local chroot_target=$MOUNT
84
display_alert "Installing bootloader" "GRUB" "info"
85
86
# getting rid of the dtb package, if installed, is hard. for now just zap it, otherwise update-grub goes bananas
87
rm -rf "$MOUNT"/boot/dtb* || true
88
89
# add config to disable os-prober, otherwise image will have the host's other OSes boot entries.
90
cat <<-grubCfgFragHostSide >>"${MOUNT}"/etc/default/grub.d/99-armbian-host-side.cfg
91
GRUB_DISABLE_OS_PROBER=true
92
grubCfgFragHostSide
93
94
# Mount the chroot...
95
mount_chroot "$chroot_target/" # this already handles /boot/efi which is required for it to work.
96
97
if [[ "${UEFI_GRUB_TARGET_BIOS}" != "" ]]; then
98
display_alert "Installing GRUB BIOS..." "${UEFI_GRUB_TARGET_BIOS} device ${LOOP}" ""
99
chroot "$chroot_target" /bin/bash -c "grub-install --verbose --target=${UEFI_GRUB_TARGET_BIOS} ${LOOP}" >>"$DEST"/"${LOG_SUBPATH}"/install.log 2>&1 || {
100
exit_with_error "${install_grub_cmdline} failed!"
101
}
102
fi
103
104
local install_grub_cmdline="update-initramfs -c -k all && update-grub && grub-install --verbose --target=${UEFI_GRUB_TARGET} --no-nvram --removable" # nvram is global to the host, even across chroot. take care.
105
display_alert "Installing GRUB EFI..." "${UEFI_GRUB_TARGET}" ""
106
chroot "$chroot_target" /bin/bash -c "$install_grub_cmdline" >>"$DEST"/"${LOG_SUBPATH}"/install.log 2>&1 || {
107
exit_with_error "${install_grub_cmdline} failed!"
108
}
109
110
# Remove host-side config.
111
rm -f "${MOUNT}"/etc/default/grub.d/99-armbian-host-side.cfg
112
113
local root_uuid
114
root_uuid=$(blkid -s UUID -o value "${LOOP}p1") # get the uuid of the root partition, this has been transposed
115
116
# Create /boot/efi/EFI/BOOT/grub.cfg (EFI/ESP) which will load /boot/grub/grub.cfg (in the rootfs, generated by update-grub)
117
cat <<-grubEfiCfg >"${MOUNT}"/boot/efi/EFI/BOOT/grub.cfg
118
search.fs_uuid ${root_uuid} root
119
set prefix=(\$root)'/boot/grub'
120
configfile \$prefix/grub.cfg
121
grubEfiCfg
122
123
umount_chroot "$chroot_target/"
124
125
}
126
127
pre_umount_final_image__900_export_kernel_and_initramfs() {
128
if [[ "${UEFI_EXPORT_KERNEL_INITRD}" == "yes" ]]; then
129
display_alert "Exporting Kernel and Initrd for" "kexec" "info"
130
# this writes to ${DESTIMG} directly, since debootstrap.sh will move them later.
131
# capture the $MOUNT/boot/vmlinuz and initrd and send it out ${DESTIMG}
132
cp "$MOUNT"/boot/vmlinuz-* "${DESTIMG}/${version}.kernel"
133
cp "$MOUNT"/boot/initrd.img-* "${DESTIMG}/${version}.initrd"
134
fi
135
}
136
137
configure_grub() {
138
display_alert "GRUB EFI kernel cmdline" "console=${SERIALCON} distro=${UEFI_GRUB_DISTRO_NAME} timeout=${UEFI_GRUB_TIMEOUT}" ""
139
140
if [[ "_${SERIALCON}_" != "__" ]]; then
141
cat <<-grubCfgFrag >>"${MOUNT}"/etc/default/grub.d/98-armbian.cfg
142
GRUB_CMDLINE_LINUX_DEFAULT="console=${SERIALCON}" # extra Kernel cmdline is configured here
143
grubCfgFrag
144
fi
145
146
cat <<-grubCfgFrag >>"${MOUNT}"/etc/default/grub.d/98-armbian.cfg
147
GRUB_TIMEOUT_STYLE=menu # Show the menu with Kernel options (Armbian or -generic)...
148
GRUB_TIMEOUT=${UEFI_GRUB_TIMEOUT} # ... for ${UEFI_GRUB_TIMEOUT} seconds, then boot the Armbian default.
149
GRUB_DISTRIBUTOR="${UEFI_GRUB_DISTRO_NAME}" # On GRUB menu will show up as "Armbian GNU/Linux" (will show up in some UEFI BIOS boot menu (F8?) as "armbian", not on others)
150
grubCfgFrag
151
152
if [[ "a${UEFI_GRUB_DISABLE_OS_PROBER}" != "a" ]]; then
153
cat <<-grubCfgFragHostSide >>"${MOUNT}"/etc/default/grub.d/98-armbian.cfg
154
GRUB_DISABLE_OS_PROBER=${UEFI_GRUB_DISABLE_OS_PROBER}
155
grubCfgFragHostSide
156
fi
157
158
if [[ "a${UEFI_GRUB_TERMINAL}" != "a" ]]; then
159
cat <<-grubCfgFragTerminal >>"${MOUNT}"/etc/default/grub.d/98-armbian.cfg
160
GRUB_TERMINAL="${UEFI_GRUB_TERMINAL}"
161
grubCfgFragTerminal
162
fi
163
}
164
165