| 1 | #!/bin/bash |
| 2 | # Copyright 2011 The ChromiumOS Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | usage() { |
| 7 | cat <<EOF |
| 8 | Usage: $PROG <image> [--force] |
| 9 | |
| 10 | In-place converts recovery <image> into an SSD image. With --force, does not |
| 11 | ask for confirmation from the user. |
| 12 | |
| 13 | EOF |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | if [ $# -lt 1 ] || [ $# -gt 3 ]; then |
| 18 | usage |
| 19 | else |
| 20 | IMAGE=$1 |
| 21 | shift |
| 22 | fi |
| 23 | |
| 24 | for arg in "$@"; do |
| 25 | case "$arg" in |
| 26 | --force) |
| 27 | IS_FORCE=${arg} |
| 28 | ;; |
| 29 | *) |
| 30 | usage |
| 31 | ;; |
| 32 | esac |
| 33 | done |
| 34 | |
| 35 | # Load common constants (and use GPT if set above) and variables. |
| 36 | . "$(dirname "$0")/common.sh" |
| 37 | |
| 38 | # Abort on errors. |
| 39 | set -e |
| 40 | |
| 41 | if [ "${IS_FORCE}" != "--force" ]; then |
| 42 | echo "This will modify ${IMAGE} in-place and convert it into an SSD image." |
| 43 | read -p "Are you sure you want to continue (y/N)? " SURE |
| 44 | SURE="${SURE:0:1}" |
| 45 | [ "${SURE}" != "y" ] && exit 1 |
| 46 | fi |
| 47 | |
| 48 | loopdev=$(loopback_partscan "${IMAGE}") |
| 49 | loop_kerna="${loopdev}p2" |
| 50 | loop_kernb="${loopdev}p4" |
| 51 | |
| 52 | # Move Kernel B to Kernel A. |
| 53 | info "Replacing Kernel partition A with Kernel partition B" |
| 54 | sudo cp "${loop_kernb}" "${loop_kerna}" |
| 55 | |
| 56 | # Overwrite the vblock. |
| 57 | info "Overwriting kernel partition A vblock with SSD vblock" |
| 58 | stateful_dir=$(make_temp_dir) |
| 59 | tmp_vblock=$(make_temp_file) |
| 60 | sudo mount -o ro "${loopdev}p1" "${stateful_dir}" |
| 61 | sudo cp "${stateful_dir}/vmlinuz_hd.vblock" "${tmp_vblock}" |
| 62 | # Unmount before overwriting image to avoid sync issues. |
| 63 | sudo umount "${stateful_dir}" |
| 64 | sudo dd if="${tmp_vblock}" of="${loop_kerna}" bs=512 conv=notrunc |
| 65 | |
| 66 | # Zero out Kernel B partition. |
| 67 | info "Zeroing out Kernel partition B" |
| 68 | # This will throw a "disk is full" error, so ignore it. |
| 69 | sudo cp /dev/zero "${loop_kernb}" 2>/dev/null || : |
| 70 | |
| 71 | info "${IMAGE} was converted to an SSD image." |