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 3/5] rootfs: Unmount rootfs mounts if not needed
Date: Sat,  8 May 2021 09:25:16 +0300	[thread overview]
Message-ID: <20210508062518.83852-4-amikan@ilbers.de> (raw)
In-Reply-To: <20210508062518.83852-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    | 62 ++++++++++++++++++++++++++++++----
 3 files changed, 57 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..0458c7c 100644
--- a/meta/classes/rootfs.bbclass
+++ b/meta/classes/rootfs.bbclass
@@ -26,9 +26,23 @@ export LANG = "C"
 export LANGUAGE = "C"
 export LC_ALL = "C"
 
+MOUNT_LOCKFILE = "${ROOTFSDIR}.lock"
+
 rootfs_do_mounts[weight] = "3"
 rootfs_do_mounts() {
     sudo -s <<'EOSUDO'
+        ( flock 9
+        set -e
+
+        count="1"
+        if [ -f '${ROOTFSDIR}.mount' ]; then
+            count=$(($(< '${ROOTFSDIR}.mount') + 1))
+        fi
+        echo $count > '${ROOTFSDIR}.mount'
+        if [ $count -gt 1 ]; then
+            exit 0
+        fi
+
         mountpoint -q '${ROOTFSDIR}/dev' || \
             mount --rbind /dev '${ROOTFSDIR}/dev'
         mount --make-rslave '${ROOTFSDIR}/dev'
@@ -56,6 +70,39 @@ rootfs_do_mounts() {
                 mount --bind '${REPO_BASE_DIR}' '${ROOTFSDIR}/base-apt'
         fi
 
+        ) 9>'${MOUNT_LOCKFILE}'
+EOSUDO
+}
+
+rootfs_undo_mounts() {
+    sudo -s <<'EOSUDO'
+        ( flock 9
+        set -e
+
+        if [ -f '${ROOTFSDIR}.mount' ]; then
+            count=$(($(< '${ROOTFSDIR}.mount') - 1))
+            echo $count > '${ROOTFSDIR}.mount'
+        else
+            exit 1
+        fi
+        if [ $count -gt 0 ]; then
+            exit 0
+        fi
+        rm ${ROOTFSDIR}.mount
+
+        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 -R ${ROOTFSDIR}/proc
+        mountpoint -q '${ROOTFSDIR}/dev' && \
+            umount -R ${ROOTFSDIR}/dev
+        ) 9>'${MOUNT_LOCKFILE}'
 EOSUDO
 }
 
@@ -159,7 +206,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 +288,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
 
-- 
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 ` [PATCH v3 2/5] buildchroot: Unmount buildchroot mounts if not needed Anton Mikanovich
2021-05-08  6:25 ` Anton Mikanovich [this message]
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-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