| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | ###### |
| 4 | # Author: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ clonezilla org> |
| 5 | # License: GPL |
| 6 | # Description: Program to make USB flash drive (FAT, EXT, BTRFS , XFS or NTFS) bootable by syslinux or extlinux |
| 7 | # Main steps: |
| 8 | # 1. Checking if partition table correct (on boundary, bootable) |
| 9 | # 2. cat mbr |
| 10 | # 3. Run "syslinux -f -i" or "extlinux -i" |
| 11 | # |
| 12 | # Update history: |
| 13 | # 2016/3/7 by Ceasar |
| 14 | # 1. Fix: use "findmnt" to get mount path |
| 15 | # 2. Fix: consider mount path with "space" character ex: "/media/user/USB\ DISK" |
| 16 | # 2016/8/27 by David Tonhofer |
| 17 | # 1. Rewrite some codes to reuse. |
| 18 | # 2. Add comments. |
| 19 | ###### |
| 20 | |
| 21 | set -u |
| 22 | |
| 23 | # Append PATH |
| 24 | export PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH |
| 25 | |
| 26 | # Settings |
| 27 | ocs_batch_mode="false" |
| 28 | |
| 29 | # |
| 30 | prog="$(basenamescip-shell system . . basename. $0)" |
| 31 | path_of_prog="$(LC_ALL=C cd "$(dirnamescip-shell system . . dirname. "$0")/../../"; pwdscip-shell system . . pwd.)" |
| 32 | |
| 33 | # Check if terminal supports colors output |
| 34 | colors_no="$(LC_ALL=C tputscip-shell system . . tput. colors 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" |
| 35 | |
| 36 | BOOTUP="" |
| 37 | if [scip-shell system . . `[`. -n "$colors_no" ]; then |
| 38 | if [scip-shell system . . `[`. "$colors_no" -ge 8 ]; then |
| 39 | [scip-shell system . . `[`. -z ${SETCOLOR_SUCCESS:-''} ] && SETCOLOR_SUCCESS="echo -en \\033[1;32m" |
| 40 | [scip-shell system . . `[`. -z ${SETCOLOR_FAILURE:-''} ] && SETCOLOR_FAILURE="echo -en \\033[1;31m" |
| 41 | [scip-shell system . . `[`. -z ${SETCOLOR_WARNING:-''} ] && SETCOLOR_WARNING="echo -en \\033[1;33m" |
| 42 | [scip-shell system . . `[`. -z ${SETCOLOR_NORMAL:-''} ] && SETCOLOR_NORMAL="echo -en \\033[0;39m" |
| 43 | BOOTUP="color" |
| 44 | fi |
| 45 | fi |
| 46 | |
| 47 | # |
| 48 | msg_are_u_sure_u_want_to_continue='Are you sure you want to continue?' |
| 49 | msg_you_have_to_enter_yes_or_no="You have to enter 'y', 'yes', 'n' or 'no'. Please enter it again!" |
| 50 | msg_do_you_want_to_make_it_bootable="Do you want to mark it as bootable ?" |
| 51 | |
| 52 | # |
| 53 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 54 | echoscip-shell system . . echo. "$prog - To make the device bootable with syslinux/extlinux" |
| 55 | echoscip-shell system . . echo. "Usage: $prog [OPTION] partition_device_holding_clonezilla_filetree" |
| 56 | echoscip-shell system . . echo. "Options:" |
| 57 | echoscip-shell system . . echo. "-b, --batch-mode batch, unattended mode. Use carefully! DANGEROUS!!!" |
| 58 | echoscip-shell system . . echo. "-L, --LABEL STRING set device via LABEL if LABEL exists" |
| 59 | echoscip-shell system . . echo. "-U, --UUID STRING set device via UUID" |
| 60 | echoscip-shell system . . echo. "This script is assumed to ACTUALLY RESIDE IN THE CORRECT PLACE OF THE CLONEZILLA FILE TREE!" |
| 61 | echoscip-shell system . . echo. "The path to it is used to find other files!" |
| 62 | echoscip-shell system . . echo. "Note:" |
| 63 | echoscip-shell system . . echo. " Device assignment priority: Partition name > UUID > LABEL (if not only one NAME-TYPE is assigned)" |
| 64 | echoscip-shell system . . echo. "Ex:" |
| 65 | echoscip-shell system . . echo. "To make \"/dev/sdg1\" bootable on GNU/Linux:" |
| 66 | echoscip-shell system . . echo. " $prog /dev/sdg1" |
| 67 | echoscip-shell system . . echo. "To make device label \"LABEL_STRING\" bootable on GNU/Linux:" |
| 68 | echoscip-shell system . . echo. " $prog -L LABEL_STRING" |
| 69 | echoscip-shell system . . echo. "To make device with UUID \"UUID_STRING\" bootable on GNU/Linux:" |
| 70 | echoscip-shell system . . echo. " $prog -U UUID_STRING" |
| 71 | } |
| 72 | # Check if root or not |
| 73 | check_if_rootscip-shell shell shell-project 0.0.0 check_if_root().() { |
| 74 | if [scip-shell system . . `[`. ! "$UID" = "0" ]; then |
| 75 | echoscip-shell system . . echo. |
| 76 | echoscip-shell system . . echo. "[$LOGNAME] You need to run this script \"`basenamescip-shell system . . basename. $0`\" as root." |
| 77 | echoscip-shell system . . echo. |
| 78 | exit 1 |
| 79 | fi |
| 80 | } |
| 81 | # |
| 82 | to_continue_or_notscip-shell shell shell-project 0.0.0 to_continue_or_not().() { |
| 83 | local prompt_msg="$1" |
| 84 | continue_choice="" |
| 85 | while [scip-shell system . . `[`. -z "$continue_choice" ]; do |
| 86 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 87 | echoscip-shell system . . echo. "$prompt_msg" |
| 88 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 89 | echoscip-shell system . . echo. -n "[y/n] " |
| 90 | read continue_choice |
| 91 | case "$continue_choice" in |
| 92 | y|Y|[yY][eE][sS]) |
| 93 | echoscip-shell system . . echo. "OK! Let's do it!" ;; |
| 94 | n|N|[nN][oO]) |
| 95 | echoscip-shell system . . echo. "Program terminated!" |
| 96 | exit 1 |
| 97 | ;; |
| 98 | *) |
| 99 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 100 | echoscip-shell system . . echo. "$msg_you_have_to_enter_yes_or_no!" |
| 101 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 102 | echoscip-shell system . . echo. "--------------------------------------------" |
| 103 | ;; |
| 104 | esac |
| 105 | done |
| 106 | } # end of to_continue_or_not |
| 107 | # |
| 108 | get_disknamescip-shell shell shell-project 0.0.0 get_diskname().() { |
| 109 | local disk=${1#/dev/*} |
| 110 | |
| 111 | if [scip-shell system . . `[`. -n "$disk" ]; then |
| 112 | echoscip-shell system . . echo. "$disk" | sedscip-shell system . . sed. -e 's/^\([^0-9]*\)[0-9]*$/\1/g' \ |
| 113 | -e 's/^\(.*[0-9]\{1,\}\)p[0-9]\{1,\}$/\1/g' |
| 114 | fi |
| 115 | } # end of get_diskname |
| 116 | # |
| 117 | get_part_numberscip-shell shell shell-project 0.0.0 get_part_number().() { |
| 118 | local disk=${1#/dev/*} |
| 119 | local num="" |
| 120 | |
| 121 | if [scip-shell system . . `[`. -n "$disk" ]; then |
| 122 | num=$(echoscip-shell system . . echo. "$disk" | sedscip-shell system . . sed. -e 's/^[^0-9]*\([0-9]*\)$/\1/g' \ |
| 123 | -e 's/^.*[0-9]\{1,\}\(p[0-9]\{1,\}\)$/\1/g') |
| 124 | fi |
| 125 | |
| 126 | echoscip-shell system . . echo. $num |
| 127 | } # end of get_part_number |
| 128 | # |
| 129 | check_if_syslinux_runsscip-shell shell shell-project 0.0.0 check_if_syslinux_runs().() { |
| 130 | local sys_exec="$1" |
| 131 | local lrc |
| 132 | # Check if the libc6-i386 (debian/ubuntu) files exists, which is required |
| 133 | # for running 32-bit syslinux |
| 134 | # E.g. On Debian AMD64 system without installing libc6-i386, |
| 135 | # $ ldd syslinux |
| 136 | # not a dynamic executable |
| 137 | # If libc6-i386 is installed: |
| 138 | # $ ldd syslinux |
| 139 | # linux-gate.so.1 => (0xf77dd000) |
| 140 | # libc.so.6 => /lib32/libc.so.6 (0xf7661000) |
| 141 | # /lib/ld-linux.so.2 (0xf77e0000) |
| 142 | # It's easier we use "syslinux -h" to test if it runs: |
| 143 | # $ ls -alFh ./syslinux |
| 144 | # -rwxrwxr-x 1 steven steven 209K Oct 7 00:32 ./syslinux* |
| 145 | # $ ./syslinux -h |
| 146 | # bash: ./syslinux: No such file or directory |
| 147 | # $ echo $? |
| 148 | # 127 |
| 149 | # The above is the case which libc6-i386 is not installed, and the |
| 150 | # return code is 127. |
| 151 | if [scip-shell system . . `[`. -z "$sys_exec" ]; then |
| 152 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 153 | echoscip-shell system . . echo. "Variable sys_exec was not assigned!" |
| 154 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 155 | echoscip-shell system . . echo. "Program terminated!" |
| 156 | exit 1 |
| 157 | fi |
| 158 | if [scip-shell system . . `[`. ! -e "$sys_exec" ]; then |
| 159 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 160 | echoscip-shell system . . echo. "File \"$sys_exec\" does not exist!" |
| 161 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 162 | echoscip-shell system . . echo. "Program terminated!" |
| 163 | exit 1 |
| 164 | fi |
| 165 | $sys_exec -h 2>/dev/nullscip-shell filesystem . . `/dev/null`. >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 166 | lrc=$? |
| 167 | if [scip-shell system . . `[`. "$lrc" -ne 0 ]; then |
| 168 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 169 | echoscip-shell system . . echo. "File to run $sys_exec." |
| 170 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 171 | echoscip-shell system . . echo. "Program terminated!" |
| 172 | exit 1 |
| 173 | fi |
| 174 | return $lrc |
| 175 | } # end of check_if_syslinux_runs |
| 176 | # |
| 177 | is_partitionscip-shell shell shell-project 0.0.0 is_partition().() { |
| 178 | # //NOTE// If new RE* is added, remember to check the function check_if_disk_busy_before_create_partition and get_not_busy_disks_or_parts have to be modified, too. |
| 179 | # IDE and SCSI disk partition: /dev/hda1, /dev/hdb1, /dev/sda1, /dev/sdb1... |
| 180 | # KVM virtual disk partition: /dev/vda1, /dev/vdb1... |
| 181 | # Xen virtual disk partition: /dev/xvda1, /dev/xvdb1, /dev/xdvc1... |
| 182 | # CCISS RAID disk partition: /dev/cciss/c0d0p1, /dev/cciss/c0d1p1... |
| 183 | # SD card: /dev/mmcblk0p1, /dev/mmcblk0p2, /dev/mmcblk0p3... |
| 184 | # NBD device: /dev/nbd0p1, /dev/nbd0p2... |
| 185 | # NVME device: /dev/nvme0n1p1, /dev/nvme0n1p2, /dev/nvme1n1p1, /dev/nvme1n1p2 |
| 186 | # FakeRAID: with nodmraid boot parameter, /dev/md126p1, /dev/md126p2... |
| 187 | # Mylex ExtremeRAID-2000 SCSI RAID controller: /dev/rd/c0d0p1, /dev/rd/c0d1p1... |
| 188 | # Compaq Smart Array controller: /dev/ida/c0d0p1, /dev/ida/c0d1p2... |
| 189 | local dev_=${1#/dev/*} |
| 190 | local RE1='x?[hsv]d[a-z]+[0-9]+' |
| 191 | local RE2='i2o/hd[a-z]+[0-9]+' |
| 192 | local RE3='cciss/c[0-9]d[0-9]p[0-9]+' |
| 193 | local RE4='mmcblk[0-9]+p+[0-9]+' |
| 194 | local RE5='md[0-9]+p+[0-9]+' |
| 195 | local RE6='rd/c[0-9]d[0-9]p[0-9]+' |
| 196 | local RE7='ida/c[0-9]d[0-9]p[0-9]+' |
| 197 | local RE8='nvme[0-9]+n[0-9]+p+[0-9]+' |
| 198 | local RE9='nbd[0-9]+p+[0-9]+' |
| 199 | local rc=1 |
| 200 | local fs_ |
| 201 | |
| 202 | if [scip-shell system . . `[`. -z "$dev_" ]; then |
| 203 | return 1 |
| 204 | fi |
| 205 | echoscip-shell system . . echo. $dev_ | grepscip-shell system . . grep. -Eq "^($RE1|$RE2|$RE3|$RE4|$RE5|$RE6|$RE7|$RE8|$RE9)$" |
| 206 | rc=$? |
| 207 | |
| 208 | return $rc |
| 209 | } # end of is_partition |
| 210 | |
| 211 | # |
| 212 | export LANG=C |
| 213 | |
| 214 | target_part_label='' |
| 215 | target_part_uuid='' |
| 216 | |
| 217 | # |
| 218 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 219 | case "$1" in |
| 220 | -b|--batch) ocs_batch_mode="true"; shift;; |
| 221 | -L|--LABEL) shift |
| 222 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 223 | # only accept argument if it doesn't look like an option |
| 224 | target_part_label=$1 |
| 225 | shift; |
| 226 | fi |
| 227 | [scip-shell system . . `[`. -z "$target_part_label" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 228 | ;; |
| 229 | -U|--UUID) shift |
| 230 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 231 | # only accept argument if it doesn't look like an option |
| 232 | target_part_uuid=$1 |
| 233 | shift; |
| 234 | fi |
| 235 | [scip-shell system . . `[`. -z "$target_part_uuid" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 236 | ;; |
| 237 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 238 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 239 | exit 2 ;; |
| 240 | *) break ;; |
| 241 | esac |
| 242 | done |
| 243 | |
| 244 | # |
| 245 | check_if_rootscip-shell shell shell-project 0.0.0 check_if_root(). |
| 246 | if [scip-shell system . . `[`. "$#" -ne 1 ]; then |
| 247 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 248 | exit 1 |
| 249 | fi |
| 250 | target_part="$1" |
| 251 | |
| 252 | if ! type parted &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 253 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 254 | echoscip-shell system . . echo. 'Program "parted" was not found on this GNU/Linux system. Please install it.' |
| 255 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 256 | echoscip-shell system . . echo. "Program terminated!" |
| 257 | exit 1 |
| 258 | fi |
| 259 | if ! type blkid &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 260 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 261 | echoscip-shell system . . echo. 'Program "blkid" was not found on this GNU/Linux system. Please install it.' |
| 262 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 263 | echoscip-shell system . . echo. "Program terminated!" |
| 264 | exit 1 |
| 265 | fi |
| 266 | if ! type mcopy &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 267 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 268 | echoscip-shell system . . echo. 'Program "mcopy" was not found on this GNU/Linux system. Please install it. On Debian or Ubuntu Linux, you can install it by: sudo apt-get install mtools' |
| 269 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 270 | echoscip-shell system . . echo. "Program terminated!" |
| 271 | exit 1 |
| 272 | fi |
| 273 | |
| 274 | if [scip-shell system . . `[`. -n "$target_part_label" -a -z "$target_part" ] ; then |
| 275 | target_part_via_label="$(LC_ALL=C blkidscip-shell system . . blkid. | grepscip-shell system . . grep. -iE "^/dev/[hsuv][bd][a-z]+[[:digit:]]+.+LABEL=\"$target_part_label\".*" | grepscip-shell system . . grep. -oE -w '^/dev/[hsuv][bd][a-z]+[[:digit:]]+'| headscip-shell system . . head. -n 1)" |
| 276 | fi |
| 277 | |
| 278 | if [scip-shell system . . `[`. -n "$target_part_uuid" -a -z "$target_part" ] ; then |
| 279 | target_part_via_uuid="$(LC_ALL=C blkidscip-shell system . . blkid. | grepscip-shell system . . grep. -iE "^/dev/[hsuv][bd][a-z]+[[:digit:]]+.+UUID=\"$target_part_uuid\".*" | grepscip-shell system . . grep. -oE -w '^/dev/[hsuv][bd][a-z]+[[:digit:]]+'| headscip-shell system . . head. -n 1)" |
| 280 | fi |
| 281 | |
| 282 | if [scip-shell system . . `[`. -z "$target_part" ] ; then |
| 283 | [scip-shell system . . `[`. -z "$target_part_via_label" -a -n "$target_part_via_uuid" ] && target_part=$target_part_via_uuid |
| 284 | [scip-shell system . . `[`. -n "$target_part_via_label" -a -z "$target_part_via_uuid" ] && target_part=$target_part_via_label |
| 285 | [scip-shell system . . `[`. -n "$target_part_via_label" -a -n "$target_part_via_uuid" -a "$target_part_via_label" != "$target_part_via_uuid" ] && target_part=$target_part_via_uuid |
| 286 | fi |
| 287 | |
| 288 | # |
| 289 | if [scip-shell system . . `[`. -z "$target_part" ]; then |
| 290 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 291 | echoscip-shell system . . echo. "No target partition could be determined!" |
| 292 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 293 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 294 | exit 1 |
| 295 | fi |
| 296 | # Make sure target_part is partition device name, not disk device name |
| 297 | if ! is_partitionscip-shell shell shell-project 0.0.0 is_partition(). $target_part ; then |
| 298 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 299 | echoscip-shell system . . echo. "\"$target_part\" is NOT a valid partition name!" |
| 300 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 301 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 302 | exit 1 |
| 303 | fi |
| 304 | |
| 305 | # Get the running OS arch |
| 306 | os_arch="$(LC_ALL=C unamescip-shell system . . uname. -m)" |
| 307 | case "$os_arch" in |
| 308 | x86_64) arch_d="x64" ;; |
| 309 | i[3456]86) arch_d="x86" ;; |
| 310 | *) arch_d="" ;; |
| 311 | esac |
| 312 | if [scip-shell system . . `[`. -z "$arch_d" ]; then |
| 313 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 314 | echoscip-shell system . . echo. "Not supported OS. This running OS is \"$os_arch\", not x86 architecture." |
| 315 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 316 | echoscip-shell system . . echo. "Program terminated!" |
| 317 | exit 1 |
| 318 | fi |
| 319 | # |
| 320 | pt_dev="$(basenamescip-shell system . . basename. $target_part)" # e.g. sdc1 |
| 321 | hd_dev="$(get_disknamescip-shell shell shell-project 0.0.0 get_diskname(). $target_part)" # e.g. sdc |
| 322 | target_disk="/dev/$hd_dev" # e.g. /dev/sdc |
| 323 | pt_dev_no="$(get_part_numberscip-shell shell shell-project 0.0.0 get_part_number(). $target_part)" # e.g. 1 |
| 324 | |
| 325 | # If the destination disk is not MBR partition table (e.g. it's GPT), exit. This program only works for MBR disk. |
| 326 | if [scip-shell system . . `[`. -z "$(LC_ALL=C parted -s $target_disk print | grepscip-shell system . . grep. -iE "^Partition Table:" | grepscip-shell system . . grep. -iE "msdos")" ]; then |
| 327 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 328 | echoscip-shell system . . echo. "The partition table of $target_disk is not for MBR (Master Boot Record). Its layout is:" |
| 329 | LC_ALL=C parted -s $target_disk print |
| 330 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 331 | echoscip-shell system . . echo. "This program is for making a bootable disk with MBR partition table." |
| 332 | echoscip-shell system . . echo. "For GPT disk, there is no need to run this program. Just make sure the partition is FAT32 with ID=ef00 and all the files are copied on that. That's all." |
| 333 | echoscip-shell system . . echo. "Program terminated!" |
| 334 | exit 1 |
| 335 | fi |
| 336 | |
| 337 | # Get machine info: |
| 338 | on_this_machine="" |
| 339 | dev_model_shown="" |
| 340 | if type dmidecode &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 341 | machine_name="$(LANG=C dmidecode -s system-product-name 2>/dev/nullscip-shell filesystem . . `/dev/null`. | headscip-shell system . . head. -n 1)" |
| 342 | if [scip-shell system . . `[`. -z "$machine_name" -o "$machine_name" = "To Be Filled By O.E.M." ]; then |
| 343 | dev_model_shown="Unknown product name" |
| 344 | else |
| 345 | dev_model_shown="$machine_name" |
| 346 | fi |
| 347 | on_this_machine="on this machine \"$dev_model_shown\"" |
| 348 | fi |
| 349 | |
| 350 | # |
| 351 | if ! grepscip-shell system . . grep. -qEw $pt_dev /proc/partitionsscip-shell filesystem . . `/proc/partitions`.; then |
| 352 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 353 | echoscip-shell system . . echo. "$target_part was NOT found!" |
| 354 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 355 | echoscip-shell system . . echo. "Available disk(s) and partition(s) $on_this_machine :" |
| 356 | echoscip-shell system . . echo. "--------------------------------------------" |
| 357 | catscip-shell system . . cat. /proc/partitionsscip-shell filesystem . . `/proc/partitions`. |
| 358 | echoscip-shell system . . echo. "--------------------------------------------" |
| 359 | echoscip-shell system . . echo. "Program terminated!" |
| 360 | exit 1 |
| 361 | fi |
| 362 | |
| 363 | # |
| 364 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 365 | echoscip-shell system . . echo. "This command will install MBR and syslinux/extlinux bootloader on $target_disk" |
| 366 | echoscip-shell system . . echo. "Clonezilla files are assumed to be on $target_part" |
| 367 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 368 | echoscip-shell system . . echo. "--------------------------------------------" |
| 369 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 370 | echoscip-shell system . . echo. "Machine: $dev_model_shown:" |
| 371 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 372 | parted -s $target_disk print |
| 373 | echoscip-shell system . . echo. "--------------------------------------------" |
| 374 | |
| 375 | if [scip-shell system . . `[`. "$ocs_batch_mode" = "false" ]; then |
| 376 | to_continue_or_notscip-shell shell shell-project 0.0.0 to_continue_or_not(). "$msg_are_u_sure_u_want_to_continue" |
| 377 | echoscip-shell system . . echo. "--------------------------------------------" |
| 378 | fi |
| 379 | |
| 380 | # 0. Check if partition is a FAT partition or NTFS partition |
| 381 | # parted -s /dev/hda1 print |
| 382 | # Disk /dev/hda1: 8590MB |
| 383 | # Sector size (logical/physical): 512B/512B |
| 384 | # Partition Table: loop |
| 385 | # |
| 386 | # Number Start End Size File system Flags |
| 387 | # 1 0.00kB 8590MB 8590MB fat32 |
| 388 | # part_fs="$(LC_ALL=C parted -s $target_disk print | grep -E "^[[:space:]]*${pt_dev_no}\>" | awk -F" " '{print $6}')" |
| 389 | blkinfo="$(mktempscip-shell system . . mktemp. /tmp/blkinfo.XXXXXXscip-shell filesystem . . `/tmp/blkinfo.XXXXXX`.)" |
| 390 | LC_ALL=C blkidscip-shell system . . blkid. -c /dev/nullscip-shell filesystem . . `/dev/null`. $target_part | grepscip-shell system . . grep. -o -E '\<TYPE="[^[:space:]]*"($|[[:space:]]+)' > $blkinfo |
| 391 | TYPE="" |
| 392 | . $blkinfo |
| 393 | |
| 394 | echoscip-shell system . . echo. "File system of $target_part: $TYPE" |
| 395 | case "$TYPE" in |
| 396 | fat16|fat32|vfat) mode="syslinux";; |
| 397 | ntfs|ext[2-4]|btrfs|xfs|ufs|ffs) mode="extlinux";; |
| 398 | *) |
| 399 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 400 | echoscip-shell system . . echo. "$target_part: this doesn't look like a valid FAT, NTFS, ext2/3/4 or btrfs file system." |
| 401 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 402 | echoscip-shell system . . echo. "Program terminated!" |
| 403 | exit 1 |
| 404 | ;; |
| 405 | esac |
| 406 | |
| 407 | echoscip-shell system . . echo. "--------------------------------------------" |
| 408 | # 1. Check if partition start/end on cylinder boundary |
| 409 | # //NOTE// This is not really required. Comment it on Sep/21/2010. |
| 410 | #if [ -n "$(LANG=C fdisk -l $target_disk | grep -iE "(not start on cylinder boundary|not end on cylinder boundary)")" ]; then |
| 411 | # [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 412 | # echo "Some partition does not start or end on cylinder boundary! This disk will not be able to boot via syslinux!" |
| 413 | # [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 414 | # echo "Program terminated!" |
| 415 | # exit 1 |
| 416 | #fi |
| 417 | |
| 418 | # 2. Bootable ? |
| 419 | bootable="$(LANG=C fdisk -l $target_disk | grepscip-shell system . . grep. -Ew "^$target_part" | awkscip-shell system . . awk. -F" " '{print $2}')" |
| 420 | if [scip-shell system . . `[`. "$bootable" != "*" ]; then |
| 421 | echoscip-shell system . . echo. "$pt_dev is not marked as bootable! The partition table of $target_disk:" |
| 422 | echoscip-shell system . . echo. "--------------------------------------------" |
| 423 | echoscip-shell system . . echo. $dev_model_shown: |
| 424 | parted -s $target_disk print |
| 425 | echoscip-shell system . . echo. "--------------------------------------------" |
| 426 | if [scip-shell system . . `[`. "$ocs_batch_mode" = "false" ]; then |
| 427 | to_continue_or_notscip-shell shell shell-project 0.0.0 to_continue_or_not(). "$msg_do_you_want_to_make_it_bootable" |
| 428 | fi |
| 429 | echoscip-shell system . . echo. "Running: parted -s $target_disk set $pt_dev_no boot on" |
| 430 | parted -s $target_disk set $pt_dev_no boot on |
| 431 | echoscip-shell system . . echo. "--------------------------------------------" |
| 432 | fi |
| 433 | |
| 434 | # 3. Install MBR |
| 435 | if [scip-shell system . . `[`. "$ocs_batch_mode" = "false" ]; then |
| 436 | to_continue_or_notscip-shell shell shell-project 0.0.0 to_continue_or_not(). "Do you want to install MBR on $target_disk $on_this_machine ?" |
| 437 | fi |
| 438 | echoscip-shell system . . echo. "Running: cat \"$path_of_prog/utils/mbr/mbr.bin\" > $target_disk" |
| 439 | catscip-shell system . . cat. "$path_of_prog/utils/mbr/mbr.bin" > $target_disk |
| 440 | if [scip-shell system . . `[`. "$?" -ne 0 ]; then |
| 441 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 442 | echoscip-shell system . . echo. "Failed to write bootloader into MBR of $target_disk" |
| 443 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 444 | echoscip-shell system . . echo. "Program terminated!" |
| 445 | exit 1 |
| 446 | fi |
| 447 | |
| 448 | echoscip-shell system . . echo. "--------------------------------------------" |
| 449 | # 4. |
| 450 | if [scip-shell system . . `[`. "$ocs_batch_mode" = "false" ]; then |
| 451 | # The bootloader has already been installed, now we need to write the syslinux/extlinux |
| 452 | # boot files to the partition (is it necessary to ask the user again?) |
| 453 | to_continue_or_notscip-shell shell shell-project 0.0.0 to_continue_or_not(). "Do you want to install the $mode boot files on $target_part $on_this_machine ?" |
| 454 | fi |
| 455 | # Since most of the cases when makeboot.sh is run, all the files are in FAT (USB flash drive normally uses FAT), we have to make syslinux executable. |
| 456 | # |
| 457 | # Check if $target_part is mounted or not |
| 458 | mnt_pnt="$(LC_ALL=C findmntscip-shell system . . findmnt. -nr -o target -S $target_part )" |
| 459 | flag_mount="" |
| 460 | if [scip-shell system . . `[`. -z "$mnt_pnt" ]; then |
| 461 | # Not mounted. Mount it. |
| 462 | destfs_tmpd="$(mktempscip-shell system . . mktemp. -d /tmp/destfs_tmpd.XXXXXXscip-shell filesystem . . `/tmp/destfs_tmpd.XXXXXX`.)" |
| 463 | mountscip-shell system . . mount. $target_part $destfs_tmpd |
| 464 | rc=$? |
| 465 | flag_mount="mounted" |
| 466 | else |
| 467 | # Already mounted. |
| 468 | destfs_tmpd=$mnt_pnt |
| 469 | rc=0 |
| 470 | fi |
| 471 | # Create the syslinux directory in the destination partition |
| 472 | if [scip-shell system . . `[`. "$rc" -eq 0 ]; then |
| 473 | mkdirscip-shell system . . mkdir. -p "$destfs_tmpd/syslinux" |
| 474 | else |
| 475 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 476 | echoscip-shell system . . echo. "Failed to mount partition $target_part!" |
| 477 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 478 | echoscip-shell system . . echo. "Program terminated!" |
| 479 | exit 1 |
| 480 | fi |
| 481 | |
| 482 | # Create manipulable copy (may not be needed depending on the situation) |
| 483 | linux_tmpd="$(mktempscip-shell system . . mktemp. -d /tmp/linux_tmp.XXXXXXscip-shell filesystem . . `/tmp/linux_tmp.XXXXXX`.)" |
| 484 | [scip-shell system . . `[`. $? -eq 0 ] || exit 1 |
| 485 | |
| 486 | echoscip-shell system . . echo. "A filesystem supporting Unix file mode for $mode is required. Copying $mode to $linux_tmpd" |
| 487 | cpscip-shell system . . cp. -fv "$path_of_prog/utils/linux/$arch_d/$mode" "$linux_tmpd" |
| 488 | chmodscip-shell system . . chmod. u+x "$linux_tmpd/$mode" |
| 489 | |
| 490 | check_if_syslinux_runsscip-shell shell shell-project 0.0.0 check_if_syslinux_runs(). "$linux_tmpd/$mode" |
| 491 | rc=$? |
| 492 | |
| 493 | if [scip-shell system . . `[`. "$rc" -eq 0 ]; then |
| 494 | case "$mode" in |
| 495 | syslinux) |
| 496 | echoscip-shell system . . echo. "Running: \"$linux_tmpd/syslinux\" -d syslinux -f -i \"$target_part\"" |
| 497 | # http://www.syslinux.org/wiki/index.php?title=SYSLINUX#Linux |
| 498 | "$linux_tmpd/syslinux" -d syslinux -f -i "$target_part" |
| 499 | s_rc="$?" |
| 500 | ;; |
| 501 | extlinux) |
| 502 | echoscip-shell system . . echo. "Running: \"$linux_tmpd/extlinux\" -i \"$destfs_tmpd/syslinux\"" |
| 503 | # http://www.syslinux.org/wiki/index.php?title=EXTLINUX |
| 504 | "$linux_tmpd/extlinux" -i "$destfs_tmpd/syslinux" |
| 505 | s_rc="$?" |
| 506 | ;; |
| 507 | esac |
| 508 | if [scip-shell system . . `[`. "$s_rc" -ne 0 ]; then |
| 509 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 510 | echoscip-shell system . . echo. "Failed to run $mode in installation mode!" |
| 511 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 512 | echoscip-shell system . . echo. "Program terminated!" |
| 513 | exit 1 |
| 514 | fi |
| 515 | # Success |
| 516 | if [scip-shell system . . `[`. "$mode" = syslinux ]; then |
| 517 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 518 | echoscip-shell system . . echo. "//NOTE// If your USB flash drive fails to boot (maybe buggy BIOS), try to use \"syslinux -d syslinux -fs $target_part\", i.e. running with \"-fs\"." |
| 519 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 520 | fi |
| 521 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
| 522 | echoscip-shell system . . echo. "$mode ran successfully in installation mode." |
| 523 | echoscip-shell system . . echo. "Done!" |
| 524 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 525 | else |
| 526 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 527 | echoscip-shell system . . echo. "Failed to run $mode!" |
| 528 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 529 | echoscip-shell system . . echo. "Program terminated!" |
| 530 | exit 1 |
| 531 | fi |
| 532 | |
| 533 | # Cleanup temporary copy |
| 534 | if [scip-shell system . . `[`. -d "$linux_tmpd" -a -n "$(echoscip-shell system . . echo. $linux_tmpd | grepscip-shell system . . grep. "linux_tmp" )" ]; then |
| 535 | rmscip-shell system . . rm. -rf $linux_tmpd |
| 536 | fi |
| 537 | |
| 538 | # Umount destination if we mounted it |
| 539 | if mountpointscip-shell system . . mountpoint. $destfs_tmpd >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 540 | if [scip-shell system . . `[`. "$flag_mount" = "mounted" ]; then |
| 541 | umountscip-shell system . . umount. $destfs_tmpd |
| 542 | fi |
| 543 | fi |
| 544 | |
| 545 | # Remove mountpoint if we created it |
| 546 | if [scip-shell system . . `[`. -d "$destfs_tmpd" -a -n "$(echoscip-shell system . . echo. $destfs_tmpd | grepscip-shell system . . grep. "destfs_tmpd" )" ]; then |
| 547 | rmscip-shell system . . rm. -rf $destfs_tmpd |
| 548 | fi |