From a81df76a064c23aeb297a3c56e7139fa30d10ca9 Mon Sep 17 00:00:00 2001 From: Bill Richardson Date: Fri, 9 Apr 2010 08:12:05 -0700 Subject: [PATCH] Replace "mount -o loop" with explict losetup use in build_image. This fixes the loop device leak. The problem is that inside the chroot, we have /etc/mtab as a symlink to /proc/mounts. That works most of the time, but if you mount something using "-o loop", it isn't cleaned up correctly when you umount it. To avoid that, we either have to replace the /etc/mtab symlink with an empty file when we create the chroot, or we have to make sure that we never execute a "mount -o loop" command from within the chroot. If we use an empty /etc/mtab file, the builds work fine, but then we can't see any mounted partitions that the host creates outside the chroot (such as USB keys), which causes other problems. Bleah. Review URL: http://codereview.chromium.org/1594020 --- build_image | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/build_image b/build_image index e06071afa7..11548f74c3 100755 --- a/build_image +++ b/build_image @@ -73,6 +73,7 @@ BOARD_ROOT="${FLAGS_build_root}/${BOARD}" LOOP_DEV= STATEFUL_LOOP_DEV= +ESP_LOOP_DEV= # What cross-build are we targeting? . "${BOARD_ROOT}/etc/make.conf.board_setup" @@ -129,6 +130,8 @@ cleanup_stateful_fs_loop() { cleanup_esp_loop() { sudo umount "$ESP_DIR" + sleep 1 + sudo losetup -d "$ESP_LOOP_DEV" } cleanup() { @@ -143,7 +146,7 @@ cleanup() { cleanup_rootfs_loop fi - if [[ -n "$ESP_DIR" ]]; then + if [[ -n "$ESP_LOOP_DEV" ]]; then cleanup_esp_loop fi @@ -358,19 +361,30 @@ ESP_IMG=${OUTPUT_DIR}/esp.image ESP_BLOCKS=16384 /usr/sbin/mkfs.vfat -C ${OUTPUT_DIR}/esp.image ${ESP_BLOCKS} ESP_DIR=${OUTPUT_DIR}/esp -mkdir -p ${ESP_DIR} -sudo mount -o loop ${ESP_IMG} ${ESP_DIR} -sudo mkdir -p ${ESP_DIR}/efi/boot -sudo grub-mkimage -p /efi/boot -o ${ESP_DIR}/efi/boot/bootx64.efi \ +ESP_LOOP_DEV=$(sudo losetup -f) +if [ -z "$ESP_LOOP_DEV" ] ; then + echo "No free loop device. Free up a loop device or reboot. exiting. " + exit 1 +fi +mkdir -p "${ESP_DIR}" +sudo losetup "${ESP_LOOP_DEV}" "${ESP_IMG}" +sudo mount "${ESP_LOOP_DEV}" "${ESP_DIR}" +sudo mkdir -p "${ESP_DIR}/efi/boot" +sudo grub-mkimage -p /efi/boot -o "${ESP_DIR}/efi/boot/bootx64.efi" \ part_gpt fat ext2 normal boot sh chain configfile linux -sudo cp ${ROOT_FS_DIR}/boot/vmlinuz ${ESP_DIR}/efi/boot/vmlinuz -cat <