OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 # This script takes a path to a rootfs.ext2 which was generated by | 7 # This script takes a path to a rootfs.ext2 which was generated by |
8 # build_image.sh and generates an image that can be used for auto | 8 # build_image.sh and generates an image that can be used for auto |
9 # update. | 9 # update. |
10 | 10 |
11 set -e | 11 set -e |
12 | 12 |
13 if [ -z "$1" ] | 13 if [ -z "$2" -o -z "$1" ]; then |
14 then | 14 echo "usage: $0 path/to/kernel_partition_img path/to/rootfs_partition_img" |
15 echo "usage: $0 path/to/rootfs.image" | |
16 exit 1 | 15 exit 1 |
17 fi | 16 fi |
18 | 17 |
19 if [ $(whoami) = "root" ] | 18 if [ $(whoami) = "root" ]; then |
20 then | |
21 echo "running $0 as root which is unneccessary" | 19 echo "running $0 as root which is unneccessary" |
22 fi | 20 fi |
23 | 21 |
| 22 KPART="$1" |
| 23 ROOT_PART="$2" |
| 24 |
| 25 KPART_SIZE=$(stat -c%s "$KPART") |
| 26 |
| 27 # Sanity check size. |
| 28 if [ "$KPART_SIZE" -gt $((16 * 1024 * 1024)) ]; then |
| 29 echo "Kernel partition size ($KPART_SIZE bytes) greater than 16 MiB." |
| 30 echo "That's too big." |
| 31 exit 1 |
| 32 fi |
| 33 |
24 FINAL_OUT_FILE=$(dirname "$1")/update.gz | 34 FINAL_OUT_FILE=$(dirname "$1")/update.gz |
25 UNCOMPRESSED_OUT_FILE="$FINAL_OUT_FILE.uncompressed" | 35 UNCOMPRESSED_OUT_FILE="$FINAL_OUT_FILE.uncompressed" |
26 | 36 |
27 ORIGINAL_LABEL=$(/sbin/e2label "$1") | 37 # First, write size of kernel partition in big endian as uint64 to out file |
| 38 # printf converts it to a number like 00000000003d0900. sed converts it to: |
| 39 # \\x00\\x00\\x00\\x00\\x00\\x3d\\x09\\x00, then xargs converts it to binary |
| 40 # with echo. |
| 41 printf %016x "$KPART_SIZE" | \ |
| 42 sed 's/\([0-9a-f][0-9a-f]\)/\\\\x\1/g' | \ |
| 43 xargs echo -ne > "$UNCOMPRESSED_OUT_FILE" |
28 | 44 |
29 # copy original over to the new file | 45 # Next, write kernel partition to the out file |
30 cp "$1" "$UNCOMPRESSED_OUT_FILE" | 46 cat "$KPART" >> "$UNCOMPRESSED_OUT_FILE" |
31 | 47 |
32 # Fix up the file system label. We prefix with 'A' | 48 # Sanity check size of output file now |
| 49 if [ $(stat -c%s "$UNCOMPRESSED_OUT_FILE") -ne $((8 + $KPART_SIZE)) ]; then |
| 50 echo "Kernel partition changed size during image generation. Aborting." |
| 51 exit 1 |
| 52 fi |
| 53 |
| 54 # Copy rootfs aside |
| 55 TMP_ROOTFS=$(mktemp) |
| 56 cp "$ROOT_PART" "$TMP_ROOTFS" |
| 57 ORIGINAL_LABEL=$(/sbin/e2label "$TMP_ROOTFS") |
33 NEW_LABEL="A${ORIGINAL_LABEL}" | 58 NEW_LABEL="A${ORIGINAL_LABEL}" |
34 /sbin/tune2fs -L "$NEW_LABEL" "$UNCOMPRESSED_OUT_FILE" | 59 /sbin/tune2fs -L "$NEW_LABEL" "$TMP_ROOTFS" |
| 60 |
| 61 # TODO(adlr): Sign TMP_ROOTFS w/ OS vendor's private key |
| 62 |
| 63 # Put rootfs into the out file |
| 64 cat "$TMP_ROOTFS" >> "$UNCOMPRESSED_OUT_FILE" |
| 65 |
| 66 rm "$TMP_ROOTFS" |
35 | 67 |
36 # compress and hash | 68 # compress and hash |
37 CS_AND_RET_CODES=$(gzip -c "$UNCOMPRESSED_OUT_FILE" | \ | 69 CS_AND_RET_CODES=$(gzip -c "$UNCOMPRESSED_OUT_FILE" | \ |
38 tee "$FINAL_OUT_FILE" | openssl sha1 -binary | \ | 70 tee "$FINAL_OUT_FILE" | openssl sha1 -binary | \ |
39 openssl base64 | tr '\n' ' '; \ | 71 openssl base64 | tr '\n' ' '; \ |
40 echo ${PIPESTATUS[*]}) | 72 echo ${PIPESTATUS[*]}) |
41 EXPECTED_RET_CODES="0 0 0 0 0" | 73 EXPECTED_RET_CODES="0 0 0 0 0" |
42 set -- $CS_AND_RET_CODES | 74 set -- $CS_AND_RET_CODES |
43 CALC_CS="$1" | 75 CALC_CS="$1" |
44 shift | 76 shift |
45 RET_CODES="$@" | 77 RET_CODES="$@" |
46 if [ "$RET_CODES" != "$EXPECTED_RET_CODES" ] | 78 if [ "$RET_CODES" != "$EXPECTED_RET_CODES" ]; then |
47 then | |
48 echo compression/hash failed. $RET_CODES | 79 echo compression/hash failed. $RET_CODES |
49 exit 1 | 80 exit 1 |
50 fi | 81 fi |
51 | 82 |
52 rm "$UNCOMPRESSED_OUT_FILE" | 83 rm "$UNCOMPRESSED_OUT_FILE" |
53 | 84 |
54 echo Success. hash is "$CALC_CS" | 85 echo Success. hash is "$CALC_CS" |
OLD | NEW |