OLD | NEW |
| (Empty) |
1 #!/bin/bash | |
2 | |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 # Script to generate a Chromium OS update for use by the update engine. | |
8 # If a source .bin is specified, the update is assumed to be a delta update. | |
9 | |
10 # --- BEGIN COMMON.SH BOILERPLATE --- | |
11 # Load common CrOS utilities. Inside the chroot this file is installed in | |
12 # /usr/lib/crosutils. Outside the chroot we find it relative to the script's | |
13 # location. | |
14 find_common_sh() { | |
15 local thisdir="$(dirname "$(readlink -f "$0")")" | |
16 local common_paths=(/usr/lib/crosutils "${thisdir}") | |
17 local path | |
18 | |
19 SCRIPT_ROOT= | |
20 for path in "${common_paths[@]}"; do | |
21 if [ -r "${path}/common.sh" ]; then | |
22 SCRIPT_ROOT=${path} | |
23 break | |
24 fi | |
25 done | |
26 | |
27 # HACK(zbehan): We have to fake GCLIENT_ROOT in case we're running inside | |
28 # au_zip enviroment. GCLIENT_ROOT detection became fatal... | |
29 [ "${SCRIPT_ROOT}" == "${thisdir}" ] && \ | |
30 export GCLIENT_ROOT="." | |
31 } | |
32 | |
33 find_common_sh | |
34 . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) | |
35 # --- END COMMON.SH BOILERPLATE --- | |
36 | |
37 # Load functions and constants for chromeos-install | |
38 # NOTE: Needs to be called from outside the chroot. | |
39 . "/usr/lib/installer/chromeos-common.sh" &> /dev/null || \ | |
40 . "${SRC_ROOT}/platform/installer/chromeos-common.sh" &> /dev/null || \ | |
41 . "./chromeos-common.sh" || \ | |
42 die "Unable to load /usr/lib/installer/chromeos-common.sh" | |
43 | |
44 SRC_MNT="" | |
45 DST_MNT="" | |
46 SRC_KERNEL="" | |
47 SRC_ROOT="" | |
48 DST_KERNEL="" | |
49 DST_ROOT="" | |
50 STATE_MNT="" | |
51 STATE_LOOP_DEV="" | |
52 | |
53 # Pass an arg to not exit 1 at the end | |
54 cleanup() { | |
55 set +e | |
56 if [ -n "$SRC_MNT" ]; then | |
57 sudo umount -d "$SRC_MNT" | |
58 [ -d "$SRC_MNT" ] && rmdir "$SRC_MNT" | |
59 SRC_MNT="" | |
60 fi | |
61 if [ -n "$DST_MNT" ]; then | |
62 sudo umount -d "$DST_MNT" | |
63 [ -d "$DST_MNT" ] && rmdir "$DST_MNT" | |
64 DST_MNT="" | |
65 fi | |
66 if [ -n "$STATE_MNT" ]; then | |
67 sudo umount "$STATE_MNT" | |
68 [ -d "$STATE_MNT" ] && rmdir "$STATE_MNT" | |
69 STATE_MNT="" | |
70 fi | |
71 if [ -n "$STATE_LOOP_DEV" ]; then | |
72 sudo losetup -d "$STATE_LOOP_DEV" | |
73 STATE_LOOP_DEV="" | |
74 fi | |
75 rm -f "$SRC_KERNEL" | |
76 rm -f "$SRC_ROOT" | |
77 rm -f "$DST_KERNEL" | |
78 rm -f "$DST_ROOT" | |
79 [ -n "$1" ] || exit 1 | |
80 } | |
81 | |
82 extract_partition_to_temp_file() { | |
83 local filename="$1" | |
84 local partition="$2" | |
85 local temp_file="$3" | |
86 if [ -z "$temp_file" ]; then | |
87 temp_file=$(mktemp /tmp/cros_generate_update_payload.XXXXXX) | |
88 echo "$temp_file" | |
89 fi | |
90 | |
91 local offset=$(partoffset "${filename}" ${partition}) # 512-byte sectors | |
92 local length=$(partsize "${filename}" ${partition}) # 512-byte sectors | |
93 local bs=512 | |
94 local sectors_per_two_mib=$((2 * 1024 * 1024 / 512)) | |
95 if [ $(( $offset % $sectors_per_two_mib )) -eq 0 -a \ | |
96 $(( $length % $sectors_per_two_mib )) -eq 0 ]; then | |
97 bs=$((2 * 1024 * 1024)) | |
98 offset=$(($offset / $sectors_per_two_mib)) | |
99 length=$(($length / $sectors_per_two_mib)) | |
100 else | |
101 warn "partition offset or length not at 2MiB boundary" | |
102 fi | |
103 dd if="$filename" of="$temp_file" bs=$bs count="$length" \ | |
104 skip="$offset" 2>/dev/null | |
105 } | |
106 | |
107 patch_kernel() { | |
108 local IMAGE="$1" | |
109 local KERN_FILE="$2" | |
110 | |
111 echo "Patching kernel" $KERN_FILE | |
112 echo " into" $IMAGE | |
113 STATE_LOOP_DEV=$(sudo losetup -f) | |
114 [ -n "$STATE_LOOP_DEV" ] || die "no free loop device" | |
115 local offset=$(partoffset "${IMAGE}" 1) | |
116 offset=$(($offset * 512)) | |
117 sudo losetup -o "$offset" "$STATE_LOOP_DEV" "$IMAGE" | |
118 STATE_MNT=$(mktemp -d /tmp/state.XXXXXX) | |
119 sudo mount --read-only "$STATE_LOOP_DEV" "$STATE_MNT" | |
120 dd if="$STATE_MNT"/vmlinuz_hd.vblock of="$KERN_FILE" conv=notrunc 2>/dev/null | |
121 sudo umount "$STATE_MNT" | |
122 STATE_MNT="" | |
123 sudo losetup -d "$STATE_LOOP_DEV" | |
124 STATE_LOOP_DEV="" | |
125 } | |
126 | |
127 extract_kern_root() { | |
128 local bin_file="$1" | |
129 local kern_out="$2" | |
130 local root_out="$3" | |
131 | |
132 if [ -z "$kern_out" ]; then | |
133 die "missing kernel output filename" | |
134 fi | |
135 if [ -z "$root_out" ]; then | |
136 die "missing root output filename" | |
137 fi | |
138 | |
139 extract_partition_to_temp_file "$bin_file" 2 "$kern_out" | |
140 if [ "$FLAGS_patch_kernel" -eq "$FLAGS_TRUE" ]; then | |
141 patch_kernel "$bin_file" "$kern_out" | |
142 fi | |
143 extract_partition_to_temp_file "$bin_file" 3 "$root_out" | |
144 } | |
145 | |
146 DEFINE_string image "" "The image that should be sent to clients." | |
147 DEFINE_string src_image "" "Optional: a source image. If specified, this makes\ | |
148 a delta update." | |
149 DEFINE_boolean old_style "$FLAGS_TRUE" "Generate an old-style .gz full update." | |
150 DEFINE_string output "" "Output file" | |
151 DEFINE_boolean outside_chroot "$FLAGS_FALSE" "Running outside of chroot." | |
152 DEFINE_boolean patch_kernel "$FLAGS_FALSE" "Whether or not to patch the kernel \ | |
153 with the patch from the stateful partition (default: false)" | |
154 DEFINE_string private_key "" "Path to private key in .pem format." | |
155 DEFINE_boolean extract "$FLAGS_FALSE" "If set, extract old/new kernel/rootfs \ | |
156 to [old|new]_[kern|root].dat. Useful for debugging (default: false)" | |
157 DEFINE_boolean full_kernel "$FLAGS_FALSE" "Generate a full kernel update even \ | |
158 if generating a delta update (default: false)" | |
159 | |
160 # Parse command line | |
161 FLAGS "$@" || exit 1 | |
162 eval set -- "${FLAGS_ARGV}" | |
163 | |
164 set -e | |
165 | |
166 if [ -n "$FLAGS_src_image" ] && \ | |
167 [ "$FLAGS_outside_chroot" -eq "$FLAGS_FALSE" ]; then | |
168 # We need to be in the chroot for generating delta images. | |
169 # by specifying --outside_chroot you can choose not to assert | |
170 # this will allow us to run this script outside chroot. | |
171 # Running this script outside chroot requires copying delta_generator binary | |
172 # and also copying few shared libraries with it. | |
173 assert_inside_chroot | |
174 fi | |
175 | |
176 locate_gpt | |
177 | |
178 if [ "$FLAGS_extract" -eq "$FLAGS_TRUE" ]; then | |
179 if [ -n "$FLAGS_src_image" ]; then | |
180 extract_kern_root "$FLAGS_src_image" old_kern.dat old_root.dat | |
181 fi | |
182 if [ -n "$FLAGS_image" ]; then | |
183 extract_kern_root "$FLAGS_image" new_kern.dat new_root.dat | |
184 fi | |
185 echo Done extracting kernel/root | |
186 exit 0 | |
187 fi | |
188 | |
189 DELTA=$FLAGS_TRUE | |
190 [ -n "$FLAGS_output" ] || die \ | |
191 "Error: you must specify an output filename with --output FILENAME" | |
192 | |
193 if [ -z "$FLAGS_src_image" ]; then | |
194 DELTA=$FLAGS_FALSE | |
195 fi | |
196 | |
197 if [ "$DELTA" -eq "$FLAGS_TRUE" -o "$FLAGS_old_style" -eq "$FLAGS_FALSE" ]; then | |
198 echo "Generating a delta update" | |
199 | |
200 # Sanity check that the real generator exists: | |
201 GENERATOR="$(which delta_generator)" | |
202 [ -x "$GENERATOR" ] || die "can't find delta_generator" | |
203 | |
204 trap cleanup INT TERM EXIT | |
205 if [ "$DELTA" -eq "$FLAGS_TRUE" ]; then | |
206 if [ "$FLAGS_full_kernel" -eq "$FLAGS_FALSE" ]; then | |
207 SRC_KERNEL=$(extract_partition_to_temp_file "$FLAGS_src_image" 2) | |
208 if [ "$FLAGS_patch_kernel" -eq "$FLAGS_TRUE" ]; then | |
209 patch_kernel "$FLAGS_src_image" "$SRC_KERNEL" | |
210 fi | |
211 echo md5sum of src kernel: | |
212 md5sum "$SRC_KERNEL" | |
213 else | |
214 echo "Generating a full kernel update." | |
215 fi | |
216 SRC_ROOT=$(extract_partition_to_temp_file "$FLAGS_src_image" 3) | |
217 | |
218 echo md5sum of src root: | |
219 md5sum "$SRC_ROOT" | |
220 fi | |
221 | |
222 DST_KERNEL=$(extract_partition_to_temp_file "$FLAGS_image" 2) | |
223 if [ "$FLAGS_patch_kernel" -eq "$FLAGS_TRUE" ]; then | |
224 patch_kernel "$FLAGS_image" "$DST_KERNEL" | |
225 fi | |
226 DST_ROOT=$(extract_partition_to_temp_file "$FLAGS_image" 3) | |
227 | |
228 if [ "$DELTA" -eq "$FLAGS_TRUE" ]; then | |
229 SRC_MNT=$(mktemp -d /tmp/src_root.XXXXXX) | |
230 sudo mount -o loop,ro "$SRC_ROOT" "$SRC_MNT" | |
231 | |
232 DST_MNT=$(mktemp -d /tmp/src_root.XXXXXX) | |
233 sudo mount -o loop,ro "$DST_ROOT" "$DST_MNT" | |
234 | |
235 sudo LD_LIBRARY_PATH=${LD_LIBRARY_PATH} PATH=${PATH} "$GENERATOR" \ | |
236 -new_dir "$DST_MNT" -new_image "$DST_ROOT" -new_kernel "$DST_KERNEL" \ | |
237 -old_dir "$SRC_MNT" -old_image "$SRC_ROOT" -old_kernel "$SRC_KERNEL" \ | |
238 -out_file "$FLAGS_output" -private_key "$FLAGS_private_key" | |
239 else | |
240 "$GENERATOR" \ | |
241 -new_image "$DST_ROOT" -new_kernel "$DST_KERNEL" \ | |
242 -out_file "$FLAGS_output" -private_key "$FLAGS_private_key" | |
243 fi | |
244 | |
245 trap - INT TERM EXIT | |
246 cleanup noexit | |
247 | |
248 if [ "$DELTA" -eq "$FLAGS_TRUE" ]; then | |
249 echo "Done generating delta." | |
250 else | |
251 echo "Done generating new style full update." | |
252 fi | |
253 else | |
254 echo "Generating old-style full update" | |
255 | |
256 trap cleanup INT TERM EXIT | |
257 DST_KERNEL=$(extract_partition_to_temp_file "$FLAGS_image" 2) | |
258 if [ "$FLAGS_patch_kernel" -eq "$FLAGS_TRUE" ]; then | |
259 patch_kernel "$FLAGS_image" "$DST_KERNEL" | |
260 fi | |
261 DST_ROOT=$(extract_partition_to_temp_file "$FLAGS_image" 3) | |
262 | |
263 GENERATOR="${SCRIPTS_DIR}/mk_memento_images.sh" | |
264 | |
265 CROS_GENERATE_UPDATE_PAYLOAD_CALLED=1 "$GENERATOR" "$DST_KERNEL" "$DST_ROOT" | |
266 mv "$(dirname "$DST_KERNEL")/update.gz" "$FLAGS_output" | |
267 | |
268 trap - INT TERM EXIT | |
269 cleanup noexit | |
270 echo "Done generating full update." | |
271 fi | |
OLD | NEW |