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