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 modify a pristine/dev Chrome OS image to be used for recovery | |
8 | |
9 . "/usr/lib/crosutils/common.sh" | |
10 | |
11 # Load functions and constants for chromeos-install | |
12 . "/usr/lib/installer/chromeos-common.sh" | |
13 | |
14 # Script must be run inside the chroot. | |
15 restart_in_chroot_if_needed $* | |
16 | |
17 get_default_board | |
18 | |
19 RECOVERY_IMAGE="recovery_image.bin" | |
20 | |
21 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built" | |
22 DEFINE_string image "" "Location of the rootfs raw image file" | |
23 DEFINE_string output "${RECOVERY_IMAGE}" \ | |
24 "(optional) output image name. Default: ${RECOVERY_IMAGE}" | |
25 | |
26 # Parse command line | |
27 FLAGS "$@" || exit 1 | |
28 eval set -- "${FLAGS_ARGV}" | |
29 | |
30 # No board, no default and no image set then we can't find the image | |
31 if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then | |
32 setup_board_warning | |
33 die "cros_mod_image_for_recovery failed. No board set and no image set" | |
34 fi | |
35 | |
36 # We have a board name but no image set. Use image at default location | |
37 if [ -z $FLAGS_image ] ; then | |
38 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" | |
39 FILENAME="chromiumos_image.bin" | |
40 FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" | |
41 fi | |
42 | |
43 # Turn path into an absolute path. | |
44 FLAGS_image=$(eval readlink -f ${FLAGS_image}) | |
45 | |
46 # Abort early if we can't find the image | |
47 if [ ! -f $FLAGS_image ] ; then | |
48 echo "No image found at $FLAGS_image" | |
49 exit 1 | |
50 fi | |
51 | |
52 set -e | |
53 | |
54 # Constants | |
55 IMAGE_DIR="$(dirname "$FLAGS_image")" | |
56 IMAGE_NAME="$(basename "$FLAGS_image")" | |
57 | |
58 # loop device utility methods mostly duplicated from | |
59 # src/platform/installer/chromeos-install | |
60 # TODO(tgao): minimize duplication by refactoring these methods into a separate | |
61 # library script which both scripts can reference | |
62 | |
63 # Set up loop device for an image file at specified offset | |
64 loop_offset_setup() { | |
65 local filename=$1 | |
66 local offset=$2 # 512-byte sectors | |
67 | |
68 LOOP_DEV=$(sudo losetup -f) | |
69 if [ -z "$LOOP_DEV" ] | |
70 then | |
71 echo "No free loop device. Free up a loop device or reboot. Exiting." | |
72 exit 1 | |
73 fi | |
74 | |
75 sudo losetup -o $(($offset * 512)) ${LOOP_DEV} ${filename} | |
76 } | |
77 | |
78 loop_offset_cleanup() { | |
79 sudo losetup -d ${LOOP_DEV} || /bin/true | |
80 } | |
81 | |
82 mount_on_loop_dev() { | |
83 TMPMNT=$(mktemp -d) | |
84 sudo mount ${LOOP_DEV} ${TMPMNT} | |
85 } | |
86 | |
87 # Unmount loop-mounted device. | |
88 umount_from_loop_dev() { | |
89 mount | grep -q " on ${TMPMNT} " && sudo umount ${TMPMNT} | |
90 } | |
91 | |
92 # Undo both mount and loop. | |
93 my_cleanup() { | |
94 umount_from_loop_dev | |
95 loop_offset_cleanup | |
96 } | |
97 | |
98 # Modifies an existing image for recovery use | |
99 update_recovery_packages() { | |
100 local image_name=$1 | |
101 | |
102 echo "Modifying image ${image_name} for recovery use" | |
103 | |
104 locate_gpt # set $GPT env var | |
105 loop_offset_setup ${image_name} $(partoffset "${image_name}" 1) | |
106 trap loop_offset_cleanup EXIT | |
107 mount_on_loop_dev "readwrite" | |
108 trap my_cleanup EXIT | |
109 sudo touch ${TMPMNT}/.recovery | |
110 umount_from_loop_dev | |
111 trap loop_offset_cleanup EXIT | |
112 loop_offset_cleanup | |
113 trap - EXIT | |
114 } | |
115 | |
116 # Main | |
117 | |
118 DST_PATH="${IMAGE_DIR}/${FLAGS_output}" | |
119 echo "Making a copy of original image ${FLAGS_image}" | |
120 cp $FLAGS_image $DST_PATH | |
121 update_recovery_packages $DST_PATH | |
122 | |
123 echo "Recovery image created at ${DST_PATH}" | |
OLD | NEW |