Debian Codegraph @

scripts/sbin/switch-pxe-menu

raw symbols SCIP
1 #!/bin/bash
2 # Author: Steven Shiau <steven _at_ clonezilla org>
3 # License: GPL
4 # Description: switch the PXE 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 PXE_CONF_DEF="$PXELINUX_DIR/default"
14
15 # functions
16 clean_tmp() {
17 [ -f "$TMP" ] && rm -f $TMP
18 [ -f "$PXE_CONF_TMP" ] && rm -f $PXE_CONF_TMP
19 }
20
21 #
22 USAGE() {
23 echo "Usage:"
24 echo "To hide, reveal or set default PXE client menu:"
25 echo "`basename $0` [OPTION] [hide|reveal|setdefault]"
26 echo "Options:"
27 language_help_prompt_by_idx_no
28 echo "-i, --menu MENU Assign MENU to be processed"
29 echo "-c, --config CONF use the CONF file instead of default one ($PXE_CONF_DEF)"
30 dialog_like_prog_help_prompt
31 echo "-o, --output-result-file FILE Output the selecting results to the FILE. It includes the results of action and image_selected."
32 echo "-v, --verbose show verbose messages"
33 echo "-h, --help display this help and exit"
34 echo
35 echo "EXAMPLE"
36 echo " To hide the menu 'local' and 'memtest', you can run:"
37 echo " $0 -i 'local memtest' hide"
38 }
39
40 select_image() {
41 all_label="$(awk '/^[[:space:]]*label[[:space:]]+.*([[:space:]]|$)+/ {print $2}' $PXE_CONF_TMP)"
42 [ -n "$VERBOSE" ] && echo "all_label: $all_label"
43 if [ -z "$all_label" ]; then
44 $DIA --title "No PXE image found!" --clear \
45 --msgbox "I can NOT find any PXE image in $PXE_CONF! \nMake sure you already setup DRBL server properly!" 0 0
46 exit 0
47 fi
48
49 MENU=""
50 for i in $all_label; do
51 # EX for i: rh-8.0-netinstall
52 # get the description for the netinstall image from pxelinux default
53 lines="$(get_pxecfg_image_block $i $PXE_CONF_TMP)"
54 begin_line="$(echo $lines | awk -F" " '{print $1}')"
55 end_line="$(echo $lines | awk -F" " '{print $2}')"
56 search_cmd="if ($begin_line..$end_line) {print}"
57 des="$(LC_ALL=C perl -n -e "$search_cmd" $PXE_CONF_TMP | grep -i "^[[:space:]]*MENU LABEL" | sed -e "s/^[[:space:]]*MENU LABEL //gi" -e "s/ /_/g")"
58
59 case "$action" in
60 "hide"|"setdefault")
61 grep_cmd="^[[:space:]]*MENU HIDE"
62 ;;
63 "reveal")
64 grep_cmd="^[[:space:]]*[#]+[[:space:]]+MENU HIDE"
65 ;;
66 esac
67 add_menu="$(LC_ALL=C perl -n -e "$search_cmd" $PXE_CONF_TMP | grep -Ei "$grep_cmd")"
68 if [ -z "$add_menu" ]; then
69 MENU="$MENU $i $des off "
70 fi
71 done
72
73 # check if we can find any target menu
74 if [ -z "$MENU" ]; then
75 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
76 echo "Error!!! No any unhidden menu was found!"
77 echo "Maybe they are all unhidden!"
78 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
79 clean_tmp
80 echo "$msg_program_stop"
81 exit 1
82 fi
83
84 #
85 eval title="\$msg_title_${action}"
86 eval list_des="\$msg_${action}_list_des"
87
88 case "$action" in
89 "hide"|"reveal")
90 $DIA \
91 --backtitle "$msg_nchc_title" \
92 --title "$title" \
93 --separate-output \
94 --checklist "$list_des ($msg_press_space_to_mark_selection)" 0 80 0 \
95 $MENU 2> $TMP
96 image_selected="$(cat $TMP)"
97 ;;
98 "setdefault")
99 # Here we use --radiolist so that the format of MENU is compatible with --checklist
100 $DIA \
101 --backtitle "$msg_nchc_title" \
102 --title "$title" \
103 --radiolist "$list_des" 0 80 0 \
104 $MENU 2> $TMP
105 image_selected="$(cat $TMP)"
106 ;;
107 esac
108 [ -f "$TMP" ] && rm -f $TMP
109 [ -z "$image_selected" ] && exit 1
110 [ -n "$VERBOSE" ] && echo "image_selected: $image_selected"
111 } # end of select_image
112
113 select_hide_reveal_setdefault() {
114 TMP=$(mktemp /tmp/menu.XXXXXX)
115 $DIA \
116 --backtitle "$msg_nchc_title" \
117 --title "$msg_hide_reveal" \
118 --menu "$msg_choose_the_action:" 0 60 0 \
119 "hide" "$msg_hide_pxe_menus" \
120 "reveal" "$msg_reveal_pxe_menus" \
121 "setdefault" "$msg_setdefault_pxe_menu" \
122 2> $TMP
123 action=$(cat $TMP)
124 [ -f "$TMP" ] && rm -f $TMP
125 [ -z "$action" ] && echo "No action selected! Program terminated!!!" && exit 1
126 } # end of select_hide_reveal_setdefault
127
128 image_selected=""
129 # Parse command-line options
130 while [ $# -gt 0 ]; do
131 case "$1" in
132 -l|--language)
133 shift
134 if [ -z "$(echo $1 |grep ^-.)" ]; then
135 # skip the -xx option, in case
136 specified_lang="$1"
137 shift
138 fi
139 ;;
140 -i|--image)
141 shift
142 # skip the -xx option, in case
143 if [ -z "$(echo $1 |grep ^-.)" ]; then
144 image_selected="$image_selected $1"
145 shift
146 fi
147 ;;
148 -c|--config)
149 shift
150 # skip the -xx option, in case
151 if [ -z "$(echo $1 |grep ^-.)" ]; then
152 PXE_CONF="$1"
153 shift
154 fi
155 ;;
156 -o|--output-result-file)
157 shift
158 # skip the -xx option, in case
159 if [ -z "$(echo $1 |grep ^-.)" ]; then
160 RESULT_FILE="$1"
161 shift
162 fi
163 ;;
164 -h|--help)
165 USAGE >& 2
166 exit 2 ;;
167 -d0|--dialog) DIA="dialog"; shift;;
168 -d1|--Xdialog) DIA="Xdialog"; shift;;
169 -d2|--whiptail) DIA="whiptail"; shift;;
170 -d3|--gdialog) DIA="gdialog"; shift;;
171 -d4|--kdialog) DIA="kdialog"; shift;;
172 -v|--verbose) VERBOSE="on"; shift;;
173 -*) echo "${0}: ${1}: invalid option" >&2
174 USAGE >& 2
175 exit 2 ;;
176 *) break ;;
177 esac
178 done
179 #
180 ask_and_load_lang_set $specified_lang
181
182 # check DIA
183 check_DIA_set_ESC $DIA
184
185 #
186 action="$1"
187 [ -z "$action" ] && select_hide_reveal_setdefault
188 [ -z "$PXE_CONF" ] && PXE_CONF=$PXE_CONF_DEF
189
190 #
191 TMP=$(mktemp /tmp/menu.XXXXXX)
192 PXE_CONF_TMP="$(mktemp /tmp/pxecfg.XXXXXX)"
193
194 # copy the original pxe_conf to be a template
195 if [ ! -f "$PXE_CONF" ]; then
196 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
197 echo -e "Unable to find any PXE config file! \nMake sure you already setup DRBL server properly!"
198 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
199 clean_tmp
200 exit 1
201 fi
202 cp -f $PXE_CONF $PXE_CONF_TMP
203
204 [ -z "$image_selected" ] && select_image
205 # Make $image_selected in one line
206 image_selected="$(echo $image_selected)"
207
208 # hide or reveal the specified menu
209 case "$action" in
210 "hide")
211 for img in $image_selected; do
212 echo "Hide the label \"$img\"."
213 hide_reveal_pxe_img $img hide $PXE_CONF_TMP
214 done
215 ;;
216 "reveal")
217 for img in $image_selected; do
218 echo "Reveal the label \"$img\"."
219 hide_reveal_pxe_img $img reveal $PXE_CONF_TMP
220 done
221 ;;
222 "setdefault")
223 echo "Set the default label \"$image_selected\"."
224 set-default-pxe-img -i $image_selected -c $PXE_CONF_TMP
225 ;;
226 esac
227 # check if all menu is hidden!!! We can not do that!
228 if ! grep -qiE "[[:space:]]*[#]+[[:space:]]*MENU[[:space:]]HIDE" $PXE_CONF_TMP; then
229 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
230 echo "$msg_error!!! $msg_you_can_not_hide_all_pxe_menus!!!"
231 echo "$msg_program_stop!!! $msg_no_modification!"
232 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
233 clean_tmp
234 exit 1
235 fi
236
237 #
238 [ -n "$VERBOSE" ] && echo "Copy the template file $PXE_CONF_TMP to actual one $PXE_CONF"
239 cp -f $PXE_CONF_TMP $PXE_CONF
240 cp_rc="$?"
241 [ -f "$PXE_CONF_TMP" ] && rm -f $PXE_CONF_TMP
242
243 # Write settings for later use
244 if [ "$cp_rc" -eq 0 -a -n "$RESULT_FILE" ]; then
245 echo "Output action and selected menus to $RESULT_FILE."
246 cat <<-RESULT_END > $RESULT_FILE
247 action="$action"
248 image_selected="$image_selected"
249 RESULT_END
250 fi
251
252 exit 0