OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 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 # Script to create a Chrome OS dev recovery image using a dev install shim | 7 # Script to create a Chrome OS dev recovery image using a dev install shim |
8 | 8 |
9 # Source constants and utility functions | 9 # Source constants and utility functions |
10 . "$(dirname "$0")/common.sh" | 10 . "$(dirname "$0")/resize_stateful_partition.sh" |
11 . "$(dirname "$0")/chromeos-common.sh" | |
12 | 11 |
13 get_default_board | 12 get_default_board |
14 locate_gpt | 13 |
| 14 # Constants |
| 15 TEMP_IMG=$(mktemp "/tmp/temp_img.XXXXXX") |
15 | 16 |
16 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built \ | 17 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built \ |
17 Default: ${DEFAULT_BOARD}" | 18 Default: ${DEFAULT_BOARD}" |
18 DEFINE_string dev_install_shim "" "Path of the developer install shim. \ | 19 DEFINE_string dev_install_shim "" "Path of the developer install shim. \ |
19 Default: (empty)" | 20 Default: (empty)" |
20 DEFINE_string payload_dir "" "Directory containing developer payload and \ | 21 DEFINE_string payload_dir "" "Directory containing developer payload and \ |
21 (optionally) a custom install script. Default: (empty)" | 22 (optionally) a custom install script. Default: (empty)" |
22 | 23 |
23 # Parse command line | 24 # Parse command line |
24 FLAGS "$@" || exit 1 | 25 FLAGS "$@" || exit 1 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 if [ ! -f $FLAGS_dev_install_shim ] ; then | 67 if [ ! -f $FLAGS_dev_install_shim ] ; then |
67 die "No dev install shim found at $FLAGS_dev_install_shim" | 68 die "No dev install shim found at $FLAGS_dev_install_shim" |
68 else | 69 else |
69 info "Using a recent dev install shim at ${FLAGS_dev_install_shim}" | 70 info "Using a recent dev install shim at ${FLAGS_dev_install_shim}" |
70 fi | 71 fi |
71 | 72 |
72 # Constants | 73 # Constants |
73 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" | 74 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" |
74 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" | 75 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" |
75 | 76 |
76 umount_from_loop_dev() { | |
77 local mnt_pt=$1 | |
78 mount | grep -q " on ${mnt_pt}" && sudo umount ${mnt_pt} | |
79 rmdir ${mnt_pt} | |
80 } | |
81 | |
82 cleanup_loop_dev() { | |
83 sudo losetup -d ${1} || /bin/true | |
84 } | |
85 | |
86 get_loop_dev() { | |
87 local loop_dev=$(sudo losetup -f) | |
88 if [ -z "${loop_dev}" ]; then | |
89 die "No free loop device. Free up a loop device or reboot. Exiting." | |
90 fi | |
91 echo ${loop_dev} | |
92 } | |
93 | |
94 # Resize stateful partition of install shim to hold payload content | |
95 # Due to this resize, we need to create a new partition table and a new image. | |
96 # (see update_partition_table() for details) | |
97 resize_partition() { | |
98 local source_part=$1 # source partition | |
99 local add_num_sectors=$2 # number of 512-byte sectors to be added | |
100 | |
101 local source_sectors=$(roundup $(numsectors ${source_part})) | |
102 info "source partition has ${source_sectors} 512-byte sectors." | |
103 local resized_sectors=$(roundup $(expr $source_sectors + $add_num_sectors)) | |
104 info "resized partition has ${resized_sectors} 512-byte sectors." | |
105 | |
106 local loop_dev=$(get_loop_dev) | |
107 trap "cleanup_loop_dev ${loop_dev}" EXIT | |
108 | |
109 # Extend the source file size to the new size. | |
110 dd if=/dev/zero of="${source_part}" bs=1 count=1 \ | |
111 seek=$((512 * ${resized_sectors} - 1)) &>/dev/null | |
112 | |
113 # Resize the partition. | |
114 sudo losetup "${loop_dev}" "${source_part}" | |
115 sudo e2fsck -fp "${loop_dev}" &> /dev/null | |
116 sudo resize2fs "${loop_dev}" &> /dev/null | |
117 # trap handler will clean up the loop device | |
118 echo "${resized_sectors}" | |
119 } | |
120 | |
121 # Update partition table with resized stateful partition and create the final | |
122 # dev recovery image | |
123 update_partition_table() { | |
124 local temp_state=$1 # stateful partition image | |
125 local resized_sectors=$2 # number of sectors in resized stateful partition | |
126 local temp_img=$(mktemp "/tmp/temp_img.XXXXXX") | |
127 | |
128 local kernel_offset=$(partoffset ${FLAGS_dev_install_shim} 2) | |
129 local kernel_count=$(partsize ${FLAGS_dev_install_shim} 2) | |
130 local rootfs_offset=$(partoffset ${FLAGS_dev_install_shim} 3) | |
131 local rootfs_count=$(partsize ${FLAGS_dev_install_shim} 3) | |
132 local oem_offset=$(partoffset ${FLAGS_dev_install_shim} 8) | |
133 local oem_count=$(partsize ${FLAGS_dev_install_shim} 8) | |
134 local esp_offset=$(partoffset ${FLAGS_dev_install_shim} 12) | |
135 local esp_count=$(partsize ${FLAGS_dev_install_shim} 12) | |
136 | |
137 local temp_pmbr=$(mktemp "/tmp/pmbr.XXXXXX") | |
138 dd if="${FLAGS_dev_install_shim}" of="${temp_pmbr}" bs=512 count=1 &>/dev/null | |
139 | |
140 # Set up a new partition table | |
141 install_gpt "${temp_img}" "${rootfs_count}" "${resized_sectors}" \ | |
142 "${temp_pmbr}" "${esp_count}" false $(roundup ${rootfs_count}) &>/dev/null | |
143 | |
144 rm -rf "${temp_pmbr}" | |
145 | |
146 # Copy into the partition parts of the file | |
147 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | |
148 seek="${START_ROOTFS_A}" skip=${rootfs_offset} count=${rootfs_count} \ | |
149 &>/dev/null | |
150 dd if="${temp_state}" of="${temp_img}" conv=notrunc bs=512 \ | |
151 seek="${START_STATEFUL}" &>/dev/null | |
152 # Copy the full kernel (i.e. with vboot sections) | |
153 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | |
154 seek="${START_KERN_A}" skip=${kernel_offset} count=${kernel_count} \ | |
155 &>/dev/null | |
156 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | |
157 seek="${START_OEM}" skip=${oem_offset} count=${oem_count} &>/dev/null | |
158 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | |
159 seek="${START_ESP}" skip=${esp_offset} count=${esp_count} &>/dev/null | |
160 | |
161 echo ${temp_img} | |
162 } | |
163 | |
164 # Creates a dev recovery image using an existing dev install shim | 77 # Creates a dev recovery image using an existing dev install shim |
165 # If successful, content of --payload_dir is copied to a directory named | 78 # If successful, content of --payload_dir is copied to a directory named |
166 # "dev_payload" under the root of stateful partition. | 79 # "dev_payload" under the root of stateful partition. |
167 create_dev_recovery_image() { | 80 create_dev_recovery_image() { |
168 local temp_state=$(mktemp "/tmp/temp_state.XXXXXX") | 81 local temp_state=$(mktemp "/tmp/temp_state.XXXXXX") |
169 local stateful_offset=$(partoffset ${FLAGS_dev_install_shim} 1) | 82 local stateful_offset=$(partoffset ${FLAGS_dev_install_shim} 1) |
170 local stateful_count=$(partsize ${FLAGS_dev_install_shim} 1) | 83 local stateful_count=$(partsize ${FLAGS_dev_install_shim} 1) |
171 dd if="${FLAGS_dev_install_shim}" of="${temp_state}" conv=notrunc bs=512 \ | 84 dd if="${FLAGS_dev_install_shim}" of="${temp_state}" conv=notrunc bs=512 \ |
172 skip=${stateful_offset} count=${stateful_count} &>/dev/null | 85 skip=${stateful_offset} count=${stateful_count} &>/dev/null |
173 | 86 |
174 local resized_sectors=$(resize_partition $temp_state $PAYLOAD_DIR_SIZE) | 87 local resized_sectors=$(enlarge_partition_image $temp_state $PAYLOAD_DIR_SIZE) |
175 | 88 |
176 # Mount resized stateful FS and copy payload content to its root directory | 89 # Mount resized stateful FS and copy payload content to its root directory |
177 local temp_mnt=$(mktemp -d "/tmp/temp_mnt.XXXXXX") | 90 local temp_mnt=$(mktemp -d "/tmp/temp_mnt.XXXXXX") |
178 local loop_dev=$(get_loop_dev) | 91 local loop_dev=$(get_loop_dev) |
179 trap "umount_from_loop_dev ${temp_mnt} && rm -f \"${temp_state}\"" EXIT | 92 trap "cleanup_loop_dev ${loop_dev}" EXIT |
180 mkdir -p "${temp_mnt}" | 93 mkdir -p "${temp_mnt}" |
181 sudo mount -o loop=${loop_dev} "${temp_state}" "${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 |
182 sudo cp -R "${FLAGS_payload_dir}" "${temp_mnt}" | 96 sudo cp -R "${FLAGS_payload_dir}" "${temp_mnt}" |
183 sudo mv "${temp_mnt}/$(basename ${FLAGS_payload_dir})" \ | 97 sudo mv "${temp_mnt}/$(basename ${FLAGS_payload_dir})" \ |
184 "${temp_mnt}/dev_payload" | 98 "${temp_mnt}/dev_payload" |
185 # Mark image as dev recovery | 99 # Mark image as dev recovery |
186 sudo touch "${temp_mnt}/.recovery" | 100 sudo touch "${temp_mnt}/.recovery" |
187 sudo touch "${temp_mnt}/.dev_recovery" | 101 sudo touch "${temp_mnt}/.dev_recovery" |
188 | 102 |
189 # TODO(tgao): handle install script (for default and custom cases) | 103 # TODO(tgao): handle install script (for default and custom cases) |
190 local temp_img=$(update_partition_table $temp_state $resized_sectors) | 104 (update_partition_table $FLAGS_dev_install_shim $temp_state \ |
| 105 $resized_sectors $TEMP_IMG) |
191 | 106 |
192 # trap handler will clean up loop device and temp mount point | 107 # trap handler will clean up loop device and temp mount point |
193 echo ${temp_img} | |
194 } | 108 } |
195 | 109 |
196 # Main | 110 # Main |
197 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" | 111 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" |
198 info "Attempting to create dev recovery image using dev install shim \ | 112 info "Attempting to create dev recovery image using dev install shim \ |
199 ${FLAGS_dev_install_shim}" | 113 ${FLAGS_dev_install_shim}" |
200 TEMP_IMG=$(create_dev_recovery_image) | 114 (create_dev_recovery_image) |
201 | 115 |
202 mv -f $TEMP_IMG $DST_PATH | 116 if [ -n ${TEMP_IMG} ] && [ -f ${TEMP_IMG} ]; then |
203 info "Dev recovery image created at ${DST_PATH}" | 117 mv -f $TEMP_IMG $DST_PATH |
| 118 info "Dev recovery image created at ${DST_PATH}" |
| 119 else |
| 120 info "Failed to create developer recovery image" |
| 121 fi |
OLD | NEW |