From e7f1a2b6bb270429bdb953a2e59273d434ee9b2c Mon Sep 17 00:00:00 2001 From: Chris Sosa Date: Tue, 28 Sep 2010 18:24:13 -0700 Subject: [PATCH] Add read only flag to mount_gpt_image Change-Id: I33ea8b2148a3e49a6c5de25f2957306130ff092e BUG= TEST=Ran and verified with and without flag. Checked mount and tried writing file to rootfs (to verify it was ro). Also re-ran cros_make_image_bootable to make sure it worked fine after changes. Review URL: http://codereview.chromium.org/3479007 --- mount_gpt_image.sh | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/mount_gpt_image.sh b/mount_gpt_image.sh index 247428047c..ee7b3cc6f6 100755 --- a/mount_gpt_image.sh +++ b/mount_gpt_image.sh @@ -18,6 +18,8 @@ get_default_board # 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 unmount $FLAGS_FALSE \ "Unmount previously mounted dir." u DEFINE_string from "/dev/sdc" \ @@ -45,9 +47,11 @@ function unmount_image() { # Don't die on error to force cleanup set +e # Reset symlinks in /usr/local. - setup_symlinks_on_root "/usr/local" "/var" \ - "${FLAGS_stateful_mountpt}" - fix_broken_symlinks "${FLAGS_rootfs_mountpt}" + if mount | grep "${FLAGS_rootfs_mountpt} (rw,bind)"; then + setup_symlinks_on_root "/usr/local" "/var" \ + "${FLAGS_stateful_mountpt}" + fix_broken_symlinks "${FLAGS_rootfs_mountpt}" + fi sudo umount "${FLAGS_rootfs_mountpt}/usr/local" sudo umount "${FLAGS_rootfs_mountpt}/var" if [[ -n "${FLAGS_esp_mountpt}" ]]; then @@ -59,10 +63,13 @@ function unmount_image() { } function get_usb_partitions() { - sudo mount "${FLAGS_from}3" "${FLAGS_rootfs_mountpt}" - sudo mount "${FLAGS_from}1" "${FLAGS_stateful_mountpt}" + local ro_flag="" + [ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ] && ro_flag="-o ro" + + sudo mount ${ro_flag} "${FLAGS_from}3" "${FLAGS_rootfs_mountpt}" + sudo mount ${ro_flag} "${FLAGS_from}1" "${FLAGS_stateful_mountpt}" if [[ -n "${FLAGS_esp_mountpt}" ]]; then - sudo mount "${FLAGS_from}12" "${FLAGS_esp_mountpt}" + sudo mount ${ro_flag} "${FLAGS_from}12" "${FLAGS_esp_mountpt}" fi } @@ -71,19 +78,22 @@ function get_gpt_partitions() { # Mount the rootfs partition using a loopback device. local offset=$(partoffset "${FLAGS_from}/${filename}" 3) - sudo mount -o loop,offset=$(( offset * 512 )) "${FLAGS_from}/${filename}" \ - "${FLAGS_rootfs_mountpt}" + local ro_flag="" + [ ${FLAGS_read_only} -eq ${FLAGS_TRUE} ] && ro_flag="-o ro" + + sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \ + "${FLAGS_from}/${filename}" "${FLAGS_rootfs_mountpt}" # Mount the stateful partition using a loopback device. offset=$(partoffset "${FLAGS_from}/${filename}" 1) - sudo mount -o loop,offset=$(( offset * 512 )) "${FLAGS_from}/${filename}" \ - "${FLAGS_stateful_mountpt}" + sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \ + "${FLAGS_from}/${filename}" "${FLAGS_stateful_mountpt}" # Mount the stateful partition using a loopback device. if [[ -n "${FLAGS_esp_mountpt}" ]]; then offset=$(partoffset "${FLAGS_from}/${filename}" 12) - sudo mount -o loop,offset=$(( offset * 512 )) "${FLAGS_from}/${filename}" \ - "${FLAGS_esp_mountpt}" + sudo mount ${ro_flag} -o loop,offset=$(( offset * 512 )) \ + "${FLAGS_from}/${filename}" "${FLAGS_esp_mountpt}" fi } @@ -108,8 +118,11 @@ function mount_image() { sudo mount --bind "${FLAGS_stateful_mountpt}/dev_image" \ "${FLAGS_rootfs_mountpt}/usr/local" # Setup symlinks in /usr/local so you can emerge packages into /usr/local. - setup_symlinks_on_root "${FLAGS_stateful_mountpt}/dev_image" \ - "${FLAGS_stateful_mountpt}/var" "${FLAGS_stateful_mountpt}" + + if [ ${FLAGS_read_only} -eq ${FLAGS_FALSE} ]; then + setup_symlinks_on_root "${FLAGS_stateful_mountpt}/dev_image" \ + "${FLAGS_stateful_mountpt}/var" "${FLAGS_stateful_mountpt}" + fi echo "Image specified by ${FLAGS_from} mounted at"\ "${FLAGS_rootfs_mountpt} successfully." }