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 <vlaviano@chromium.org>
Tested-by: Richard Barnette <jrbarnette@chromium.org>
This commit is contained in:
J. Richard Barnette 2011-07-27 15:50:03 -07:00 committed by Richard Barnette
parent 25bf949c63
commit 0fad3d7171
3 changed files with 78 additions and 198 deletions

View File

@ -12,6 +12,8 @@
SCRIPT_ROOT=$(dirname "$0") SCRIPT_ROOT=$(dirname "$0")
. "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1 . "${SCRIPT_ROOT}/build_library/build_common.sh" || exit 1
. "${SCRIPT_ROOT}/build_library/build_gpt.sh" || exit 1
DEFINE_string board "${DEFAULT_BOARD}" \ DEFINE_string board "${DEFAULT_BOARD}" \
"The board to build an image for." "The board to build an image for."
@ -706,16 +708,14 @@ create_base_image() {
trap delete_prompt EXIT trap delete_prompt EXIT
# Create the GPT-formatted image. # Create the GPT-formatted image.
${BUILD_LIBRARY_DIR}/build_gpt.sh \ build_gpt "${OUTPUT_DIR}/${image_name}" \
--arch=${ARCH} \ "${ROOT_FS_IMG}" \
--board=${FLAGS_board} \ "${OUTPUT_DIR}/vmlinuz.image" \
--rootfs_partition_size=${FLAGS_rootfs_partition_size} \ "${STATEFUL_FS_IMG}" \
"${OUTPUT_DIR}" \ "${ESP_FS_IMG}"
"${OUTPUT_DIR}/${image_name}"
# Pre-set "sucessful" bit in gpt, so we will never mark-for-death # Emit helpful scripts for testers, etc.
# a partition on an SDCard/USB stick. emit_gpt_scripts "${OUTPUT_DIR}/${image_name}" "${OUTPUT_DIR}"
${GPT} add -i 2 -S 1 "${OUTPUT_DIR}/${image_name}"
trap - EXIT trap - EXIT
} }

View File

@ -1,147 +1,90 @@
#!/bin/bash
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. # 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 # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) emit_gpt_scripts() {
. "${SCRIPT_ROOT}/common.sh" || exit 1 local image="$1"
local dir="$2"
# We're invoked only by build_image, which runs in the chroot local pack="$dir/pack_partitions.sh"
assert_inside_chroot local unpack="$dir/unpack_partitions.sh"
INSTALLER_ROOT=/usr/lib/installer cat >"$unpack" <<HEADER
. "${INSTALLER_ROOT}/chromeos-common.sh" || exit 1 #!/bin/bash -eu
# File automatically generated. Do not edit.
BUILD_LIBRARY_DIR=${SCRIPTS_DIR}/build_library TARGET=\${1:-}
if [[ -z "\$TARGET" ]]; then
get_default_board echo "Usage: \$0 DEVICE" 1>&2
# 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 <<EOF
Usage: $(basename $0) [flags] IMAGEDIR OUTDEV
This takes the image components in IMAGEDIR and creates a bootable,
GPT-formatted image in OUTDEV. OUTDEV can be a file or block device.
EOF
)
# Parse command line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
if [[ -z "$FLAGS_board" ]] ; then
error "--board is required."
exit 1 exit 1
fi fi
set -x
HEADER
if [[ -z "$1" || -z "$2" ]] ; then $GPT show "$image" | sed -e 's/^/# /' >>"$unpack"
flags_help cp "$unpack" "$pack"
exit 1
fi
IMAGEDIR="$1"
OUTDEV="$2"
if [[ -n "$FLAGS_arch" ]]; then $GPT show -q "$image" |
ARCH=${FLAGS_arch} while read start size part x; do
else local file="part_$part"
# Figure out ARCH from the given toolchain. local target="\"\$TARGET\""
# TODO: Move to common.sh as a function after scripts are switched over. local dd_args="bs=512 count=$size"
TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }') echo "dd if=$target of=$file $dd_args skip=$start" >>"$unpack"
case "$TC_ARCH" in echo "dd if=$file of=$target $dd_args seek=$start conv=notrunc" \
arm*) >>"$pack"
ARCH="arm" done
;;
*86)
ARCH="x86"
;;
*x86_64)
ARCH="amd64"
;;
*)
error "Unable to determine ARCH from toolchain: $CHOST"
exit 1
esac
fi
# Only now can we die on error. shflags functions leak non-zero error codes, chmod +x "$unpack" "$pack"
# so will die prematurely if 'set -e' is specified before now. }
set -e
# Die on uninitialized variables.
set -u
# 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" build_gpt() {
if [[ ! -s ${KERNEL_IMG} ]]; then local outdev="$1"
error "Can't find ${KERNEL_IMG}" local rootfs_img="$2"
exit 1 local kernel_img="$3"
fi local stateful_img="$4"
local esp_img="$5"
STATEFUL_IMG="${IMAGEDIR}/stateful_partition.image" # We'll need some code to put in the PMBR, for booting on legacy BIOS.
if [ ! -s ${STATEFUL_IMG} ]; then local pmbr_img
error "Can't find ${STATEFUL_IMG}" if [ "$ARCH" = "arm" ]; then
exit 1 pmbr_img=/dev/zero
fi 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" # Create the GPT. This has the side-effect of setting some global vars
if [ ! -s ${ESP_IMG} ]; then # describing the partition table entries (see the comments in the source).
error "Can't find ${ESP_IMG}" install_gpt "$outdev" $(numsectors "$rootfs_img") \
exit 1 $(numsectors "$stateful_img") $pmbr_img $(numsectors "$esp_img") \
fi false $FLAGS_rootfs_partition_size
# We'll need some code to put in the PMBR, for booting on legacy BIOS. local sudo=
if [[ "$ARCH" = "arm" ]]; then if [ ! -w "$outdev" ] ; then
PMBRCODE=/dev/zero # use sudo when writing to a block device.
else sudo=sudo
PMBRCODE=$(readlink -f /usr/share/syslinux/gptmbr.bin) fi
fi
# Create the GPT. This has the side-effect of setting some global vars # Now populate the partitions.
# describing the partition table entries (see the comments in the source). echo "Copying stateful partition..."
install_gpt $OUTDEV $(numsectors $ROOTFS_IMG) $(numsectors $STATEFUL_IMG) \ $sudo dd if="$stateful_img" of="$outdev" conv=notrunc bs=512 \
$PMBRCODE $(numsectors $ESP_IMG) false $FLAGS_rootfs_partition_size seek=$START_STATEFUL
# Emit helpful scripts for testers, etc. echo "Copying kernel..."
${BUILD_LIBRARY_DIR}/emit_gpt_scripts.sh "${OUTDEV}" "${IMAGEDIR}" $sudo dd if="$kernel_img" of="$outdev" conv=notrunc bs=512 \
seek=$START_KERN_A
sudo= echo "Copying rootfs..."
if [ ! -w "$OUTDEV" ] ; then $sudo dd if="$rootfs_img" of="$outdev" conv=notrunc bs=512 \
# use sudo when writing to a block device. seek=$START_ROOTFS_A
sudo=sudo
fi
# Now populate the partitions. echo "Copying EFI system partition..."
echo "Copying stateful partition..." $sudo dd if="$esp_img" of="$outdev" conv=notrunc bs=512 \
$sudo dd if=${STATEFUL_IMG} of=${OUTDEV} conv=notrunc bs=512 \ seek=$START_ESP
seek=${START_STATEFUL}
echo "Copying kernel..." # Pre-set "sucessful" bit in gpt, so we will never mark-for-death
$sudo dd if=${KERNEL_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_KERN_A} # a partition on an SDCard/USB stick.
$GPT add -i 2 -S 1 "$outdev"
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

View File

@ -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