From 545dc57e9751d18e6b403dcbb13667ffebf0c675 Mon Sep 17 00:00:00 2001 From: Hung-Te Lin Date: Sat, 17 Sep 2011 15:44:49 +0800 Subject: [PATCH] crosutils: allow factory scripts to be executed in a limited bundle environment This CL allows factory related scripts to be executed in a prepared and limited environment which can be made by extracting a factory bundle: - always load scripts from $SCRIPT_ROOT - allow running bundled binary programs from $SCRIPT_ROOT/../bin - override GCLIENT_ROOT for the layout of a bundle BUG=chrome-os-partner:5979 TEST=(outside cros environment) ./make_factory_package --config mp_factory.conf # test omaha ./make_factory_package --config rma.conf # test --usbimg ./make_factory_package --config mp_ssd.conf # test --diskimg Change-Id: Ibf85fa267f6fe53ae88e41fd7f62ef7110b2b0bf Reviewed-on: http://gerrit.chromium.org/gerrit/7905 Tested-by: Hung-Te Lin Reviewed-by: Nick Sanders --- lib/cros_image_common.sh | 10 ++++++++ make_factory_package.sh | 45 +++++++++++----------------------- make_universal_factory_shim.sh | 13 ++++++---- mk_memento_images.sh | 45 ++++++++-------------------------- 4 files changed, 42 insertions(+), 71 deletions(-) diff --git a/lib/cros_image_common.sh b/lib/cros_image_common.sh index 6c5f316b97..d550240889 100644 --- a/lib/cros_image_common.sh +++ b/lib/cros_image_common.sh @@ -45,6 +45,16 @@ image_has_part_tools() { image_has_command cgpt || image_has_command parted } +# Finds if specified tool can be found by current path; updates system path if +# the tool is available in given folder. +image_find_tool() { + local tool="$1" + local alternative_folder="$(readlink -f "$2")" + if ! image_has_command "$tool" && [ -x "$alternative_folder/$tool" ]; then + PATH="$alternative_folder:$PATH"; export PATH + fi +} + # Finds the best partition tool and print partition offset image_part_offset() { local file="$1" diff --git a/make_factory_package.sh b/make_factory_package.sh index 20a034e5f9..a862606837 100755 --- a/make_factory_package.sh +++ b/make_factory_package.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright (c) 2009 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 # found in the LICENSE file. @@ -11,40 +11,23 @@ # miniomaha lives in src/platform/dev/ and miniomaha partition sets live # in src/platform/dev/static. -# --- BEGIN COMMON.SH BOILERPLATE --- -# Load common CrOS utilities. Inside the chroot this file is installed in -# /usr/lib/crosutils. Outside the chroot we find it relative to the script's -# location. -find_common_sh() { - local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) - local path +# This script may be executed in a full CrOS source tree or an extracted factory +# bundle with limited tools, so we must always load scripts from $SCRIPT_ROOT +# and search for binary programs in $SCRIPT_ROOT/../bin - SCRIPT_ROOT= - for path in "${common_paths[@]}"; do - if [ -r "${path}/common.sh" ]; then - SCRIPT_ROOT=${path} - break - fi - done -} +SCRIPT="$(readlink -f "$0")" +SCRIPT_ROOT="$(dirname "$SCRIPT")" +. "$SCRIPT_ROOT/lib/cros_image_common.sh" || exit 1 +image_find_tool "cgpt" "$SCRIPT_ROOT/../bin" -find_common_sh -. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; } -# --- END COMMON.SH BOILERPLATE --- +if [ -f "$SCRIPT_ROOT/../dev/devserver.py" ]; then + # Running within an extracted factory bundle + GCLIENT_ROOT="$(readlink -f "$SCRIPT_ROOT/..")" +fi +. "$SCRIPT_ROOT/common.sh" || exit 1 +. "$SCRIPT_ROOT/chromeos-common.sh" || exit 1 -# Load functions and constants for chromeos-install -# NOTE: This script needs to be called from outside the chroot. -. "/usr/lib/installer/chromeos-common.sh" &> /dev/null || \ -. "${SRC_ROOT}/platform/installer/chromeos-common.sh" || \ - die "Unable to load /usr/lib/installer/chromeos-common.sh" - -# Load functions designed for image processing -. "${SCRIPT_ROOT}/lib/cros_image_common.sh" || - die "Cannot load required library: lib/cros_image_common.sh; Abort." - -SCRIPT="$0" get_default_board - FLAGS_NONE='none' # Flags diff --git a/make_universal_factory_shim.sh b/make_universal_factory_shim.sh index 82a2dccc67..b35af9bfdf 100755 --- a/make_universal_factory_shim.sh +++ b/make_universal_factory_shim.sh @@ -1,5 +1,5 @@ #!/bin/sh -# + # 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. @@ -9,11 +9,14 @@ # CAUTION: Recovery shim images are not supported yet because they require the # kernel partitions to be laid out in a special way -SCRIPT="$0" -SCRIPT_ROOT=$(dirname "$SCRIPT") +# This script may be executed in a full CrOS source tree or an extracted factory +# bundle with limited tools, so we must always load scripts from $SCRIPT_ROOT +# and search for binary programs in $SCRIPT_ROOT/../bin -# Load functions designed for image processing -. "${SCRIPT_ROOT}/lib/cros_image_common.sh" || exit 1 +SCRIPT="$(readlink -f "$0")" +SCRIPT_ROOT="$(dirname "$SCRIPT")" +. "$SCRIPT_ROOT/lib/cros_image_common.sh" || exit 1 +image_find_tool "cgpt" "$SCRIPT_ROOT/../bin" # CGPT Header: PMBR, header, table; sec_table, sec_header CGPT_START_SIZE=$((1 + 1 + 32)) diff --git a/mk_memento_images.sh b/mk_memento_images.sh index 5bd669f4c3..a3edbb28e4 100755 --- a/mk_memento_images.sh +++ b/mk_memento_images.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright (c) 2009 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 # found in the LICENSE file. @@ -8,35 +8,16 @@ # build_image.sh and generates an image that can be used for auto # update. +# This script may be executed in a full CrOS source tree or an extracted factory +# bundle with limited tools, so we must always load scripts from $SCRIPT_ROOT +# and search for binary programs in $SCRIPT_ROOT/../bin + +SCRIPT="$(readlink -f "$0")" +SCRIPT_ROOT="$(dirname "$SCRIPT")" +. "$SCRIPT_ROOT/lib/cros_image_common.sh" || exit 1 +image_find_tool "cgpt" "$SCRIPT_ROOT/../bin" + set -e - -# --- BEGIN COMMON.SH BOILERPLATE --- -# Load common CrOS utilities. Inside the chroot this file is installed in -# /usr/lib/crosutils. Outside the chroot we find it relative to the script's -# location. -find_common_sh() { - local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) - local path - - SCRIPT_ROOT= - for path in "${common_paths[@]}"; do - if [ -r "${path}/common.sh" ]; then - SCRIPT_ROOT=${path} - break - fi - done -} - -find_common_sh -. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; } -# --- END COMMON.SH BOILERPLATE --- - -# Load functions designed for image processing -if ! . "${SCRIPT_ROOT}/lib/cros_image_common.sh"; then - echo "ERROR: Cannot load required library: lib/cros_image_common.sh; Abort." - exit 1 -fi - # We need 2-3 non-zero parameters. if [ "$#" -lt 2 ] || [ "$#" -gt 3 ] || [ -z "$1" ] || [ -z "$2" ]; then echo " @@ -56,12 +37,6 @@ Examples: exit 1 fi -if [ "$CROS_GENERATE_UPDATE_PAYLOAD_CALLED" != "1" ]; then - echo "WARNING:" - echo " This script should only be called from cros_generate_update_payload" - echo " Please run that script with --help to see how to use it." -fi - if ! image_has_command pigz; then (echo "WARNING:" echo " Your system does not have pigz (parallel gzip) installed."