| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | |
| 5 | # Load the setting |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | # |
| 10 | check_if_root |
| 11 | |
| 12 | # functions |
| 13 | USAGE() { |
| 14 | echo "Usage: $0 [Option] [on|off]" |
| 15 | echo "Options:" |
| 16 | language_help_prompt_by_idx_no |
| 17 | echo "-n, --no-check-clients Do NOT check if clients' directories exist in $drblroot." |
| 18 | echo "-c, --no-create-ssi-template Do NOT create DRBL single system image template again. Default is to create SSI template when on." |
| 19 | echo "-d, --drbl-mode [0|1|2] Assign DRBL mode (0: Full DRBL mode, 1: DRBL SSI mode, 2: Do NOT provide diskless Linux service to clients)." |
| 20 | echo "-t, --template HOST Use HOST as the template to create etc and var tarballs. If not assigned, $0 will find the first client one in dir $drblroot." |
| 21 | echo "-x, --exclude-X-config When creating template, exclude X config." |
| 22 | echo "-s, --no-stop-ocs do NOT stop ocs. (It's better to stop OCS so that the unnecessary services in rc1.d will be removed!)." |
| 23 | echo "-z, --clonezilla-mode [0|1|2|3] Assign Clonezilla mode (0: Full DRBL mode, 1: Clonezilla box mode, 2: Do NOT provide clonezilla service to clients, 3: Use Clonezilla live as the OS of clients)" |
| 24 | echo "-h, --help Display this help and exit" |
| 25 | echo "Example:" |
| 26 | echo "To turn on the DRBL SSI mode, run:" |
| 27 | echo "$0 -d 1" |
| 28 | echo "To turn on the DRBL SSI mode and Clonezilla box mode, run:" |
| 29 | echo "$0 -d 1 -z 1" |
| 30 | } # end of USAGE |
| 31 | # |
| 32 | set_clonezilla_mode() { |
| 33 | local mode=$1 |
| 34 | echo -n "Setting clonezilla_mode=\"$mode\" in /etc/drbl/drbl_deploy.conf and /etc/drbl/drblpush.conf... " |
| 35 | perl -pi -e "s/^[[:space:]]*clonezilla_mode=.*/clonezilla_mode=\"$mode\"/g" /etc/drbl/drbl_deploy.conf |
| 36 | perl -pi -e "s/^[[:space:]]*clonezilla_mode=.*/clonezilla_mode=$mode/g" /etc/drbl/drblpush.conf |
| 37 | echo "done!" |
| 38 | } # end of set_clonezilla_mode |
| 39 | # |
| 40 | set_drbl_mode() { |
| 41 | local mode=$1 |
| 42 | echo -n "Setting drbl_mode=\"$mode\" in /etc/drbl/drbl_deploy.conf and /etc/drbl/drblpush.conf... " |
| 43 | perl -pi -e "s/^[[:space:]]*drbl_mode=.*/drbl_mode=\"$mode\"/g" /etc/drbl/drbl_deploy.conf |
| 44 | perl -pi -e "s/^[[:space:]]*drbl_mode=.*/drbl_mode=$mode/g" /etc/drbl/drblpush.conf |
| 45 | echo "done!" |
| 46 | } # end of set_drbl_mode |
| 47 | # |
| 48 | # |
| 49 | create_drbl_ssi_clonezilla_box_template() { |
| 50 | # if template is not assigned, find one. |
| 51 | if [ -z "$template" ]; then |
| 52 | for ih in $drblroot/*; do |
| 53 | # use the 1st one drbl client we found as template |
| 54 | if [ -d "$ih" ]; then |
| 55 | template="$(basename $ih)" |
| 56 | break |
| 57 | fi |
| 58 | done |
| 59 | fi |
| 60 | echo "Creating template tarball for DRBL SSI and Clonezilla box..." |
| 61 | [ "$exclude_template_X_conf" = "yes" ] && gen_ssi_extra_opt="-x" |
| 62 | [ "$stop_ocs" = "no" ] && gen_ssi_extra_opt="$gen_ssi_extra_opt --no-stop-ocs" |
| 63 | drbl-gen-ssi-files -t $template $gen_ssi_extra_opt |
| 64 | } |
| 65 | |
| 66 | # |
| 67 | check_full_drbl_mode_clients() { |
| 68 | client_ip_lists="$(LC_ALL=C get-client-ip-list)" |
| 69 | for ih in $client_ip_lists; do |
| 70 | if [ ! -d $drblroot/$ih/etc ]; then |
| 71 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 72 | echo "$drblroot/$ih does NOT exist!!! You have to run \"drblpush -i\" again and choose to use Full DRBL or Full Clonezilla mode. We can NOT turn on Full DRBL or Full Clonezilla mode!" |
| 73 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 74 | echo "Program terminated!!!" |
| 75 | exit 1 |
| 76 | fi |
| 77 | done |
| 78 | } |
| 79 | |
| 80 | # default setting |
| 81 | template="" |
| 82 | check_clients_files="yes" |
| 83 | create_ssi_template="yes" |
| 84 | stop_ocs="yes" |
| 85 | |
| 86 | # Parse command-line options |
| 87 | while [ $# -gt 0 ]; do |
| 88 | case "$1" in |
| 89 | -l|--language) |
| 90 | shift |
| 91 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 92 | # skip the -xx option, in case |
| 93 | specified_lang="$1" |
| 94 | shift |
| 95 | fi |
| 96 | ;; |
| 97 | -s|--no-stop-ocs) |
| 98 | stop_ocs="no" |
| 99 | shift;; |
| 100 | -n|--no-check-clients) |
| 101 | check_clients_files="no" |
| 102 | shift;; |
| 103 | -c|--no-create-ssi-template) |
| 104 | create_ssi_template="no" |
| 105 | shift;; |
| 106 | -x|--exclude-X-config) |
| 107 | exclude_template_X_conf="yes" |
| 108 | shift;; |
| 109 | -t|--template) |
| 110 | shift |
| 111 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 112 | # skip the -xx option, in case |
| 113 | template="$1" |
| 114 | shift |
| 115 | fi |
| 116 | ;; |
| 117 | -d|--drbl-mode) |
| 118 | shift |
| 119 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 120 | # skip the -xx option, in case |
| 121 | drbl_mode="$1" |
| 122 | shift |
| 123 | fi |
| 124 | [ -z "$drbl_mode" ] && USAGE && exit 1 |
| 125 | ;; |
| 126 | -z|--clonezilla-box-mode) |
| 127 | shift |
| 128 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 129 | # skip the -xx option, in case |
| 130 | clonezilla_mode="$1" |
| 131 | shift |
| 132 | fi |
| 133 | [ -z "$clonezilla_mode" ] && USAGE && exit 1 |
| 134 | ;; |
| 135 | -h|--help) |
| 136 | USAGE >& 2 |
| 137 | exit 2 ;; |
| 138 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 139 | USAGE >& 2 |
| 140 | exit 2 ;; |
| 141 | *) break ;; |
| 142 | esac |
| 143 | done |
| 144 | |
| 145 | # |
| 146 | ask_and_load_lang_set $specified_lang |
| 147 | |
| 148 | # Set the config files for all the process. |
| 149 | PXE_CONF=$pxecfg_pd/pxelinux.cfg/default |
| 150 | GRUB_CONF=$GRUB_EFINB_DIR/grub.cfg |
| 151 | |
| 152 | case "$drbl_mode" in |
| 153 | 0) # full drbl mode |
| 154 | [ "$check_clients_files" = "yes" ] && check_full_drbl_mode_clients |
| 155 | echo "Full DRBL mode. Remove clientdir opt for label drbl in pxelinux config... " |
| 156 | # For PXE |
| 157 | hide_reveal_pxe_img drbl reveal $PXE_CONF |
| 158 | remove_opt_in_pxelinux_cfg_block drbl clientdir |
| 159 | # For uEFI |
| 160 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 161 | if [ -e "$GRUB_CONF" ]; then |
| 162 | hide_reveal_grub_efi_ent drbl-client reveal $GRUB_CONF |
| 163 | remove_opt_in_grub_efi_cfg_block drbl-client clientdir |
| 164 | fi |
| 165 | |
| 166 | set_drbl_mode full_drbl_mode |
| 167 | ;; |
| 168 | 1) # drbl ssi mode |
| 169 | echo "DRBL SSI mode. Set clientdir opt for label drbl in pxelinux config... " |
| 170 | # For PXE |
| 171 | hide_reveal_pxe_img drbl reveal $PXE_CONF |
| 172 | add_opt_in_pxelinux_cfg_block drbl clientdir node_root |
| 173 | # For uEFI |
| 174 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 175 | if [ -e "$GRUB_CONF" ]; then |
| 176 | hide_reveal_grub_efi_ent drbl-client reveal $GRUB_CONF |
| 177 | add_opt_in_grub_efi_cfg_block drbl-client clientdir node_root |
| 178 | fi |
| 179 | |
| 180 | set_drbl_mode drbl_ssi_mode |
| 181 | ;; |
| 182 | 2) # none |
| 183 | echo "DRBL service is set as unavailable." |
| 184 | create_ssi_template="no" |
| 185 | # Remove clientdir then hide it |
| 186 | # For PXE |
| 187 | remove_opt_in_pxelinux_cfg_block drbl clientdir |
| 188 | hide_reveal_pxe_img drbl hide $PXE_CONF |
| 189 | # For uEFI |
| 190 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 191 | if [ -e "$GRUB_CONF" ]; then |
| 192 | remove_opt_in_grub_efi_cfg_block drbl-client clientdir |
| 193 | hide_reveal_grub_efi_ent drbl-client hide $GRUB_CONF |
| 194 | fi |
| 195 | |
| 196 | set_drbl_mode none |
| 197 | ;; |
| 198 | esac |
| 199 | |
| 200 | case "$clonezilla_mode" in |
| 201 | 0) # full clonezilla mode |
| 202 | [ "$check_clients_files" = "yes" ] && check_full_drbl_mode_clients |
| 203 | echo "Full clonezilla mode. Remove clientdir opt for label clonezilla in pxelinux config... " |
| 204 | # For PXE |
| 205 | remove_opt_in_pxelinux_cfg_block clonezilla clientdir |
| 206 | # For uEFI |
| 207 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 208 | if [ -e "$GRUB_CONF" ]; then |
| 209 | remove_opt_in_grub_efi_cfg_block clonezilla-se-client clientdir |
| 210 | fi |
| 211 | |
| 212 | # TODO: how do we know to use it's as full_clonezilla_mode or none ? |
| 213 | set_clonezilla_mode full_clonezilla_mode |
| 214 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 215 | echo "$msg_run_dcs_clonezilla_start_to_show_pxemenu" |
| 216 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 217 | ;; |
| 218 | 1) # clonezilla box mode |
| 219 | echo "Clonezilla box mode. Set clientdir opt for label clonezilla in pxelinux config... " |
| 220 | # For PXE |
| 221 | add_opt_in_pxelinux_cfg_block clonezilla clientdir node_root |
| 222 | # For uEFI |
| 223 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 224 | if [ -e "$GRUB_CONF" ]; then |
| 225 | add_opt_in_grub_efi_cfg_block clonezilla-se-client clientdir node_root |
| 226 | fi |
| 227 | |
| 228 | set_clonezilla_mode clonezilla_box_mode |
| 229 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 230 | echo "$msg_run_dcs_clonezilla_start_to_show_pxemenu" |
| 231 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 232 | ;; |
| 233 | 2) # none |
| 234 | echo "Clonezilla service is set as unavailable." |
| 235 | create_ssi_template="no" |
| 236 | # Remove clientdir then hide it |
| 237 | # For PXE |
| 238 | remove_opt_in_pxelinux_cfg_block clonezilla clientdir |
| 239 | # For uEFI |
| 240 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 241 | if [ -e "$GRUB_CONF" ]; then |
| 242 | remove_opt_in_grub_efi_cfg_block clonezilla-se-client clientdir |
| 243 | fi |
| 244 | |
| 245 | set_clonezilla_mode none |
| 246 | ;; |
| 247 | 3) # clonezilla-live mode |
| 248 | echo "Use Clonezilla live as the OS of clients. Set clientdir opt for label clonezilla in pxelinux config... " |
| 249 | create_ssi_template="no" |
| 250 | # No need to add any options in this mode. Later drbl-ocs-live-prep will add the required options. |
| 251 | |
| 252 | set_clonezilla_mode clonezilla_live_mode |
| 253 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 254 | echo "$msg_run_dcs_clonezilla_start_to_show_pxemenu" |
| 255 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 256 | ;; |
| 257 | esac |
| 258 | # No matter which mode of Clonezilla, we hide the Clonezilla menu first. Only dcs will reveal or hide the menu |
| 259 | # For PXE |
| 260 | hide_reveal_pxe_img clonezilla hide $PXE_CONF |
| 261 | hide_reveal_pxe_img Clonezilla-live hide $PXE_CONF |
| 262 | |
| 263 | # For uEFI |
| 264 | # Older version of GNU/Linux, like CentOS 6 does not support grub uEFI network boot. |
| 265 | if [ -e "$GRUB_CONF" ]; then |
| 266 | hide_reveal_grub_efi_ent clonezilla-se-client hide $GRUB_CONF |
| 267 | hide_reveal_grub_efi_ent "clonezilla-live-client" hide $GRUB_CONF |
| 268 | fi |
| 269 | |
| 270 | # |
| 271 | if [ "$create_ssi_template" = "yes" ]; then |
| 272 | if [ -n "$(echo "$drbl_mode" | grep -Ew "(1|2)")" -o \ |
| 273 | -n "$(echo "$clonezilla_mode" | grep -Ew "(1|2)")" ]; then |
| 274 | create_drbl_ssi_clonezilla_box_template |
| 275 | fi |
| 276 | fi |
| 277 | |
| 278 | exit 0 |