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 create a Chrome OS dev recovery image using a dev install shim | |
8 | |
9 # Source constants and utility functions | |
10 . "$(dirname "$0")/resize_stateful_partition.sh" | |
11 | |
12 get_default_board | |
13 | |
14 # Constants | |
15 TEMP_IMG=$(mktemp "/tmp/temp_img.XXXXXX") | |
16 | |
17 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built \ | |
18 Default: ${DEFAULT_BOARD}" | |
19 DEFINE_string dev_install_shim "" "Path of the developer install shim. \ | |
20 Default: (empty)" | |
21 DEFINE_string payload_dir "" "Directory containing developer payload and \ | |
22 (optionally) a custom install script. Default: (empty)" | |
23 | |
24 # Parse command line | |
25 FLAGS "$@" || exit 1 | |
26 eval set -- "${FLAGS_ARGV}" | |
27 | |
28 set -e | |
29 | |
30 # No board set and no default set then we can't proceed | |
31 if [ -z $FLAGS_board ] ; then | |
32 setup_board_warning | |
33 die "No board set" | |
34 fi | |
35 | |
36 # Abort early if --payload_dir is not set, invalid or empty | |
37 if [ -z $FLAGS_payload_dir ] ; then | |
38 die "flag --payload_dir not set" | |
39 fi | |
40 | |
41 if [ ! -d "${FLAGS_payload_dir}" ] ; then | |
42 die "${FLAGS_payload_dir} is not a directory" | |
43 fi | |
44 | |
45 PAYLOAD_DIR_SIZE= | |
46 if [ -z "$(ls -A $FLAGS_payload_dir)" ] ; then | |
47 die "${FLAGS_payload_dir} is empty" | |
48 else | |
49 # Get directory size in 512-byte sectors so we can resize stateful partition | |
50 PAYLOAD_DIR_SIZE=\ | |
51 "$(du --apparent-size -B 512 ${FLAGS_payload_dir} | awk '{print $1}')" | |
52 info "${FLAGS_payload_dir} has ${PAYLOAD_DIR_SIZE} 512-byte sectors" | |
53 fi | |
54 | |
55 DEV_INSTALL_SHIM="dev_install_shim.bin" | |
56 # We have a board name but no dev_install_shim set. Try find a recent one | |
57 if [ -z $FLAGS_dev_install_shim ] ; then | |
58 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" | |
59 FLAGS_dev_install_shim=\ | |
60 "${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${DEV_INSTALL_SHIM}" | |
61 fi | |
62 | |
63 # Turn relative path into an absolute path. | |
64 FLAGS_dev_install_shim=$(eval readlink -f ${FLAGS_dev_install_shim}) | |
65 | |
66 # Abort early if we can't find the install shim | |
67 if [ ! -f $FLAGS_dev_install_shim ] ; then | |
68 die "No dev install shim found at $FLAGS_dev_install_shim" | |
69 else | |
70 info "Using a recent dev install shim at ${FLAGS_dev_install_shim}" | |
71 fi | |
72 | |
73 # Constants | |
74 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" | |
75 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" | |
76 | |
77 # Creates a dev recovery image using an existing dev install shim | |
78 # If successful, content of --payload_dir is copied to a directory named | |
79 # "dev_payload" under the root of stateful partition. | |
80 create_dev_recovery_image() { | |
81 local temp_state=$(mktemp "/tmp/temp_state.XXXXXX") | |
82 local stateful_offset=$(partoffset ${FLAGS_dev_install_shim} 1) | |
83 local stateful_count=$(partsize ${FLAGS_dev_install_shim} 1) | |
84 dd if="${FLAGS_dev_install_shim}" of="${temp_state}" conv=notrunc bs=512 \ | |
85 skip=${stateful_offset} count=${stateful_count} &>/dev/null | |
86 | |
87 local resized_sectors=$(enlarge_partition_image $temp_state $PAYLOAD_DIR_SIZE) | |
88 | |
89 # Mount resized stateful FS and copy payload content to its root directory | |
90 local temp_mnt=$(mktemp -d "/tmp/temp_mnt.XXXXXX") | |
91 local loop_dev=$(get_loop_dev) | |
92 trap "cleanup_loop_dev ${loop_dev}" EXIT | |
93 mkdir -p "${temp_mnt}" | |
94 sudo mount -o loop=${loop_dev} "${temp_state}" "${temp_mnt}" | |
95 trap "umount_from_loop_dev ${temp_mnt} && rm -f \"${temp_state}\"" EXIT | |
96 sudo cp -R "${FLAGS_payload_dir}" "${temp_mnt}/dev_payload" | |
97 | |
98 # Mark image as dev recovery | |
99 sudo touch "${temp_mnt}/.recovery" | |
100 sudo touch "${temp_mnt}/.dev_recovery" | |
101 | |
102 # TODO(tgao): handle install script (for default and custom cases) | |
103 (update_partition_table $FLAGS_dev_install_shim $temp_state \ | |
104 $resized_sectors $TEMP_IMG) | |
105 | |
106 # trap handler will clean up loop device and temp mount point | |
107 } | |
108 | |
109 # Main | |
110 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" | |
111 info "Attempting to create dev recovery image using dev install shim \ | |
112 ${FLAGS_dev_install_shim}" | |
113 (create_dev_recovery_image) | |
114 | |
115 if [ -n ${TEMP_IMG} ] && [ -f ${TEMP_IMG} ]; then | |
116 mv -f $TEMP_IMG $DST_PATH | |
117 info "Dev recovery image created at ${DST_PATH}" | |
118 else | |
119 info "Failed to create developer recovery image" | |
120 fi | |
OLD | NEW |