| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: Program to dump AoE image to 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] disk_device image_name" |
| 17 | language_help_prompt_by_idx_no |
| 18 | echo "-x, --interactive Interactive mode to input parameters." |
| 19 | echo "Ex:" |
| 20 | echo "To dump /dev/hda as an image with name prefix winaoe:" |
| 21 | echo " $prog /dev/hda winaoe" |
| 22 | echo "To use interactive mode, run:" |
| 23 | echo " $prog -x" |
| 24 | } |
| 25 | # |
| 26 | check_if_digits() { |
| 27 | local var_="$1" |
| 28 | local prompt_="$2" |
| 29 | if [ -n "$(echo ${var_} | grep -iE "[^[:digit:]]")" ]; then |
| 30 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 31 | echo "${prompt_} \"${var_}\"" |
| 32 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 33 | echo "$msg_program_stop" |
| 34 | exit 1 |
| 35 | fi |
| 36 | } |
| 37 | |
| 38 | ############## |
| 39 | #### main #### |
| 40 | ############## |
| 41 | # |
| 42 | while [ $# -gt 0 ]; do |
| 43 | case "$1" in |
| 44 | -l|--language) |
| 45 | shift |
| 46 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 47 | # skip the -xx option, in case |
| 48 | specified_lang="$1" |
| 49 | shift |
| 50 | fi |
| 51 | ;; |
| 52 | -x|--interactive) |
| 53 | run_mode="interactive" |
| 54 | shift;; |
| 55 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 56 | USAGE >& 2 |
| 57 | exit 2 ;; |
| 58 | *) break ;; |
| 59 | esac |
| 60 | done |
| 61 | # |
| 62 | device=$1 |
| 63 | img_name_prefix=$2 |
| 64 | |
| 65 | # |
| 66 | ask_and_load_lang_set $specified_lang |
| 67 | |
| 68 | # check DIA |
| 69 | check_DIA_set_ESC $DIA |
| 70 | |
| 71 | # Stop local mounting service |
| 72 | [ -e /etc/init.d/mkswapfile ] && /etc/init.d/mkswapfile stop |
| 73 | |
| 74 | # |
| 75 | if [ "$run_mode" = "interactive" ]; then |
| 76 | # Since we borrow the function from clonezilla, we'd better to change some title here: |
| 77 | msg_nchc_clonezilla_orig="$msg_nchc_clonezilla" |
| 78 | msg_nchc_clonezilla="DRBL" |
| 79 | # To get device |
| 80 | get_target_hd_name_from_local_machine "$msg_local_source_disk \n$msg_linux_disk_naming $msg_press_space_to_mark_selection" |
| 81 | device="/dev/$target_hd" |
| 82 | # To get SAN Boot image name |
| 83 | get_target_dir_name_when_saving # get $target_dir |
| 84 | img_name_prefix="$target_dir" |
| 85 | # Return to original one. |
| 86 | msg_nchc_clonezilla="$msg_nchc_clonezilla_orig" |
| 87 | else |
| 88 | # In old version we use 3 parameters, now only 2. We have to show warning message if mornot 2. |
| 89 | if [ "$#" -ne 2 ]; then |
| 90 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 91 | echo "You must assign 2 parameters. No more, no less!!!" |
| 92 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 93 | USAGE |
| 94 | echo "$msg_program_stop" |
| 95 | exit 1 |
| 96 | fi |
| 97 | fi |
| 98 | |
| 99 | # |
| 100 | for i in device img_name_prefix; do |
| 101 | eval var_tmp=\$$i |
| 102 | if [ -z "$var_tmp" ]; then |
| 103 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 104 | echo "$i is nothing!" |
| 105 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 106 | USAGE |
| 107 | echo "$msg_program_stop" |
| 108 | exit 1 |
| 109 | fi |
| 110 | done |
| 111 | |
| 112 | # Check if the image name contains reserved characters |
| 113 | if [ -n "$(echo $img_name_prefix | grep -iE "aoe-[[:digit:]]+-[[:digit:]]+")" ]; then |
| 114 | img_name_p1="$(echo $img_name_prefix | sed -e "s/aoe-[[:digit:]]*-[[:digit:]]*//g")" |
| 115 | img_name_p2="$(echo $img_name_prefix | sed -e "s/$img_name_p1//g")" |
| 116 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 117 | echo "The input name contains reserved characters: $img_name_p2" |
| 118 | echo "Please reassign the input name without $img_name_p2" |
| 119 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 120 | echo "$msg_program_stop" |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | # |
| 125 | mkdir -p $sanboot_img_dump_dir |
| 126 | |
| 127 | # Find the size of disk we want to dump |
| 128 | # Ref: http://www.etherboot.org/wiki/sanboot/transfer |
| 129 | # Disk /dev/sda: 160.0 GB, 160041885696 bytes |
| 130 | # 255 heads, 63 sectors/track, 19457 cylinders |
| 131 | # Units = cylinders of 16065 * 512 = 8225280 bytes |
| 132 | # |
| 133 | # Device Boot Start End Blocks Id System |
| 134 | # /dev/sda1 * 1 11519 92526336 7 HPFS/NTFS |
| 135 | # /dev/sda2 11520 12735 9767520 83 Linux |
| 136 | # /dev/sda3 12736 12978 1951897+ 82 Linux swap / Solaris |
| 137 | # /dev/sda4 12979 19457 52042567+ 5 Extended |
| 138 | # /dev/sda5 12979 19457 52042536 83 Linux |
| 139 | |
| 140 | # Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors |
| 141 | # Units: sectors of 1 * 512 = 512 bytes |
| 142 | # Sector size (logical/physical): 512 bytes / 512 bytes |
| 143 | # I/O size (minimum/optimal): 512 bytes / 512 bytes |
| 144 | # Disklabel type: dos |
| 145 | # Disk identifier: 0xa5b75bb7 |
| 146 | # |
| 147 | # Device Boot Start End Sectors Size Id Type |
| 148 | # /dev/sda1 * 2048 206847 204800 100M 7 HPFS/NTFS/exFAT |
| 149 | # /dev/sda2 206848 16775167 16568320 7.9G 7 HPFS/NTFS/exFAT |
| 150 | |
| 151 | # bs in the above example is 512. |
| 152 | bs="$(LC_ALL=C fdisk -l $device | grep -iE '^Units: sectors' | awk -F'=' '{print $2}' | sed -e 's/bytes//g' -e 's/[[:space:]]//g')" |
| 153 | check_if_digits "$bs" "Oops... The cylinder size was not found correctly! The one we found is" |
| 154 | # Find the largest cylinder in the "End" column. By doing this, we can dump multiple partitions. In the above example, the count will be 16775167. //NOTE// This is not the end of disk, instead it's the end of the last partition |
| 155 | count="$(LC_ALL=C fdisk -l $device | grep -A10000 -Ei "Device[[:space:]]*Boot" | grep -vEi "Device[[:space:]]*Boot" | sed -e "s/*//g" | awk -F' ' '{print $3}' | sort -rn | head -n 1)" |
| 156 | check_if_digits "$count" "Oops... A correct number of cylinders was not found! The one we found is" |
| 157 | # The disk_size is not the physical disk size of $device. Instead it's for the MBR and all the partitions, not including the unused space after the last partition. |
| 158 | disk_size="$(echo "scale=2; $count * $bs /1000/1000/1000" | bc -l) GB" # unit: GB |
| 159 | |
| 160 | for i in bs count; do |
| 161 | eval var_tmp=\$$i |
| 162 | if [ -z "$var_tmp" ]; then |
| 163 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 164 | echo "$i is nothing!" |
| 165 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 166 | USAGE |
| 167 | echo "$msg_program_stop" |
| 168 | exit 1 |
| 169 | fi |
| 170 | if [ -n "$(echo $var_tmp | grep -iE '[^[:digit:]]')" ]; then |
| 171 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 172 | echo "$i not digits!" |
| 173 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 174 | echo "$msg_program_stop" |
| 175 | exit 1 |
| 176 | fi |
| 177 | done |
| 178 | |
| 179 | # Assign vbladed <shelf> <slot> to image name. Find a free one |
| 180 | # Here we will set the file name as IMAGENMAE.aoe-<shelf>-<slot>, e.g. image1.aoe-1-1 |
| 181 | shelf_no="" |
| 182 | slot_no="" |
| 183 | for i in `seq 0 $aoe_shelf_max`; do |
| 184 | for j in `seq 0 $aoe_slot_max`; do |
| 185 | if [ -z "$(unalias ls &>/dev/null; ls -alFh $sanboot_img_dump_dir/*.aoe-$i-$j-for-* 2>/dev/null)" ]; then |
| 186 | shelf_no=$i |
| 187 | slot_no=$j |
| 188 | fi |
| 189 | [ -n "$slot_no" ] && break |
| 190 | done |
| 191 | [ -n "$shelf_no" ] && break |
| 192 | done |
| 193 | echo "Available AoE shelf #: $shelf_no, slot #:$slot_no" |
| 194 | |
| 195 | # MAC address of the ethx linked to drbl server |
| 196 | ethx_link_2_drbl="$(get-port-link-2-drbl-srv)" |
| 197 | if [ -n "$ethx_link_2_drbl" ]; then |
| 198 | # To avoid strange character ":" in the file name, we use "-" |
| 199 | mac_add="$(drbl-get-macadd $ethx_link_2_drbl | tr "[A-Z]" "[a-z]" | tr ":" "-")" |
| 200 | else |
| 201 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 202 | echo "The ethernet port connected to DRBL server was NOT found!" |
| 203 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 204 | echo "$msg_program_stop" |
| 205 | exit 1 |
| 206 | fi |
| 207 | aoe_img_name="${img_name_prefix}.aoe-${shelf_no}-${slot_no}-for-${mac_add}" |
| 208 | # |
| 209 | echo "Disk $device partition table:" |
| 210 | echo ================================================ |
| 211 | LC_ALL=C fdisk -l $device |
| 212 | echo ================================================ |
| 213 | echo "Dumping $device (size: $disk_size) as image $sanboot_img_dump_dir/$aoe_img_name on server by:" |
| 214 | echo dd if=$device of=$sanboot_img_dump_dir/${aoe_img_name} bs=$bs count=$count |
| 215 | echo "$msg_this_might_take_several_minutes... " |
| 216 | trigger_dd_status_report "$device" "$disk_size" & |
| 217 | dd_report_sig_pid=$! |
| 218 | start_time="$(date +%s%N)" |
| 219 | dd if=$device of=$sanboot_img_dump_dir/${aoe_img_name} bs=$bs count=$count |
| 220 | end_time="$(date +%s%N)" |
| 221 | kill -9 $dd_report_sig_pid &>/dev/null |
| 222 | echo "done!" |