This file is indexed.

/usr/sbin/ocs-install-grub is in clonezilla 3.21.13-1.

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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ nchc org tw>, Ceasar Sun <ceasar _at_ clonezilla org>
# 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] GRUB_PARTITION"
    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 "GRUB_PARTITION is where the partition which grub will be installed. E.g. /dev/sda1, /dev/sdb1..."
    echo "Ex:"
    echo "To install grub on /dev/sda with possible grub partitions sda1 and sda2, run"
    echo "   $ocs -p \"sda1 sda2\" -s sda auto"
    echo "To install grub on /dev/sda1, run"
    echo "   $ocs /dev/sda1"
    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() {
  local boot_loader_pos
  [ "$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)
  if [ -n "$rootp_installed_grub" ]; then
    # Boot loader on root partition
    boot_loader_pos="$rootp_installed_grub"
  else
    # Boot loader on MBR
    boot_loader_pos="$grub_hd"
  fi
  echo "Running: run_grub1_from_restored_os \"$grub_partition\" \"$found_root_partition\" \"/dev/$boot_loader_pos\""
  run_grub1_from_restored_os "$grub_partition" "$found_root_partition" "/dev/$boot_loader_pos"
  rc="$?"
} # end of do_run_grub1_from_restored_os
#
do_run_grub1_from_running_os() {
  local grub1_patch_flag=""
  local rc_grub1_ext4
  local boot_loader_pos
  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
      if [ -n "$rootp_installed_grub" ]; then
        # Boot loader on root partition
        boot_loader_pos="$rootp_installed_grub"
      else
        # Boot loader on MBR
        boot_loader_pos="$grub_hd"
      fi
      echo "Running: $grub_install_exec $grub_no_floopy_opt --root-directory=$hd_img /dev/$boot_loader_pos"
      $grub_install_exec $grub_no_floopy_opt --root-directory=$hd_img /dev/$boot_loader_pos
      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() {
  local boot_loader_pos
  [ "$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)
  if [ -n "$rootp_installed_grub" ]; then
    # Boot loader on root partition
    boot_loader_pos="$rootp_installed_grub"
  else
    # Boot loader on MBR
    boot_loader_pos="$grub_hd"
  fi
  echo "Running: run_grub2_from_restored_os \"$grub_partition\" \"$found_root_partition\" \"/dev/$boot_loader_pos\""
  run_grub2_from_restored_os "$grub_partition" "$found_root_partition" "/dev/$boot_loader_pos"
  rc="$?"
} # end of do_run_grub2_from_restored_os
#
do_run_grub2_from_running_os() {
  local grub2_patch_flag=""
  local boot_loader_pos
  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
    if [ -n "$rootp_installed_grub" ]; then
      # Boot loader on root partition
      boot_loader_pos="$rootp_installed_grub"
    else
      # Boot loader on MBR
      boot_loader_pos="$grub_hd"
    fi
    echo "Running: $grub_install_exec --force --recheck $grub_no_floopy_opt --root-directory=$hd_img /dev/$boot_loader_pos"
    $grub_install_exec --force --recheck $grub_no_floopy_opt --root-directory=$hd_img /dev/$boot_loader_pos
    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
   # //NOTE// "selected_hd" could be sda, sdb, or "sda sdb" (more than 1)
   # If it's not assigned, all the partition(s) on all the disk(s) will be searched.
   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 ocs-install-grub program is only for BIOS/MBR boot loader,
# not for EFI unless it's a GPT disk with a special "bios_boot" partition
# exists in the machine using legacy BIOS.
if ! `is_mbr_partitition_table_disk /dev/$grub_hd`; then
  # In this case, it might be GPT or some other non MBR (msdos) case.
  if `is_gpt_disk_with_bios_boot_part_in_legacy_bios /dev/$grub_hd`; then
    # This is the case for a GPT disk with a special "bios_boot" partition
    # exists in the machine using legacy BIOS. -> continue
    # We also set check_before_install as "no" to let GRUB could be reinstalled.
    check_before_install="no"
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Disk /dev/$grub_hd is not MBR format or GPT disk with a special 'bios_boot' partition. Skip the reinstallation of grub-pc (MBR) program."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 5
  fi
fi

# Even it's blank hard drive, because this program is run after partitions and MBR restored, so we can just detect the MBR on the disk.
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
    # Maybe the boot loader is not on MBR, and it's on root partition.
    # E.g.
    # (1) For MBR, its content is like the mbr.bin from syslinux:
    # root@debian:/tmp# dd if=/dev/sda bs=512 count=1 2>/dev/null |strings
    # RPf1
    # Missing operating system.
    # f`f1
    # |fRfP
    # Ht[y9Y[
    # Multiple active partitions.
    # |XFSBu  f
    # Operating system load error.
    # (2) Instead, the grub boot loader is on root partition (/dev/sda2) for OpenSuSE 13.2 default installation
    # root@debian:/tmp# dd if=/dev/sda2 bs=512 count=1 2>/dev/null |strings
    # ZRr=
    # `|f
    # \|f1
    # GRUB 
    # Geom
    # Hard Disk
    # Read
    #  Error
    # (3) partition table for OpenSuSE 13.2 default installation
    # root@debian:/tmp# parted -s /dev/sda print
    # Model: VMware, VMware Virtual S (scsi)
    # Disk /dev/sda: 21.5GB
    # Sector size (logical/physical): 512B/512B
    # Partition Table: msdos
    # Disk Flags: 
    # 
    # Number  Start   End     Size    Type     File system     Flags
    #  1      1049kB  1185MB  1184MB  primary  linux-swap(v1)
    #  2      1185MB  11.9GB  10.7GB  primary  btrfs           boot
    #  3      11.9GB  21.5GB  9548MB  primary  xfs
    # Therefore check:
    # (1) mbr.bin in /dev/$grub_hd
    # (2) grub in partition
    rootp_installed_grub=""
    mbr_strings="$(LC_ALL=C dd if=/dev/$grub_hd bs=512 count=1 2>/dev/null | strings)"
    if [ -n "$(echo "$mbr_strings" | grep -E "Missing operating system")" -a \
         -n "$(echo "$mbr_strings" | grep -E "Multiple active partitions")" -a \
         -n "$(echo "$mbr_strings" | grep -E "Operating system load error")" ]; then
      # Use all possible partitions if it's auto
      if [ -z "$selected_parts" ]; then
        get_partition_list false
        # The variable "partition_list" is got from get_partition_list.
        avail_part="$partition_list"
      else
        avail_part="$selected_parts"
      fi
      for ip in $avail_part; do
        rootp_strings="$(LC_ALL=C dd if=/dev/$ip bs=512 count=1 2>/dev/null | strings | grep -E "GRUB")"
        if [ -n "$rootp_strings" ]; then
	  rootp_installed_grub="$ip"
	  break
	fi
      done
      if [ -n "$rootp_installed_grub" ]; then
        echo "Found boot loader grub in the root partition /dev/$rootp_installed_grub."
      fi
    else
      echo "The boot loader on /dev/$grub_hd is not grub. Skip running grub-install."
      exit 5
    fi
  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.
   if [ -n "$selected_parts" ]; then
     mount_root_partition_for_separate_boot_part -p "$selected_parts" $hd_img  # return variable $found_root_partition
   else
     mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
   fi
   # 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/"
     elif [ -d "$hd_img/boot/grub2" -a -n "$(chroot $hd_img/ which grub2-install 2>/dev/null)" ]; then
	# For OpenSUSE 42.1 , use grub2 vy " grub2-install" without *.mod 
       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:
         if [ -n "$selected_parts" ]; then
           mount_root_partition_for_separate_boot_part -p "$selected_parts" $hd_img  # return variable $found_root_partition
         else
           mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
         fi
         # 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:
         if [ -n "$selected_parts" ]; then
           mount_root_partition_for_separate_boot_part -p "$selected_parts" $hd_img  # return variable $found_root_partition
         else
           mount_root_partition_for_separate_boot_part $hd_img  # return variable $found_root_partition
         fi
         # 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/"
     elif [ -d "$hd_img/boot/grub2" -a -n "$(chroot $hd_img/ which grub2-install 2>/dev/null)" ]; then
	# For OpenSUSE 42.1 , use grub2 vy " grub2-install" without *.mod 
       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