This file is indexed.

/usr/sbin/ocs-install-grub is in clonezilla 3.10.11-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ nchc org tw>
# Description: Program to reinstall grub on a MBR hard drive. This program is aimed to use the grub in the restored OS first, and if it fails, try to use the grub from the running OS (live CD, for example).

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Load the config in ocs-live.conf. This is specially for Clonezilla live. It will overwrite some settings of /etc/drbl/drbl-ocs.conf, such as $DIA...
[ -e "/etc/ocs/ocs-live.conf" ] && . /etc/ocs/ocs-live.conf

# Settings
verbose="no"
grub_partition="" # e.g. /dev/sda1
selected_hd=""    # e.g. sda
selected_parts="" # e.g. sda1, sda2
grub_ver_in_restored_os=""
grub2_boot_dir=""
check_before_install="yes"  # By default we check if grub is already in the boot loader. If so, we continue to run grub-install

#
USAGE() {
    echo "$ocs - To install grub on a hard drive."
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs [OPTION] DISK"
    echo "Options:"
    echo "-g, --grub-version VER  Assign the grub version as VER (1 or 2)"
    echo "-s, --selected-hd  DISk Assign the disk to search for _only_, i.e. not to search those not in the lists. This is used for not to touch those disk which are not restored. E.g. sda or sdb..." 
    echo "-p, --selected-parts PARTITION  Assign the partitions to search for _only_, i.e. not to search those not in the lists. This is used for not to touch those partitions which are not restored. E.g. sda1, sda2. For multiple partitions, use something like \"sda1 sda2\""
    echo "-n, --no-check-before-install   By default we check if grub is already in the boot loader. If so, we continue to run grub-install. This option will go without checking."
    echo "DISK is where the hard drive grub will be installed. E.g. /dev/sda, /dev/sdb..."
    echo "Ex:"
    echo "To install grub on /dev/sda with possible grub partitions sda1 and sda2, run"
    echo "   $ocs -p \"sda1 sda2\" /dev/sda"
    echo "To install grub and let $ocs to find the grub disk, run"
    echo "   $ocs auto"
    echo
} # end of USAGE

check_grub1_config_file() {
  local ghd_="$1"
  if [ -z "$ghd_" ]; then
    echo "No variable ghd in function check_grub1_config_file!"
    exit 1
  fi
  # Check if grub config exists or not
  [ ! -e "$ghd_/boot/grub/menu.lst" -a ! -e "$ghd_/boot/grub/grub.conf" ] && echo "$msg_uppercase_Warning!!! Can NOT find the grub config file \"menu.lst\" or \"grub.conf\" in the system restored or cloned!!!"
  
  if [ -f "$ghd_/boot/grub/menu.lst" -a ! -e "$ghd_/boot/grub/grub.conf" ]; then
    # For some version of grub, only honors grub.conf, we have to deal with.
    # Test if soft link works, if yes, link it. If not, copy it.
    # if it's VFAT, grub-install will fail when try to link menu.lst as grub.conf, we just copy it.
    (cd $ghd_/boot/grub/ 
     ln -fs menu.lst grub.conf 2>/dev/null
    )
    if [ ! -e "$ghd_/boot/grub/grub.conf" ]; then
      cp -f $ghd_/boot/grub/menu.lst $ghd_/boot/grub/grub.conf
    fi
  fi
  if [ ! -f "$ghd_/boot/grub/menu.lst" -a -e "$ghd_/boot/grub/grub.conf" ]; then
    # For the grub from debian, only honors menu.lst, we have to deal with.
    # Test if soft link works, if yes, link it. If not, copy it.
    # if it's VFAT, grub-install will fail when try to link grub.conf as menu.lst, we just copy it.
    (cd $ghd_/boot/grub/ 
     ln -fs grub.conf menu.lst 2>/dev/null
    )
    if [ ! -e "$ghd_/boot/grub/menu.lst" ]; then
      cp -f $ghd_/boot/grub/grub.conf $ghd_/boot/grub/menu.lst
    fi
  fi
} # end of check_grub1_config_file
do_run_grub1_from_restored_os() {
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Trying to use the grub1 in the restored OS..."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  unmount_wait_and_try $grub_partition 2>/dev/null
  unmount_wait_and_try $hd_img 2>/dev/null
  # "sleep 0.5" was added to avoid ocs-restore-mdisks run too fast error.
  # Without it, it might show this error when ocs-restore-mdisks is run:
  # rmdir: failed to remove `/tmp/hd_img.weAw4i': Device or resource busy
  [ -d "$hd_img/boot" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img/boot)
  [ -d "$hd_img" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img)
  echo "Running: run_grub1_from_restored_os \"$grub_partition\" \"$found_root_partition\" \"/dev/$grub_hd\""
  run_grub1_from_restored_os "$grub_partition" "$found_root_partition" "/dev/$grub_hd"
  rc="$?"
} # end of do_run_grub1_from_restored_os
do_run_grub1_from_running_os() {
  local grub1_patch_flag=""
  local rc_grub1_ext4
  prepare_grub1_files_if_required
  show_grub1_ext4_warning $grub_partition
  rc_grub1_ext4=$?
  if [ "$rc_grub1_ext4" -eq 0 ]; then
    [ -f "$hd_img/boot/grub/device.map" ] && rm -f $hd_img/boot/grub/device.map
    echo "Trying to use the grub1 from the running OS..."
    check_grub_install_version 1
    rc_grub_check=$?
    if [ "$rc_grub_check" -eq 0 ]; then
      echo "Running: $grub_install_exec $grub_no_floopy_opt --root-directory=$hd_img /dev/$grub_hd"
      $grub_install_exec $grub_no_floopy_opt --root-directory=$hd_img /dev/$grub_hd
      rc="$?"
      # Remove the device.map in case it provents the restored OS fails to run update-grub due to different boot device name (e.g. sda <-> hda issue)
      [ -f "$hd_img/boot/grub/device.map" ] && rm -f $hd_img/boot/grub/device.map
      
      check_grub1_config_file $hd_img
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "The required version of grub-install for grub 1 was not found!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "Command grub-install is skipped!"
      rc=1
    fi
  fi
  rc="$rc_grub1_ext4"

  unmount_wait_and_try $grub_partition 2>/dev/null
  unmount_wait_and_try $hd_img 2>/dev/null
  # "sleep 0.5" was added to avoid ocs-restore-mdisks run too fast error.
  # Without it, it might show this error when ocs-restore-mdisks is run:
  # rmdir: failed to remove `/tmp/hd_img.weAw4i': Device or resource busy
  [ -d "$hd_img/boot" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img/boot)
  [ -d "$hd_img" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img)
} # end of do_run_grub1_from_running_os
do_run_grub2_from_restored_os() {
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Trying to use the grub2 in the restored OS..."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  unmount_wait_and_try $grub_partition 2>/dev/null
  unmount_wait_and_try $hd_img 2>/dev/null
  # "sleep 0.5" was added to avoid ocs-restore-mdisks run too fast error.
  # Without it, it might show this error when ocs-restore-mdisks is run:
  # rmdir: failed to remove `/tmp/hd_img.weAw4i': Device or resource busy
  [ -d "$hd_img/boot" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img/boot)
  [ -d "$hd_img" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img)
  echo "Running: run_grub2_from_restored_os \"$grub_partition\" \"$found_root_partition\" \"/dev/$grub_hd\""
  run_grub2_from_restored_os "$grub_partition" "$found_root_partition" "/dev/$grub_hd"
  rc="$?"
} # end of do_run_grub2_from_restored_os
#
do_run_grub2_from_running_os() {
  local grub2_patch_flag=""
  prepare_grub2_files_if_required
  [ -f "$hd_img/boot/grub/device.map" ] && rm -f $hd_img/boot/grub/device.map
  echo "Trying to use the grub2 from the running OS..."
  check_grub_install_version 2
  rc_grub_check=$?
  if [ "$rc_grub_check" -eq 0 ]; then
    # Before running grub-install, we need to patch it if grub2_boot_dir="/boot/grub2/", since in Debian/Ubuntu, grub2 boot dir is /boot/grub, while for Fedora 16 it's /boot/grub2. Will report a wishlist on grub website. We only patch the grub-install on Clonezilla live since if not, it's difficult.
    if [ -e "$(which $grub_install_exec)" -a \
         -n "$(grep boot=live /proc/cmdline)" -a \
         -n "$(echo $grub2_boot_dir | grep "/boot/grub2")" ]; then
      cp -a "$(which $grub_install_exec)" "$(which $grub_install_exec)".orig
      perl -pi -e 's|(^grubdir=.*)/grub(\".*)|$1/grub2$2|g' "$(which $grub_install_exec)"
      grub2_patch_flag="true"
    fi
    echo "Running: $grub_install_exec --force --recheck $grub_no_floopy_opt --root-directory=$hd_img /dev/$grub_hd"
    $grub_install_exec --force --recheck $grub_no_floopy_opt --root-directory=$hd_img /dev/$grub_hd
    rc="$?"
    # Revert
    if [ "$grub2_patch_flag" = "true" ]; then
      mv -f "$(which $grub_install_exec)".orig "$(which $grub_install_exec)"
    fi
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "The required version of grub-install for grub 2 was not found!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "Command grub-install is skipped!"
    rc=1
  fi
  unmount_wait_and_try $grub_partition 2>/dev/null
  unmount_wait_and_try $hd_img 2>/dev/null
  # "sleep 0.5" was added to avoid ocs-restore-mdisks run too fast error.
  # Without it, it might show this error when ocs-restore-mdisks is run:
  # rmdir: failed to remove `/tmp/hd_img.weAw4i': Device or resource busy
  [ -d "$hd_img/boot" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img/boot)
  [ -d "$hd_img" -a -n "$hd_img" ] && (sleep 0.5; rmdir $hd_img)
} # end of do_run_grub2_from_running_os
show_grub1_ext4_warning () {
  local grub_pt_="$1"
  local grub_part_fs
  local DISTRIB_ID
  grub_part_fs="$(LC_ALL=C ocs-get-part-info $grub_pt_ filesystem)"
  if [ "$grub_part_fs" = "ext4" ]; then
    # Check if it's Debian Linux
    [ -e /etc/lsb-release ] && . /etc/lsb-release
    if [ "$DISTRIB_ID" = "Ubuntu" ] ;then
      # Ubuntu linux. Its grub1 has been patched to support ext4.
      return 0
    else
      if [ -e /etc/debian_version ]; then
        # It's Debian. Its grub1 does not support ext4 for sure.
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
        echo "Warning! Found grub partition ($grub_pt_) file system is ext4! The grub 1 from Debian Linux does not support file system ext4! Skip re-installing grub 1. The restored OS might fail to boot."
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        return 3
      else
        # Otherwise distributios...
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
        echo "Warning! Found grub partition ($grub_pt_) file system is ext4! Not all versions of grub 1 supports file system ext4!"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        return 0
      fi
    fi
  fi
} # end of show_grub1_ext4_warning


####################
### Main program ###
####################

ocs_file="$0"
ocs=`basename $ocs_file`
#
# -s or -p option is used to pass the selected device, which means only these device will be searched for grub partition. If no assigning, all the available partitions on the system will be searched.
while [ $# -gt 0 ]; do
  case "$1" in
    -g|--grub-version)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         grub_ver_in_restored_os="$1"
         shift
       fi
       [ -z "$grub_ver_in_restored_os" ] && echo "-g is used, but no grub_ver_in_restored_os assigned." && exit 1
       ;;
    -s|--selected-hd)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         selected_hd="$1"
         shift
       fi
       if [ -z "$selected_hd" ]; then
         echo "-s is used, but no selected_hd assigned."
         echo "$msg_program_stop"
         [ "$save_restore_error_log" = "yes" ] && copy_error_log
         exit 1
       fi
       ;;
    -p|--selected-parts)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
         selected_parts="$1"
         shift
       fi
       if [ -z "$selected_parts" ]; then
         echo "-p is used, but no selected_parts assigned."
         echo "$msg_program_stop"
         [ "$save_restore_error_log" = "yes" ] && copy_error_log
         exit 1
       fi
       ;;
    -n|--no-check-before-install)
       # Option to force install grub even if boot loader is not found as grub
       shift
       check_before_install="no"
       ;;
    -*)     echo "${0}: ${1}: invalid option"
            USAGE >& 2
            echo "$msg_program_stop"
            [ "$save_restore_error_log" = "yes" ] && copy_error_log
            exit 2 ;;
    *)      break ;;
  esac
done
grub_partition="$1"

if [ -z "$grub_partition" ]; then
  USAGE
  exit 1
fi

# From OpenSuSE 10.3 or later, the original grub-install is renamed as grub-install.unsupported, and OpenSuSE uses a script to run grub-install by yast2. Bad! Very bad idea! They should use another name for this grub-install.
if [ -e /etc/SuSE-release -a -e /usr/sbin/grub-install.unsupported ]; then
  grub_install_exec="grub-install.unsupported"
elif type grub2-install &>/dev/null; then
  grub_install_exec="grub2-install"
else
  grub_install_exec="grub-install"
fi

# if grub_partition is not set, set default
if [ "$grub_partition" = "auto" ]; then
   grub_partition=""   # reset it as none
   check_grub_partition "$selected_hd"
   # We get $found_grub_partition from check_grub_partition, it might be 1 or 2 or more partitions, e.g. /dev/sda1 /dev/hda1
   # 3 cases:
   # (1) selected_parts is assigned
   # From the selected_parts, we can decide which one got from check_grub_partition is really for the target disk
   # (2) selected_hd is assigned
   # found_grub_partition e.g.: /dev/sda1 /dev/hda1
   # selected_hd e.g.: sda sdb
   # (3) no selected_parts or selected_hd is assigned
   if [ -n "$selected_parts" ]; then
     # found_grub_partition e.g.: /dev/sda1 /dev/hda1 /dev/cciss/c0d0p1
     # selected_parts e.g.: sda1 sda2 sda5 cciss/c0d0p1 cciss/c0d0p2
     for i in $found_grub_partition; do
       for j in $selected_parts; do
         if [  "${i#/dev/*}" = "$j" ]; then
          grub_partition="$i"  # e.g. /dev/sda1
          break
         fi
       done
       [ -n "$grub_partition" ] && break
     done
   elif [ -n "$selected_hd" ]; then
     # found_grub_partition e.g.: /dev/sda1 /dev/hda1
     # selected_hd e.g.: sda sdb
     for i in $found_grub_partition; do
       for j in $selected_hd; do
         if [ "$(get_diskname $i)" = "$j" ]; then
          grub_partition="$i"  # e.g. /dev/sda1
          break
         fi
       done
       [ -n "$grub_partition" ] && break
     done
   else
     # No assignment, use the one found. However, maybe there are more than 1. We only take the first one.
     if [ "$(LC_ALL=C echo $found_grub_partition | wc -w)" -gt 1 ]; then
       grub_partition="$(echo $found_grub_partition | awk -F" " '{print $1}')"
       echo "More than one grub config partitions exist: $found_grub_partition. Choose the 1st one: $grub_partition."
     else
       grub_partition="$found_grub_partition"
     fi
   fi
   if [ -n "$grub_partition" ]; then
     echo "Found grub partition: $grub_partition"
   else
     echo "The grub directory is NOT found. Maybe it does not exist (so other boot manager exists) or the file system is not supported in the kernel. Skip running $grub_install_exec."
     exit 1
   fi
   # Check if grub_partition is on the list of target partitions
   if [ -n "$selected_parts" ]; then
     # Only if we assign the selected partitions, we have to check...
     # E.g.
     # grub_partition=/dev/sda1
     # echo "sda1 sda3" | grep -Ewo "${grub_partition#/dev/*}"
     if [ -n "$(echo $selected_parts | grep -Ewo "${grub_partition#/dev/*}")" ]; then
       echo "Found grub partition \"$grub_partition\", which is on the restored partitions list ($selected_parts). Will run $grub_install_exec later."
     else
       echo "The found grub partition \"$grub_partition\" is _NOT_ on the restored partitions list ($selected_parts). Skip running $grub_install_exec."
       exit 3
     fi
   fi
fi
# grub_partition is like "/dev/hda1" or "/dev/sda1"
# So grub_hd is like "hda" or "sda"
# Remove the leading space, just in case.
grub_partition="$(LC_ALL=C echo "$grub_partition" | sed -r -e "s/^[[:space:]]*//g")"
grub_hd="$(get_diskname $grub_partition)"
# active this device
fdisk -l /dev/$grub_hd &>/dev/null

# This install_grub_hd function is only for BIOS/MBR boot loader, not for EFI. Therefore if $selected_hd is GPT disk, skip it.
if ! `is_mbr_partitition_table_disk /dev/$grub_hd`; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Disk /dev/$grub_hd is not MBR format. Skip the reinstallation of grub-pc (MBR) program."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 5
fi

if [ "$check_before_install" = "yes" ]; then
  # Check if grub exists on the MBR. If not, skip this function.
  if [ -z "$(LANG=C dd if=/dev/$grub_hd bs=512 count=1 2>/dev/null | strings | grep "GRUB")" ]; then
     echo "The boot loader on /dev/$grub_hd is not grub. Skip running grub-install."
     exit 5
  else 
     echo "Found boot loader grub in the MBR of disk /dev/$grub_hd."
  fi
fi
# process the boot loader
hd_img="$(mktemp -d /tmp/hd_img.XXXXXX)"
mount $grub_partition $hd_img
# check if --no-floppy is supported.
grub_no_floopy_opt=""
if [ -n "$($grub_install_exec --help | grep "\--no-floppy")" ]; then
  grub_no_floopy_opt="--no-floppy"
fi
# Check if /boot is in its own partition, if so, different mount point.
# grub root-directory must have "/boot/". If no "/boot/", it must be the grub partition itself (i.e. a separate partition will be mounted as /boot, and it conains /grub/ in the partition.)
if [ ! -d "$hd_img/boot/" ]; then
   # In this case, /boot must exist in its own partition, 
   # not same partition with / (root partition).

   # remount it as /tmp/hd_img.XXXXXX/boot
   unmount_wait_and_try $hd_img
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "There is a separate boot partition in target device. Trying to mount root partition for $grub_install_exec to work with that..."
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   # Important! We have to mount the root dir first. Otherwise in FC6, CentOS 5 or later, grub-install will refuse to do that without root device. It will show the error message like: "Could not find device for /mnt"... For Debian or Ubuntu, no such problem. 
   # Actually we can modify /sbin/grub-install (grub-0.97-13) in FC6 or Centos5:
   # -----------
   # ...
   # # Get the root drive.
   # root_device=`find_device ${rootdir}` || exit 1
   # ...
   # -----------
   # as 
   # root_device=`find_device ${rootdir}`
   # i.e. remove "|| exit 1". However, it's better to follow the way distribution does. Therefore we modify ocs-function instead of modifying grub-install.
   mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
   # just in case no /boot in root partition
   mkdir -p $hd_img/boot
   mount $grub_partition $hd_img/boot/

   # TODO
   # do_run_update_initrd_from_restored_os

   if [ -z "$grub_ver_in_restored_os" ]; then
     # Check if it's grub2. For Fedora 16, the grub2 dir is in /boot/grub2. For other distribution (e.g. Ubuntu 11.10, Debian 6), it's in /boot/grub/.
     if [ -n "$(find $hd_img/boot/grub/ -iname "*.mod" -print 2>/dev/null)" ]; then
       grub_ver_in_restored_os="2"
       grub2_boot_dir="/boot/grub/"
     elif [ -n "$(find $hd_img/boot/grub2/ -iname "*.mod" -print 2>/dev/null)" ]; then
       grub_ver_in_restored_os="2"
       grub2_boot_dir="/boot/grub2/"
     else
       grub_ver_in_restored_os="1"
     fi
   fi

   if [ "$grub_ver_in_restored_os" = "2" ]; then
     echo "Found grub 2 installed in the restored OS."
     if [ -z "$run_grub2_from_restored_os_mode" ]; then
       # We will get variable run_grub2_from_restored_os_mode with this
       test_run_grub2_from_restored_os "$found_root_partition"
     fi
     if [ "$run_grub2_from_restored_os_mode" = "yes" ]; then
       do_run_grub2_from_restored_os
       if [ "$rc" -ne 0 ]; then
         [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
         echo "Unable to use the grub 2 in the restored OS, trying another method..."
         [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         # Plan B:
         mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
         # just in case no /boot in root partition
         mkdir -p $hd_img/boot
         mount $grub_partition $hd_img/boot/
         do_run_grub2_from_running_os
       fi
     else
       do_run_grub2_from_running_os
     fi
   else
     # grub version 1
     echo "Found grub 1 installed in the restored OS."
     if [ -z "$run_grub1_from_restored_os_mode" ]; then
       # We will get variable run_grub1_from_restored_os_mode with this
       test_run_grub1_from_restored_os "$found_root_partition"
     fi
     if [ "$run_grub1_from_restored_os_mode" = "yes" ]; then
       do_run_grub1_from_restored_os
       if [ "$rc" -ne 0 ]; then
         [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
         echo "Unable to use the grub 1 in the restored OS, trying another method..."
         [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         # Plan B:
         mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
         # just in case no /boot in root partition
         mkdir -p $hd_img/boot
         mount $grub_partition $hd_img/boot/
         do_run_grub1_from_running_os
       fi
     else
       do_run_grub1_from_running_os
     fi
   fi
else
   # In this case, /boot must co-exist with root system in same partition, 
   # Then found_root_partition is the grub partition
   found_root_partition="$grub_partition"
   # Check if it's grub2. For Fedora 16, the grub2 dir is in /boot/grub2. For other distribution (e.g. Ubuntu 11.10, Debian 6), it's in /boot/grub/.
   if [ -n "$(find $hd_img/boot/grub/ -iname "*.mod" -print 2>/dev/null)" ]; then
     grub_ver_in_restored_os="2"
     grub2_boot_dir="/boot/grub/"
   elif [ -n "$(find $hd_img/boot/grub2/ -iname "*.mod" -print 2>/dev/null)" ]; then
     grub_ver_in_restored_os="2"
     grub2_boot_dir="/boot/grub2/"
   else
     grub_ver_in_restored_os="1"
   fi
   if [ "$grub_ver_in_restored_os" = "2" ]; then
     echo "Found grub 2 installed in the restored OS."
     if [ -z "$run_grub2_from_restored_os_mode" ]; then
       # We will get variable run_grub2_from_restored_os_mode with this
       test_run_grub2_from_restored_os "$found_root_partition"
     fi
     if [ "$run_grub2_from_restored_os_mode" = "yes" ]; then
       do_run_grub2_from_restored_os
       if [ "$rc" -ne 0 ]; then
         [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
         echo "Unable to use the grub 2 in the restored OS, trying another method..."
         [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         # Plan B:
         # //NOTE/// In this case, /boot must co-exist with root system in same partition, 
         hd_img="$(mktemp -d /tmp/hd_img.XXXXXX)"
         mount $grub_partition $hd_img
         do_run_grub2_from_running_os
       fi
     else
       do_run_grub2_from_running_os
     fi
   else
     # grub version 1
     echo "Found grub 1 installed in the restored OS."
     if [ -z "$run_grub1_from_restored_os_mode" ]; then
       # We will get variable run_grub1_from_restored_os_mode with this
       test_run_grub1_from_restored_os "$found_root_partition"
     fi
     if [ "$run_grub1_from_restored_os_mode" = "yes" ]; then
       do_run_grub1_from_restored_os
       if [ "$rc" -ne 0 ]; then
         [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
         echo "Unable to use the grub 1 in the restored OS, trying another method..."
         [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         # Plan B:
         # //NOTE/// In this case, /boot must co-exist with root system in same partition, 
         hd_img="$(mktemp -d /tmp/hd_img.XXXXXX)"
         mount $grub_partition $hd_img
         do_run_grub1_from_running_os
       fi
     else
       do_run_grub1_from_running_os
     fi
   fi
fi

if [ "$rc" -ne 0 ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Failed to install grub!!!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    if [ "$grub_ver_in_restored_os" -eq "1" ]; then
      show_grub1_ext4_warning $grub_partition
    fi
    [ "$debug_mode" = "on" ] && sulogin
else
    echo "done!"
fi
exit $rc