| 1 | #!/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_tmp() { |
| 17 | [ -f "$TMP" ] && rm -f $TMP |
| 18 | [ -f "$GRUB_NB_CONF_TMP" ] && rm -f $GRUB_NB_CONF_TMP |
| 19 | } |
| 20 | |
| 21 | # |
| 22 | USAGE() { |
| 23 | echo "Usage:" |
| 24 | echo "To hide, reveal or set default uEFI network Boot 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 ($GRUB_NB_CONF_DEF)" |
| 30 | dialog_like_prog_help_prompt |
| 31 | echo "-v, --verbose show verbose messages" |
| 32 | echo "-h, --help display this help and exit" |
| 33 | echo |
| 34 | echo "EXAMPLE" |
| 35 | echo " To hide the menu 'local' and 'memtest', you can run:" |
| 36 | echo " $0 -i 'local memtest' hide" |
| 37 | } |
| 38 | |
| 39 | select_image() { |
| 40 | all_label="$(LC_ALL=C grep -Eo -- "^[[:space:]]*[#]*[[:space:]]*menuentry[[:space:]]+.*--id[[:space:]]+.*[[:space:]]+\{" $GRUB_NB_CONF_TMP | awk -F" " '{ print $(NF-1)}')" |
| 41 | [ -n "$VERBOSE" ] && echo "all_label: $all_label" |
| 42 | if [ -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="$(echo $lines | awk -F" " '{print $1}')" |
| 54 | end_line="$(echo $lines | awk -F" " '{print $2}')" |
| 55 | search_cmd="if ($begin_line..$end_line) {print}" |
| 56 | des="$(LC_ALL=C perl -n -e "$search_cmd" $GRUB_NB_CONF_TMP | grep -Eo "^[[:space:]]*[#]*[[:space:]]*menuentry.*[[:space:]]+--id" | 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 perl -n -e "$search_cmd" $GRUB_NB_CONF_TMP | grep -Ei "$grep_cmd")" |
| 67 | if [ -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 [ -z "$MENU" ]; then |
| 74 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 75 | echo "Error!!! No any unhidden menu was found!" |
| 76 | echo "Maybe they are all unhidden!" |
| 77 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 78 | clean_tmp |
| 79 | 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="$(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="$(cat $TMP)" |
| 105 | ;; |
| 106 | esac |
| 107 | [ -f "$TMP" ] && rm -f $TMP |
| 108 | [ -z "$image_selected" ] && exit 1 |
| 109 | [ -n "$VERBOSE" ] && echo "image_selected: $image_selected" |
| 110 | } # end of select_image |
| 111 | |
| 112 | select_hide_reveal_setdefault() { |
| 113 | TMP=$(mktemp /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=$(cat $TMP) |
| 123 | [ -f "$TMP" ] && rm -f $TMP |
| 124 | [ -z "$action" ] && 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 [ $# -gt 0 ]; do |
| 130 | case "$1" in |
| 131 | -l|--language) |
| 132 | shift |
| 133 | if [ -z "$(echo $1 |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 [ -z "$(echo $1 |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 [ -z "$(echo $1 |grep ^-.)" ]; then |
| 151 | GRUB_NB_CONF="$1" |
| 152 | shift |
| 153 | fi |
| 154 | ;; |
| 155 | -h|--help) |
| 156 | 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 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 165 | 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 | [ -z "$action" ] && select_hide_reveal_setdefault |
| 179 | [ -z "$GRUB_NB_CONF" ] && GRUB_NB_CONF=$GRUB_NB_CONF_DEF |
| 180 | |
| 181 | # |
| 182 | TMP=$(mktemp /tmp/menu.XXXXXX) |
| 183 | GRUB_NB_CONF_TMP="$(mktemp /tmp/grub_nb_cfg.XXXXXX)" |
| 184 | |
| 185 | # copy the original pxe_conf to be a template |
| 186 | if [ ! -f "$GRUB_NB_CONF" ]; then |
| 187 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 188 | echo -e "Unable to find any uEFI network Boot config file! \nMake sure you already setup DRBL server properly!" |
| 189 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 190 | clean_tmp |
| 191 | exit 1 |
| 192 | fi |
| 193 | cp -f $GRUB_NB_CONF $GRUB_NB_CONF_TMP |
| 194 | |
| 195 | [ -z "$image_selected" ] && select_image |
| 196 | |
| 197 | # hide or reveal the specified menu |
| 198 | case "$action" in |
| 199 | "hide") |
| 200 | for img in $image_selected; do |
| 201 | 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 | echo "Reveal the label \"$img\"." |
| 208 | hide_reveal_grub_efi_ent $img reveal $GRUB_NB_CONF_TMP |
| 209 | done |
| 210 | ;; |
| 211 | "setdefault") |
| 212 | 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 [ -z "$(grep -iE "^[[:space:]]*menuentry[[:space:]]+" $GRUB_NB_CONF_TMP)" ]; then |
| 218 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 219 | echo "$msg_error!!! $msg_you_can_not_hide_all_pxe_menus!!!" |
| 220 | echo "$msg_program_stop!!! $msg_no_modification!" |
| 221 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 222 | clean_tmp |
| 223 | exit 1 |
| 224 | fi |
| 225 | |
| 226 | # |
| 227 | [ -n "$VERBOSE" ] && echo "Copy the template file $GRUB_NB_CONF_TMP to actual one $GRUB_NB_CONF" |
| 228 | cp -f $GRUB_NB_CONF_TMP $GRUB_NB_CONF |
| 229 | [ -f "$GRUB_NB_CONF_TMP" ] && rm -f $GRUB_NB_CONF_TMP |
| 230 | exit 0 |