Debian Codegraph @

sbin/gen-grub-efi-nb-menu

raw symbols SCIP
1 #!/bin/bash
2 # Author: Steven Shiau <steven _at_ clonezilla org>
3 # License: GPL
4 # Description: Output grub efi network boot config on DRBL server.
5
6 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
7
8 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
9 [ -e /etc/drbl/drbl-ocs.conf ] && . /etc/drbl/drbl-ocs.conf
10 [ -e $DRBL_SCRIPT_PATH/sbin/ocs-functions ] && . $DRBL_SCRIPT_PATH/sbin/ocs-functions
11
12 export LC_ALL=C
13 #
14 USAGE() {
15 echo "Generate the Grub EFI network boot menu for DRBL clients."
16 echo "Usage: $(basename $0) [OPTION]"
17 echo "Options:"
18 echo "-c, --console OPT: set the console output parameters."
19 echo "-d, --dir DIR: Assign the netinstall files are in DIR instead of default dir $pxecfg_pd"
20 echo "-m, --bg-mode [text|graphic]: Set the default GRUB menu mode to text or graphic"
21 echo "-s, --serial OPT: set the grub menu to work with serial console output. OPT is like: serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
22 echo "-g, --graphic [y|n]: Option to set client to use graphical mode or not when booting GNU/Linux (now only for Fedora Core)"
23 echo "-p, --grub-efi-dir DIR: Assign grub config dir as DIR instead of deafult dir $GRUB_EFINB_DIR"
24 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."
25 echo "-v, --verbose: verbose mode."
26 echo "Ex:"
27 echo "To create the console output in DRBL client:"
28 echo "$(basename $0) -v -s \"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\" -c \"console=ttyS0,38400n81 console=tty0\" "
29 echo "Note! You must put the \" before and after your serial OPT and console OPT."
30 }
31
32 # Default settings
33 # GRUB_EFINB_DIR is from drbl.conf, and GRUB_EFI_D is for using only in the program.
34 bg_mode="graphic"
35
36 #
37 check_if_root
38
39 # Parse command-line options
40 while [ $# -gt 0 ]; do
41 case "$1" in
42 -s|--serial)
43 shift
44 if [ -z "$(echo $1 |grep ^-.)" ]; then
45 # skip the -xx option, in case
46 GRUB_SERIAL_OUTPUT="$1"
47 shift
48 fi
49 [ -z "$GRUB_SERIAL_OUTPUT" ] && USAGE && exit 1
50 ;;
51 -c|--console)
52 shift
53 if [ -z "$(echo $1 |grep ^-.)" ]; then
54 # skip the -xx option, in case
55 CONSOLE_OUTPUT="$1"
56 shift
57 fi
58 [ -z "$CONSOLE_OUTPUT" ] && USAGE && exit 1
59 ;;
60 -m|--bg-mode)
61 shift;
62 # skip the -xx option, in case
63 if [ -z "$(echo $1 |grep ^-.)" ]; then
64 bg_mode="$1"
65 shift
66 fi
67 [ -z "$bg_mode" ] && USAGE && exit 1
68 ;;
69 -g|--graphic)
70 shift
71 if [ -z "$(echo $1 |grep ^-.)" ]; then
72 # skip the -xx option, in case
73 graphical_boot="$1"
74 shift
75 fi
76 [ -z "$graphical_boot" ] && USAGE && exit 1
77 ;;
78 -d|--dir)
79 shift
80 if [ -z "$(echo $1 |grep ^-.)" ]; then
81 # skip the -xx option, in case
82 TDIR="$1"
83 shift
84 fi
85 [ -z "$TDIR" ] && USAGE && exit 1
86 ;;
87 -p|--grub-efi-dir)
88 shift
89 if [ -z "$(echo $1 |grep ^-.)" ]; then
90 # skip the -xx option, in case
91 GRUB_EFI_D="$1"
92 shift
93 fi
94 [ -z "$GRUB_EFI_D" ] && USAGE && exit 1
95 ;;
96 -t|--tftp-server)
97 shift
98 if [ -z "$(echo $1 |grep ^-.)" ]; then
99 # skip the -xx option, in case
100 tftp_server_ip="$1"
101 shift
102 fi
103 [ -z "$tftp_server_ip" ] && USAGE && exit 1
104 ;;
105 -v|--verbose)
106 shift; verbose="on"
107 ;;
108 -*) echo "${0}: ${1}: invalid option" >&2
109 USAGE >& 2
110 exit 2 ;;
111 *) break ;;
112 esac
113 done
114
115 [ -z "$GRUB_EFI_D" ] && GRUB_EFI_D="$GRUB_EFINB_DIR"
116 [ -z "$TDIR" ] && TDIR="$pxecfg_pd"
117
118 # Find the IP address of this server. Here we choose the first one in the list, since it's just an example.
119
120 echo "Generating default GRUB network boot config ($GRUB_EFI_D/grub.cfg)..."
121 # clean the old one
122 [ -f "$GRUB_EFI_D/grub.cfg" ] && rm -f $GRUB_EFI_D/grub.cfg
123
124 # get the OS_Version and OS_type
125 check_distribution_name
126
127 # $graphical_boot is in drbl.conf or from command parameter.
128 case "$graphical_boot" in
129 y|Y|[yY][eE][sS])
130 case "$OS_Version" in
131 FC*)
132 echo "Use graphical boot for DRBL Fedora Core client..."
133 # We'd better use "rhgb" instead of "quiet rhgb" since if something goes wrong in net initrd, it's not easy to debug.
134 drbl_dist_append="rhgb"
135 ;;
136 esac
137 ;;
138 n|N|[nN][oO])
139 drbl_dist_append=""
140 ;;
141 esac
142 case "$OS_Version" in
143 DBN*)
144 # Put quiet for Debian and Ubuntu clients.
145 quiet_opt="quiet"
146 ;;
147 *)
148 # Do not put quiet since for centos 6 clients since it seems after pxelinux booting, no output on screen.
149 quiet_opt=""
150 ;;
151 esac
152 #
153 # GRUB Linux menu mode
154 # find the drbl background logo and memtest
155 drbl_logo="$(basename $grub_bg_img)"
156 # show some prompts
157 [ -n "$GRUB_SERIAL_OUTPUT" ] && echo "GRUB serial output param: $GRUB_SERIAL_OUTPUT"
158 [ -n "$CONSOLE_OUTPUT" ] && echo "Linux console output param: $CONSOLE_OUTPUT"
159 [ -n "$drbl_dist_append" ] && echo "Extra param: $drbl_dist_append"
160
161 # read drbl_deploy.conf if this program is run again after drblpush is done.
162 [ -f /etc/drbl/drbl_deploy.conf ] && . /etc/drbl/drbl_deploy.conf
163 [ "$drbl_mode" = "drbl_ssi_mode" ] && drbl_mode_opt="clientdir=node_root"
164 [ "$clonezilla_mode" = "clonezilla_box_mode" ] && clonezilla_mode_opt="clientdir=node_root"
165
166 # Get version number.
167 case "$OS_Version" in
168 RH*|FC*|CO*|MDK*|MDV*|SUSE*)
169 drbl_ver="$(rpm -q drbl | sed -e "s/^drbl-//g")"
170 ocs_ver="$(rpm -q clonezilla | sed -e "s/^clonezilla-//g")"
171 ;;
172 DBN*)
173 drbl_ver="$(dpkg -l drbl | tail -n 1 | awk -F" " '{print $3}')"
174 ocs_ver="$(dpkg -l clonezilla | tail -n 1 | awk -F" " '{print $3}')"
175 ;;
176 esac
177 [ -z "$drbl_ver" ] && drbl_ver="Unknown"
178 [ -z "$ocs_ver" ] && ocs_ver="Unknown"
179
180 #
181 [ -f "$GRUB_EFI_D/grub.cfg" ] && mv -f $GRUB_EFI_D/grub.cfg $GRUB_EFI_D/grub.cfg.drblsaved
182
183 # Kernel EDD (sysfs interface to BIOS EDD information) is useful for partclone.ntfsreloc. However, if it's compiled as builtin in kernel, it might be off by default, as the kernel config:
184 # CONFIG_EDD=y
185 # CONFIG_EDD_OFF=y
186 # If so, we have to put edd=on in boot parameter so the edd will be on when booting
187 edd_opt=""
188 if [ -e "$pxecfg_pd/kernel_version_in_initrd.txt" ]; then
189 # Client's kernel config exists, i.e. this program is run after client's kernel is copied.
190 client_kver="$(cat $pxecfg_pd/kernel_version_in_initrd.txt 2>/dev/null)"
191 if [ -n "$(grep -Ew "CONFIG_EDD=y" $drbl_common_root/boot/config-${client_kver} 2>/dev/null)" ] && [ -n "$(grep -Ew "CONFIG_EDD_OFF=y" $drbl_common_root/boot/config-${client_kver} 2>/dev/null)" ]; then
192 edd_opt="edd=on"
193 fi
194 else
195 # Client's kernel config does not exists, i.e. this program is run BEFORE client's kernel is copied. Here we assume the running kernel on server has the same config with that on client.
196 client_kver="$(uname -r)"
197 if [ -n "$(grep -Ew "CONFIG_EDD=y" /boot/config-${client_kver} 2>/dev/null)" ] && [ -n "$(grep -Ew "CONFIG_EDD_OFF=y" /boot/config-${client_kver} 2>/dev/null)" ]; then
198 edd_opt="edd=on"
199 fi
200 fi
201
202 # Bacground mode
203 case "$bg_mode" in
204 text*|TEXT*) g_bg_f="no" ;;
205 graphic*|GRAPHIC*) g_bg_f="yes" ;;
206 esac
207
208 # Prepare required files
209 cp $drbl_syscfg/boot-local-efi.cfg $GRUB_EFI_D/
210
211 grub_ver="$(LC_ALL=C cat $pxecfg_pd/GRUB_VERSION)"
212 echo "Adding GRUB EFI boot menu for DRBL, Clonezilla..."
213 cat <<-GRUB_END > $GRUB_EFI_D/grub.cfg
214 # Created by gen-grub-efi-nb-menu! Do NOT edit unless you know what you are doing!
215 GRUB_END
216
217 # GRUB serial console
218 # Ref: ttps://www.gnu.org/software/grub/manual/grub.html#serial
219 if [ -n "$GRUB_SERIAL_OUTPUT" ]; then
220 cat <<-GRUB_END >> $GRUB_EFI_D/grub.cfg
221 $GRUB_SERIAL_OUTPUT
222 terminal_input --append serial
223 terminal_output --append serial
224
225 GRUB_END
226 fi
227
228 if [ -z "$tftp_server_ip" ]; then
229 tftp_server_ip="$(LC_ALL=C get-all-nic-ip -b | awk -F" " '{print $1}')"
230 fi
231
232 cat <<-GRUB_END >> $GRUB_EFI_D/grub.cfg
233 set default=drbl-client
234 set timeout_style=menu
235 set timeout=7
236 set hidden_timeout_quiet=false
237 set graphic_bg=${g_bg_f}
238 # tftp_server_ip is assigned or got from Clonezilla, not from grub. It can be be nothing.
239 set tftp_server_ip=${tftp_server_ip}
240
241 # Dynamically detect if we booted via HTTP or TFTP using built-in test command.
242 # The grub variable "http_url" only exists if netboot client is set as HTTPBoot. PXEBoot does not have that.
243 if [ -n "\${http_url}" ]; then
244 # HTTPBoot case
245 if [ -n "\${tftp_server_ip}" ]; then
246 set tftpd_opt="http,\${tftp_server_ip}"
247 else
248 set tftpd_opt="http"
249 fi
250 set tftp_path="/tftpboot/nbi_img"
251 set grub_prefix_path="/tftpboot/nbi_img/grub"
252 else
253 # PXEBoot case
254 if [ -n "\${tftp_server_ip}" ]; then
255 set tftpd_opt="tftp,\${tftp_server_ip}"
256 else
257 set tftpd_opt="tftp"
258 fi
259 set tftp_path=""
260 set grub_prefix_path="/grub"
261 fi
262 set root=\$tftpd_opt
263 set prefix=(\$tftpd_opt)\$grub_prefix_path
264
265 # Load required modules from our newly configured HTTP/TFTP prefix
266 insmod png
267
268 # "run_load_netboot" is the flag to avoid load_netboot to be run more than once.
269 #
270 function load_netboot {
271 echo "Grub CPU and platform: \$grub_cpu, \$grub_platform"
272 echo 'Network status: '
273 net_ls_cards
274 net_ls_addr
275 net_ls_routes
276 # sleep 5
277
278 if [ -s "\$prefix/grub.cfg-drbl-\$net_default_mac" ]; then
279 echo "Loading config file \$prefix/grub.cfg-drbl-\$net_default_mac..."
280 # Before using configfile to run another config file, set run_load_netboot
281 # and export so that it will be honored in configfile.
282 set run_load_netboot=no
283 export run_load_netboot
284 configfile \$prefix/grub.cfg-drbl-\$net_default_mac
285 fi
286
287 if [ -s "\$prefix/grub.cfg-drbl-\$net_default_ip" ]; then
288 echo "Loading config file \$prefix/grub.cfg-drbl-\$net_default_ip..."
289 # Before using configfile to run another config file, set run_load_netboot
290 # and export so that it will be honored in configfile.
291 set run_load_netboot=no
292 export run_load_netboot
293 configfile \$prefix/grub.cfg-drbl-\$net_default_ip
294 fi
295 }
296 #
297 function load_gfxterm {
298 set gfxmode=auto
299 insmod efi_gop
300 insmod efi_uga
301 insmod gfxterm
302 terminal_output gfxterm
303 }
304
305 # load_netboot should be run once, otherwise it will loop.
306 # i.e., for default grub.cfg, run load_netboot.
307 # Otherwise (e.g., grub.cfg-drbl-192.168.37.3, grub.cfg-drbl-00:0c:29:1d:9a:d1) skip it.
308 if [ "\$run_load_netboot" != "no" ]; then
309 load_netboot
310 fi
311
312 # Somehow the grub2 from CentOS 7 will look for unicode.pf2.pf2 if using "loadfont unicode.pf2". While in older Debian/Ubuntu it's OK to use "loadfont unicode.pf2".
313 if [ x"\${graphic_bg}" = xyes ]; then
314 if loadfont unicode; then
315 load_gfxterm
316 elif loadfont unicode.pf2; then
317 load_gfxterm
318 fi
319 fi
320 if background_image drblwp.png; then
321 set color_normal=black/black
322 set color_highlight=magenta/black
323 else
324 set color_normal=cyan/blue
325 set color_highlight=white/blue
326 fi
327
328 # Uncomment the following for serial console
329 # The command serial initializes the serial unit 0 with the speed 38400bps.
330 # The serial unit 0 is usually called ‘COM1’. If COM2, use ‘--unit=1’ instead.
331 #serial --unit=0 --speed=38400
332 #terminal_input serial
333 #terminal_output serial
334
335 # Decide if the commands: linux/initrd (default) or linuxefi/initrdefi
336 set linux_cmd=linux
337 set initrd_cmd=initrd
338 export linux_cmd initrd_cmd
339 # Since "linux/initrd" works for uEFI boot, no matter it's secure boot or not. Just use them.
340 # if [ "\${grub_cpu}" = "x86_64" -o "\${grub_cpu}" = "i386" ];then
341 # set linux_cmd=linuxefi
342 # set initrd_cmd=initrdefi
343 # fi
344
345 GRUB_END
346
347 cat <<-GRUB_END >> $GRUB_EFI_D/grub.cfg
348 menuentry "$FULL_OS_Version $powerful_client_menu_label" --id drbl-client {
349 echo "Enter DRBL..."
350 echo "Loading Linux kernel vmlinuz-pxe..."
351 \$linux_cmd vmlinuz-pxe devfs=nomount drblthincli=off selinux=0 drbl_bootp=\$net_default_next_server $quiet_opt $edd_opt $CONSOLE_OUTPUT $drbl_mode_opt $drbl_dist_append
352 echo "Loading initial ramdisk initrd-pxe.img..."
353 \$initrd_cmd initrd-pxe.img
354 }
355
356 #menuentry "Clonezilla" --id clonezilla-se-client {
357 # echo "Enter Clonezilla..."
358 # echo 'Loading Linux kernel vmlinuz-pxe...'
359 # \$linux_cmd vmlinuz-pxe devfs=nomount drblthincli=off selinux=0 $quiet_opt text 1 drbl_bootp=\$net_default_next_server $edd_opt $CONSOLE_OUTPUT $clonezilla_mode_opt $drbl_dist_append
360 # echo 'Loading initial ramdisk initrd-pxe.img...'
361 # \$initrd_cmd initrd-pxe.img
362 #}
363
364 menuentry "Local operating system (if available)" --id local-disk {
365 echo "Booting first local disk..."
366 # Generate boot menu automatically
367 configfile grub/boot-local-efi.cfg
368 # If not chainloaded, definitely no uEFI boot loader was found.
369 echo "No uEFI boot loader was found!"
370 sleep 15
371 }
372
373 GRUB_END
374
375 if [ -e "$pxecfg_pd/mt86+x64.efi" ]; then
376 cat <<-GRUB_END >> $GRUB_EFI_D/grub.cfg
377 menuentry "Memtest using Memtest86+" --id memtest86+ {
378 linux mt86+x64.efi keyboard=both
379 }
380
381 GRUB_END
382 fi
383
384 cat <<-GRUB_END >> $GRUB_EFI_D/grub.cfg
385 menuentry "Reboot" --id reboot {
386 echo "System rebooting..."
387 reboot
388 }
389
390 menuentry "Shutdown" --id shutdown {
391 echo "System shutting down..."
392 halt
393 }
394
395 menuentry 'uEFI firmware setup' 'uefi-firmware' {
396 echo "Entering uEFI firmware setup..."
397 insmod efifwsetup
398 fwsetup
399 }
400
401 GRUB_END
402
403 # Append the config for those netinstall files
404 output_netinstall_boot_menu --efi $TDIR $GRUB_EFI_D/grub.cfg
405
406 echo "done!"