diff --git a/build_library/disk_layout_util.sh b/build_library/disk_layout_util.sh index 719a4093b7..4c3c140b09 100644 --- a/build_library/disk_layout_util.sh +++ b/build_library/disk_layout_util.sh @@ -16,45 +16,6 @@ get_disk_layout_path() { done } -emit_gpt_scripts() { - local image="$1" - local dir="$2" - - local pack="${dir}/pack_partitions.sh" - local unpack="${dir}/unpack_partitions.sh" - - cat >"$unpack" <
> "$unpack" - - cat >>"${unpack}" <
&2 - exit 1 -fi -set -x -HEADER - - $GPT show "${image}" | sed -e 's/^/# /' >>"${unpack}" - cp "${unpack}" "${pack}" - - $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 - - chmod +x "${unpack}" "${pack}" -} - write_partition_script() { local image_type=$1 local partition_script_path=$2 @@ -139,32 +100,54 @@ emit_gpt_scripts() { local pack="${dir}/pack_partitions.sh" local unpack="${dir}/unpack_partitions.sh" + local mount="${dir}/mount_image.sh" + local umount="${dir}/umount_image.sh" - cat >"${unpack}" <
"${unpack}" <&2 +if [[ -z \${TARGET} ]]; then + echo "Usage: \$0 " 1>&2 + echo "Example: \$0 chromiumos_image.bin" 1>&2 exit 1 fi set -x -HEADER +$(${GPT} show "${image}" | sed -e 's/^/# /') +EOF - $GPT show "${image}" | sed -e 's/^/# /' >>"${unpack}" - cp "${unpack}" "${pack}" + for x in "${pack}" "${mount}" "${umount}"; do + cp "${unpack}" "${x}" + done - $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 + while read start size part x; do + local file="part_${part}" + local dir="dir_${part}" + local target='"${TARGET}"' + local dd_args="bs=512 count=${size}" + local start_b=$(( start * 512 )) + local size_b=$(( size * 512 )) + echo "dd if=${target} of=${file} ${dd_args} skip=${start}" >>"${unpack}" + echo "dd if=${file} of=${target} ${dd_args} seek=${start} conv=notrunc" \ + >>"${pack}" + if [[ ${size} -gt 1 ]]; then + cat <<-EOF >>"${mount}" +mkdir -p ${dir} +sudo mount -o loop,offset=${start_b},sizelimit=${size_b} ${target} ${dir} || \ + rmdir ${dir} +EOF + cat <<-EOF >>"${umount}" +if [[ -d ${dir} ]]; then + sudo umount ${dir} || : + rmdir ${dir} +fi +EOF + fi + done < <(${GPT} show -q "${image}") - chmod +x "${unpack}" "${pack}" + chmod +x "${unpack}" "${pack}" "${mount}" "${umount}" } build_gpt() {