| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: Program to start service for AoE images 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 | # |
| 13 | prog="$(basename $0)" |
| 14 | # |
| 15 | USAGE() { |
| 16 | echo "Usage: $prog [OPTION] {start|stop|restart|status}" |
| 17 | echo "-d, --img-dir DIR Scan the AoE image directory DIR when starting" |
| 18 | echo "Ex:" |
| 19 | echo "To start AoE service for images in default image dir $sanboot_img_dump_dir:" |
| 20 | echo " $prog start" |
| 21 | echo "To start AoE service for images in /work/img/:" |
| 22 | echo " $prog -d /work/img/ start" |
| 23 | echo "To start AoE service for images in /work/img1/ and /work/img2/:" |
| 24 | echo " $prog -d \"/work/img1/ /work/img2/\" start" |
| 25 | echo "To stop AoE service:" |
| 26 | echo " $prog stop" |
| 27 | } |
| 28 | # |
| 29 | start() { |
| 30 | if ! type vblade &>/dev/null; then |
| 31 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 32 | echo "vblade not found!" |
| 33 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 34 | echo "$msg_program_stop!!" |
| 35 | fi |
| 36 | sys_eth_ports="$(LC_ALL=C get-all-nic-ip --drbl-client-eth-port)" |
| 37 | IP_MAC_TMP="$(mktemp /tmp/ip_mac_tmp.XXXXXX)" |
| 38 | trap "[ -f "$IP_MAC_TMP" ] && rm -f $IP_MAC_TMP" HUP INT QUIT TERM EXIT |
| 39 | parse_dhcpd_conf $IP_MAC_TMP |
| 40 | # The output of parse_dhcpd_confi is like: |
| 41 | # hostname IP ADD MAC ADD |
| 42 | # debian101 192.168.61.1 00:02:e3:53:34:30 |
| 43 | # debian102 192.168.61.2 00:02:e3:54:73:39 |
| 44 | |
| 45 | # image name is like: sanboot.aoe-0-0-for-00-50-56-02-01-02 |
| 46 | echo "The AoE image dir: $aoe_img_dir" |
| 47 | for img_dir in $aoe_img_dir; do |
| 48 | for i in $img_dir/*.aoe-*; do |
| 49 | if [ ! -e "$i" ]; then |
| 50 | echo "$i is not a normal file. Skip this." |
| 51 | continue |
| 52 | fi |
| 53 | # If the filename is not the format, skip it |
| 54 | if [ -z "$(basename $i | grep -E "for-([[:alnum:]]+-){5}[[:alnum:]]+$")" ]; then |
| 55 | echo "$i is not in DRBL AoE image file name format. Skip this." |
| 56 | continue |
| 57 | fi |
| 58 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 59 | echo "AoE image found: $i" |
| 60 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 61 | shelf_no="$(echo $i | grep -Eo "aoe-[[:digit:]]+-[[:digit:]]+-for-.*" | awk -F'-' '{print $2}')" |
| 62 | slot_no="$(echo $i | grep -Eo "aoe-[[:digit:]]+-[[:digit:]]+-for-.*" | awk -F'-' '{print $3}')" |
| 63 | mac_address_dash="$(echo $i | grep -Eo "aoe-[[:digit:]]+-[[:digit:]]+-for-.*" | sed -e 's/^aoe-.*-for-//g')" |
| 64 | mac_address_normal="$(echo $mac_address_dash | sed -e 's/-/:/g')" |
| 65 | mac_name_cfg_for_pxe="01-${mac_address_dash}" |
| 66 | echo "Checking if vblade service for aoe:e${shelf_no}.${slot_no} is running..." |
| 67 | ps_status="$(ps -ef | grep -Ew "vblade[[:space:]]+${shelf_no}[[:space:]]+${slot_no}" | grep -v "grep")" |
| 68 | if [ -n "$ps_status" ]; then |
| 69 | echo "vblade service for aoe.e${shelf_no}.${slot_no} is running. Skip this one" |
| 70 | continue |
| 71 | fi |
| 72 | # Find the right eth port instead of running on all. This can be done by parsing the dhcpd.conf if MAC addresses are used in dhcpd.conf. |
| 73 | client_ip="$(grep -iEw "$mac_address_normal" $IP_MAC_TMP | awk -F' ' '{print $2}')" |
| 74 | if [ -n "$client_ip" ]; then |
| 75 | # We can find the IP address from dhcpd.conf |
| 76 | # The output of "ip route get 192.168.205.10" is like: |
| 77 | # 192.168.205.10 dev eth2 src 192.168.205.254 |
| 78 | eth_ports_list="$(ip route get $client_ip | grep -iEo 'dev[[:space:]]+.*src' | awk -F' ' '{print $2}')" |
| 79 | echo "This server uses ethernet port $eth_ports_list to connect client $mac_address_normal" |
| 80 | else |
| 81 | # "range" is used in dhcpd.conf, we have to run vblade on all the available ether port except uplink port. |
| 82 | eth_ports_list="$sys_eth_ports" |
| 83 | echo "No idea which ethernet port on the server connects to client $mac_address_normal. Running virtual blade on all available ethernet ports except uplink port..." |
| 84 | fi |
| 85 | |
| 86 | if [ -z "$eth_ports_list" ]; then |
| 87 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 88 | echo "No ethernet port on the server connecting to client $mac_address_normal was found!" |
| 89 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 90 | echo "$msg_program_stop!!" |
| 91 | fi |
| 92 | echo -n "Starting virtual blade for ${i##*/} on " |
| 93 | for j in $eth_ports_list; do |
| 94 | echo -n "$j, " |
| 95 | vbladed ${shelf_no} ${slot_no} $j $i |
| 96 | rc=$? |
| 97 | if [ "$rc" -gt 0 ]; then |
| 98 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 99 | echo "Error when starting vbladed by: vbladed ${shelf_no} ${slot_no} $j $i" |
| 100 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 101 | continue |
| 102 | fi |
| 103 | done |
| 104 | echo |
| 105 | |
| 106 | # Set pxelinux config file |
| 107 | cp -f $PXE_CONF_DEF $PXELINUX_DIR/$mac_name_cfg_for_pxe |
| 108 | hide_reveal_pxe_img "AoE-client" reveal $PXELINUX_DIR/$mac_name_cfg_for_pxe |
| 109 | set-default-pxe-img -i "AoE-client" -c $PXELINUX_DIR/$mac_name_cfg_for_pxe |
| 110 | # Change the line: append aoe:e0.0 |
| 111 | drbl-tune-pxecfg-block AoE-client "^([[:space:]]*)append aoe:.*" "\$1append aoe:e${shelf_no}.${slot_no}" $PXELINUX_DIR/$mac_name_cfg_for_pxe |
| 112 | done |
| 113 | done |
| 114 | [ -e "$IP_MAC_TMP" ] && rm -f $IP_MAC_TMP |
| 115 | |
| 116 | # Use gpxelinux.0 instead of pxelinux.0 in dhcpd.conf |
| 117 | if ! grep -iEq '^[[:space:]]*filename[[:space:]]*=[[:space:]]*[\"]*gpxelinux.0[\"]*' $DHCPDCONF_DIR/dhcpd.conf; then |
| 118 | echo "Use gpxelinux.0 instead of pxelinux.0 in dhcpd.conf..." |
| 119 | perl -pi -e 's|(^filename[[:space:]]*=[[:space:]]*"*pxelinux.0"*.*)|#$1|g' $DHCPDCONF_DIR/dhcpd.conf |
| 120 | perl -pi -e 's|(^[#]*[[:space:]]*filename[[:space:]]*=[[:space:]]*"*gpxelinux.0"*.*)|filename = "gpxelinux.0";|g' $DHCPDCONF_DIR/dhcpd.conf |
| 121 | # Restart dhcpd service, since different distributions have different names |
| 122 | for serv_st in dhcpd isc-dhcp-server dhcp3-server; do |
| 123 | [ -e "/etc/init.d/$serv_st" ] && /etc/init.d/$serv_st restart |
| 124 | done |
| 125 | fi |
| 126 | } # end of start |
| 127 | |
| 128 | stop() { |
| 129 | local pxe_mac_files |
| 130 | echo -n "Stopping vblade process... " |
| 131 | pkill -9 vblade |
| 132 | echo "done!" |
| 133 | # |
| 134 | for i in $PXELINUX_DIR/01-*; do |
| 135 | [ -e "$i" ] && pxe_mac_files="$pxe_mac_files $(basename $i)" |
| 136 | done |
| 137 | # Remove spaces in the line head |
| 138 | pxe_mac_files="$(echo $pxe_mac_files | sed -e "s/^[[:space:]]*//g")" |
| 139 | if [ -n "$pxe_mac_files" ]; then |
| 140 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 141 | echo "The pxelinux config files used for AoE clients in dir $PXELINUX_DIR/ are: ${pxe_mac_files// /, }" |
| 142 | echo "Do you want to remove them now ?" |
| 143 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 144 | echo -n "[Y/n] " |
| 145 | read ans_rm_pxelinux_cfg |
| 146 | case "$ans_rm_pxelinux_cfg" in |
| 147 | n|N|[nN][oO]) true ;; |
| 148 | *) |
| 149 | for i in $pxe_mac_files; do |
| 150 | rm -fv $PXELINUX_DIR/$i |
| 151 | done |
| 152 | ;; |
| 153 | esac |
| 154 | fi |
| 155 | # Ask if want to use gpxelinux.0 instead of pxelinux.0 in dhcpd.conf |
| 156 | if grep -iEq '^[[:space:]]*filename[[:space:]]*=[[:space:]]*[\"]*gpxelinux.0[\"]*' $DHCPDCONF_DIR/dhcpd.conf; then |
| 157 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 158 | echo "Since AoE booting service was stopped. Do you want to use pxelinux.0 instead of gpxelinux.0 in your dhcpd.conf ?" |
| 159 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 160 | echo -n "[Y/n] " |
| 161 | read ans_use_pxelinux_0 |
| 162 | case "$ans_use_pxelinux_0" in |
| 163 | n|N|[nN][oO]) true ;; |
| 164 | *) |
| 165 | echo "Use pxelinux.0 instead of gpxelinux.0 in dhcpd.conf..." |
| 166 | perl -pi -e 's|(^[#]*[[:space:]]*filename[[:space:]]*=[[:space:]]*"*pxelinux.0"*.*)|filename = "pxelinux.0";|g' $DHCPDCONF_DIR/dhcpd.conf |
| 167 | perl -pi -e 's|(^filename[[:space:]]*=[[:space:]]*"*gpxelinux.0"*.*)|#$1|g' $DHCPDCONF_DIR/dhcpd.conf |
| 168 | # Restart dhcpd service, since different distributions have different names |
| 169 | for serv_st in dhcpd isc-dhcp-server dhcp3-server; do |
| 170 | [ -e "/etc/init.d/$serv_st" ] && /etc/init.d/$serv_st restart |
| 171 | done |
| 172 | ;; |
| 173 | esac |
| 174 | fi |
| 175 | } # end of stop |
| 176 | |
| 177 | # |
| 178 | restart() { |
| 179 | stop |
| 180 | echo -n "Wait for 2 secs..." |
| 181 | sleep 2 |
| 182 | echo |
| 183 | start |
| 184 | } |
| 185 | |
| 186 | status() { |
| 187 | ps -efw | grep -iE "vblade [[:digit:]]+ [[:digit:]]+" |
| 188 | } |
| 189 | |
| 190 | ############## |
| 191 | #### main #### |
| 192 | ############## |
| 193 | # |
| 194 | while [ $# -gt 0 ]; do |
| 195 | case "$1" in |
| 196 | -d|--img-dir) |
| 197 | shift |
| 198 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 199 | # skip the -xx option, in case |
| 200 | aoe_img_dir="$1" |
| 201 | shift |
| 202 | fi |
| 203 | ;; |
| 204 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 205 | USAGE >& 2 |
| 206 | exit 2 ;; |
| 207 | *) break ;; |
| 208 | esac |
| 209 | done |
| 210 | # |
| 211 | [ -z "$aoe_img_dir" ] && aoe_img_dir="$sanboot_img_dump_dir" |
| 212 | |
| 213 | # |
| 214 | ask_and_load_lang_set |
| 215 | |
| 216 | # |
| 217 | case $1 in |
| 218 | start) start;; |
| 219 | stop) stop;; |
| 220 | restart) restart;; |
| 221 | status) status;; |
| 222 | *) echo "Usage: $0 {start|stop|restart|status}" |
| 223 | exit 1 |
| 224 | esac |