fix(build_image): Migrate to new disk_util commands

This commit is contained in:
Michael Marineau 2013-12-26 15:40:02 -08:00
parent 9bc36ee344
commit 52e1dba3ec
5 changed files with 31 additions and 183 deletions

View File

@ -21,7 +21,7 @@ DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
"Default all bootloaders to use kernel-based root fs integrity checking."
DEFINE_string output_root "${DEFAULT_BUILD_ROOT}/images" \
"Directory in which to place image result directories (named by version)"
DEFINE_string disk_layout "default" \
DEFINE_string disk_layout "base" \
"The disk layout type to use for this image."
# include upload options
@ -74,7 +74,6 @@ check_gsutil_opts
. "${BUILD_LIBRARY_DIR}/toolchain_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
. "${BUILD_LIBRARY_DIR}/dev_image_util.sh" || exit 1

View File

@ -4,140 +4,22 @@
. "${SRC_ROOT}/platform/dev/toolchain_utils.sh" || exit 1
# Overlays are parts of the disk that live on the state partition
ROOT_OVERLAYS=(var opt srv home usr/local)
cleanup_mounts() {
local prev_ret=$?
# Disable die on error.
set +e
# See if we ran out of space. Only show if we errored out via a trap.
if [[ ${prev_ret} -ne 0 ]]; then
local df=$(df -B 1M "${root_fs_dir}")
if [[ ${df} == *100%* ]]; then
error "Here are the biggest files (by disk usage):"
# Send final output to stderr to match `error` behavior.
sudo find "${root_fs_dir}" -xdev -type f -printf '%b %P\n' | \
awk '$1 > 16 { $1 = $1 * 512; print }' | sort -n | tail -100 1>&2
error "Target image has run out of space:"
error "${df}"
fi
fi
echo "Cleaning up mounts"
safe_umount_tree "${root_fs_dir}"
safe_umount_tree "${state_fs_dir}"
safe_umount_tree "${esp_fs_dir}"
safe_umount_tree "${oem_fs_dir}"
# Turn die on error back on.
set -e
}
create_base_image() {
local image_name=$1
local rootfs_verification_enabled=$2
get_disk_layout_type
check_valid_layout "base"
check_valid_layout "${DISK_LAYOUT_TYPE}"
local disk_layout="${FLAGS_disk_layout:-base}"
local disk_img="${BUILD_DIR}/${image_name}"
local mbr_img="/usr/share/syslinux/gptmbr.bin"
local root_fs_dir="${BUILD_DIR}/rootfs"
info "Using image type ${DISK_LAYOUT_TYPE}"
info "Using image type ${disk_layout}"
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
format --mbr_boot_code="${mbr_img}" "${disk_img}"
root_fs_dir="${BUILD_DIR}/rootfs"
state_fs_dir="${BUILD_DIR}/state"
esp_fs_dir="${BUILD_DIR}/esp"
oem_fs_dir="${BUILD_DIR}/oem"
trap "cleanup_mounts && delete_prompt" EXIT
cleanup_mounts &> /dev/null
local root_fs_label="ROOT-A"
local root_fs_num=$(get_num ${root_fs_label})
local root_fs_img="${BUILD_DIR}/rootfs.image"
local root_fs_bytes=$(get_filesystem_size ${root_fs_num})
local state_fs_label="STATE"
local state_fs_num=$(get_num ${state_fs_label})
local state_fs_img="${BUILD_DIR}/state.image"
local state_fs_bytes=$(get_filesystem_size ${state_fs_num})
local state_fs_uuid=$(uuidgen)
local esp_fs_label="EFI-SYSTEM"
local esp_fs_num=$(get_num ${esp_fs_label})
local esp_fs_img="${BUILD_DIR}/esp.image"
local esp_fs_bytes=$(get_filesystem_size ${esp_fs_num})
local oem_fs_label="OEM"
local oem_fs_num=$(get_num ${oem_fs_label})
local oem_fs_img="${BUILD_DIR}/oem.image"
local oem_fs_bytes=$(get_filesystem_size ${oem_fs_num})
local oem_fs_uuid=$(uuidgen)
local fs_block_size=$(get_fs_block_size)
# Build root FS image.
info "Building ${root_fs_img}"
truncate --size="${root_fs_bytes}" "${root_fs_img}"
/sbin/mkfs.ext2 -F -q -b ${fs_block_size} "${root_fs_img}" \
"$((root_fs_bytes / fs_block_size))"
/sbin/tune2fs -L "${root_fs_label}" \
-U clear \
-T 20091119110000 \
-c 0 \
-i 0 \
-m 0 \
-r 0 \
-e remount-ro \
"${root_fs_img}"
mkdir -p "${root_fs_dir}"
sudo mount -o loop "${root_fs_img}" "${root_fs_dir}"
df -h "${root_fs_dir}"
# Build state FS disk image.
info "Building ${state_fs_img}"
truncate --size="${state_fs_bytes}" "${state_fs_img}"
/sbin/mkfs.ext4 -F -q "${state_fs_img}"
/sbin/tune2fs -L "${state_fs_label}" -U "${state_fs_uuid}" \
-c 0 -i 0 "${state_fs_img}"
mkdir -p "${state_fs_dir}"
sudo mount -o loop "${state_fs_img}" "${state_fs_dir}"
# Build ESP disk image.
info "Building ${esp_fs_img}"
truncate --size="${esp_fs_bytes}" "${esp_fs_img}"
/usr/sbin/mkfs.vfat "${esp_fs_img}"
# Build OEM FS disk image.
info "Building ${oem_fs_img}"
truncate --size="${oem_fs_bytes}" "${oem_fs_img}"
/sbin/mkfs.ext4 -F -q "${oem_fs_img}"
/sbin/tune2fs -L "${oem_fs_label}" -U "${oem_fs_uuid}" \
-c 0 -i 0 "${oem_fs_img}"
mkdir -p "${oem_fs_dir}"
sudo mount -o loop "${oem_fs_img}" "${oem_fs_dir}"
# Prepare state partition with some pre-created directories.
info "Binding directories from state partition onto the rootfs"
for i in "${ROOT_OVERLAYS[@]}"; do
sudo mkdir -p "${state_fs_dir}/overlays/$i"
sudo mkdir -p "${root_fs_dir}/$i"
sudo mount --bind "${state_fs_dir}/overlays/$i" "${root_fs_dir}/$i"
done
# TODO(bp): remove these temporary fixes for /mnt/stateful_partition going moving
sudo mkdir -p "${root_fs_dir}/mnt/stateful_partition/"
sudo ln -s /media/state/overlays/usr/local "${root_fs_dir}/mnt/stateful_partition/dev_image"
sudo ln -s /media/state/overlays/home "${root_fs_dir}/mnt/stateful_partition/home"
sudo ln -s /media/state/overlays/var "${root_fs_dir}/mnt/stateful_partition/var_overlay"
sudo ln -s /media/state/etc "${root_fs_dir}/mnt/stateful_partition/etc"
info "Binding directories from OEM partition onto the rootfs"
sudo mkdir -p "${root_fs_dir}/usr/share/oem"
sudo mount --bind "${oem_fs_dir}" "${root_fs_dir}/usr/share/oem"
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
mount "${disk_img}" "${root_fs_dir}"
trap "cleanup_mounts '${root_fs_dir}' && delete_prompt" EXIT
# First thing first, install baselayout with USE=build to create a
# working directory tree. Don't use binpkgs due to the use flag change.
@ -182,19 +64,12 @@ create_base_image() {
# Zero all fs free space to make it more compressible so auto-update
# payloads become smaller, not fatal since it won't work on linux < 3.2
sudo fstrim "${root_fs_dir}" || true
sudo fstrim "${state_fs_dir}" || true
if [[ -d "${root_fs_dir}/media/state" ]]; then
sudo fstrim "${root_fs_dir}/media/state" || true
fi
cleanup_mounts
# Create the GPT-formatted image.
build_gpt "${BUILD_DIR}/${image_name}" \
"${root_fs_img}" \
"${state_fs_img}" \
"${esp_fs_img}" \
"${oem_fs_img}"
# Clean up temporary files.
rm -f "${root_fs_img}" "${state_fs_img}" "${esp_fs_img}" "{oem_fs_img}"
cleanup_mounts "${root_fs_dir}"
trap "delete_prompt" EXIT
# Emit helpful scripts for testers, etc.
emit_gpt_scripts "${BUILD_DIR}/${image_name}" "${BUILD_DIR}"

View File

@ -96,6 +96,11 @@ create_boot_desc() {
EOF
}
cleanup_mounts() {
echo "Cleaning up mounts"
"${BUILD_LIBRARY_DIR}/disk_util" umount "$1" || true
}
delete_prompt() {
echo "An error occurred in your build so your latest output directory" \
"is invalid."

View File

@ -13,11 +13,12 @@ install_dev_packages() {
local image_name=$1
info "Adding developer packages to ${image_name}"
local disk_layout="${FLAGS_disk_layout:-base}"
local root_fs_dir="${BUILD_DIR}/rootfs"
trap "unmount_image ; delete_prompt" EXIT
mount_image "${BUILD_DIR}/${image_name}" "${root_fs_dir}" \
"${state_fs_dir}" "${esp_fs_dir}"
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
mount "${BUILD_DIR}/${image_name}" "${root_fs_dir}"
trap "cleanup_mounts '${root_fs_dir}' && delete_prompt" EXIT
# Install developer packages described in coreos-dev.
emerge_to_image --root="${root_fs_dir}" coreos-base/coreos-dev
@ -37,11 +38,14 @@ install_dev_packages() {
# Zero all fs free space, not fatal since it won't work on linux < 3.2
sudo fstrim "${root_fs_dir}" || true
sudo fstrim "${state_fs_dir}" || true
if [[ -d "${root_fs_dir}/media/state" ]]; then
sudo fstrim "${root_fs_dir}/media/state" || true
fi
info "Developer image built and stored at ${image_name}"
cleanup_mounts
cleanup_mounts "${root_fs_dir}"
trap "delete_prompt" EXIT
if should_build_image ${image_name}; then
${SCRIPTS_DIR}/bin/cros_make_image_bootable "${BUILD_DIR}" \

View File

@ -1,35 +0,0 @@
# 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.
# This global array variable is used to remember options from
# mount_image so that unmount_image can do its job.
MOUNT_GPT_OPTIONS=( )
# mount_image - Mount the root, stateful, and optionally ESP partitions
# in a Chromium OS image.
# $1: path to image to be mounted
# $2: path to root fs mount point
# $3: path to stateful fs mount point
# $4: path to ESP fs mount point; if empty the ESP will not be mounted
mount_image() {
local image_dir="$(dirname $1)"
local image="$(basename $1)"
MOUNT_GPT_OPTIONS=( -r "$2" -s "$3" )
if [ $# -ge 4 ]; then
MOUNT_GPT_OPTIONS=( "${MOUNT_GPT_OPTIONS[@]}" -e "$4" )
fi
"${SCRIPTS_DIR}/mount_gpt_image.sh" --from="$image_dir" --image="$image" \
"${MOUNT_GPT_OPTIONS[@]}"
}
# unmount_image - Unmount the file systems mounted in the previous
# call to mount_image.
# No arguments
unmount_image() {
"${SCRIPTS_DIR}/mount_gpt_image.sh" -u "${MOUNT_GPT_OPTIONS[@]}"
MOUNT_GPT_OPTIONS=( )
}