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" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 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}" |
70 fi | 70 fi |
71 | 71 |
72 # Constants | 72 # Constants |
73 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" | 73 INSTALL_SHIM_DIR="$(dirname "$FLAGS_dev_install_shim")" |
74 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" | 74 DEV_RECOVERY_IMAGE="dev_recovery_image.bin" |
75 | 75 |
76 umount_from_loop_dev() { | 76 umount_from_loop_dev() { |
77 local mnt_pt=$1 | 77 local mnt_pt=$1 |
78 mount | grep -q " on ${mnt_pt}" && sudo umount ${mnt_pt} | 78 mount | grep -q " on ${mnt_pt}" && sudo umount ${mnt_pt} |
| 79 rmdir ${mnt_pt} |
79 } | 80 } |
80 | 81 |
81 cleanup_loop_dev() { | 82 cleanup_loop_dev() { |
82 sudo losetup -d ${1} || /bin/true | 83 sudo losetup -d ${1} || /bin/true |
83 } | 84 } |
84 | 85 |
85 get_loop_dev() { | 86 get_loop_dev() { |
86 local loop_dev=$(sudo losetup -f) | 87 local loop_dev=$(sudo losetup -f) |
87 if [ -z "${loop_dev}" ]; then | 88 if [ -z "${loop_dev}" ]; then |
88 die "No free loop device. Free up a loop device or reboot. Exiting." | 89 die "No free loop device. Free up a loop device or reboot. Exiting." |
(...skipping 11 matching lines...) Expand all Loading... |
100 local source_sectors=$(roundup $(numsectors ${source_part})) | 101 local source_sectors=$(roundup $(numsectors ${source_part})) |
101 info "source partition has ${source_sectors} 512-byte sectors." | 102 info "source partition has ${source_sectors} 512-byte sectors." |
102 local resized_sectors=$(roundup $(expr $source_sectors + $add_num_sectors)) | 103 local resized_sectors=$(roundup $(expr $source_sectors + $add_num_sectors)) |
103 info "resized partition has ${resized_sectors} 512-byte sectors." | 104 info "resized partition has ${resized_sectors} 512-byte sectors." |
104 | 105 |
105 local loop_dev=$(get_loop_dev) | 106 local loop_dev=$(get_loop_dev) |
106 trap "cleanup_loop_dev ${loop_dev}" EXIT | 107 trap "cleanup_loop_dev ${loop_dev}" EXIT |
107 | 108 |
108 # Extend the source file size to the new size. | 109 # Extend the source file size to the new size. |
109 dd if=/dev/zero of="${source_part}" bs=1 count=1 \ | 110 dd if=/dev/zero of="${source_part}" bs=1 count=1 \ |
110 seek=$((512 * ${resized_sectors} - 1)) | 111 seek=$((512 * ${resized_sectors} - 1)) &>/dev/null |
111 | 112 |
112 # Resize the partition. | 113 # Resize the partition. |
113 sudo losetup "${loop_dev}" "${source_part}" | 114 sudo losetup "${loop_dev}" "${source_part}" |
114 sudo e2fsck -fp "${loop_dev}" &> /dev/null | 115 sudo e2fsck -fp "${loop_dev}" &> /dev/null |
115 sudo resize2fs "${loop_dev}" &> /dev/null | 116 sudo resize2fs "${loop_dev}" &> /dev/null |
116 cleanup_loop_dev "${loop_dev}" | 117 # trap handler will clean up the loop device |
117 | |
118 echo "${resized_sectors}" | 118 echo "${resized_sectors}" |
119 } | 119 } |
120 | 120 |
121 # Update partition table with resized stateful partition and create the final | 121 # Update partition table with resized stateful partition and create the final |
122 # dev recovery image | 122 # dev recovery image |
123 update_partition_table() { | 123 update_partition_table() { |
124 local temp_state=$1 # stateful partition image | 124 local temp_state=$1 # stateful partition image |
125 local resized_sectors=$2 # number of sectors in resized stateful partition | 125 local resized_sectors=$2 # number of sectors in resized stateful partition |
126 local temp_img=$(mktemp) | 126 local temp_img=$(mktemp "/tmp/temp_img.XXXXXX") |
127 | 127 |
128 local kernel_offset=$(partoffset ${FLAGS_dev_install_shim} 2) | 128 local kernel_offset=$(partoffset ${FLAGS_dev_install_shim} 2) |
129 local kernel_count=$(partsize ${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) | 130 local rootfs_offset=$(partoffset ${FLAGS_dev_install_shim} 3) |
131 local rootfs_count=$(partsize ${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) | 132 local oem_offset=$(partoffset ${FLAGS_dev_install_shim} 8) |
133 local oem_count=$(partsize ${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) | 134 local esp_offset=$(partoffset ${FLAGS_dev_install_shim} 12) |
135 local esp_count=$(partsize ${FLAGS_dev_install_shim} 12) | 135 local esp_count=$(partsize ${FLAGS_dev_install_shim} 12) |
136 | 136 |
137 local temp_pmbr=$(mktemp) | 137 local temp_pmbr=$(mktemp "/tmp/pmbr.XXXXXX") |
138 dd if="${FLAGS_dev_install_shim}" of="${temp_pmbr}" bs=512 count=1 | 138 dd if="${FLAGS_dev_install_shim}" of="${temp_pmbr}" bs=512 count=1 &>/dev/null |
139 | 139 |
140 # Set up a new partition table | 140 # Set up a new partition table |
141 install_gpt "${temp_img}" "${rootfs_count}" "${resized_sectors}" \ | 141 install_gpt "${temp_img}" "${rootfs_count}" "${resized_sectors}" \ |
142 "${temp_pmbr}" "${esp_count}" false $(roundup ${rootfs_count}) &>/dev/null | 142 "${temp_pmbr}" "${esp_count}" false $(roundup ${rootfs_count}) &>/dev/null |
143 | 143 |
144 rm -rf "${temp_pmbr}" | 144 rm -rf "${temp_pmbr}" |
145 | 145 |
146 # Copy into the partition parts of the file | 146 # Copy into the partition parts of the file |
147 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | 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} | 148 seek="${START_ROOTFS_A}" skip=${rootfs_offset} count=${rootfs_count} \ |
| 149 &>/dev/null |
149 dd if="${temp_state}" of="${temp_img}" conv=notrunc bs=512 \ | 150 dd if="${temp_state}" of="${temp_img}" conv=notrunc bs=512 \ |
150 seek="${START_STATEFUL}" | 151 seek="${START_STATEFUL}" &>/dev/null |
151 # Copy the full kernel (i.e. with vboot sections) | 152 # Copy the full kernel (i.e. with vboot sections) |
152 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | 153 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ |
153 seek="${START_KERN_A}" skip=${kernel_offset} count=${kernel_count} | 154 seek="${START_KERN_A}" skip=${kernel_offset} count=${kernel_count} \ |
| 155 &>/dev/null |
154 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | 156 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ |
155 seek="${START_OEM}" skip=${oem_offset} count=${oem_count} | 157 seek="${START_OEM}" skip=${oem_offset} count=${oem_count} &>/dev/null |
156 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ | 158 dd if="${FLAGS_dev_install_shim}" of="${temp_img}" conv=notrunc bs=512 \ |
157 seek="${START_ESP}" skip=${esp_offset} count=${esp_count} | 159 seek="${START_ESP}" skip=${esp_offset} count=${esp_count} &>/dev/null |
158 | 160 |
159 echo ${temp_img} | 161 echo ${temp_img} |
160 } | 162 } |
161 | 163 |
162 # Creates a dev recovery image using an existing dev install shim | 164 # Creates a dev recovery image using an existing dev install shim |
163 # If successful, content of --payload_dir is copied to a directory named | 165 # If successful, content of --payload_dir is copied to a directory named |
164 # "dev_payload" under the root of stateful partition. | 166 # "dev_payload" under the root of stateful partition. |
165 create_dev_recovery_image() { | 167 create_dev_recovery_image() { |
166 local temp_state=$(mktemp) | 168 local temp_state=$(mktemp "/tmp/temp_state.XXXXXX") |
167 local stateful_offset=$(partoffset ${FLAGS_dev_install_shim} 1) | 169 local stateful_offset=$(partoffset ${FLAGS_dev_install_shim} 1) |
168 local stateful_count=$(partsize ${FLAGS_dev_install_shim} 1) | 170 local stateful_count=$(partsize ${FLAGS_dev_install_shim} 1) |
169 dd if="${FLAGS_dev_install_shim}" of="${temp_state}" conv=notrunc bs=512 \ | 171 dd if="${FLAGS_dev_install_shim}" of="${temp_state}" conv=notrunc bs=512 \ |
170 skip=${stateful_offset} count=${stateful_count} | 172 skip=${stateful_offset} count=${stateful_count} &>/dev/null |
171 | 173 |
172 local resized_sectors=$(resize_partition $temp_state $PAYLOAD_DIR_SIZE) | 174 local resized_sectors=$(resize_partition $temp_state $PAYLOAD_DIR_SIZE) |
173 | 175 |
174 # Mount resized stateful FS and copy payload content to its root directory | 176 # Mount resized stateful FS and copy payload content to its root directory |
175 local temp_mnt=$(mktemp -d) | 177 local temp_mnt=$(mktemp -d "/tmp/temp_mnt.XXXXXX") |
176 local loop_dev=$(get_loop_dev) | 178 local loop_dev=$(get_loop_dev) |
177 trap "umount_from_loop_dev ${temp_mnt} && cleanup_loop_dev ${loop_dev}" EXIT | 179 trap "umount_from_loop_dev ${temp_mnt} && cleanup_loop_dev ${loop_dev}" EXIT |
178 mkdir -p "${temp_mnt}" | 180 mkdir -p "${temp_mnt}" |
179 sudo mount -o loop=${loop_dev} "${temp_state}" "${temp_mnt}" | 181 sudo mount -o loop=${loop_dev} "${temp_state}" "${temp_mnt}" |
180 sudo cp -R "${FLAGS_payload_dir}" "${temp_mnt}" | 182 sudo cp -R "${FLAGS_payload_dir}" "${temp_mnt}" |
181 sudo mv "${temp_mnt}/$(basename ${FLAGS_payload_dir})" \ | 183 sudo mv "${temp_mnt}/$(basename ${FLAGS_payload_dir})" \ |
182 "${temp_mnt}/dev_payload" | 184 "${temp_mnt}/dev_payload" |
183 # Mark image as dev recovery | 185 # Mark image as dev recovery |
184 sudo touch "${temp_mnt}/.recovery" | 186 sudo touch "${temp_mnt}/.recovery" |
185 sudo touch "${temp_mnt}/.dev_recovery" | 187 sudo touch "${temp_mnt}/.dev_recovery" |
186 | 188 |
187 # TODO(tgao): handle install script (for default and custom cases) | 189 # TODO(tgao): handle install script (for default and custom cases) |
188 local temp_img=$(update_partition_table $temp_state $resized_sectors) | 190 local temp_img=$(update_partition_table $temp_state $resized_sectors) |
189 | 191 |
190 umount_from_loop_dev "${temp_mnt}" | |
191 cleanup_loop_dev ${loop_dev} | |
192 rm -f "${temp_state}" | 192 rm -f "${temp_state}" |
| 193 # trap handler will clean up loop device and temp mount point |
193 echo ${temp_img} | 194 echo ${temp_img} |
194 } | 195 } |
195 | 196 |
196 # Main | 197 # Main |
197 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" | 198 DST_PATH="${INSTALL_SHIM_DIR}/${DEV_RECOVERY_IMAGE}" |
198 info "Attempting to create dev recovery image using dev install shim \ | 199 info "Attempting to create dev recovery image using dev install shim \ |
199 ${FLAGS_dev_install_shim}" | 200 ${FLAGS_dev_install_shim}" |
200 TEMP_IMG=$(create_dev_recovery_image) | 201 TEMP_IMG=$(create_dev_recovery_image) |
201 | 202 |
202 mv -f $TEMP_IMG $DST_PATH | 203 mv -f $TEMP_IMG $DST_PATH |
203 info "Dev recovery image created at ${DST_PATH}" | 204 info "Dev recovery image created at ${DST_PATH}" |
OLD | NEW |