OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 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 |
| 9 # update. |
| 10 |
| 11 set -e |
| 12 |
| 13 # --- BEGIN COMMON.SH BOILERPLATE --- |
| 14 # Load common CrOS utilities. Inside the chroot this file is installed in |
| 15 # /usr/lib/crosutils. Outside the chroot we find it relative to the script's |
| 16 # location. |
| 17 find_common_sh() { |
| 18 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) |
| 19 local path |
| 20 |
| 21 SCRIPT_ROOT= |
| 22 for path in "${common_paths[@]}"; do |
| 23 if [ -r "${path}/common.sh" ]; then |
| 24 SCRIPT_ROOT=${path} |
| 25 break |
| 26 fi |
| 27 done |
| 28 } |
| 29 |
| 30 find_common_sh |
| 31 . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) |
| 32 # --- END COMMON.SH BOILERPLATE --- |
| 33 |
| 34 # Load functions designed for image processing |
| 35 if ! . "${SCRIPT_ROOT}/lib/cros_image_common.sh"; then |
| 36 echo "ERROR: Cannot load required library: lib/cros_image_common.sh; Abort." |
| 37 exit 1 |
| 38 fi |
| 39 |
| 40 if [ -z "$2" -o -z "$1" ] || [ "${#@}" -ne 2 -a "${#@}" -ne 3 ]; then |
| 41 echo "usage: $0 path/to/kernel_partition_img path/to/rootfs_partition_img" |
| 42 echo " or $0 path/to/chromiumos_img kern_part_no rootfs_part_no" |
| 43 exit 1 |
| 44 fi |
| 45 |
| 46 if [ "$CROS_GENERATE_UPDATE_PAYLOAD_CALLED" != "1" ]; then |
| 47 echo "WARNING:" |
| 48 echo " This script should only be called from cros_generate_update_payload" |
| 49 echo " Please run that script with --help to see how to use it." |
| 50 fi |
| 51 |
| 52 if ! image_has_command pigz; then |
| 53 (echo "WARNING:" |
| 54 echo " Your system does not have pigz (parallel gzip) installed." |
| 55 echo " COMPRESSING WILL BE VERY SLOW. It is recommended to install pigz" |
| 56 if image_has_command apt-get; then |
| 57 echo " by 'sudo apt-get install pigz'." |
| 58 elif image_has_command emerge; then |
| 59 echo " by 'sudo emerge pigz'." |
| 60 fi) >&2 |
| 61 fi |
| 62 |
| 63 if [ $(whoami) = "root" ]; then |
| 64 echo "running $0 as root which is unneccessary" |
| 65 fi |
| 66 |
| 67 # Determine the offset size, and file name of parameters |
| 68 if [ -z "$3" ]; then |
| 69 # kernnel_img rootfs_img |
| 70 KPART="$1" |
| 71 ROOT_PART="$2" |
| 72 KPART_SIZE=$(stat -c%s "$KPART") |
| 73 ROOT_PART_SIZE=$(stat -c%s "$ROOT_PART") |
| 74 KPART_OFFSET=0 |
| 75 KPART_SECTORS=$((KPART_SIZE / 512)) |
| 76 ROOT_OFFSET=0 |
| 77 ROOT_SECTORS=$((ROOT_PART_SIZE / 512)) |
| 78 else |
| 79 # chromiumos_img kern_part_no rootfs_part_no |
| 80 KPART="$1" |
| 81 ROOT_PART="$1" |
| 82 KPART_OFFSET="$(image_part_offset "$KPART" "$2")" || |
| 83 image_die "cannot retieve kernel partition offset" |
| 84 KPART_SECTORS="$(image_part_size "$KPART" "$2")" || |
| 85 image_die "cannot retieve kernel partition size" |
| 86 ROOT_OFFSET="$(image_part_offset "$ROOT_PART" "$3")" || |
| 87 image_die "cannot retieve root partition offset" |
| 88 ROOT_SECTORS="$(image_part_size "$ROOT_PART" "$3")" || |
| 89 image_die "cannot retieve root partition size" |
| 90 KPART_SIZE=$((KPART_SECTORS * 512)) |
| 91 fi |
| 92 |
| 93 # Sanity check size. |
| 94 if [ "$KPART_SIZE" -gt $((16 * 1024 * 1024)) ]; then |
| 95 echo "Kernel partition size ($KPART_SIZE bytes) greater than 16 MiB." |
| 96 echo "That's too big." |
| 97 exit 1 |
| 98 fi |
| 99 |
| 100 FINAL_OUT_FILE=$(dirname "$1")/update.gz |
| 101 |
| 102 # Update payload format: |
| 103 # [kernel_size: big-endian uint64][kernel_blob][rootfs_blob] |
| 104 |
| 105 # Prepare kernel_size by using printf as a number like 00000000003d0900, then |
| 106 # sed to convert as: \x00\x00\x00\x00\x00\x3d\x09\x00, finally echo -e to |
| 107 # convert into binary. |
| 108 KPART_SIZE_SIGNATURE="$(printf "%016x" "$KPART_SIZE" | |
| 109 sed 's/\([0-9a-f][0-9a-f]\)/\\x\1/g')" |
| 110 |
| 111 # Build the blob! |
| 112 CS_AND_RET_CODES="$( |
| 113 (echo -en "$KPART_SIZE_SIGNATURE" |
| 114 echo "Compressing kernel..." >&2 |
| 115 image_dump_partial_file "$KPART" "$KPART_OFFSET" "$KPART_SECTORS" |
| 116 echo "Compressing rootfs..." >&2 |
| 117 image_dump_partial_file "$ROOT_PART" "$ROOT_OFFSET" "$ROOT_SECTORS") | |
| 118 image_gzip_compress -c -9 | |
| 119 tee "$FINAL_OUT_FILE" | |
| 120 openssl sha1 -binary | |
| 121 openssl base64 | |
| 122 tr '\n' ' ' |
| 123 echo ${PIPESTATUS[*]})" |
| 124 |
| 125 EXPECTED_RET_CODES="0 0 0 0 0 0" |
| 126 set -- $CS_AND_RET_CODES |
| 127 CALC_CS="$1" |
| 128 shift |
| 129 RET_CODES="$@" |
| 130 if [ "$RET_CODES" != "$EXPECTED_RET_CODES" ]; then |
| 131 echo compression/hash failed. $RET_CODES |
| 132 exit 1 |
| 133 fi |
| 134 |
| 135 echo Success. hash is "$CALC_CS" |
OLD | NEW |