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