blob: d5663f4f788ba050ff468c4e19c6884b1a7daeae [file] [log] [blame]
Chris Sosafb78b422010-04-19 14:56:18 -07001#!/bin/bash
2
Kees Cook224817f2012-06-28 12:33:59 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosafb78b422010-04-19 14:56:18 -07004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Helper script that mounts chromium os image from a device or directory
8# and creates mount points for /var and /usr/local (if in dev_mode).
9
Kees Cook84a4c7a2011-10-18 13:21:41 -070010# Helper scripts should be run from the same location as this script.
LaMont Jones52be7272019-10-21 08:16:35 -060011echo "Entering $0 $*" >&2
LaMont Jones52be7272019-10-21 08:16:35 -060012
Kees Cook84a4c7a2011-10-18 13:21:41 -070013SCRIPT_ROOT=$(dirname "$(readlink -f "$0")")
David James359d3e12012-07-10 13:09:48 -070014. "${SCRIPT_ROOT}/common.sh" || exit 1
Alex Deymod5449c82015-02-05 11:28:48 -080015. "${SCRIPT_ROOT}/build_library/filesystem_util.sh" || exit 1
16. "${SCRIPT_ROOT}/build_library/disk_layout_util.sh" || exit 1
Mike Frysingerdf7d5e62019-08-21 23:24:51 -040017. "${SCRIPT_ROOT}/build_library/ext2_sb_util.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080018
Gwendal Grignou01cf7f12018-05-03 12:12:23 -070019if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
20 INSTALL_ROOT="${SRC_ROOT}/platform2/chromeos-common-script/share"
Dale Curtisd569a902011-02-02 20:30:14 -080021else
Gwendal Grignou0f618492014-04-07 20:05:58 +000022 INSTALL_ROOT=/usr/share/misc
Dale Curtisd569a902011-02-02 20:30:14 -080023fi
Greg Spencer798d75f2011-02-01 22:04:49 -080024# Load functions and constants for chromeos-install
David James359d3e12012-07-10 13:09:48 -070025. "${INSTALL_ROOT}/chromeos-common.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080026
Bill Richardsonfc752ff2010-06-11 09:16:09 -070027locate_gpt
Chris Sosa702618f2010-05-14 12:52:32 -070028
Alex Deymod5449c82015-02-05 11:28:48 -080029# Default value for FLAGS_image.
30DEFAULT_IMAGE="chromiumos_image.bin"
31
Chris Sosafb78b422010-04-19 14:56:18 -070032# Flags.
33DEFINE_string board "$DEFAULT_BOARD" \
34 "The board for which the image was built." b
Alex Deymo0b8ba822015-03-02 21:36:33 -080035DEFINE_boolean read_only ${FLAGS_FALSE} \
Chris Sosae7f1a2b2010-09-28 18:24:13 -070036 "Mount in read only mode -- skips stateful items."
Alex Deymo0b8ba822015-03-02 21:36:33 -080037DEFINE_boolean safe ${FLAGS_FALSE} \
Nick Sanders867fde22010-11-16 20:05:06 -080038 "Mount rootfs in read only mode."
Alex Deymo0b8ba822015-03-02 21:36:33 -080039DEFINE_boolean unmount ${FLAGS_FALSE} \
Mike Frysinger57aac272018-04-30 03:30:33 -040040 "Unmount previously mounted image." u
Alex Deymod5449c82015-02-05 11:28:48 -080041DEFINE_string from "" \
Nick Sandersf9c49ad2011-03-21 18:56:04 -070042 "Directory, image, or device with image on it" f
Alex Deymod5449c82015-02-05 11:28:48 -080043DEFINE_string image "${DEFAULT_IMAGE}" \
Chris Sosa702618f2010-05-14 12:52:32 -070044 "Name of the bin file if a directory is specified in the from flag" i
Alex Deymod5449c82015-02-05 11:28:48 -080045DEFINE_string partition_script "partition_script.sh" \
46 "Name of the script with the partition layout if a directory is specified"
Alex Deymo3249ec62015-03-05 14:09:38 -080047DEFINE_string rootfs_mountpt "/tmp/m" "Mount point for rootfs" r
48DEFINE_string stateful_mountpt "/tmp/s" \
Alex Deymod5449c82015-02-05 11:28:48 -080049 "Mount point for stateful partition" s
Alex Deymo3249ec62015-03-05 14:09:38 -080050DEFINE_string esp_mountpt "" \
Alex Deymod5449c82015-02-05 11:28:48 -080051 "Mount point for esp partition" e
Alex Deymo3249ec62015-03-05 14:09:38 -080052DEFINE_boolean delete_mountpts ${FLAGS_FALSE} \
53 "Delete the mountpoint directories when unmounting."
Chris Sosafb78b422010-04-19 14:56:18 -070054DEFINE_boolean most_recent ${FLAGS_FALSE} "Use the most recent image dir" m
Nicolas Norvez1b86c8d2017-12-12 11:02:12 -080055DEFINE_string local_build_dir "/build" \
Nicolas Norvez89d3f992017-11-20 16:52:12 -080056 "Temporary root directory (under the sysroot) where ebuilds can install "\
57"temporary files during the build."
Chris Sosafb78b422010-04-19 14:56:18 -070058
59# Parse flags
60FLAGS "$@" || exit 1
61eval set -- "${FLAGS_ARGV}"
62
63# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080064switch_to_strict_mode
Chris Sosafb78b422010-04-19 14:56:18 -070065
Mike Frysinger1e4bd4c2018-04-30 04:22:08 -040066# We don't accept any positional args, so reject to catch typos.
67if [[ $# -ne 0 ]]; then
68 die_notrace "${SCRIPT_NAME} takes no arguments; given: $*"
69fi
70
Chris Sosaf37f64d2012-03-09 15:45:08 -080071# Find the last image built on the board.
Alex Deymod5449c82015-02-05 11:28:48 -080072if [[ ${FLAGS_most_recent} -eq ${FLAGS_TRUE} ]] ; then
Alex Klein999443c2018-10-17 12:02:05 -060073 FLAGS_from="${IMAGES_DIR}/${FLAGS_board}/latest"
Chris Sosaf37f64d2012-03-09 15:45:08 -080074fi
75
Nick Sandersf9c49ad2011-03-21 18:56:04 -070076# If --from is a block device, --image can't also be specified.
Alex Deymod5449c82015-02-05 11:28:48 -080077if [[ -b "${FLAGS_from}" ]]; then
78 if [[ "${FLAGS_image}" != "${DEFAULT_IMAGE}" ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -080079 die_notrace "-i ${FLAGS_image} can't be used with block device ${FLAGS_from}"
Nick Sandersf9c49ad2011-03-21 18:56:04 -070080 fi
81fi
82
83# Allow --from /foo/file.bin
Alex Deymod5449c82015-02-05 11:28:48 -080084if [[ -f "${FLAGS_from}" ]]; then
Nick Sandersf9c49ad2011-03-21 18:56:04 -070085 # If --from is specified as a file, --image cannot be also specified.
Alex Deymod5449c82015-02-05 11:28:48 -080086 if [[ "${FLAGS_image}" != "${DEFAULT_IMAGE}" ]]; then
Brian Harring7f175a52012-03-02 05:37:00 -080087 die_notrace "-i ${FLAGS_image} can't be used with --from file ${FLAGS_from}"
Nick Sandersf9c49ad2011-03-21 18:56:04 -070088 fi
Alex Deymod5449c82015-02-05 11:28:48 -080089 # The order is important here. We want to override FLAGS_image before
90 # destroying FLAGS_from.
91 FLAGS_image="$(basename "${FLAGS_from}")"
92 FLAGS_from="$(dirname "${FLAGS_from}")"
Nick Sandersf9c49ad2011-03-21 18:56:04 -070093fi
94
Mike Frysinger4993c942019-08-21 23:15:12 -040095# Fixes symlinks that are incorrectly prefixed with the build root $1
96# rather than the real running root '/'.
97fix_broken_symlinks() {
98 local build_root=$1
99 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
100 local symlink
101 for symlink in ${symlinks}; do
102 echo "Fixing ${symlink}"
103 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
104 # Trim spaces from target (bashism).
105 target=${target/ /}
106 # Make new target (removes rootfs prefix).
107 new_target=$(echo ${target} | sed "s#${build_root}##")
108
109 echo "Fixing symlink ${symlink}"
110 sudo unlink "${symlink}"
111 sudo ln -sf "${new_target}" "${symlink}"
112 done
113}
114
Joshua Emelefcc79712017-03-14 17:34:30 -0700115load_image_partition_numbers() {
Alex Deymo0b8ba822015-03-02 21:36:33 -0800116 local partition_script="${FLAGS_from}/${FLAGS_partition_script}"
117 # Attempt to load the partition script from the rootfs when not found in the
118 # FLAGS_from directory.
119 if [[ ! -f "${partition_script}" ]]; then
120 partition_script="${FLAGS_rootfs_mountpt}/${PARTITION_SCRIPT_PATH}"
121 fi
122 if [[ -f "${partition_script}" ]]; then
123 . "${partition_script}"
124 load_partition_vars
Joshua Emelefcc79712017-03-14 17:34:30 -0700125 return
Alex Deymo0b8ba822015-03-02 21:36:33 -0800126 fi
Joshua Emelefcc79712017-03-14 17:34:30 -0700127
128 # Without a partition script, infer numbers from the payload image.
129 local image
130 if [[ -b "${FLAGS_from}" ]]; then
131 image="${FLAGS_from}"
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400132 elif [[ -n "${FLAGS_from}" ]]; then
Joshua Emelefcc79712017-03-14 17:34:30 -0700133 image="${FLAGS_from}/${FLAGS_image}"
134 if [[ ! -f "${image}" ]]; then
135 die "Image ${image} does not exist."
136 fi
137 fi
138 PARTITION_NUM_STATE="$(get_image_partition_number "${image}" "STATE")"
139 PARTITION_NUM_ROOT_A="$(get_image_partition_number "${image}" "ROOT-A")"
140 PARTITION_NUM_OEM="$(get_image_partition_number "${image}" "OEM")"
141 PARTITION_NUM_EFI_SYSTEM="$(get_image_partition_number "${image}" \
142 "EFI-SYSTEM")"
Alex Deymo0b8ba822015-03-02 21:36:33 -0800143}
144
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800145unmount_local_build_root() {
146 local build_dir="${FLAGS_rootfs_mountpt}/${FLAGS_local_build_dir}"
147 local rootfs="${build_dir}/rootfs"
148 info "Unmounting temporary rootfs ${rootfs}."
149 if [[ -d "${rootfs}" ]]; then
150 sudo umount "${rootfs}"
151 sudo rmdir "${rootfs}"
152 fi
Nicolas Norvez789c1b72017-12-14 13:17:56 -0800153 if [[ -d "${build_dir}" ]]; then
154 sudo rmdir "${build_dir}"
155 fi
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800156 sudo rm -rf "${LOCAL_BUILDROOT_MOUNTPOINT}"
157}
158
Chris Sosa702618f2010-05-14 12:52:32 -0700159# Common unmounts for either a device or directory
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400160unmount_image() {
Mike Frysinger84f66f52012-01-12 12:00:07 -0500161 info "Unmounting image from ${FLAGS_stateful_mountpt}" \
Chris Sosafb78b422010-04-19 14:56:18 -0700162 "and ${FLAGS_rootfs_mountpt}"
163 # Don't die on error to force cleanup
164 set +e
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800165
166 if [[ ${FLAGS_read_only} -eq ${FLAGS_FALSE} ]]; then
Nicolas Norvez1b86c8d2017-12-12 11:02:12 -0800167 if [[ ${FLAGS_safe} -eq ${FLAGS_FALSE} ]]; then
168 unmount_local_build_root
169 fi
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800170 fi
171
Chris Sosa9673f3b2010-05-18 13:24:40 -0700172 # Reset symlinks in /usr/local.
Mike Frysinger3c74b3d2012-10-12 14:26:03 -0400173 if mount | egrep -q ".* ${FLAGS_stateful_mountpt} .*\(rw,"; then
Mike Frysinger79d7ddd2015-03-08 22:18:52 -0400174 setup_symlinks_on_root "." "/var" "${FLAGS_stateful_mountpt}"
Chris Sosae7f1a2b2010-09-28 18:24:13 -0700175 fix_broken_symlinks "${FLAGS_rootfs_mountpt}"
176 fi
Mike Frysingerae2e3c32013-06-26 20:20:55 -0400177
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400178 local loopdev
Alex Deymod5449c82015-02-05 11:28:48 -0800179 local filename
180 if [[ -b "${FLAGS_from}" ]]; then
181 filename="${FLAGS_from}"
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400182 elif [[ -n "${FLAGS_from}" ]]; then
Alex Deymod5449c82015-02-05 11:28:48 -0800183 filename="${FLAGS_from}/${FLAGS_image}"
184 if [[ ! -f "${filename}" ]]; then
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400185 die "Image ${filename} does not exist."
Mike Frysinger9140d772013-08-05 18:44:11 -0400186 fi
Alex Deymod5449c82015-02-05 11:28:48 -0800187 fi
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400188 if [[ -z "${filename}" ]]; then
189 warn "Umount called without passing the image. Some filesystems can't" \
190 "be unmounted in this way."
191 else
192 loopdev="$(loopback_partscan "${filename}")"
193 fi
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400194
Alex Deymod5449c82015-02-05 11:28:48 -0800195 # Unmount in reverse order: EFI, OEM, stateful and rootfs.
196 local var_name mountpoint fs_format fs_options
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400197 local part_label part_num part_loop
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800198 for part_label in EFI_SYSTEM OEM STATE ROOT_A; do
199 var_name="${part_label}_MOUNTPOINT"
200 mountpoint="${!var_name}"
Alex Deymod5449c82015-02-05 11:28:48 -0800201 [[ -n "${mountpoint}" ]] || continue
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800202 var_name="PARTITION_NUM_${part_label}"
203 part_num="${!var_name}"
Mike Frysinger2cf96672018-04-30 04:08:32 -0400204 if [[ -z "${part_num}" ]]; then
205 # Depending on how it was mounted, clear all existing mounts.
206 sudo umount -R "${mountpoint}"
207 continue
208 fi
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400209 part_loop="${loopdev}p${part_num}"
Alex Deymod5449c82015-02-05 11:28:48 -0800210
211 if [[ -z "${filename}" ]]; then
212 # TODO(deymo): Remove this legacy umount.
213 if ! mountpoint -q "${mountpoint}"; then
214 die "You must pass --image or --from when using --unmount to unmount" \
215 "this image."
216 fi
217 safe_umount_tree "${mountpoint}"
218 continue
219 fi
220
Alex Deymod5449c82015-02-05 11:28:48 -0800221 # Get the variables loaded with load_partition_vars during mount_*.
222 var_name="FS_FORMAT_${part_num}"
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800223 fs_format="${!var_name}"
Alex Deymod5449c82015-02-05 11:28:48 -0800224 var_name="FS_OPTIONS_${part_num}"
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800225 fs_options="${!var_name}"
Alex Deymod5449c82015-02-05 11:28:48 -0800226
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400227 fs_umount "${part_loop}" "${mountpoint}" "${fs_format}" "${fs_options}"
Mike Frysinger9140d772013-08-05 18:44:11 -0400228 done
229
Mike Frysinger34d2ce42019-07-12 20:26:39 -0400230 if [[ -n "${loopdev}" ]]; then
231 sudo losetup -d "${loopdev}"
232 fi
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400233
Alex Deymo3249ec62015-03-05 14:09:38 -0800234 # We need to remove the mountpoints after we unmount all the partitions since
235 # there could be nested mounts.
236 if [[ ${FLAGS_delete_mountpts} -eq ${FLAGS_TRUE} ]]; then
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800237 for part_label in EFI_SYSTEM OEM STATE ROOT_A; do
238 var_name="${part_label}_MOUNTPOINT"
239 mountpoint="${!var_name}"
Alex Deymo3249ec62015-03-05 14:09:38 -0800240 # Check this is a directory.
241 [[ -n "${mountpoint}" && -d "${mountpoint}" ]] || continue
242 fs_remove_mountpoint "${mountpoint}"
243 done
244 fi
245
Brian Harring7f175a52012-03-02 05:37:00 -0800246 switch_to_strict_mode
Chris Sosafb78b422010-04-19 14:56:18 -0700247}
248
Alex Deymod5449c82015-02-05 11:28:48 -0800249mount_usb_partitions() {
250 local ro_rw="rw"
251 local rootfs_ro_rw="rw"
252 if [[ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ]]; then
253 ro_rw="ro"
254 fi
255 if [[ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ||
256 ${FLAGS_safe} -eq ${FLAGS_TRUE} ]]; then
257 rootfs_ro_rw="ro"
258 fi
Chris Sosae7f1a2b2010-09-28 18:24:13 -0700259
Alex Deymod5449c82015-02-05 11:28:48 -0800260 if [[ -f "${FLAGS_from}/${FLAGS_partition_script}" ]]; then
261 . "${FLAGS_from}/${FLAGS_partition_script}"
262 load_partition_vars
263 fi
264
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800265 fs_mount "${FLAGS_from}${PARTITION_NUM_ROOT_A}" "${ROOT_A_MOUNTPOINT}" \
266 "${FS_FORMAT_ROOT_A}" "${rootfs_ro_rw}"
267 fs_mount "${FLAGS_from}${PARTITION_NUM_STATE}" "${STATE_MOUNTPOINT}" \
268 "${FS_FORMAT_STATE}" "${ro_rw}"
269 fs_mount "${FLAGS_from}${PARTITION_NUM_OEM}" "${OEM_MOUNTPOINT}" \
270 "${FS_FORMAT_OEM}" "${ro_rw}"
Mike Frysingerae2e3c32013-06-26 20:20:55 -0400271
Steven 'Steve' Kendall7fbc3132016-06-03 13:19:00 -0400272 if [[ -n "${FLAGS_esp_mountpt}" && \
273 -e ${FLAGS_from}${PARTITION_NUM_EFI_SYSTEM} ]]; then
274 fs_mount "${FLAGS_from}${PARTITION_NUM_EFI_SYSTEM}" \
Ian Coolidge094d5432017-02-09 01:18:17 -0800275 "${EFI_SYSTEM_MOUNTPOINT}" "${FS_FORMAT_EFI_SYSTEM}" "${ro_rw}"
Chris Masone3adf3f72010-07-09 16:44:18 -0700276 fi
Chris Sosa702618f2010-05-14 12:52:32 -0700277}
278
Alex Deymod5449c82015-02-05 11:28:48 -0800279mount_gpt_partitions() {
280 local filename="${FLAGS_from}/${FLAGS_image}"
Chris Sosa702618f2010-05-14 12:52:32 -0700281
Alex Deymod5449c82015-02-05 11:28:48 -0800282 local ro_rw="rw"
283 if [[ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ]]; then
284 ro_rw="ro"
Chris Sosa4e155ea2013-12-19 16:29:37 -0800285 fi
286
Alex Deymod5449c82015-02-05 11:28:48 -0800287 if [[ ! -f "${filename}" ]]; then
288 die "Image ${filename} does not exist."
Nick Sanders867fde22010-11-16 20:05:06 -0800289 fi
290
Ian Coolidge429e9c42017-02-10 00:49:36 -0800291 if [[ -f "${FLAGS_from}/${FLAGS_partition_script}" ]]; then
292 . "${FLAGS_from}/${FLAGS_partition_script}"
293 load_partition_vars
294 fi
295
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400296 local loopdev="$(loopback_partscan "${filename}")"
Chris Sosae7f1a2b2010-09-28 18:24:13 -0700297
Alex Deymod5449c82015-02-05 11:28:48 -0800298 # Mount in order: rootfs, stateful, OEM and EFI.
299 local var_name mountpoint fs_format
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400300 local part_label part_num part_loop part_ro_rw
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800301 for part_label in ROOT_A STATE OEM EFI_SYSTEM; do
302 var_name="${part_label}_MOUNTPOINT"
303 mountpoint="${!var_name}"
Alex Deymod5449c82015-02-05 11:28:48 -0800304 [[ -n "${mountpoint}" ]] || continue
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800305
306 var_name="PARTITION_NUM_${part_label}"
307 part_num="${!var_name}"
Joshua Emeled60789f2017-06-23 17:09:45 -0700308 [[ -n "${part_num}" ]] || continue
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400309 part_loop="${loopdev}p${part_num}"
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800310
Alex Deymod5449c82015-02-05 11:28:48 -0800311 var_name="FS_FORMAT_${part_num}"
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800312 fs_format="${!var_name}"
Alex Deymod5449c82015-02-05 11:28:48 -0800313
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400314 # For the rootfs, make sure it's writable so callers can modify it,
315 # unless the caller explicitly requested otherwise.
316 # cros_make_image_bootable should restore the bit if needed.
Alex Deymod5449c82015-02-05 11:28:48 -0800317 part_ro_rw="${ro_rw}"
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400318 if [[ "${part_label}" == ROOT_* ]]; then
319 if [[ ${FLAGS_safe} -eq ${FLAGS_TRUE} ]]; then
320 part_ro_rw="ro"
321 elif [[ ${FLAGS_read_only} -eq ${FLAGS_FALSE} ]]; then
322 enable_rw_mount "${part_loop}"
323 fi
Alex Deymod5449c82015-02-05 11:28:48 -0800324 fi
Alex Deymod5449c82015-02-05 11:28:48 -0800325
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400326 if ! fs_mount "${part_loop}" "${mountpoint}" "${fs_format}" \
327 "${part_ro_rw}"; then
328 error "mount failed: image=${filename} device=${part_loop}" \
329 "target=${mountpoint} format=${fs_format} ro/rw=${part_ro_rw}"
330 sudo losetup -d "${loopdev}"
Gaurav Shah05618a62014-01-20 15:06:54 -0800331 return 1
332 fi
Alex Deymod5449c82015-02-05 11:28:48 -0800333 done
Mike Frysinger0b2e7262018-04-30 04:07:58 -0400334
335 # Detach the loopback now even though we have mounts. This way when the last
336 # mount is freed, the kernel will automatically release the loopback.
337 sudo losetup -d "${loopdev}"
Chris Sosa702618f2010-05-14 12:52:32 -0700338}
339
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800340# Create a local buildroot that can be used by ebuilds that need to install
341# temporary files during the build even though those files should not be in the
342# final image. This is typically the case of ebuilds that install files to
343# Android's /vendor directory before board_specific_setup repacks them to the
344# final vendor image. Those ebuilds can instead install files to
Nicolas Norvez1b86c8d2017-12-12 11:02:12 -0800345# /build/rootfs/opt/google/containers/.../vendor where board_specific_setup
346# will pick them up and add them to the final vendor image.
347# To avoid running out of space in the root partition during the build, use a
348# separate directory outside of the image and bindmount it to the local
349# buildroot.
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800350mount_local_build_root() {
351 local build_dir="${FLAGS_rootfs_mountpt}/${FLAGS_local_build_dir}"
352 local rootfs="${build_dir}/rootfs"
353 if [[ ! -d "${rootfs}" ]]; then
354 sudo mkdir -p "${rootfs}"
355 fi
356 info "Mounting temporary rootfs ${LOCAL_BUILDROOT_MOUNTPOINT} to ${rootfs}."
357 if [[ ! -d "${LOCAL_BUILDROOT_MOUNTPOINT}" ]]; then
358 sudo mkdir -p "${LOCAL_BUILDROOT_MOUNTPOINT}"
359 fi
360 sudo mount --bind "${LOCAL_BUILDROOT_MOUNTPOINT}" "${rootfs}"
361}
362
Chris Sosa702618f2010-05-14 12:52:32 -0700363# Mount a gpt based image.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400364mount_image() {
Chris Sosafb78b422010-04-19 14:56:18 -0700365 mkdir -p "${FLAGS_rootfs_mountpt}"
366 mkdir -p "${FLAGS_stateful_mountpt}"
Chris Masone3adf3f72010-07-09 16:44:18 -0700367 if [[ -n "${FLAGS_esp_mountpt}" ]]; then
Will Drewryfb48fa32010-07-02 16:35:27 -0500368 mkdir -p "${FLAGS_esp_mountpt}"
Chris Masone3adf3f72010-07-09 16:44:18 -0700369 fi
Chris Sosa702618f2010-05-14 12:52:32 -0700370 # Get the partitions for the image / device.
Alex Deymod5449c82015-02-05 11:28:48 -0800371 if [[ -b "${FLAGS_from}" ]]; then
372 mount_usb_partitions
373 elif ! mount_gpt_partitions; then
J. Richard Barnette25d540d2012-01-18 15:40:10 -0800374 echo "Current loopback device status:"
375 sudo losetup --all | sed 's/^/ /'
376 die "Failed to mount all partitions in ${FLAGS_from}/${FLAGS_image}"
Chris Sosa702618f2010-05-14 12:52:32 -0700377 fi
378
Mike Frysinger141ef062019-07-12 22:10:29 -0400379 # Mount directories and setup symlinks. Create dirs on demand in case they
380 # were wiped out for some reason (devs like to dev!).
381 mkdir_and_mount() {
382 local src="$1" dst="$2"
383 if [[ ! -d "${src}" ]]; then
384 sudo mkdir "${src}"
385 fi
386 if [[ ! -d "${dst}" ]]; then
387 sudo mkdir "${dst}"
388 fi
389 sudo mount --bind "${src}" "${dst}"
390 }
391 mkdir_and_mount "${FLAGS_stateful_mountpt}" \
Chris Sosa4e155ea2013-12-19 16:29:37 -0800392 "${FLAGS_rootfs_mountpt}/mnt/stateful_partition"
Mike Frysinger141ef062019-07-12 22:10:29 -0400393 mkdir_and_mount "${FLAGS_stateful_mountpt}/var_overlay" \
Chris Sosafb78b422010-04-19 14:56:18 -0700394 "${FLAGS_rootfs_mountpt}/var"
Mike Frysinger141ef062019-07-12 22:10:29 -0400395 mkdir_and_mount "${FLAGS_stateful_mountpt}/dev_image" \
Chris Sosa9673f3b2010-05-18 13:24:40 -0700396 "${FLAGS_rootfs_mountpt}/usr/local"
Chris Sosae7f1a2b2010-09-28 18:24:13 -0700397
Alex Deymod5449c82015-02-05 11:28:48 -0800398 if [[ ${FLAGS_read_only} -eq ${FLAGS_FALSE} ]]; then
Nicolas Norvez1b86c8d2017-12-12 11:02:12 -0800399 if [[ ${FLAGS_safe} -eq ${FLAGS_FALSE} ]]; then
400 mount_local_build_root
401 fi
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800402 # Setup symlinks in /usr/local so you can emerge packages into /usr/local.
Mike Frysinger79d7ddd2015-03-08 22:18:52 -0400403 setup_symlinks_on_root "." \
Kees Cook224817f2012-06-28 12:33:59 -0700404 "${FLAGS_stateful_mountpt}/var_overlay" "${FLAGS_stateful_mountpt}"
Chris Sosae7f1a2b2010-09-28 18:24:13 -0700405 fi
Mike Frysinger84f66f52012-01-12 12:00:07 -0500406 info "Image specified by ${FLAGS_from} mounted at"\
Chris Sosafb78b422010-04-19 14:56:18 -0700407 "${FLAGS_rootfs_mountpt} successfully."
408}
409
Chris Sosac60e5672010-11-12 10:54:25 -0800410# Turn paths into absolute paths.
Alex Deymod5449c82015-02-05 11:28:48 -0800411[[ -n "${FLAGS_from}" ]] && FLAGS_from="$(readlink -f "${FLAGS_from}")"
412FLAGS_rootfs_mountpt="$(readlink -f "${FLAGS_rootfs_mountpt}")"
413FLAGS_stateful_mountpt="$(readlink -f "${FLAGS_stateful_mountpt}")"
414
415# Partition mountpoints based on the flags.
Ian Coolidge392f7bb2017-02-04 17:08:31 -0800416ROOT_A_MOUNTPOINT="${FLAGS_rootfs_mountpt}"
417STATE_MOUNTPOINT="${FLAGS_stateful_mountpt}"
418OEM_MOUNTPOINT="${FLAGS_rootfs_mountpt}/usr/share/oem"
419EFI_SYSTEM_MOUNTPOINT="${FLAGS_esp_mountpt}"
Nicolas Norvez89d3f992017-11-20 16:52:12 -0800420LOCAL_BUILDROOT_MOUNTPOINT="${FLAGS_rootfs_mountpt}-local-build-dir"
Chris Sosafb78b422010-04-19 14:56:18 -0700421
Joshua Emelefcc79712017-03-14 17:34:30 -0700422# Read the image partition numbers from the GPT.
423load_image_partition_numbers
424
Chris Sosa702618f2010-05-14 12:52:32 -0700425# Perform desired operation.
Alex Deymod5449c82015-02-05 11:28:48 -0800426if [[ ${FLAGS_unmount} -eq ${FLAGS_TRUE} ]]; then
Chris Sosa702618f2010-05-14 12:52:32 -0700427 unmount_image
Chris Sosafb78b422010-04-19 14:56:18 -0700428else
Chris Sosa702618f2010-05-14 12:52:32 -0700429 mount_image
Chris Sosafb78b422010-04-19 14:56:18 -0700430fi