Debian Codegraph @

scripts/sbin/switch-efi-nb-menu

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # Author: Steven Shiau <steven _at_ clonezilla org>
3 # License: GPL
4 # Description: switch the uEFI network Boot menu in the config file
5
6 # Load DRBL setting and functions
7 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
8
9 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
10
11 #
12 # DIA (="dialog") is loaded from drbl.conf
13 GRUB_NB_CONF_DEF="$GRUB_EFINB_DIR/grub.cfg"
14
15 # functions
16 clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp().() {
17 [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP
18 [scip-shell system . . `[`. -f "$GRUB_NB_CONF_TMP" ] && rmscip-shell system . . rm. -f $GRUB_NB_CONF_TMP
19 }
20
21 #
22 USAGEscip-shell shell shell-project 0.0.0 USAGE().() {
23 echoscip-shell system . . echo. "Usage:"
24 echoscip-shell system . . echo. "To hide, reveal or set default uEFI network Boot client menu:"
25 echoscip-shell system . . echo. "`basenamescip-shell system . . basename. $0` [OPTION] [hide|reveal|setdefault]"
26 echoscip-shell system . . echo. "Options:"
27 language_help_prompt_by_idx_no
28 echoscip-shell system . . echo. "-i, --menu MENU Assign MENU to be processed"
29 echoscip-shell system . . echo. "-c, --config CONF use the CONF file instead of default one ($GRUB_NB_CONF_DEF)"
30 dialog_like_prog_help_prompt
31 echoscip-shell system . . echo. "-v, --verbose show verbose messages"
32 echoscip-shell system . . echo. "-h, --help display this help and exit"
33 echoscip-shell system . . echo.
34 echoscip-shell system . . echo. "EXAMPLE"
35 echoscip-shell system . . echo. " To hide the menu 'local' and 'memtest', you can run:"
36 echoscip-shell system . . echo. " $0 -i 'local memtest' hide"
37 }
38
39 select_imagescip-shell shell shell-project 0.0.0 select_image().() {
40 all_label="$(LC_ALL=C grepscip-shell system . . grep. -Eo -- "^[[:space:]]*[#]*[[:space:]]*menuentry[[:space:]]+.*--id[[:space:]]+.*[[:space:]]+\{" $GRUB_NB_CONF_TMP | awkscip-shell system . . awk. -F" " '{ print $(NF-1)}')"
41 [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "all_label: $all_label"
42 if [scip-shell system . . `[`. -z "$all_label" ]; then
43 $DIA --title "No uEFI network Boot image found!" --clear \
44 --msgbox "I can NOT find any uEFI network Boot image in $GRUB_NB_CONF! \nMake sure you already setup DRBL server properly!" 0 0
45 exit 0
46 fi
47
48 MENU=""
49 for i in $all_label; do
50 # EX for i: netinstall-Debian-jessie-amd64
51 # get the description for the netinstall image from grub NB default
52 lines="$(get_grub_efi_image_block $i $GRUB_NB_CONF_TMP)"
53 begin_line="$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $1}')"
54 end_line="$(echoscip-shell system . . echo. $lines | awkscip-shell system . . awk. -F" " '{print $2}')"
55 search_cmd="if ($begin_line..$end_line) {print}"
56 des="$(LC_ALL=C perlscip-shell system . . perl. -n -e "$search_cmd" $GRUB_NB_CONF_TMP | grepscip-shell system . . grep. -Eo "^[[:space:]]*[#]*[[:space:]]*menuentry.*[[:space:]]+--id" | sedscip-shell system . . sed. -r -e "s/^[[:space:]]*[#]*[[:space:]]*menuentry[[:space:]]+\"//g" -e "s/\"[[:space:]]+--id//g" -e "s/[[:space:]]/_/g")"
57
58 case "$action" in
59 "hide"|"setdefault")
60 grep_cmd="^[[:space:]]*[#]+[[:space:]]*menuentry"
61 ;;
62 "reveal")
63 grep_cmd="^[[:space:]]*menuentry"
64 ;;
65 esac
66 add_menu="$(LC_ALL=C perlscip-shell system . . perl. -n -e "$search_cmd" $GRUB_NB_CONF_TMP | grepscip-shell system . . grep. -Ei "$grep_cmd")"
67 if [scip-shell system . . `[`. -z "$add_menu" ]; then
68 MENU="$MENU $i $des off "
69 fi
70 done
71
72 # check if we can find any target menu
73 if [scip-shell system . . `[`. -z "$MENU" ]; then
74 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
75 echoscip-shell system . . echo. "Error!!! No any unhidden menu was found!"
76 echoscip-shell system . . echo. "Maybe they are all unhidden!"
77 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
78 clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp().
79 echoscip-shell system . . echo. "$msg_program_stop"
80 exit 1
81 fi
82
83 #
84 eval title="\$msg_title_${action}"
85 eval list_des="\$msg_${action}_list_des"
86
87 case "$action" in
88 "hide"|"reveal")
89 $DIA \
90 --backtitle "$msg_nchc_title" \
91 --title "$title" \
92 --separate-output \
93 --checklist "$list_des ($msg_press_space_to_mark_selection)" 0 80 0 \
94 $MENU 2> $TMP
95 image_selected="$(catscip-shell system . . cat. $TMP)"
96 ;;
97 "setdefault")
98 # Here we use --radiolist so that the format of MENU is compatible with --checklist
99 $DIA \
100 --backtitle "$msg_nchc_title" \
101 --title "$title" \
102 --radiolist "$list_des" 0 80 0 \
103 $MENU 2> $TMP
104 image_selected="$(catscip-shell system . . cat. $TMP)"
105 ;;
106 esac
107 [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP
108 [scip-shell system . . `[`. -z "$image_selected" ] && exit 1
109 [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "image_selected: $image_selected"
110 } # end of select_image
111
112 select_hide_reveal_setdefaultscip-shell shell shell-project 0.0.0 select_hide_reveal_setdefault().() {
113 TMP=$(mktempscip-shell system . . mktemp. /tmp/menu.XXXXXXscip-shell filesystem . . `/tmp/menu.XXXXXX`.)
114 $DIA \
115 --backtitle "$msg_nchc_title" \
116 --title "$msg_hide_reveal" \
117 --menu "$msg_choose_the_action:" 0 60 0 \
118 "hide" "$msg_hide_pxe_menus" \
119 "reveal" "$msg_reveal_pxe_menus" \
120 "setdefault" "$msg_setdefault_pxe_menu" \
121 2> $TMP
122 action=$(catscip-shell system . . cat. $TMP)
123 [scip-shell system . . `[`. -f "$TMP" ] && rmscip-shell system . . rm. -f $TMP
124 [scip-shell system . . `[`. -z "$action" ] && echoscip-shell system . . echo. "No action selected! Program terminated!!!" && exit 1
125 } # end of select_hide_reveal_setdefault
126
127 image_selected=""
128 # Parse command-line options
129 while [scip-shell system . . `[`. $# -gt 0 ]; do
130 case "$1" in
131 -l|--language)
132 shift
133 if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then
134 # skip the -xx option, in case
135 specified_lang="$1"
136 shift
137 fi
138 ;;
139 -i|--image)
140 shift
141 # skip the -xx option, in case
142 if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then
143 image_selected="$image_selected $1"
144 shift
145 fi
146 ;;
147 -c|--config)
148 shift
149 # skip the -xx option, in case
150 if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then
151 GRUB_NB_CONF="$1"
152 shift
153 fi
154 ;;
155 -h|--help)
156 USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2
157 exit 2 ;;
158 -d0|--dialog) DIA="dialog"; shift;;
159 -d1|--Xdialog) DIA="Xdialog"; shift;;
160 -d2|--whiptail) DIA="whiptail"; shift;;
161 -d3|--gdialog) DIA="gdialog"; shift;;
162 -d4|--kdialog) DIA="kdialog"; shift;;
163 -v|--verbose) VERBOSE="on"; shift;;
164 -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2
165 USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2
166 exit 2 ;;
167 *) break ;;
168 esac
169 done
170 #
171 ask_and_load_lang_set $specified_lang
172
173 # check DIA
174 check_DIA_set_ESC $DIA
175
176 #
177 action="$1"
178 [scip-shell system . . `[`. -z "$action" ] && select_hide_reveal_setdefaultscip-shell shell shell-project 0.0.0 select_hide_reveal_setdefault().
179 [scip-shell system . . `[`. -z "$GRUB_NB_CONF" ] && GRUB_NB_CONF=$GRUB_NB_CONF_DEF
180
181 #
182 TMP=$(mktempscip-shell system . . mktemp. /tmp/menu.XXXXXXscip-shell filesystem . . `/tmp/menu.XXXXXX`.)
183 GRUB_NB_CONF_TMP="$(mktempscip-shell system . . mktemp. /tmp/grub_nb_cfg.XXXXXXscip-shell filesystem . . `/tmp/grub_nb_cfg.XXXXXX`.)"
184
185 # copy the original pxe_conf to be a template
186 if [scip-shell system . . `[`. ! -f "$GRUB_NB_CONF" ]; then
187 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
188 echoscip-shell system . . echo. -e "Unable to find any uEFI network Boot config file! \nMake sure you already setup DRBL server properly!"
189 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
190 clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp().
191 exit 1
192 fi
193 cpscip-shell system . . cp. -f $GRUB_NB_CONF $GRUB_NB_CONF_TMP
194
195 [scip-shell system . . `[`. -z "$image_selected" ] && select_imagescip-shell shell shell-project 0.0.0 select_image().
196
197 # hide or reveal the specified menu
198 case "$action" in
199 "hide")
200 for img in $image_selected; do
201 echoscip-shell system . . echo. "Hide the label \"$img\"."
202 hide_reveal_grub_efi_ent $img hide $GRUB_NB_CONF_TMP
203 done
204 ;;
205 "reveal")
206 for img in $image_selected; do
207 echoscip-shell system . . echo. "Reveal the label \"$img\"."
208 hide_reveal_grub_efi_ent $img reveal $GRUB_NB_CONF_TMP
209 done
210 ;;
211 "setdefault")
212 echoscip-shell system . . echo. "Set the default label \"$image_selected\"."
213 set-default-grub-efi-img -i $image_selected -c $GRUB_NB_CONF_TMP
214 ;;
215 esac
216 # check if all menu is hidden!!! We can not do that!
217 if [scip-shell system . . `[`. -z "$(grepscip-shell system . . grep. -iE "^[[:space:]]*menuentry[[:space:]]+" $GRUB_NB_CONF_TMP)" ]; then
218 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
219 echoscip-shell system . . echo. "$msg_error!!! $msg_you_can_not_hide_all_pxe_menus!!!"
220 echoscip-shell system . . echo. "$msg_program_stop!!! $msg_no_modification!"
221 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
222 clean_tmpscip-shell shell shell-project 0.0.0 clean_tmp().
223 exit 1
224 fi
225
226 #
227 [scip-shell system . . `[`. -n "$VERBOSE" ] && echoscip-shell system . . echo. "Copy the template file $GRUB_NB_CONF_TMP to actual one $GRUB_NB_CONF"
228 cpscip-shell system . . cp. -f $GRUB_NB_CONF_TMP $GRUB_NB_CONF
229 [scip-shell system . . `[`. -f "$GRUB_NB_CONF_TMP" ] && rmscip-shell system . . rm. -f $GRUB_NB_CONF_TMP
230 exit 0