From 0fad3d71718fe7261a620f8fb21c7a10498ac621 Mon Sep 17 00:00:00 2001 From: "J. Richard Barnette" Date: Wed, 27 Jul 2011 15:50:03 -0700 Subject: [PATCH] Make build_gpt.sh into a shell function library. Converted build_library/build_gpt.sh from a shell script to a shell library defining a function to replace the script. Converted build_library/emit_gpt_scripts.sh into a shell function that is defined in build_library/build_gpt.sh. BUG=chromium-os:17390 TEST=build_image TEST=inspect and run pack_ and unpack_partitions.sh on new image Change-Id: Ifdcb58f492f871e120cbec9c67bdeab94d1a4d3f Reviewed-on: http://gerrit.chromium.org/gerrit/4866 Reviewed-by: Vince Laviano Tested-by: Richard Barnette --- build_image | 18 +-- build_library/build_gpt.sh | 195 +++++++++++------------------- build_library/emit_gpt_scripts.sh | 63 ---------- 3 files changed, 78 insertions(+), 198 deletions(-) delete mode 100755 build_library/emit_gpt_scripts.sh diff --git a/build_image b/build_image index 17b514c619..0748b583ef 100755 --- a/build_image +++ b/build_image @@ -12,6 +12,8 @@ SCRIPT_ROOT=$(dirname "$0") . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 +. "${SCRIPT_ROOT}/build_library/build_gpt.sh" || exit 1 + DEFINE_string board "${DEFAULT_BOARD}" \ "The board to build an image for." @@ -706,16 +708,14 @@ create_base_image() { trap delete_prompt EXIT # Create the GPT-formatted image. - ${BUILD_LIBRARY_DIR}/build_gpt.sh \ - --arch=${ARCH} \ - --board=${FLAGS_board} \ - --rootfs_partition_size=${FLAGS_rootfs_partition_size} \ - "${OUTPUT_DIR}" \ - "${OUTPUT_DIR}/${image_name}" + build_gpt "${OUTPUT_DIR}/${image_name}" \ + "${ROOT_FS_IMG}" \ + "${OUTPUT_DIR}/vmlinuz.image" \ + "${STATEFUL_FS_IMG}" \ + "${ESP_FS_IMG}" - # Pre-set "sucessful" bit in gpt, so we will never mark-for-death - # a partition on an SDCard/USB stick. - ${GPT} add -i 2 -S 1 "${OUTPUT_DIR}/${image_name}" + # Emit helpful scripts for testers, etc. + emit_gpt_scripts "${OUTPUT_DIR}/${image_name}" "${OUTPUT_DIR}" trap - EXIT } diff --git a/build_library/build_gpt.sh b/build_library/build_gpt.sh index b4a87890b9..f23f91b75b 100755 --- a/build_library/build_gpt.sh +++ b/build_library/build_gpt.sh @@ -1,147 +1,90 @@ -#!/bin/bash - # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) -. "${SCRIPT_ROOT}/common.sh" || exit 1 +emit_gpt_scripts() { + local image="$1" + local dir="$2" -# We're invoked only by build_image, which runs in the chroot -assert_inside_chroot + local pack="$dir/pack_partitions.sh" + local unpack="$dir/unpack_partitions.sh" -INSTALLER_ROOT=/usr/lib/installer -. "${INSTALLER_ROOT}/chromeos-common.sh" || exit 1 - -BUILD_LIBRARY_DIR=${SCRIPTS_DIR}/build_library - -get_default_board - -# Flags. -DEFINE_string arch "" \ - "The target architecture (\"arm\" or \"x86\")." -DEFINE_string board "$DEFAULT_BOARD" \ - "The board to build an image for." -DEFINE_integer rootfs_partition_size 1024 \ - "rootfs parition size in MBs." - -# Usage. -FLAGS_HELP=$(cat <"$unpack" <
&2 exit 1 fi +set -x +HEADER -if [[ -z "$1" || -z "$2" ]] ; then - flags_help - exit 1 -fi -IMAGEDIR="$1" -OUTDEV="$2" + $GPT show "$image" | sed -e 's/^/# /' >>"$unpack" + cp "$unpack" "$pack" -if [[ -n "$FLAGS_arch" ]]; then - ARCH=${FLAGS_arch} -else - # Figure out ARCH from the given toolchain. - # TODO: Move to common.sh as a function after scripts are switched over. - TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') - case "$TC_ARCH" in - arm*) - ARCH="arm" - ;; - *86) - ARCH="x86" - ;; - *x86_64) - ARCH="amd64" - ;; - *) - error "Unable to determine ARCH from toolchain: $CHOST" - exit 1 - esac -fi + $GPT show -q "$image" | + while read start size part x; do + local file="part_$part" + local target="\"\$TARGET\"" + local dd_args="bs=512 count=$size" + echo "dd if=$target of=$file $dd_args skip=$start" >>"$unpack" + echo "dd if=$file of=$target $dd_args seek=$start conv=notrunc" \ + >>"$pack" + done -# Only now can we die on error. shflags functions leak non-zero error codes, -# so will die prematurely if 'set -e' is specified before now. -set -e -# Die on uninitialized variables. -set -u + chmod +x "$unpack" "$pack" +} -# Check for missing parts. -ROOTFS_IMG="${IMAGEDIR}/rootfs.image" -if [[ ! -s ${ROOTFS_IMG} ]]; then - error "Can't find ${ROOTFS_IMG}" - exit 1 -fi -KERNEL_IMG="${IMAGEDIR}/vmlinuz.image" -if [[ ! -s ${KERNEL_IMG} ]]; then - error "Can't find ${KERNEL_IMG}" - exit 1 -fi +build_gpt() { + local outdev="$1" + local rootfs_img="$2" + local kernel_img="$3" + local stateful_img="$4" + local esp_img="$5" -STATEFUL_IMG="${IMAGEDIR}/stateful_partition.image" -if [ ! -s ${STATEFUL_IMG} ]; then - error "Can't find ${STATEFUL_IMG}" - exit 1 -fi + # We'll need some code to put in the PMBR, for booting on legacy BIOS. + local pmbr_img + if [ "$ARCH" = "arm" ]; then + pmbr_img=/dev/zero + elif [ "$ARCH" = "x86" ]; then + pmbr_img=$(readlink -f /usr/share/syslinux/gptmbr.bin) + else + error "Unknown architecture: $ARCH" + return 1 + fi -ESP_IMG="${IMAGEDIR}/esp.image" -if [ ! -s ${ESP_IMG} ]; then - error "Can't find ${ESP_IMG}" - exit 1 -fi + # Create the GPT. This has the side-effect of setting some global vars + # describing the partition table entries (see the comments in the source). + install_gpt "$outdev" $(numsectors "$rootfs_img") \ + $(numsectors "$stateful_img") $pmbr_img $(numsectors "$esp_img") \ + false $FLAGS_rootfs_partition_size -# We'll need some code to put in the PMBR, for booting on legacy BIOS. -if [[ "$ARCH" = "arm" ]]; then - PMBRCODE=/dev/zero -else - PMBRCODE=$(readlink -f /usr/share/syslinux/gptmbr.bin) -fi + local sudo= + if [ ! -w "$outdev" ] ; then + # use sudo when writing to a block device. + sudo=sudo + fi -# Create the GPT. This has the side-effect of setting some global vars -# describing the partition table entries (see the comments in the source). -install_gpt $OUTDEV $(numsectors $ROOTFS_IMG) $(numsectors $STATEFUL_IMG) \ - $PMBRCODE $(numsectors $ESP_IMG) false $FLAGS_rootfs_partition_size + # Now populate the partitions. + echo "Copying stateful partition..." + $sudo dd if="$stateful_img" of="$outdev" conv=notrunc bs=512 \ + seek=$START_STATEFUL -# Emit helpful scripts for testers, etc. -${BUILD_LIBRARY_DIR}/emit_gpt_scripts.sh "${OUTDEV}" "${IMAGEDIR}" + echo "Copying kernel..." + $sudo dd if="$kernel_img" of="$outdev" conv=notrunc bs=512 \ + seek=$START_KERN_A -sudo= -if [ ! -w "$OUTDEV" ] ; then - # use sudo when writing to a block device. - sudo=sudo -fi + echo "Copying rootfs..." + $sudo dd if="$rootfs_img" of="$outdev" conv=notrunc bs=512 \ + seek=$START_ROOTFS_A -# Now populate the partitions. -echo "Copying stateful partition..." -$sudo dd if=${STATEFUL_IMG} of=${OUTDEV} conv=notrunc bs=512 \ - seek=${START_STATEFUL} + echo "Copying EFI system partition..." + $sudo dd if="$esp_img" of="$outdev" conv=notrunc bs=512 \ + seek=$START_ESP -echo "Copying kernel..." -$sudo dd if=${KERNEL_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_KERN_A} - -echo "Copying rootfs..." -$sudo dd if=${ROOTFS_IMG} of=${OUTDEV} conv=notrunc bs=512 \ - seek=${START_ROOTFS_A} - -echo "Copying EFI system partition..." -$sudo dd if=${ESP_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_ESP} - -# Clean up temporary files. -if [[ -n "${MBR_IMG:-}" ]]; then - rm "${MBR_IMG}" -fi + # Pre-set "sucessful" bit in gpt, so we will never mark-for-death + # a partition on an SDCard/USB stick. + $GPT add -i 2 -S 1 "$outdev" +} diff --git a/build_library/emit_gpt_scripts.sh b/build_library/emit_gpt_scripts.sh deleted file mode 100755 index 2c19c66751..0000000000 --- a/build_library/emit_gpt_scripts.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. -# -# Emit scripts to pack and unpack the partitions from a GPT disk image. - -SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) -. "${SCRIPT_ROOT}/common.sh" || exit 1 - -# We're invoked only by build_image, which runs in the chroot -assert_inside_chroot - -INSTALLER_ROOT=/usr/lib/installer -. "${INSTALLER_ROOT}/chromeos-common.sh" || exit 1 - -locate_gpt - -set -e - -# Usage -IMAGE=${1:-} -DIR=${2:-} -if [[ -z "$IMAGE" || -z "$DIR" ]]; then - echo "Usage: $0 GPT_DEVICE DIRECTORY" 1>&2 - exit 1 -fi - -PACK="${DIR}/pack_partitions.sh" -UNPACK="${DIR}/unpack_partitions.sh" - -TMP=$(mktemp) -$GPT show "$IMAGE" > $TMP - -HEADER='#!/bin/bash -eu -# File generated by emit_gpt_scripts.sh. Do not edit. -TARGET=${1:-} -if [[ -z "$TARGET" ]]; then - echo "Usage: $0 DEVICE" 1>&2 - exit 1 -fi -set -x' - -echo "$HEADER" > "$PACK" -echo "$HEADER" > "$UNPACK" -cat $TMP | sed -e 's/^/# /' >> "$PACK" -cat $TMP | sed -e 's/^/# /' >> "$UNPACK" - -$GPT show -q "$IMAGE" | \ - while read start size part x; do \ - file="part_$part" - loc="\"\$TARGET\"" - echo "dd if=$loc of=$file bs=512 skip=$start count=$size" \ - >> "$UNPACK" - echo \ - "dd if=$file of=$loc bs=512 seek=$start count=$size conv=notrunc" \ - >> "$PACK" - done - -chmod +x "$PACK" "$UNPACK" - -rm $TMP