| 1 | #!/bin/bashscip-shell filesystem . . `/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.confscip-shell source . . `/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 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 25 | echoscip-shell system . . echo. "Generate the Grub EFI network boot loader for DRBL clients." |
| 26 | echoscip-shell system . . echo. "Usage: $prog [OPTION]" |
| 27 | echoscip-shell system . . echo. "Options:" |
| 28 | echoscip-shell system . . echo. "-s, --secure-boot-client: Enabled secure boot for uEFI network boot client. This is still in testing." |
| 29 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Ex:" |
| 31 | echoscip-shell system . . echo. "To create Grub EFI network boot loader for DRBL clients with TFTP server IP address 192.168.67.254:" |
| 32 | echoscip-shell system . . 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 [scip-shell system . . `[`. $# -gt 0 ]; do |
| 43 | case "$1" in |
| 44 | -t|--tftp-server) |
| 45 | shift |
| 46 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 47 | # skip the -xx option, in case |
| 48 | tftp_server_ip="$1" |
| 49 | shift |
| 50 | fi |
| 51 | [scip-shell system . . `[`. -z "$tftp_server_ip" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 52 | ;; |
| 53 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 54 | USAGEscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -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/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 70 | GRUB_MKIMG=grub2-mkimage |
| 71 | elif type grub-mkimage >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 72 | GRUB_MKIMG=grub-mkimage |
| 73 | else |
| 74 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 75 | echoscip-shell system . . echo. "Command grub2-mkimage or grub-mkimage not found!" |
| 76 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 77 | echoscip-shell system . . echo. "Skip creating grub2 uEFI network boot image." |
| 78 | exit 1 |
| 79 | fi |
| 80 | if type grub2-mknetdir >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 81 | GRUB_MKNETD=grub2-mknetdir |
| 82 | elif type grub-mknetdir >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 83 | GRUB_MKNETD=grub-mknetdir |
| 84 | else |
| 85 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 86 | echoscip-shell system . . echo. "Command grub2-mknetdir or grub-mknetdir not found!" |
| 87 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 88 | echoscip-shell system . . echo. "Skip creating grub2 uEFI network boot modules." |
| 89 | exit 1 |
| 90 | fi |
| 91 | # |
| 92 | if [scip-shell system . . `[`. -z "$GRUB_EFINB_DIR" ]; then |
| 93 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 94 | echoscip-shell system . . echo. "\"\$GRUB_EFINB_DIR\" _NOT_ defined!" |
| 95 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 96 | echoscip-shell system . . echo. "$msg_program_stop!" |
| 97 | exit 1 |
| 98 | fi |
| 99 | echoscip-shell system . . echo. "Removing the old $GRUB_EFINB_DIR if it exists..." |
| 100 | if [scip-shell system . . `[`. -d "$GRUB_EFINB_DIR" ]; then |
| 101 | rmscip-shell system . . rm. -rf $GRUB_EFINB_DIR |
| 102 | fi |
| 103 | mkdirscip-shell system . . 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 [scip-shell system . . `[`. ! -h "$(dirnamescip-shell system . . dirname. $GRUB_EFINB_DIR)"/grub-efi.cfg ]; then |
| 107 | lnscip-shell system . . ln. -fs -r "$GRUB_EFINB_DIR" "$(dirnamescip-shell system . . dirname. $GRUB_EFINB_DIR)"/grub-efi.cfg |
| 108 | fi |
| 109 | |
| 110 | # Get the grub version |
| 111 | grub_v="$(LC_ALL=C $GRUB_MKIMG --version | awkscip-shell system . . awk. -F" " '{print $NF}')" |
| 112 | if [scip-shell system . . `[`. -n "$grub_v" ]; then |
| 113 | echoscip-shell system . . echo. "$grub_v" > $pxecfg_pd/GRUB_VERSION |
| 114 | else |
| 115 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 116 | echoscip-shell system . . echo. "Warngin! Grub version _NOT_ found!" |
| 117 | [scip-shell system . . `[`. "$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 | echoscip-shell system . . echo. "Preparing background img and font..." |
| 125 | unicode_font="$(LC_ALL=C findscip-shell system . . find. /usr/share/grub/scip-shell filesystem . . `/usr/share/grub/`. /boot/efi/EFI/scip-shell filesystem . . `/boot/efi/EFI/`. -name "unicode.pf2" -print 2>/dev/nullscip-shell filesystem . . `/dev/null`. | uniqscip-shell system . . uniq. | headscip-shell system . . head. -n 1)" |
| 126 | if [scip-shell system . . `[`. -n "$unicode_font" ]; then |
| 127 | cpscip-shell system . . cp. -fv $unicode_font $pxecfg_pd/ |
| 128 | fi |
| 129 | cpscip-shell system . . cp. -fv $grub_bg_img $pxecfg_pd/ |
| 130 | |
| 131 | # Generate the template grub.cfg |
| 132 | if [scip-shell system . . `[`. -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 unamescip-shell system . . uname. -m)" |
| 144 | sys_arch="$(LC_ALL=C getconfscip-shell system . . getconf. LONG_BIT)" |
| 145 | dest_bootloader="" |
| 146 | case "$sys_cputype" in |
| 147 | i686|x86_64) |
| 148 | case "$sys_arch" in |
| 149 | 32) echoscip-shell system . . echo. "System architecture is 32-bit." |
| 150 | dest_bootloader="i386" |
| 151 | ;; |
| 152 | 64) echoscip-shell system . . echo. "System architecture is 64-bit." |
| 153 | dest_bootloader="i386 x86_64";; |
| 154 | *) |
| 155 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 156 | echoscip-shell system . . echo. "Unknown system architecture in drbl-gen-grub-efi-nb!" |
| 157 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 158 | echoscip-shell system . . 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 [scip-shell system . . `[`. ! -e /usr/lib/grub/i386-efi/moddep.lstscip-shell filesystem . . `/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 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 192 | echoscip-shell system . . echo. "Preparing files for UEFI Secure Boot netboot clients..." |
| 193 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 194 | if [scip-shell system . . `[`. -e /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed ]; then |
| 195 | cpscip-shell system . . cp. -fv /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed $pxecfg_pd/shim${SB_EFI_NAME}.efi |
| 196 | echoscip-shell system . . echo. "Copied from /usr/lib/shim/shim${SB_EFI_NAME}.efi.signed" > $pxecfg_pd/shim${SB_EFI_NAME}.efi.info |
| 197 | else |
| 198 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 199 | echoscip-shell system . . echo. "Warngin! \"/usr/lib/shim/shim${SB_EFI_NAME}.efi.signed\" not found! It will not support ${EFI_ARCH} netboot client." |
| 200 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 201 | fi |
| 202 | if [scip-shell system . . `[`. -e /usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed ]; then |
| 203 | cpscip-shell system . . cp. -fv /usr/lib/grub/${EFI_ARCH}-efi-signed/grubnet${SB_EFI_NAME}.efi.signed $pxecfg_pd/grub${SB_EFI_NAME}.efi |
| 204 | echoscip-shell system . . 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 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 207 | echoscip-shell system . . 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 | [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. ! -e "/usr/lib/grub/${EFI_ARCH}-efi/${im}.mod" ]; then |
| 215 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 216 | echoscip-shell system . . echo. "Missing grub2 module ${im}.mod... Exluding ${im}.mod..." |
| 217 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 218 | efi_required_mod="$(echoscip-shell system . . echo. $efi_required_mod | sedscip-shell system . . sed. -r -e "s/$im //g")" |
| 219 | fi |
| 220 | done |
| 221 | echoscip-shell system . . 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 [scip-shell system . . `[`. "$rc" -ne 0 ]; then |
| 225 | # When grub-mkimage fails, an empty output file will still be created. We'd better to clean it. |
| 226 | rmscip-shell system . . rm. -f $out_f |
| 227 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 228 | echoscip-shell system . . echo. "Warning! Failed to create $out_f!" |
| 229 | echoscip-shell system . . echo. "This server won't be able to serve uEFI network boot clients." |
| 230 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 231 | sleepscip-shell system . . sleep. 5 |
| 232 | else |
| 233 | # Create a tag file |
| 234 | echoscip-shell system . . echo. "Created by program \"$prog\" with command:" > ${out_f}.info |
| 235 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Preparing the grub modules in $GRUB_EFINB_DIR..." |
| 241 | $GRUB_MKNETD --net-directory=$GRUB_EFINB_DIR --subdir=/ >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 242 | echoscip-shell system . . echo. "The uEFI network booting is ready." |