OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # Utility methods used to resize a stateful partition and update the GPT table |
| 8 |
| 9 # Source constants and utility functions |
| 10 . "$(dirname "$0")/common.sh" |
| 11 . "$(dirname "$0")/chromeos-common.sh" |
| 12 |
| 13 locate_gpt |
| 14 |
| 15 umount_from_loop_dev() { |
| 16 local mnt_pt=$1 |
| 17 mount | grep -q " on ${mnt_pt}" && sudo umount -d ${mnt_pt} |
| 18 rmdir ${mnt_pt} |
| 19 } |
| 20 |
| 21 cleanup_loop_dev() { |
| 22 sudo losetup -d ${1} || /bin/true |
| 23 } |
| 24 |
| 25 get_loop_dev() { |
| 26 local loop_dev=$(sudo losetup -f) |
| 27 if [ -z "${loop_dev}" ]; then |
| 28 die "No free loop device. Free up a loop device or reboot. Exiting." |
| 29 fi |
| 30 echo ${loop_dev} |
| 31 } |
| 32 |
| 33 # Resize stateful partition of install shim to hold payload content |
| 34 # Due to this resize, we need to create a new partition table and a new image. |
| 35 # (see update_partition_table() for details) |
| 36 enlarge_partition_image() { |
| 37 local source_part=$1 # source partition |
| 38 local add_num_sectors=$2 # number of 512-byte sectors to be added |
| 39 |
| 40 local source_sectors=$(roundup $(numsectors ${source_part})) |
| 41 info "source partition has ${source_sectors} 512-byte sectors." |
| 42 local resized_sectors=$(roundup $(expr $source_sectors + $add_num_sectors)) |
| 43 info "resized partition has ${resized_sectors} 512-byte sectors." |
| 44 |
| 45 local loop_dev=$(get_loop_dev) |
| 46 trap "cleanup_loop_dev ${loop_dev}" EXIT |
| 47 |
| 48 # Extend the source file size to the new size. |
| 49 dd if=/dev/zero of="${source_part}" bs=1 count=1 \ |
| 50 seek=$((512 * ${resized_sectors} - 1)) &>/dev/null |
| 51 |
| 52 # Resize the partition. |
| 53 sudo losetup "${loop_dev}" "${source_part}" |
| 54 sudo e2fsck -fp "${loop_dev}" &> /dev/null |
| 55 sudo resize2fs "${loop_dev}" &> /dev/null |
| 56 # trap handler will clean up the loop device |
| 57 echo "${resized_sectors}" |
| 58 } |
| 59 |
| 60 # Update partition table with resized stateful partition |
| 61 update_partition_table() { |
| 62 local src_img=$1 # source image |
| 63 local temp_state=$2 # stateful partition image |
| 64 local resized_sectors=$3 # number of sectors in resized stateful partition |
| 65 local temp_img=$4 |
| 66 |
| 67 local kernel_offset=$(partoffset ${src_img} 2) |
| 68 local kernel_count=$(partsize ${src_img} 2) |
| 69 local rootfs_offset=$(partoffset ${src_img} 3) |
| 70 local rootfs_count=$(partsize ${src_img} 3) |
| 71 local oem_offset=$(partoffset ${src_img} 8) |
| 72 local oem_count=$(partsize ${src_img} 8) |
| 73 local esp_offset=$(partoffset ${src_img} 12) |
| 74 local esp_count=$(partsize ${src_img} 12) |
| 75 |
| 76 local temp_pmbr=$(mktemp "/tmp/pmbr.XXXXXX") |
| 77 dd if="${src_img}" of="${temp_pmbr}" bs=512 count=1 &>/dev/null |
| 78 |
| 79 trap "rm -rf \"${temp_pmbr}\"" EXIT |
| 80 # Set up a new partition table |
| 81 install_gpt "${temp_img}" "${rootfs_count}" "${resized_sectors}" \ |
| 82 "${temp_pmbr}" "${esp_count}" false $(roundup ${rootfs_count}) |
| 83 |
| 84 # Copy into the partition parts of the file |
| 85 dd if="${src_img}" of="${temp_img}" conv=notrunc bs=512 \ |
| 86 seek="${START_ROOTFS_A}" skip=${rootfs_offset} count=${rootfs_count} |
| 87 dd if="${temp_state}" of="${temp_img}" conv=notrunc bs=512 \ |
| 88 seek="${START_STATEFUL}" |
| 89 # Copy the full kernel (i.e. with vboot sections) |
| 90 dd if="${src_img}" of="${temp_img}" conv=notrunc bs=512 \ |
| 91 seek="${START_KERN_A}" skip=${kernel_offset} count=${kernel_count} |
| 92 dd if="${src_img}" of="${temp_img}" conv=notrunc bs=512 \ |
| 93 seek="${START_OEM}" skip=${oem_offset} count=${oem_count} |
| 94 dd if="${src_img}" of="${temp_img}" conv=notrunc bs=512 \ |
| 95 seek="${START_ESP}" skip=${esp_offset} count=${esp_count} |
| 96 } |
OLD | NEW |