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 v2 3/5] rootfs: Unmount rootfs mounts if not needed
Date: Wed, 21 Apr 2021 17:58:53 +0300	[thread overview]
Message-ID: <20210421145855.66257-4-amikan@ilbers.de> (raw)
In-Reply-To: <20210421145855.66257-1-amikan@ilbers.de>

Count the usage of rootfs mounts and trigger unmount only after all
corresponded tasks are finished.

Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
 meta/classes/image.bbclass                    | 15 ----
 meta/classes/initramfs.bbclass                |  2 +
 meta/classes/rootfs.bbclass                   | 68 +++++++++++++++++--
 .../buildchroot/buildchroot.inc               |  7 ++
 4 files changed, 70 insertions(+), 22 deletions(-)

diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index 9f9b3f8..12d1616 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -199,21 +199,6 @@ do_rootfs_finalize() {
             find "${ROOTFSDIR}/usr/bin" \
                 -maxdepth 1 -name 'qemu-*-static' -type f -delete
 
-        mountpoint -q '${ROOTFSDIR}/isar-apt' && \
-            umount -l ${ROOTFSDIR}/isar-apt
-        rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/isar-apt
-
-        mountpoint -q '${ROOTFSDIR}/base-apt' && \
-            umount -l ${ROOTFSDIR}/base-apt
-        rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/base-apt
-
-        mountpoint -q '${ROOTFSDIR}/dev' && \
-            umount -l ${ROOTFSDIR}/dev
-        mountpoint -q '${ROOTFSDIR}/sys' && \
-            umount -l ${ROOTFSDIR}/proc
-        mountpoint -q '${ROOTFSDIR}/sys' && \
-            umount -l ${ROOTFSDIR}/sys
-
         rm -f "${ROOTFSDIR}/etc/apt/apt.conf.d/55isar-fallback.conf"
 
         rm -f "${ROOTFSDIR}/etc/apt/sources.list.d/isar-apt.list"
diff --git a/meta/classes/initramfs.bbclass b/meta/classes/initramfs.bbclass
index 10a642b..e895afa 100644
--- a/meta/classes/initramfs.bbclass
+++ b/meta/classes/initramfs.bbclass
@@ -38,5 +38,7 @@ do_generate_initramfs() {
 
     rm -rf "${INITRAMFS_IMAGE_FILE}"
     cp "${INITRAMFS_ROOTFS}/initrd.img" "${INITRAMFS_IMAGE_FILE}"
+
+    rootfs_undo_mounts
 }
 addtask generate_initramfs after do_rootfs before do_build
diff --git a/meta/classes/rootfs.bbclass b/meta/classes/rootfs.bbclass
index f9151c5..7bb04e5 100644
--- a/meta/classes/rootfs.bbclass
+++ b/meta/classes/rootfs.bbclass
@@ -26,8 +26,41 @@ export LANG = "C"
 export LANGUAGE = "C"
 export LC_ALL = "C"
 
+ROOTFS_CNT_LOCKFILE = "${ROOTFSDIR}.mount_lock"
+
+rootfs_mount_cnt_inc() {
+    sudo -s <<'EOSUDO'
+        ( flock 9
+        set -e
+        count="1"
+        if [ -f '${ROOTFSDIR}/.mount' ]; then
+            count=$(($(< '${ROOTFSDIR}/.mount') + 1))
+        fi
+        echo $count | tee '${ROOTFSDIR}/.mount'
+        ) 9>'${ROOTFS_CNT_LOCKFILE}'
+EOSUDO
+}
+
+rootfs_mount_cnt_dec() {
+    sudo -s <<'EOSUDO'
+        ( flock 9
+        set -e
+        if [ -f '${ROOTFSDIR}/.mount' ]; then
+            count=$(($(< '${ROOTFSDIR}/.mount') - 1))
+            echo $count | tee '${ROOTFSDIR}/.mount'
+        else
+            exit 1
+        fi
+        ) 9>'${ROOTFS_CNT_LOCKFILE}'
+EOSUDO
+}
+
+
 rootfs_do_mounts[weight] = "3"
 rootfs_do_mounts() {
+    if [ $(rootfs_mount_cnt_inc) -gt 1 ]; then
+        return
+    fi
     sudo -s <<'EOSUDO'
         mountpoint -q '${ROOTFSDIR}/dev' || \
             mount --rbind /dev '${ROOTFSDIR}/dev'
@@ -59,6 +92,26 @@ rootfs_do_mounts() {
 EOSUDO
 }
 
+rootfs_undo_mounts() {
+    if [ $(rootfs_mount_cnt_dec) -gt 0 ]; then
+        return
+    fi
+    sudo -s <<'EOSUDO'
+        mountpoint -q '${ROOTFSDIR}/base-apt' && \
+            umount ${ROOTFSDIR}/base-apt && \
+            rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/base-apt
+        mountpoint -q '${ROOTFSDIR}/isar-apt' && \
+            umount ${ROOTFSDIR}/isar-apt && \
+            rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/isar-apt
+        mountpoint -q '${ROOTFSDIR}/sys' && \
+            umount -R ${ROOTFSDIR}/sys
+        mountpoint -q '${ROOTFSDIR}/proc' && \
+            umount ${ROOTFSDIR}/proc
+        mountpoint -q '${ROOTFSDIR}/dev' && \
+            umount -R ${ROOTFSDIR}/dev
+EOSUDO
+}
+
 rootfs_do_qemu() {
     if [ '${@repr(d.getVar('ROOTFS_ARCH') == d.getVar('HOST_ARCH'))}' = 'False' ]
     then
@@ -159,7 +212,7 @@ python do_rootfs_install() {
 
     # Mount after configure commands, so that they have time to copy
     # 'isar-apt' (sdkchroot):
-    cmds = ['rootfs_prepare'] + configure_cmds + ['rootfs_do_mounts'] + install_cmds
+    cmds = ['rootfs_prepare'] + configure_cmds + ['rootfs_do_mounts'] + install_cmds  + ['rootfs_undo_mounts']
 
     # NOTE: The weights specify how long each task takes in seconds and are used
     # by the MultiStageProgressReporter to render a progress bar for this task.
@@ -241,12 +294,13 @@ python do_rootfs_postprocess() {
     progress_reporter.update(0)
 
     cmds = d.getVar("ROOTFS_POSTPROCESS_COMMAND")
-    if cmds is None or not cmds.strip():
-        return
-    cmds = cmds.split()
-    for i, cmd in enumerate(cmds):
-        bb.build.exec_func(cmd, d)
-        progress_reporter.update(int(i / len(cmds) * 100))
+    if cmds is not None and cmds.strip():
+        cmds = cmds.split()
+        for i, cmd in enumerate(cmds):
+            bb.build.exec_func(cmd, d)
+            progress_reporter.update(int(i / len(cmds) * 100))
+
+    bb.build.exec_func('rootfs_undo_mounts', d)
 }
 addtask rootfs_postprocess before do_rootfs
 
diff --git a/meta/recipes-devtools/buildchroot/buildchroot.inc b/meta/recipes-devtools/buildchroot/buildchroot.inc
index 5a2befb..b80e703 100644
--- a/meta/recipes-devtools/buildchroot/buildchroot.inc
+++ b/meta/recipes-devtools/buildchroot/buildchroot.inc
@@ -49,6 +49,13 @@ rootfs_do_mounts_append() {
 EOSUDO
 }
 
+rootfs_undo_mounts_append() {
+    sudo -s <<'EOSUDO'
+    mountpoint -q '${BUILDCHROOT_DIR}/downloads' && \
+        umount ${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-04-21 14:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21 14:58 [PATCH v2 0/5] isar: Rebuild mount logic Anton Mikanovich
2021-04-21 14:58 ` [PATCH v2 1/5] dpkg: Make mount buildroot reliable Anton Mikanovich
2021-04-21 14:58 ` [PATCH v2 2/5] buildchroot: Unmount buildchroot if not needed Anton Mikanovich
2021-04-21 14:58 ` Anton Mikanovich [this message]
2021-08-12  5:39   ` [PATCH v2 3/5] rootfs: Unmount rootfs mounts " Jan Kiszka
2021-08-12  9:09     ` Anton Mikanovich
2021-08-12  9:25       ` Jan Kiszka
2021-08-13 16:27         ` Anton Mikanovich
2021-08-15 19:05           ` Jan Kiszka
2021-04-21 14:58 ` [PATCH v2 4/5] wic: Unmount dirs after usage Anton Mikanovich
2021-04-21 14:58 ` [PATCH v2 5/5] events: Warn if mounted paths left 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=20210421145855.66257-4-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