Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1242)

Unified Diff: mod_image_for_recovery.sh

Issue 3391018: Issue 6759: reduce size of stateful partition for non-developer recovery image (Closed) Base URL: http://git.chromium.org/git/crosutils.git
Patch Set: incorporate wad's algo for resizing Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mod_image_for_dev_recovery.sh ('k') | resize_stateful_partition.sh » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mod_image_for_recovery.sh
diff --git a/mod_image_for_recovery.sh b/mod_image_for_recovery.sh
index 6acf46daa80b338d1cce948101e2f56219e280e7..7d59095753a2e78dd2e14084fb5e274e292a3c9c 100755
--- a/mod_image_for_recovery.sh
+++ b/mod_image_for_recovery.sh
@@ -6,16 +6,15 @@
# Script to modify a pristine/dev Chrome OS image to be used for recovery
-. "$(dirname "$0")/common.sh"
-
-# Load functions and constants for chromeos-install
-. "$(dirname "$0")/chromeos-common.sh"
+. "$(dirname "$0")/resize_stateful_partition.sh"
# Script must be run inside the chroot.
restart_in_chroot_if_needed $*
get_default_board
+# Constants
+TEMP_IMG=$(mktemp "/tmp/temp_img.XXXXXX")
RECOVERY_IMAGE="recovery_image.bin"
DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built"
@@ -33,7 +32,7 @@ if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then
die "mod_image_for_recovery failed. No board set and no image set"
fi
-# We have a board name but no image set. Use image at default location
+# We have a board name but no image set. Use image at default location
if [ -z $FLAGS_image ] ; then
IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
FILENAME="chromiumos_image.bin"
@@ -49,75 +48,73 @@ if [ ! -f $FLAGS_image ] ; then
exit 1
fi
+set -u
set -e
# Constants
IMAGE_DIR="$(dirname "$FLAGS_image")"
-IMAGE_NAME="$(basename "$FLAGS_image")"
-
-# loop device utility methods mostly duplicated from
-# src/platform/installer/chromeos-install
-# TODO(tgao): minimize duplication by refactoring these methods into a separate
-# library script which both scripts can reference
-
-# Set up loop device for an image file at specified offset
-loop_offset_setup() {
- local filename=$1
- local offset=$2 # 512-byte sectors
-
- LOOP_DEV=$(sudo losetup -f)
- if [ -z "$LOOP_DEV" ]
- then
- echo "No free loop device. Free up a loop device or reboot. Exiting."
- exit 1
- fi
-
- sudo losetup -o $(($offset * 512)) ${LOOP_DEV} ${filename}
-}
-
-loop_offset_cleanup() {
- sudo losetup -d ${LOOP_DEV} || /bin/true
-}
-
-mount_on_loop_dev() {
- TMPMNT=$(mktemp -d)
- sudo mount ${LOOP_DEV} ${TMPMNT}
-}
-# Unmount loop-mounted device.
-umount_from_loop_dev() {
- mount | grep -q " on ${TMPMNT} " && sudo umount ${TMPMNT}
-}
-
-# Undo both mount and loop.
-my_cleanup() {
- umount_from_loop_dev
- loop_offset_cleanup
-}
-
-# Modifies an existing image for recovery use
-update_recovery_packages() {
- local image_name=$1
-
- echo "Modifying image ${image_name} for recovery use"
-
- locate_gpt # set $GPT env var
- loop_offset_setup ${image_name} $(partoffset "${image_name}" 1)
- trap loop_offset_cleanup EXIT
- mount_on_loop_dev "readwrite"
- trap my_cleanup EXIT
- sudo touch ${TMPMNT}/.recovery
- umount_from_loop_dev
- trap loop_offset_cleanup EXIT
- loop_offset_cleanup
+# Creates a dev recovery image using an existing dev install shim
+# If successful, content of --payload_dir is copied to a directory named
+# "dev_payload" under the root of stateful partition.
+create_recovery_image() {
+ local src_img=$1 # base image
+ local src_state=$(mktemp "/tmp/src_state.XXXXXX")
+ local stateful_offset=$(partoffset ${src_img} 1)
+ local stateful_count=$(partsize ${src_img} 1)
+
+ dd if="${src_img}" of="${src_state}" conv=notrunc bs=512 \
+ skip=${stateful_offset} count=${stateful_count}
+
+ # Mount original stateful partition to figure out its actual size
+ local src_loop_dev=$(get_loop_dev)
+ trap "cleanup_loop_dev ${src_loop_dev}" EXIT
+
+ # Setup loop dev
+ sudo losetup $src_loop_dev $src_state
+ local block_size=$(sudo /sbin/dumpe2fs $src_loop_dev | grep "Block size:" \
+ | tr -d ' ' | cut -f2 -d:)
+ echo "block_size = $block_size"
+ local min_size=$(sudo /sbin/resize2fs -P $src_loop_dev | tr -d ' ' \
+ | cut -f2 -d:)
+ echo "min_size = $min_size $block_size blocks"
+
+ # Add 20%, convert to 512-byte sectors and round up to 2Mb boundary
+ local min_sectors=$(roundup $(((min_size * block_size * 120) / (512 * 100))))
+ echo "min_sectors = ${min_sectors} 512-byte blocks"
+ sudo e2fsck -fp "${src_loop_dev}"
+ # Resize using 512-byte sectors
+ sudo /sbin/resize2fs $src_loop_dev ${min_sectors}s
+
+ # Delete the loop
trap - EXIT
+ cleanup_loop_dev ${src_loop_dev}
+
+ # Truncate the image at the new size
+ dd if=/dev/zero of=$src_state bs=512 seek=$min_sectors count=0
+
+ # Mount and touch .recovery # Soon not to be needed :/
+ local new_mnt=$(mktemp -d "/tmp/src_mnt.XXXXXX")
+ mkdir -p "${new_mnt}"
+ local new_loop_dev=$(get_loop_dev)
+ trap "cleanup_loop_dev ${new_loop_dev} && rmdir ${new_mnt} && \
+ rm -f ${src_state}" EXIT
+ sudo mount -o loop=${new_loop_dev} "${src_state}" "${new_mnt}"
+ trap "umount_from_loop_dev ${new_mnt} && rm -f ${src_state}" EXIT
+ sudo touch "${new_mnt}/.recovery"
+
+ (update_partition_table $src_img $src_state $min_sectors $TEMP_IMG)
+ # trap handler will handle unmount and clean up of loop device and temp files
}
# Main
-
DST_PATH="${IMAGE_DIR}/${FLAGS_output}"
echo "Making a copy of original image ${FLAGS_image}"
-cp $FLAGS_image $DST_PATH
-update_recovery_packages $DST_PATH
+(create_recovery_image $FLAGS_image)
-echo "Recovery image created at ${DST_PATH}"
+if [ -n ${TEMP_IMG} ] && [ -f ${TEMP_IMG} ]; then
+ mv -f $TEMP_IMG $DST_PATH
+ echo "Recovery image created at ${DST_PATH}"
+else
+ echo "Failed to create recovery image"
+fi
« no previous file with comments | « mod_image_for_dev_recovery.sh ('k') | resize_stateful_partition.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698