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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mod_image_for_dev_recovery.sh ('k') | resize_stateful_partition.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 modify a pristine/dev Chrome OS image to be used for recovery 7 # Script to modify a pristine/dev Chrome OS image to be used for recovery
8 8
9 . "$(dirname "$0")/common.sh" 9 . "$(dirname "$0")/resize_stateful_partition.sh"
10
11 # Load functions and constants for chromeos-install
12 . "$(dirname "$0")/chromeos-common.sh"
13 10
14 # Script must be run inside the chroot. 11 # Script must be run inside the chroot.
15 restart_in_chroot_if_needed $* 12 restart_in_chroot_if_needed $*
16 13
17 get_default_board 14 get_default_board
18 15
16 # Constants
17 TEMP_IMG=$(mktemp "/tmp/temp_img.XXXXXX")
19 RECOVERY_IMAGE="recovery_image.bin" 18 RECOVERY_IMAGE="recovery_image.bin"
20 19
21 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built" 20 DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built"
22 DEFINE_string image "" "Location of the rootfs raw image file" 21 DEFINE_string image "" "Location of the rootfs raw image file"
23 DEFINE_string output "${RECOVERY_IMAGE}" \ 22 DEFINE_string output "${RECOVERY_IMAGE}" \
24 "(optional) output image name. Default: ${RECOVERY_IMAGE}" 23 "(optional) output image name. Default: ${RECOVERY_IMAGE}"
25 24
26 # Parse command line 25 # Parse command line
27 FLAGS "$@" || exit 1 26 FLAGS "$@" || exit 1
28 eval set -- "${FLAGS_ARGV}" 27 eval set -- "${FLAGS_ARGV}"
29 28
30 # No board, no default and no image set then we can't find the image 29 # No board, no default and no image set then we can't find the image
31 if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then 30 if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then
32 setup_board_warning 31 setup_board_warning
33 die "mod_image_for_recovery failed. No board set and no image set" 32 die "mod_image_for_recovery failed. No board set and no image set"
34 fi 33 fi
35 34
36 # We have a board name but no image set. Use image at default location 35 # We have a board name but no image set. Use image at default location
37 if [ -z $FLAGS_image ] ; then 36 if [ -z $FLAGS_image ] ; then
38 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" 37 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
39 FILENAME="chromiumos_image.bin" 38 FILENAME="chromiumos_image.bin"
40 FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" 39 FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}"
41 fi 40 fi
42 41
43 # Turn path into an absolute path. 42 # Turn path into an absolute path.
44 FLAGS_image=$(eval readlink -f ${FLAGS_image}) 43 FLAGS_image=$(eval readlink -f ${FLAGS_image})
45 44
46 # Abort early if we can't find the image 45 # Abort early if we can't find the image
47 if [ ! -f $FLAGS_image ] ; then 46 if [ ! -f $FLAGS_image ] ; then
48 echo "No image found at $FLAGS_image" 47 echo "No image found at $FLAGS_image"
49 exit 1 48 exit 1
50 fi 49 fi
51 50
51 set -u
52 set -e 52 set -e
53 53
54 # Constants 54 # Constants
55 IMAGE_DIR="$(dirname "$FLAGS_image")" 55 IMAGE_DIR="$(dirname "$FLAGS_image")"
56 IMAGE_NAME="$(basename "$FLAGS_image")"
57 56
58 # loop device utility methods mostly duplicated from 57 # Creates a dev recovery image using an existing dev install shim
59 # src/platform/installer/chromeos-install 58 # If successful, content of --payload_dir is copied to a directory named
60 # TODO(tgao): minimize duplication by refactoring these methods into a separate 59 # "dev_payload" under the root of stateful partition.
61 # library script which both scripts can reference 60 create_recovery_image() {
61 local src_img=$1 # base image
62 local src_state=$(mktemp "/tmp/src_state.XXXXXX")
63 local stateful_offset=$(partoffset ${src_img} 1)
64 local stateful_count=$(partsize ${src_img} 1)
62 65
63 # Set up loop device for an image file at specified offset 66 dd if="${src_img}" of="${src_state}" conv=notrunc bs=512 \
64 loop_offset_setup() { 67 skip=${stateful_offset} count=${stateful_count}
65 local filename=$1
66 local offset=$2 # 512-byte sectors
67 68
68 LOOP_DEV=$(sudo losetup -f) 69 # Mount original stateful partition to figure out its actual size
69 if [ -z "$LOOP_DEV" ] 70 local src_loop_dev=$(get_loop_dev)
70 then 71 trap "cleanup_loop_dev ${src_loop_dev}" EXIT
71 echo "No free loop device. Free up a loop device or reboot. Exiting."
72 exit 1
73 fi
74 72
75 sudo losetup -o $(($offset * 512)) ${LOOP_DEV} ${filename} 73 # Setup loop dev
76 } 74 sudo losetup $src_loop_dev $src_state
75 local block_size=$(sudo /sbin/dumpe2fs $src_loop_dev | grep "Block size:" \
76 | tr -d ' ' | cut -f2 -d:)
77 echo "block_size = $block_size"
78 local min_size=$(sudo /sbin/resize2fs -P $src_loop_dev | tr -d ' ' \
79 | cut -f2 -d:)
80 echo "min_size = $min_size $block_size blocks"
77 81
78 loop_offset_cleanup() { 82 # Add 20%, convert to 512-byte sectors and round up to 2Mb boundary
79 sudo losetup -d ${LOOP_DEV} || /bin/true 83 local min_sectors=$(roundup $(((min_size * block_size * 120) / (512 * 100))))
80 } 84 echo "min_sectors = ${min_sectors} 512-byte blocks"
85 sudo e2fsck -fp "${src_loop_dev}"
86 # Resize using 512-byte sectors
87 sudo /sbin/resize2fs $src_loop_dev ${min_sectors}s
81 88
82 mount_on_loop_dev() { 89 # Delete the loop
83 TMPMNT=$(mktemp -d) 90 trap - EXIT
84 sudo mount ${LOOP_DEV} ${TMPMNT} 91 cleanup_loop_dev ${src_loop_dev}
85 }
86 92
87 # Unmount loop-mounted device. 93 # Truncate the image at the new size
88 umount_from_loop_dev() { 94 dd if=/dev/zero of=$src_state bs=512 seek=$min_sectors count=0
89 mount | grep -q " on ${TMPMNT} " && sudo umount ${TMPMNT}
90 }
91 95
92 # Undo both mount and loop. 96 # Mount and touch .recovery # Soon not to be needed :/
93 my_cleanup() { 97 local new_mnt=$(mktemp -d "/tmp/src_mnt.XXXXXX")
94 umount_from_loop_dev 98 mkdir -p "${new_mnt}"
95 loop_offset_cleanup 99 local new_loop_dev=$(get_loop_dev)
96 } 100 trap "cleanup_loop_dev ${new_loop_dev} && rmdir ${new_mnt} && \
101 rm -f ${src_state}" EXIT
102 sudo mount -o loop=${new_loop_dev} "${src_state}" "${new_mnt}"
103 trap "umount_from_loop_dev ${new_mnt} && rm -f ${src_state}" EXIT
104 sudo touch "${new_mnt}/.recovery"
97 105
98 # Modifies an existing image for recovery use 106 (update_partition_table $src_img $src_state $min_sectors $TEMP_IMG)
99 update_recovery_packages() { 107 # trap handler will handle unmount and clean up of loop device and temp files
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 } 108 }
115 109
116 # Main 110 # Main
117
118 DST_PATH="${IMAGE_DIR}/${FLAGS_output}" 111 DST_PATH="${IMAGE_DIR}/${FLAGS_output}"
119 echo "Making a copy of original image ${FLAGS_image}" 112 echo "Making a copy of original image ${FLAGS_image}"
120 cp $FLAGS_image $DST_PATH 113 (create_recovery_image $FLAGS_image)
121 update_recovery_packages $DST_PATH
122 114
123 echo "Recovery image created at ${DST_PATH}" 115 if [ -n ${TEMP_IMG} ] && [ -f ${TEMP_IMG} ]; then
116 mv -f $TEMP_IMG $DST_PATH
117 echo "Recovery image created at ${DST_PATH}"
118 else
119 echo "Failed to create recovery image"
120 fi
OLDNEW
« 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