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 # Load common constants. This should be the first executable line. | |
11 # The path to common.sh should be relative to your script's location. | |
12 . "$(dirname "$0")/common.sh" | |
13 | |
14 # Load functions and constants for chromeos-install | |
15 . "$(dirname "$0")/chromeos-common.sh" | |
16 | |
17 SRC_MNT="" | |
18 DST_MNT="" | |
19 SRC_KERNEL="" | |
20 SRC_ROOT="" | |
21 DST_KERNEL="" | |
22 DST_ROOT="" | |
23 STATE_MNT="" | |
24 STATE_LOOP_DEV="" | |
25 | |
26 # Pass an arg to not exit 1 at the end | |
27 cleanup() { | |
28 set +e | |
29 echo "Cleaning up" | |
30 if [ -n "$SRC_MNT" ]; then | |
31 sudo umount -d "$SRC_MNT" | |
32 [ -d "$SRC_MNT" ] && rmdir "$SRC_MNT" | |
33 SRC_MNT="" | |
34 fi | |
35 if [ -n "$DST_MNT" ]; then | |
36 sudo umount -d "$DST_MNT" | |
37 [ -d "$DST_MNT" ] && rmdir "$DST_MNT" | |
38 DST_MNT="" | |
39 fi | |
40 if [ -n "$STATE_MNT" ]; then | |
41 sudo umount "$STATE_MNT" | |
42 [ -d "$STATE_MNT" ] && rmdir "$STATE_MNT" | |
43 STATE_MNT="" | |
44 fi | |
45 if [ -n "$STATE_LOOP_DEV" ]; then | |
46 sudo losetup -d "$STATE_LOOP_DEV" | |
47 STATE_LOOP_DEV="" | |
48 fi | |
49 rm -f "$SRC_KERNEL" | |
50 rm -f "$SRC_ROOT" | |
51 rm -f "$DST_KERNEL" | |
52 rm -f "$DST_ROOT" | |
53 [ -n "$1" ] || exit 1 | |
54 } | |
55 | |
56 extract_partition_to_temp_file() { | |
57 local filename="$1" | |
58 local partition="$2" | |
59 local temp_file=$(mktemp /tmp/generate_update_payload.XXXXXX) | |
60 | |
61 local offset=$(partoffset "${filename}" ${partition}) # 512-byte sectors | |
62 local length=$(partsize "${filename}" ${partition}) # 512-byte sectors | |
63 local bs=512 | |
64 local sectors_per_two_mib=$((2 * 1024 * 1024 / 512)) | |
65 if [ $(( $offset % $sectors_per_two_mib )) -eq 0 -a \ | |
66 $(( $length % $sectors_per_two_mib )) -eq 0 ]; then | |
67 bs=$((2 * 1024 * 1024)) | |
68 offset=$(($offset / $sectors_per_two_mib)) | |
69 length=$(($length / $sectors_per_two_mib)) | |
70 else | |
71 warn "partition offset or length not at 2MiB boundary" | |
72 fi | |
73 dd if="$filename" of="$temp_file" bs=$bs count="$length" skip="$offset" | |
74 echo "$temp_file" | |
75 } | |
76 | |
77 patch_kernel() { | |
78 local IMAGE="$1" | |
79 local KERN_FILE="$2" | |
80 | |
81 STATE_LOOP_DEV=$(sudo losetup -f) | |
82 if [ -z "$STATE_LOOP_DEV" ]; then | |
petkov
2010/08/06 06:53:00
[ -n "$STATE_LOOP_DEV" ] || die "no free loop devi
| |
83 echo "no free loop device." | |
84 exit 1 | |
85 fi | |
86 local offset=$(partoffset "${IMAGE}" 1) | |
87 offset=$(($offset * 512)) | |
88 sudo losetup -o "$offset" "$STATE_LOOP_DEV" "$IMAGE" | |
89 STATE_MNT=$(mktemp -d /tmp/state.XXXXXX) | |
90 sudo mount "$STATE_LOOP_DEV" "$STATE_MNT" | |
91 dd if="$STATE_MNT"/vmlinuz_hd.vblock of="$KERN_FILE" conv=notrunc | |
92 sudo umount "$STATE_MNT" | |
93 STATE_MNT="" | |
94 sudo losetup -d "$STATE_LOOP_DEV" | |
95 STATE_LOOP_DEV="" | |
96 } | |
97 | |
98 # We should be in the chroot. | |
99 assert_inside_chroot | |
100 | |
101 DEFINE_string image "" "The image that should be sent to clients." | |
102 DEFINE_string src_image "" "Optional: a source image. If specified, this makes\ | |
103 a delta update." | |
104 DEFINE_boolean old_style "$FLAGS_TRUE" "Generate an old-style .gz full update." | |
105 DEFINE_string output "" "Output file" | |
106 | |
107 # Parse command line | |
108 FLAGS "$@" || exit 1 | |
109 eval set -- "${FLAGS_ARGV}" | |
110 | |
111 set -e | |
112 | |
113 locate_gpt | |
114 | |
115 DELTA=$FLAGS_TRUE | |
116 [ -n "$FLAGS_output" ] || die \ | |
117 "Error: you must specify an output filename with --output FILENAME" | |
118 | |
119 if [ -z "$FLAGS_src_image" ]; then | |
120 DELTA=$FLAGS_FALSE | |
121 if [ "$FLAGS_old_style" = "$FLAGS_TRUE" ]; then | |
122 echo "Generating an old-style full update" | |
123 else | |
124 die "Generating a new-style full update not yet supported" | |
125 fi | |
126 fi | |
127 | |
128 if [ "$DELTA" -eq "$FLAGS_TRUE" ]; then | |
129 echo "Generating a delta update" | |
130 | |
131 # Sanity check that the real generator exists: | |
132 GENERATOR="$(dirname "$0")/../platform/update_engine/delta_generator" | |
133 [ -x "$GENERATOR" ] || die "$GENERATOR doesn't exist, or isn't executable" | |
134 | |
135 trap cleanup INT TERM EXIT | |
136 SRC_KERNEL=$(extract_partition_to_temp_file "$FLAGS_src_image" 2) | |
137 patch_kernel "$FLAGS_src_image" "$SRC_KERNEL" | |
138 SRC_ROOT=$(extract_partition_to_temp_file "$FLAGS_src_image" 3) | |
139 | |
140 echo md5sum of src kernel: | |
141 md5sum "$SRC_KERNEL" | |
142 echo md5sum of src root: | |
143 md5sum "$SRC_ROOT" | |
144 | |
145 DST_KERNEL=$(extract_partition_to_temp_file "$FLAGS_image" 2) | |
146 patch_kernel "$FLAGS_image" "$DST_KERNEL" | |
147 DST_ROOT=$(extract_partition_to_temp_file "$FLAGS_image" 3) | |
148 | |
149 SRC_MNT=$(mktemp -d /tmp/src_root.XXXXXX) | |
150 sudo mount -o loop,ro "$SRC_ROOT" "$SRC_MNT" | |
151 | |
152 DST_MNT=$(mktemp -d /tmp/src_root.XXXXXX) | |
153 sudo mount -o loop,ro "$DST_ROOT" "$DST_MNT" | |
154 | |
155 sudo "$GENERATOR" \ | |
156 -new_dir "$DST_MNT" -new_image "$DST_ROOT" -new_kernel "$DST_KERNEL" \ | |
157 -old_dir "$SRC_MNT" -old_image "$SRC_ROOT" -old_kernel "$SRC_KERNEL" \ | |
158 -out_file "$FLAGS_output" | |
159 | |
160 trap - INT TERM EXIT | |
161 cleanup noexit | |
162 echo "Done generating delta." | |
163 else | |
164 echo "Generating full update" | |
165 | |
166 trap cleanup INT TERM EXIT | |
167 DST_KERNEL=$(extract_partition_to_temp_file "$FLAGS_image" 2) | |
168 patch_kernel "$FLAGS_image" "$DST_KERNEL" | |
169 DST_ROOT=$(extract_partition_to_temp_file "$FLAGS_image" 3) | |
170 | |
171 GENERATOR="$(dirname "$0")/mk_memento_images.sh" | |
172 | |
173 "$GENERATOR" "$DST_KERNEL" "$DST_ROOT" | |
174 mv "$(dirname "$DST_KERNEL")/update.gz" "$FLAGS_output" | |
175 | |
176 trap - INT TERM EXIT | |
177 cleanup noexit | |
178 echo "Done generating full update." | |
179 fi | |
OLD | NEW |