public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Anton Mikanovich <amikan@ilbers.de>
To: isar-users@googlegroups.com
Cc: Anton Mikanovich <amikan@ilbers.de>
Subject: [PATCH v3 2/5] buildchroot: Unmount buildchroot mounts if not needed
Date: Sat,  8 May 2021 09:25:15 +0300	[thread overview]
Message-ID: <20210508062518.83852-3-amikan@ilbers.de> (raw)
In-Reply-To: <20210508062518.83852-1-amikan@ilbers.de>

Count the usage of BUILDCHROOT_DIR mounts and trigger unmount of them
after all tasks using chroot are finished.

Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
 meta/classes/buildchroot.bbclass              | 47 +++++++++++++++++++
 meta/classes/dpkg-base.bbclass                |  1 +
 meta/classes/ext4-img.bbclass                 |  2 +
 meta/classes/fit-img.bbclass                  |  2 +
 meta/classes/image-tools-extension.bbclass    |  2 +
 meta/classes/image.bbclass                    |  9 ++++
 meta/classes/ubi-img.bbclass                  |  2 +
 meta/classes/ubifs-img.bbclass                |  2 +
 meta/classes/wic-img.bbclass                  |  1 +
 .../buildchroot/buildchroot.inc               |  8 ++++
 10 files changed, 76 insertions(+)

diff --git a/meta/classes/buildchroot.bbclass b/meta/classes/buildchroot.bbclass
index 1a8ee13..97d5301 100644
--- a/meta/classes/buildchroot.bbclass
+++ b/meta/classes/buildchroot.bbclass
@@ -27,8 +27,20 @@ buildchroot_do_mounts() {
     sudo -s <<'EOSUDO'
         ( flock 9
         set -e
+
+        count="1"
+        if [ -f '${BUILDCHROOT_DIR}.mount' ]; then
+            count=$(($(< '${BUILDCHROOT_DIR}.mount') + 1))
+        fi
+        echo $count > '${BUILDCHROOT_DIR}.mount'
+        if [ $count -gt 1 ]; then
+            exit 0
+        fi
+
+        mkdir -p '${BUILDCHROOT_DIR}/isar-apt'
         mountpoint -q '${BUILDCHROOT_DIR}/isar-apt' ||
             mount --bind '${REPO_ISAR_DIR}/${DISTRO}' '${BUILDCHROOT_DIR}/isar-apt'
+        mkdir -p '${BUILDCHROOT_DIR}/downloads'
         mountpoint -q '${BUILDCHROOT_DIR}/downloads' ||
             mount --bind '${DL_DIR}' '${BUILDCHROOT_DIR}/downloads'
         mountpoint -q '${BUILDCHROOT_DIR}/dev' ||
@@ -59,3 +71,38 @@ buildchroot_do_mounts() {
         ) 9>'${MOUNT_LOCKFILE}'
 EOSUDO
 }
+
+buildchroot_undo_mounts() {
+    sudo -s <<'EOSUDO'
+        ( flock 9
+        set -e
+
+        if [ -f '${BUILDCHROOT_DIR}.mount' ]; then
+            count=$(($(< '${BUILDCHROOT_DIR}.mount') - 1))
+            echo $count > '${BUILDCHROOT_DIR}.mount'
+        else
+            exit 1
+        fi
+        if [ $count -gt 0 ]; then
+            exit 0
+        fi
+        rm ${BUILDCHROOT_DIR}.mount
+
+        mountpoint -q '${BUILDCHROOT_DIR}/base-apt' && \
+            umount ${BUILDCHROOT_DIR}/base-apt && \
+            rmdir --ignore-fail-on-non-empty ${BUILDCHROOT_DIR}/base-apt
+        mountpoint -q '${BUILDCHROOT_DIR}/sys' && \
+            umount -R ${BUILDCHROOT_DIR}/sys
+        mountpoint -q '${BUILDCHROOT_DIR}/proc' && \
+            umount -R ${BUILDCHROOT_DIR}/proc
+        mountpoint -q '${BUILDCHROOT_DIR}/dev' && \
+            umount -R ${BUILDCHROOT_DIR}/dev
+        mountpoint -q '${BUILDCHROOT_DIR}/downloads' && \
+            umount ${BUILDCHROOT_DIR}/downloads && \
+            rmdir --ignore-fail-on-non-empty ${BUILDCHROOT_DIR}/downloads
+        mountpoint -q '${BUILDCHROOT_DIR}/isar-apt' && \
+            umount ${BUILDCHROOT_DIR}/isar-apt && \
+            rmdir --ignore-fail-on-non-empty ${BUILDCHROOT_DIR}/isar-apt
+        ) 9>'${MOUNT_LOCKFILE}'
+EOSUDO
+}
diff --git a/meta/classes/dpkg-base.bbclass b/meta/classes/dpkg-base.bbclass
index d5e70f1..ed162b3 100644
--- a/meta/classes/dpkg-base.bbclass
+++ b/meta/classes/dpkg-base.bbclass
@@ -181,6 +181,7 @@ def dpkg_do_mounts(d):
     bb.build.exec_func("buildchroot_do_mounts", d)
 
 def dpkg_undo_mounts(d):
+    bb.build.exec_func("buildchroot_undo_mounts", d)
     buildroot = d.getVar('BUILDROOT', True)
     if not ismount(buildroot):
         bb.warn('Path %s not mounted!' % buildroot)
diff --git a/meta/classes/ext4-img.bbclass b/meta/classes/ext4-img.bbclass
index 334dc64..26e0137 100644
--- a/meta/classes/ext4-img.bbclass
+++ b/meta/classes/ext4-img.bbclass
@@ -17,6 +17,8 @@ do_ext4_image() {
 
     sudo chroot ${BUILDCHROOT_DIR} /sbin/mke2fs ${MKE2FS_ARGS} \
                 -F -d '${PP_ROOTFS}' '${PP_DEPLOY}/${EXT4_IMAGE_FILE}'
+
+    image_undo_mounts
 }
 
 addtask ext4_image before do_image after do_image_tools
diff --git a/meta/classes/fit-img.bbclass b/meta/classes/fit-img.bbclass
index 221ac3f..da8e1da 100644
--- a/meta/classes/fit-img.bbclass
+++ b/meta/classes/fit-img.bbclass
@@ -23,5 +23,7 @@ do_fit_image() {
     # Create fit image using buildchroot tools
     sudo chroot ${BUILDCHROOT_DIR} /usr/bin/mkimage ${MKIMAGE_ARGS} \
                 -f '${PP_WORK}/${FIT_IMAGE_SOURCE}' '${PP_DEPLOY}/${FIT_IMAGE_FILE}'
+
+    image_undo_mounts
 }
 addtask fit_image before do_image after do_image_tools do_transform_template
diff --git a/meta/classes/image-tools-extension.bbclass b/meta/classes/image-tools-extension.bbclass
index 0b067ff..4738479 100644
--- a/meta/classes/image-tools-extension.bbclass
+++ b/meta/classes/image-tools-extension.bbclass
@@ -40,5 +40,7 @@ do_install_imager_deps() {
         apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
             --allow-unauthenticated --allow-downgrades install \
             ${IMAGER_INSTALL}'
+
+    buildchroot_undo_mounts
 }
 addtask install_imager_deps before do_image_tools
diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index ec93cab..9f9b3f8 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -62,6 +62,15 @@ image_do_mounts() {
     buildchroot_do_mounts
 }
 
+image_undo_mounts() {
+    buildchroot_undo_mounts
+    sudo flock ${MOUNT_LOCKFILE} -c ' \
+        umount "${BUILDROOT_DEPLOY}"
+        umount "${BUILDROOT_ROOTFS}"
+        umount "${BUILDROOT_WORK}"
+    '
+}
+
 ROOTFSDIR = "${IMAGE_ROOTFS}"
 ROOTFS_FEATURES += "clean-package-cache generate-manifest export-dpkg-status"
 ROOTFS_PACKAGES += "${IMAGE_PREINSTALL} ${IMAGE_INSTALL}"
diff --git a/meta/classes/ubi-img.bbclass b/meta/classes/ubi-img.bbclass
index 2178b50..0f4c4cb 100644
--- a/meta/classes/ubi-img.bbclass
+++ b/meta/classes/ubi-img.bbclass
@@ -26,5 +26,7 @@ do_ubi_image() {
     # Create ubi image using buildchroot tools
     sudo chroot ${BUILDCHROOT_DIR} /usr/sbin/ubinize ${UBINIZE_ARGS} \
                 -o '${PP_DEPLOY}/${UBI_IMAGE_FILE}' '${PP_WORK}/${UBINIZE_CFG}'
+
+    image_undo_mounts
 }
 addtask ubi_image before do_image after do_image_tools do_transform_template
diff --git a/meta/classes/ubifs-img.bbclass b/meta/classes/ubifs-img.bbclass
index 78926f6..e422b46 100644
--- a/meta/classes/ubifs-img.bbclass
+++ b/meta/classes/ubifs-img.bbclass
@@ -21,6 +21,8 @@ do_ubifs_image() {
     # Create ubifs image using buildchroot tools
     sudo chroot ${BUILDCHROOT_DIR} /usr/sbin/mkfs.ubifs ${MKUBIFS_ARGS} \
                 -r '${PP_ROOTFS}' '${PP_DEPLOY}/${UBIFS_IMAGE_FILE}'
+
+    image_undo_mounts
 }
 
 addtask ubifs_image before do_image after do_image_tools
diff --git a/meta/classes/wic-img.bbclass b/meta/classes/wic-img.bbclass
index 042a546..bb3a3ee 100644
--- a/meta/classes/wic-img.bbclass
+++ b/meta/classes/wic-img.bbclass
@@ -200,4 +200,5 @@ EOSUDO
     done
     rm -rf ${BUILDCHROOT_DIR}/${WICTMP}
     rm -rf ${IMAGE_ROOTFS}/../pseudo
+    buildchroot_undo_mounts
 }
diff --git a/meta/recipes-devtools/buildchroot/buildchroot.inc b/meta/recipes-devtools/buildchroot/buildchroot.inc
index 5a2befb..0c1fb7f 100644
--- a/meta/recipes-devtools/buildchroot/buildchroot.inc
+++ b/meta/recipes-devtools/buildchroot/buildchroot.inc
@@ -49,6 +49,14 @@ rootfs_do_mounts_append() {
 EOSUDO
 }
 
+rootfs_undo_mounts_append() {
+    sudo -s <<'EOSUDO'
+    mountpoint -q '${BUILDCHROOT_DIR}/downloads' && \
+        umount ${BUILDCHROOT_DIR}/downloads && \
+        rmdir --ignore-fail-on-non-empty ${BUILDCHROOT_DIR}/downloads
+EOSUDO
+}
+
 ROOTFS_POSTPROCESS_COMMAND =+ "buildchroot_install_files"
 buildchroot_install_files() {
     sudo mkdir -p "${BUILDCHROOT_DIR}/home/builder"
-- 
2.25.1


  parent reply	other threads:[~2021-05-08  6:25 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-08  6:25 [PATCH v3 0/5] Rebuild mount logic Anton Mikanovich
2021-05-08  6:25 ` [PATCH v3 1/5] dpkg: Make mount buildroot reliable Anton Mikanovich
2021-06-08  8:09   ` Henning Schild
2021-06-08  8:31     ` Anton Mikanovich
2021-06-08  8:49       ` Henning Schild
2021-07-01 11:55   ` Jan Kiszka
2021-05-08  6:25 ` Anton Mikanovich [this message]
2021-05-08  6:25 ` [PATCH v3 3/5] rootfs: Unmount rootfs mounts if not needed Anton Mikanovich
2021-05-08  6:25 ` [PATCH v3 4/5] wic: Unmount dirs after usage Anton Mikanovich
2021-05-08  6:25 ` [PATCH v3 5/5] events: Warn if mounted paths left Anton Mikanovich
2021-07-01 11:51   ` Jan Kiszka
2021-06-08  7:58 ` [PATCH v3 0/5] Rebuild mount logic Henning Schild
2021-06-08  8:15   ` Anton Mikanovich
2021-06-08  8:42     ` Henning Schild
2021-06-08  8:54     ` Henning Schild
2021-06-10  7:12       ` Anton Mikanovich
2021-06-10 15:02         ` Henning Schild
2021-06-21 15:54 ` Jan Kiszka
2021-06-22 14:40   ` Anton Mikanovich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210508062518.83852-3-amikan@ilbers.de \
    --to=amikan@ilbers.de \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox