File: ocs-update-initrd

package info (click to toggle)
clonezilla 5.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,168 kB
  • sloc: sh: 41,203; perl: 193; python: 59; makefile: 26
file content (239 lines) | stat: -rwxr-xr-x 9,946 bytes parent folder | download | duplicates (3)
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
#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to update initramfs for the deployed OS in the hard drive.

#
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=""

#
USAGE() {
    echo "$ocs - To update initramfs on a hard drive."
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs [OPTION] BOOT_PARTITION"
    echo "BOOT_PARTITION is the partition has /boot dir where Linux kernel, initramfs (and grub files) are located."
    echo "It can be with or without /dev/, e.g., /dev/sda1 or sda1. If \"auto\" is assigned, $ocs will try to find the boot partition."
    echo 
    echo "OPTION:"
    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. It can be with or without /dev/, e.g, /dev/sda or sda." 
    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. It can be with or without /dev/, e.g., /dev/sda1 or sda1. For multiple partitions, use something like \"sda1 sda2\""
    echo 
    echo "Ex:"
    echo "To update initramfs on /dev/sda with possible boot partitions sda1 and sda2, run"
    echo "   $ocs -p \"sda1 sda2\" -s sda auto"
    echo "To update initramfs on /dev/sda1, run"
    echo "   $ocs /dev/sda1"
    echo "To update initramfs and let $ocs to find the boot partition, run"
    echo "   $ocs auto"
    echo
} # end of USAGE


####################
### 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
    -s|--selected-hd)
       shift
       if [ -z "$(echo $1 |grep ^-.)" ]; then
         # skip the -xx option, in case 
	 selected_hd="$(strip_leading_dev $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="$(strip_leading_dev $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
       ;;
    -*)     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="$(format_dev_name_with_leading_dev $1)"

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

#
check_if_root

# If grub_partition is not set, set default. Here grub partition is the same as
# boot partition where grub-related files, Linux kernel and initramfs files are put.
# Hence grub partition is the same with boot partition. We keep the similar codes with that in ocs-install-grub.
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 boot and 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 boot and grub partition: $grub_partition"
   else
     echo "The boot and grub directory was 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)."
     else
       echo "The found grub partition \"$grub_partition\" is _NOT_ on the restored partitions list ($selected_parts)."
       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

# process the boot loader
hd_img="$(mktemp -d /tmp/hd_img.XXXXXX)"
mount $grub_partition $hd_img

# 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
   unmount_wait_and_try $grub_partition 2>/dev/null
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"
fi
unmount_wait_and_try $hd_img 2>/dev/null

# Let do_run_update_initrd_from_restored_os do the real job
do_run_update_initrd_from_restored_os "$grub_partition" "$found_root_partition"
rc="$?"

if [ "$rc" -eq 199 ]; then
    # Just show warning
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Failed to create initrd in the restored OS due to different system architecture (e.g., i686 vs x86-64)."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    [ "$debug_mode" = "on" ] && sulogin
elif [ "$rc" -ne 0 ]; then
    # Show error
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Failed to create initrd in the restored OS."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    [ "$debug_mode" = "on" ] && sulogin
else
    echo "done!"
fi

exit $rc