| 1 | #!/bin/bash |
| 2 | # License: GPL |
| 3 | # Author: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc ogr tw> |
| 4 | # Description: Program to prepare grub uEFI network booting image |
| 5 | |
| 6 | # |
| 7 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | . /etc/drbl/drbl-ocs.conf |
| 10 | . $DRBL_SCRIPT_PATH/sbin/ocs-functions |
| 11 | |
| 12 | # Settings |
| 13 | grub_mkimg_compress_opt="-C xz" |
| 14 | #efi_required_mod="part_gpt part_msdos hfsplus fat ext2 ntfs btrfs normal chain boot configfile linux appleldr minicmd multiboot iso9660 acpi efi_gop efi_uga elf loadbios loopback video_fb usb search fixvideo png gfxterm gfxterm_background gfxterm_menu echo videotest video reboot halt gfxmenu gettext tftp efinet net gzio xzio" |
| 15 | # Rerfer to the file systems list in /usr/lib/grub/x86_64-efi/fs.lst. This is for client's local disk boot. |
| 16 | efi_required_mod="normal tftp efinet chain echo net gzio xzio linux \ |
| 17 | linuxefi efi_gop efi_uga png gfxterm gfxterm_background gfxterm_menu \ |
| 18 | serial part_gpt part_msdos boot multiboot progress search \ |
| 19 | $GRUB_FS_MOD_drbl_ocs configfile test sleep tr reboot halt" |
| 20 | |
| 21 | prog="$0" |
| 22 | |
| 23 | # |
| 24 | USAGE() { |
| 25 | echo "Generate the Grub EFI network boot loader for DRBL clients." |
| 26 | echo "Usage: $prog [OPTION]" |
| 27 | echo "Options:" |
| 28 | echo "-s, --secure-boot-client: Enabled secure boot for uEFI network boot client. This is still in testing." |
| 29 | echo "-t, --tftp-server IPADD: Assign the IP address IPADD for the TFTP server. If not assigned, automatically obtain from network boot server. Howerver, grub <= 2.0.2 does not support receiving that. Not sure if newer version will support it or not. Hence this is a workaround." |
| 30 | echo "Ex:" |
| 31 | echo "To create Grub EFI network boot loader for DRBL clients with TFTP server IP address 192.168.67.254:" |
| 32 | echo "$prog -t 192.168.67.254" |
| 33 | } |
| 34 | |
| 35 | ################ |
| 36 | ##### Main ##### |
| 37 | ################ |
| 38 | check_if_root |
| 39 | ask_and_load_lang_set |
| 40 | |
| 41 | # Parse command-line options |
| 42 | while [ $# -gt 0 ]; do |
| 43 | case "$1" in |
| 44 | -t|--tftp-server) |
| 45 | shift |
| 46 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 47 | # skip the -xx option, in case |
| 48 | tftp_server_ip="$1" |
| 49 | shift |
| 50 | fi |
| 51 | [ -z "$tftp_server_ip" ] && USAGE && exit 1 |
| 52 | ;; |
| 53 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 54 | USAGE >& 2 |
| 55 | exit 2 ;; |
| 56 | *) break ;; |
| 57 | esac |
| 58 | done |
| 59 | |
| 60 | # Decide if it's something like (tftp,192.168.22.254) or (tftp) in grub config. |
| 61 | # This will only be used in non-secure boot mechanism. |
| 62 | if [ -n "$tftp_server_ip" ]; then |
| 63 | tftp_srv_opt="tftp,$tftp_server_ip" |
| 64 | else |
| 65 | tftp_srv_opt="tftp" |
| 66 | fi |
| 67 | |
| 68 | # Find the grub2 commands: grub-mkimage, grub-mknetdir |
| 69 | if type grub2-mkimage >/dev/null 2>&1; then |
| 70 | GRUB_MKIMG=grub2-mkimage |
| 71 | elif type grub-mkimage >/dev/null 2>&1; then |
| 72 | GRUB_MKIMG=grub-mkimage |
| 73 | else |
| 74 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 75 | echo "Command grub2-mkimage or grub-mkimage not found!" |
| 76 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 77 | echo "Skip creating grub2 uEFI network boot image." |
| 78 | exit 1 |
| 79 | fi |
| 80 | if type grub2-mknetdir >/dev/null 2>&1; then |
| 81 | GRUB_MKNETD=grub2-mknetdir |
| 82 | elif type grub-mknetdir >/dev/null 2>&1; then |
| 83 | GRUB_MKNETD=grub-mknetdir |
| 84 | else |
| 85 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 86 | echo "Command grub2-mknetdir or grub-mknetdir not found!" |
| 87 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 88 | echo "Skip creating grub2 uEFI network boot modules." |
| 89 | exit 1 |
| 90 | fi |
| 91 | # |
| 92 | if [ -z "$GRUB_EFINB_DIR" ]; then |
| 93 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 94 | echo "\"\$GRUB_EFINB_DIR\" _NOT_ defined!" |
| 95 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 96 | echo "$msg_program_stop!" |
| 97 | exit 1 |
| 98 | fi |
| 99 | echo "Removing the old $GRUB_EFINB_DIR if it exists..." |
| 100 | if [ -d "$GRUB_EFINB_DIR" ]; then |
| 101 | rm -rf $GRUB_EFINB_DIR |
| 102 | fi |
| 103 | mkdir -p $GRUB_EFINB_DIR |
| 104 | |
| 105 | # link dir grub as grub-efi.cfg in /tftpboot/nbi_img/. This is for backward compatibility. |
| 106 | if [ ! -h "$(dirname $GRUB_EFINB_DIR)"/grub-efi.cfg ]; then |
| 107 | ln -fs -r "$GRUB_EFINB_DIR" "$(dirname $GRUB_EFINB_DIR)"/grub-efi.cfg |
| 108 | fi |
| 109 | |
| 110 | # Get the grub version |
| 111 | grub_v="$(LC_ALL=C $GRUB_MKIMG --version | awk -F" " '{print $NF}')" |
| 112 | if [ -n "$grub_v" ]; then |
| 113 | echo "$grub_v" > $pxecfg_pd/GRUB_VERSION |
| 114 | else |
| 115 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 116 | echo "Warngin! Grub version _NOT_ found!" |
| 117 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 118 | fi |
| 119 | |
| 120 | # Prepare some files, e.g. background img and fonts |
| 121 | # Unicode is located at: |
| 122 | # Debian: /usr/share/grub/unicode.pf2 |
| 123 | # CetnOS: /boot/efi/EFI/centos/fonts/unicode.pf2 |
| 124 | echo "Preparing background img and font..." |
| 125 | unicode_font="$(LC_ALL=C find /usr/share/grub/ /boot/efi/EFI/ -name "unicode.pf2" -print 2>/dev/null | uniq | head -n 1)" |
| 126 | if [ -n "$unicode_font" ]; then |
| 127 | cp -fv $unicode_font $pxecfg_pd/ |
| 128 | fi |
| 129 | cp -fv $grub_bg_img $pxecfg_pd/ |
| 130 | |
| 131 | # Generate the template grub.cfg |
| 132 | if [ -n "$tftp_server_ip" ]; then |
| 133 | gen-grub-efi-nb-menu -t $tftp_server_ip |
| 134 | else |
| 135 | gen-grub-efi-nb-menu |
| 136 | fi |
| 137 | |
| 138 | # For x86-64 arch, we create bootx64.efi and bootia32.efi. |
| 139 | # For i386 arch, we only create bootia32.efi. |
| 140 | # First we find the installed OS arch |
| 141 | # //NOTE// This could be in the chroot environment, therefore we have to use "getconf LONG_BIT" instead of "uname -m" |
| 142 | # E.g., if on X86_64 OS, the chroot env is i386: "getconf LONG_BIT" will give 32, while "uname -m" will give x86_64 |
| 143 | sys_cputype="$(LC_ALL=C uname -m)" |
| 144 | sys_arch="$(LC_ALL=C getconf LONG_BIT)" |
| 145 | dest_bootloader="" |
| 146 | case "$sys_cputype" in |
| 147 | i686|x86_64) |
| 148 | case "$sys_arch" in |
| 149 | 32) echo "System architecture is 32-bit." |
| 150 | dest_bootloader="i386" |
| 151 | ;; |
| 152 | 64) echo "System architecture is 64-bit." |
| 153 | dest_bootloader="i386 x86_64";; |
| 154 | *) |
| 155 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 156 | echo "Unknown system architecture in drbl-gen-grub-efi-nb!" |
| 157 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 158 | echo "$msg_program_stop!" |
| 159 | exit 1 |
| 160 | ;; |
| 161 | esac |
| 162 | ;; |
| 163 | aarch64) |
| 164 | dest_bootloader="arm64" |
| 165 | ;; |
| 166 | esac |
| 167 | |
| 168 | for i in $dest_bootloader; do |
| 169 | export EFI_ARCH=$i |
| 170 | case "$EFI_ARCH" in |
| 171 | i386) if [ ! -e /usr/lib/grub/i386-efi/moddep.lst ]; then |
| 172 | # On some system, like CentOS 7, there is no i386 efi from grub2. |
| 173 | continue |
| 174 | fi |
| 175 | SB_EFI_NAME="ia32" |
| 176 | out_f="$pxecfg_pd/bootia32.efi" |
| 177 | ;; |
| 178 | x86_64) SB_EFI_NAME="x64" |
| 179 | out_f="$pxecfg_pd/bootx64.efi" |
| 180 | ;; |
| 181 | arm64) SB_EFI_NAME="aa64" |
| 182 | out_f="$pxecfg_pd/bootaa64.efi" |
| 183 | ;; |
| 184 | esac |
| 185 | # For amd64 arch, the files for secure boot: |
| 186 | # /usr/lib/shim/shimx64.efi.signed -> /tftpboot/nbi_img/bootx64.efi |
| 187 | # /usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed -> /tftpboot/nbi_img/grubx64.efi |
| 188 | # For arm64 arch, |
| 189 | # /usr/lib/shim/shimaa64.efi.signed -> /tftpboot/nbi_img/bootaa64.efi |
| 190 | # /usr/lib/grub/arm64-efi-signed/grubnetaa64.efi.signed -> /tftpboot/nbi_img/grubaa64.efi |
| 191 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 192 | echo "Preparing files for UEFI Secure Boot netboot clients..." |
| 193 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 194 | if [ -e /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed ]; then |
| 195 | cp -fv /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed $pxecfg_pd/shim${SB_EFI_NAME}.efi |
| 196 | echo "Copied from /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed" > $pxecfg_pd/shim${SB_EFI_NAME}.efi.info |
| 197 | else |
| 198 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 199 | echo "Warngin! \"/usr/lib/shim/shim${SB_EFI_NAME}.efi.signed\" not found! It will not support ${EFI_ARCH} netboot client." |
| 200 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 201 | fi |
| 202 | if [ -e /usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed ]; then |
| 203 | cp -fv /usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed $pxecfg_pd/grub${SB_EFI_NAME}.efi |
| 204 | echo "Copied from /usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed" > $pxecfg_pd/grub${SB_EFI_NAME}.efi.info |
| 205 | else |
| 206 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 207 | echo "Warngin! \"/usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed\" not found! It will not support ${EFI_ARCH} netboot client." |
| 208 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 209 | fi |
| 210 | |
| 211 | # Create NB file for non-secure boot netboot clients |
| 212 | # Checking the required modules |
| 213 | for im in $efi_required_mod; do |
| 214 | if [ ! -e "/usr/lib/grub/${EFI_ARCH}-efi/${im}.mod" ]; then |
| 215 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 216 | echo "Missing grub2 module ${im}.mod... Exluding ${im}.mod..." |
| 217 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 218 | efi_required_mod="$(echo $efi_required_mod | sed -r -e "s/$im //g")" |
| 219 | fi |
| 220 | done |
| 221 | echo "Creating the uEFI network booting bootable image $out_f..." |
| 222 | $GRUB_MKIMG $grub_mkimg_compress_opt -O ${EFI_ARCH}-efi -o $out_f --prefix="($tftp_srv_opt)/grub/" $efi_required_mod |
| 223 | rc=$? |
| 224 | if [ "$rc" -ne 0 ]; then |
| 225 | # When grub-mkimage fails, an empty output file will still be created. We'd better to clean it. |
| 226 | rm -f $out_f |
| 227 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 228 | echo "Warning! Failed to create $out_f!" |
| 229 | echo "This server won't be able to serve uEFI network boot clients." |
| 230 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 231 | sleep 5 |
| 232 | else |
| 233 | # Create a tag file |
| 234 | echo "Created by program \"$prog\" with command:" > ${out_f}.info |
| 235 | echo "$GRUB_MKIMG $grub_mkimg_compress_opt -O ${EFI_ARCH}-efi -o $out_f --prefix='(tftp)/grub/' $efi_required_mod" >> ${out_f}.info |
| 236 | fi |
| 237 | done |
| 238 | |
| 239 | # Suppress the output to avoid confusion. |
| 240 | echo "Preparing the grub modules in $GRUB_EFINB_DIR..." |
| 241 | $GRUB_MKNETD --net-directory=$GRUB_EFINB_DIR --subdir=/ >/dev/null |
| 242 | echo "The uEFI network booting is ready." |