fix(cros_make_image_bootable): Switch to disk_util mount/umount

This commit is contained in:
Michael Marineau 2013-12-28 18:35:15 -08:00
parent e2c2271579
commit 306a2f6cbc
5 changed files with 11 additions and 213 deletions

View File

@ -77,6 +77,8 @@ DEFINE_string image "coreos_base.img" \
"Full path to the coreos image to make bootable."
DEFINE_string arch "x86" \
"Architecture to make bootable for: arm, x86, or amd64"
DEFINE_string disk_layout "base" \
"The disk layout type to use for this image."
DEFINE_boolean enable_rootfs_verification ${FLAGS_FALSE} \
"Default all bootloaders to NOT use kernel-based root fs integrity checking."
DEFINE_string au_key "" \
@ -98,9 +100,7 @@ FLAGS "${@}" || exit 1
switch_to_strict_mode -u
mount_gpt_cleanup() {
"${SCRIPTS_DIR}/mount_gpt_image.sh" \
-u -r "${rootfs_mountpoint}" \
-s "${statefs_mountpoint}" -e "${espfs_mountpoint}"
"${BUILD_LIBRARY_DIR}/disk_util" umount "${rootfs_mountpoint}" || true
}
make_image_bootable() {
@ -112,12 +112,9 @@ make_image_bootable() {
enable_rootfs_verification_flag=--enable_rootfs_verification
fi
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${FLAGS_disk_layout}" \
mount "${image}" "${rootfs_mountpoint}"
trap "mount_gpt_cleanup" EXIT
"${SCRIPTS_DIR}/mount_gpt_image.sh" --from "$(dirname "${image}")" \
--image "$(basename ${image})" -r "${rootfs_mountpoint}" \
-s "${statefulfs_mountpoint}"
legacy_offset_size_export ${image}
if [ -n "${FLAGS_production_track}" ]; then
# Replace /etc/lsb-release on the image.
@ -146,7 +143,6 @@ make_image_bootable() {
'$2 == mnt { print $1 }' /proc/mounts)
# Make the filesystem un-mountable as read-write.
# mount_gpt_image.sh will undo this as needed.
# TODO(wad) make sure there is parity in the signing scripts.
if [ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]; then
# TODO(wad) this would be a good place to reset any other ext2 metadata.
@ -155,8 +151,7 @@ make_image_bootable() {
fi
trap - EXIT
${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${rootfs_mountpoint}" \
-s "${statefulfs_mountpoint}"
"${BUILD_LIBRARY_DIR}/disk_util" umount "${rootfs_mountpoint}"
}
verify_image_rootfs() {
@ -179,13 +174,9 @@ command line parameters are correct"
# Store output and temporary files next to image.
rootfs_mountpoint="${IMAGE_DIR}/rootfs_dir"
statefulfs_mountpoint="${IMAGE_DIR}/stateful_dir"
espfs_mountpoint="${IMAGE_DIR}/esp"
# Create the directories if they don't exist.
mkdir -p ${rootfs_mountpoint}
mkdir -p ${statefulfs_mountpoint}
mkdir -p ${espfs_mountpoint}
make_image_bootable "${IMAGE}"
if [ ${FLAGS_fsck_rootfs} -eq ${FLAGS_TRUE} ]; then
@ -193,5 +184,3 @@ if [ ${FLAGS_fsck_rootfs} -eq ${FLAGS_TRUE} ]; then
fi
rmdir ${rootfs_mountpoint}
rmdir ${statefulfs_mountpoint}
rmdir ${espfs_mountpoint}

View File

@ -149,6 +149,7 @@ if should_build_image ${COREOS_PRODUCTION_IMAGE_NAME}; then
"${BUILD_DIR}" \
${COREOS_PRODUCTION_IMAGE_NAME} \
--production_track="dev-channel" \
--disk_layout="${FLAGS_disk_layout}" \
--au_key=${SRC_ROOT}/third_party/coreos-overlay/coreos-base/coreos-au-key/files/update-payload-key.pub.pem
upload_image "${BUILD_DIR}/${COREOS_PRODUCTION_IMAGE_NAME}"

View File

@ -76,7 +76,8 @@ create_base_image() {
emit_gpt_scripts "${BUILD_DIR}/${image_name}" "${BUILD_DIR}"
${SCRIPTS_DIR}/bin/cros_make_image_bootable "${BUILD_DIR}" \
"${image_name}" --noenable_rootfs_verification
"${image_name}" --disk_layout="${disk_layout}" \
--noenable_rootfs_verification
trap - EXIT
}

View File

@ -49,7 +49,8 @@ install_dev_packages() {
if should_build_image ${image_name}; then
${SCRIPTS_DIR}/bin/cros_make_image_bootable "${BUILD_DIR}" \
"${image_name}" --noenable_rootfs_verification
"${image_name}" --disk_layout="${disk_layout}" \
--noenable_rootfs_verification
fi
trap - EXIT

View File

@ -1,194 +0,0 @@
#!/bin/bash
# Copyright (c) 2012 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.
# Helper script that mounts chromium os image from a device or directory
# and creates mount points for /var and /usr/local (if in dev_mode).
# Helper scripts should be run from the same location as this script.
SCRIPT_ROOT=$(dirname "$(readlink -f "$0")")
. "${SCRIPT_ROOT}/common.sh" || exit 1
if [ $INSIDE_CHROOT -ne 1 ]; then
INSTALL_ROOT="$SRC_ROOT/platform/installer/"
else
INSTALL_ROOT=/usr/lib/installer/
fi
# Load functions and constants for chromeos-install
. "${INSTALL_ROOT}/chromeos-common.sh" || exit 1
locate_gpt
# Flags.
DEFINE_string board "$DEFAULT_BOARD" \
"The board for which the image was built." b
DEFINE_boolean read_only $FLAGS_FALSE \
"Mount in read only mode -- skips stateful items."
DEFINE_boolean safe $FLAGS_FALSE \
"Mount rootfs in read only mode."
DEFINE_boolean unmount $FLAGS_FALSE \
"Unmount previously mounted dir." u
DEFINE_string from "/dev/sdc" \
"Directory, image, or device with image on it" f
DEFINE_string image "$COREOS_IMAGE_NAME" \
"Name of the bin file if a directory is specified in the from flag" i
DEFINE_string "rootfs_mountpt" "/tmp/m" "Mount point for rootfs" "r"
DEFINE_string "stateful_mountpt" "/tmp/s" \
"Mount point for stateful partition" "s"
DEFINE_string "esp_mountpt" "" \
"Mount point for esp partition" "e"
DEFINE_boolean most_recent ${FLAGS_FALSE} "Use the most recent image dir" m
# Parse flags
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
# Die on error
switch_to_strict_mode
# Find the last image built on the board.
if [ ${FLAGS_most_recent} -eq ${FLAGS_TRUE} ] ; then
FLAGS_from="$(${SCRIPT_ROOT}/get_latest_image.sh --board="${FLAGS_board}")"
fi
# Check for conflicting args.
# If --from is a block device, --image can't also be specified.
if [ -b "${FLAGS_from}" ]; then
if [ "${FLAGS_image}" != "$COREOS_IMAGE_NAME" ]; then
die_notrace "-i ${FLAGS_image} can't be used with block device ${FLAGS_from}"
fi
fi
# Allow --from /foo/file.bin
if [ -f "${FLAGS_from}" ]; then
# If --from is specified as a file, --image cannot be also specified.
if [ "${FLAGS_image}" != "$COREOS_IMAGE_NAME" ]; then
die_notrace "-i ${FLAGS_image} can't be used with --from file ${FLAGS_from}"
fi
pathname=$(dirname "${FLAGS_from}")
filename=$(basename "${FLAGS_from}")
FLAGS_image="${filename}"
FLAGS_from="${pathname}"
fi
# Common unmounts for either a device or directory
unmount_image() {
info "Unmounting image from ${FLAGS_stateful_mountpt}" \
"and ${FLAGS_rootfs_mountpt}"
# Don't die on error to force cleanup
set +e
safe_umount "${FLAGS_rootfs_mountpt}/usr/local"
safe_umount "${FLAGS_rootfs_mountpt}/var"
if [[ -n "${FLAGS_esp_mountpt}" ]]; then
safe_umount "${FLAGS_esp_mountpt}"
fi
safe_umount "${FLAGS_stateful_mountpt}"
safe_umount "${FLAGS_rootfs_mountpt}"
switch_to_strict_mode
}
get_usb_partitions() {
local ro_flag=""
local safe_flag=""
[ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ] && ro_flag="-o ro"
[ ${FLAGS_read_only} -eq ${FLAGS_TRUE} -o \
${FLAGS_safe} -eq ${FLAGS_TRUE} ] && safe_flag="-o ro -t ext2"
sudo mount ${safe_flag} "${FLAGS_from}4" "${FLAGS_rootfs_mountpt}"
sudo mount ${ro_flag} "${FLAGS_from}10" "${FLAGS_stateful_mountpt}"
if [[ -n "${FLAGS_esp_mountpt}" ]]; then
sudo mount ${ro_flag} "${FLAGS_from}2" "${FLAGS_esp_mountpt}"
fi
}
get_gpt_partitions() {
local filename="${FLAGS_image}"
legacy_offset_size_export "${FLAGS_from}/${FLAGS_image}"
# Mount the rootfs partition using a loopback device.
local offset=$(partoffset "${FLAGS_from}/${filename}" ${NUM_ROOTFS_A})
local ro_flag=""
local safe_flag=""
if [ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ]; then
ro_flag=",ro"
fi
if [ ${FLAGS_read_only} -eq ${FLAGS_TRUE} -o \
${FLAGS_safe} -eq ${FLAGS_TRUE} ]; then
safe_flag=",ro"
else
# Make sure any callers can actually mount and modify the fs
# if desired.
# cros_make_image_bootable should restore the bit if needed.
enable_rw_mount "${FLAGS_from}/${filename}" "$(( offset * 512 ))"
fi
if ! sudo mount -o loop,offset=$(( offset * 512 ))${safe_flag} \
"${FLAGS_from}/${filename}" "${FLAGS_rootfs_mountpt}" ; then
error "mount failed: options=${safe_flag} offset=$(( offset * 512 ))" \
"target=${FLAGS_rootfs_mountpt}"
return 1
fi
# Mount the stateful partition using a loopback device.
offset=$(partoffset "${FLAGS_from}/${filename}" ${NUM_STATEFUL})
if ! sudo mount -o loop,offset=$(( offset * 512 ))${ro_flag} \
"${FLAGS_from}/${filename}" "${FLAGS_stateful_mountpt}" ; then
error "mount failed: options=${ro_flag} offset=$(( offset * 512 ))" \
"target=${FLAGS_stateful_mountpt}"
return 1
fi
# Mount the esp partition using a loopback device.
if [[ -n "${FLAGS_esp_mountpt}" ]]; then
offset=$(partoffset "${FLAGS_from}/${filename}" ${NUM_ESP})
if ! sudo mount -o loop,offset=$(( offset * 512 ))${ro_flag} \
"${FLAGS_from}/${filename}" "${FLAGS_esp_mountpt}" ; then
error "mount failed: options=${ro_flag} offset=$(( offset * 512 ))" \
"target=${FLAGS_esp_mountpt}"
return 1
fi
fi
}
# Mount a gpt based image.
mount_image() {
mkdir -p "${FLAGS_rootfs_mountpt}"
mkdir -p "${FLAGS_stateful_mountpt}"
if [[ -n "${FLAGS_esp_mountpt}" ]]; then
mkdir -p "${FLAGS_esp_mountpt}"
fi
# Get the partitions for the image / device.
if [ -b ${FLAGS_from} ] ; then
get_usb_partitions
elif ! get_gpt_partitions ; then
echo "Current loopback device status:"
sudo losetup --all | sed 's/^/ /'
die "Failed to mount all partitions in ${FLAGS_from}/${FLAGS_image}"
fi
# Mount directories and setup symlinks.
sudo mount --bind "${FLAGS_stateful_mountpt}/overlays/var" \
"${FLAGS_rootfs_mountpt}/var"
sudo mount --bind "${FLAGS_stateful_mountpt}/overlays/usr/local" \
"${FLAGS_rootfs_mountpt}/usr/local"
info "Image specified by ${FLAGS_from} mounted at"\
"${FLAGS_rootfs_mountpt} successfully."
}
# Turn paths into absolute paths.
FLAGS_from=`eval readlink -f ${FLAGS_from}`
FLAGS_rootfs_mountpt=`eval readlink -f ${FLAGS_rootfs_mountpt}`
FLAGS_stateful_mountpt=`eval readlink -f ${FLAGS_stateful_mountpt}`
# Perform desired operation.
if [ ${FLAGS_unmount} -eq ${FLAGS_TRUE} ] ; then
unmount_image
else
mount_image
fi