| 1 | #!/bin/bashscip-shell filesystem . . `/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 | [scip-shell system . . `[`. -e /etc/drbl/drbl-ocs.confscip-shell filesystem . . `/etc/drbl/drbl-ocs.conf`. ] && . /etc/drbl/drbl-ocs.confscip-shell source . . `/etc/drbl/drbl-ocs.conf`. |
| 10 | [scip-shell system . . `[`. -e $DRBL_SCRIPT_PATH/sbin/ocs-functions ] && . $DRBL_SCRIPT_PATH/sbin/ocs-functions |
| 11 | |
| 12 | # |
| 13 | prog="$(basenamescip-shell system . . basename. $0)" |
| 14 | # |
| 15 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 16 | echoscip-shell system . . echo. "Usage: $prog [OPTION] disk_device image_name" |
| 17 | language_help_prompt_by_idx_no |
| 18 | echoscip-shell system . . echo. "-x, --interactive Interactive mode to input parameters." |
| 19 | echoscip-shell system . . echo. "Ex:" |
| 20 | echoscip-shell system . . echo. "To dump /dev/hda as an image with name prefix winaoe:" |
| 21 | echoscip-shell system . . echo. " $prog /dev/hda winaoe" |
| 22 | echoscip-shell system . . echo. "To use interactive mode, run:" |
| 23 | echoscip-shell system . . echo. " $prog -x" |
| 24 | } |
| 25 | # |
| 26 | check_if_digitsscip-shell shell shell-project 0.0.0 check_if_digits().() { |
| 27 | local var_="$1" |
| 28 | local prompt_="$2" |
| 29 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. ${var_} | grepscip-shell system . . grep. -iE "[^[:digit:]]")" ]; then |
| 30 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 31 | echoscip-shell system . . echo. "${prompt_} \"${var_}\"" |
| 32 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 33 | echoscip-shell system . . echo. "$msg_program_stop" |
| 34 | exit 1 |
| 35 | fi |
| 36 | } |
| 37 | |
| 38 | ############## |
| 39 | #### main #### |
| 40 | ############## |
| 41 | # |
| 42 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 43 | case "$1" in |
| 44 | -l|--language) |
| 45 | shift |
| 46 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 56 | USAGEscip-shell shell shell-project 0.0.0 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 | [scip-shell system . . `[`. -e /etc/init.d/mkswapfilescip-shell filesystem . . `/etc/init.d/mkswapfile`. ] && /etc/init.d/mkswapfilescip-shell filesystem . . `/etc/init.d/mkswapfile`. stop |
| 73 | |
| 74 | # |
| 75 | if [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. "$#" -ne 2 ]; then |
| 90 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 91 | echoscip-shell system . . echo. "You must assign 2 parameters. No more, no less!!!" |
| 92 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 93 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 94 | echoscip-shell system . . 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 [scip-shell system . . `[`. -z "$var_tmp" ]; then |
| 103 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 104 | echoscip-shell system . . echo. "$i is nothing!" |
| 105 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 106 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 107 | echoscip-shell system . . echo. "$msg_program_stop" |
| 108 | exit 1 |
| 109 | fi |
| 110 | done |
| 111 | |
| 112 | # Check if the image name contains reserved characters |
| 113 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $img_name_prefix | grepscip-shell system . . grep. -iE "aoe-[[:digit:]]+-[[:digit:]]+")" ]; then |
| 114 | img_name_p1="$(echoscip-shell system . . echo. $img_name_prefix | sedscip-shell system . . sed. -e "s/aoe-[[:digit:]]*-[[:digit:]]*//g")" |
| 115 | img_name_p2="$(echoscip-shell system . . echo. $img_name_prefix | sedscip-shell system . . sed. -e "s/$img_name_p1//g")" |
| 116 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 117 | echoscip-shell system . . echo. "The input name contains reserved characters: $img_name_p2" |
| 118 | echoscip-shell system . . echo. "Please reassign the input name without $img_name_p2" |
| 119 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 120 | echoscip-shell system . . echo. "$msg_program_stop" |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | # |
| 125 | mkdirscip-shell system . . 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 | grepscip-shell system . . grep. -iE '^Units: sectors' | awkscip-shell system . . awk. -F'=' '{print $2}' | sedscip-shell system . . sed. -e 's/bytes//g' -e 's/[[:space:]]//g')" |
| 153 | check_if_digitsscip-shell shell shell-project 0.0.0 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 | grepscip-shell system . . grep. -A10000 -Ei "Device[[:space:]]*Boot" | grepscip-shell system . . grep. -vEi "Device[[:space:]]*Boot" | sedscip-shell system . . sed. -e "s/*//g" | awkscip-shell system . . awk. -F' ' '{print $3}' | sortscip-shell system . . sort. -rn | headscip-shell system . . head. -n 1)" |
| 156 | check_if_digitsscip-shell shell shell-project 0.0.0 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="$(echoscip-shell system . . 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 [scip-shell system . . `[`. -z "$var_tmp" ]; then |
| 163 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 164 | echoscip-shell system . . echo. "$i is nothing!" |
| 165 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 166 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 167 | echoscip-shell system . . echo. "$msg_program_stop" |
| 168 | exit 1 |
| 169 | fi |
| 170 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $var_tmp | grepscip-shell system . . grep. -iE '[^[:digit:]]')" ]; then |
| 171 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 172 | echoscip-shell system . . echo. "$i not digits!" |
| 173 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 174 | echoscip-shell system . . 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 `seqscip-shell system . . seq. 0 $aoe_shelf_max`; do |
| 184 | for j in `seqscip-shell system . . seq. 0 $aoe_slot_max`; do |
| 185 | if [scip-shell system . . `[`. -z "$(unalias ls &>/dev/nullscip-shell filesystem . . `/dev/null`.; lsscip-shell system . . ls. -alFh $sanboot_img_dump_dir/*.aoe-$i-$j-for-* 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ]; then |
| 186 | shelf_no=$i |
| 187 | slot_no=$j |
| 188 | fi |
| 189 | [scip-shell system . . `[`. -n "$slot_no" ] && break |
| 190 | done |
| 191 | [scip-shell system . . `[`. -n "$shelf_no" ] && break |
| 192 | done |
| 193 | echoscip-shell system . . 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 [scip-shell system . . `[`. -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 | trscip-shell system . . tr. "[A-Z]" "[a-z]" | trscip-shell system . . tr. ":" "-")" |
| 200 | else |
| 201 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 202 | echoscip-shell system . . echo. "The ethernet port connected to DRBL server was NOT found!" |
| 203 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 204 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "Disk $device partition table:" |
| 210 | echoscip-shell system . . echo. ================================================ |
| 211 | LC_ALL=C fdisk -l $device |
| 212 | echoscip-shell system . . echo. ================================================ |
| 213 | echoscip-shell system . . echo. "Dumping $device (size: $disk_size) as image $sanboot_img_dump_dir/$aoe_img_name on server by:" |
| 214 | echoscip-shell system . . echo. dd if=$device of=$sanboot_img_dump_dir/${aoe_img_name} bs=$bs count=$count |
| 215 | echoscip-shell system . . echo. "$msg_this_might_take_several_minutes... " |
| 216 | trigger_dd_status_report "$device" "$disk_size" & |
| 217 | dd_report_sig_pid=$! |
| 218 | start_time="$(datescip-shell system . . date. +%s%N)" |
| 219 | ddscip-shell system . . dd. if=$device of=$sanboot_img_dump_dir/${aoe_img_name} bs=$bs count=$count |
| 220 | end_time="$(datescip-shell system . . date. +%s%N)" |
| 221 | killscip-shell system . . kill. -9 $dd_report_sig_pid &>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 222 | echoscip-shell system . . echo. "done!" |