From: Anton Mikanovich <amikan@ilbers.de>
To: isar-users@googlegroups.com
Cc: Anton Mikanovich <amikan@ilbers.de>
Subject: [PATCH v2 2/5] buildchroot: Unmount buildchroot if not needed
Date: Wed, 21 Apr 2021 17:58:52 +0300 [thread overview]
Message-ID: <20210421145855.66257-3-amikan@ilbers.de> (raw)
In-Reply-To: <20210421145855.66257-1-amikan@ilbers.de>
Count the usage of BUILDCHROOT_DIR mount and trigger unmount of it after
all tasks using chroot are finished.
Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
meta/classes/buildchroot.bbclass | 58 ++++++++++++++++++++++
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/isar-events.bbclass | 1 +
meta/classes/ubi-img.bbclass | 2 +
meta/classes/ubifs-img.bbclass | 2 +
meta/classes/wic-img.bbclass | 1 +
10 files changed, 80 insertions(+)
diff --git a/meta/classes/buildchroot.bbclass b/meta/classes/buildchroot.bbclass
index 1a8ee13..1d50025 100644
--- a/meta/classes/buildchroot.bbclass
+++ b/meta/classes/buildchroot.bbclass
@@ -21,12 +21,45 @@ python __anonymous() {
d.setVar('BUILDCHROOT_DIR', rootfs)
}
+BUILDCHROOT_CNT_LOCKFILE = "${BUILDCHROOT_DIR}.mount_lock"
+
+buildchroot_mount_cnt_inc() {
+ sudo -s <<'EOSUDO'
+ ( flock 9
+ set -e
+ count="1"
+ if [ -f '${BUILDCHROOT_DIR}/.mount' ]; then
+ count=$(($(< '${BUILDCHROOT_DIR}/.mount') + 1))
+ fi
+ echo $count | tee '${BUILDCHROOT_DIR}/.mount'
+ ) 9>'${BUILDCHROOT_CNT_LOCKFILE}'
+EOSUDO
+}
+
+buildchroot_mount_cnt_dec() {
+ sudo -s <<'EOSUDO'
+ ( flock 9
+ set -e
+ if [ -f '${BUILDCHROOT_DIR}/.mount' ]; then
+ count=$(($(< '${BUILDCHROOT_DIR}/.mount') - 1))
+ echo $count | tee '${BUILDCHROOT_DIR}/.mount'
+ else
+ exit 1
+ fi
+ ) 9>'${BUILDCHROOT_CNT_LOCKFILE}'
+EOSUDO
+}
+
MOUNT_LOCKFILE = "${BUILDCHROOT_DIR}.lock"
buildchroot_do_mounts() {
+ if [ $(buildchroot_mount_cnt_inc) -gt 1 ]; then
+ return
+ fi
sudo -s <<'EOSUDO'
( flock 9
set -e
+ mkdir -p '${BUILDCHROOT_DIR}/isar-apt'
mountpoint -q '${BUILDCHROOT_DIR}/isar-apt' ||
mount --bind '${REPO_ISAR_DIR}/${DISTRO}' '${BUILDCHROOT_DIR}/isar-apt'
mountpoint -q '${BUILDCHROOT_DIR}/downloads' ||
@@ -59,3 +92,28 @@ buildchroot_do_mounts() {
) 9>'${MOUNT_LOCKFILE}'
EOSUDO
}
+
+buildchroot_undo_mounts() {
+ if [ $(buildchroot_mount_cnt_dec) -gt 0 ]; then
+ return
+ fi
+ sudo -s <<'EOSUDO'
+ ( flock 9
+ set -e
+ 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 ${BUILDCHROOT_DIR}/proc
+ mountpoint -q '${BUILDCHROOT_DIR}/dev' && \
+ umount -R ${BUILDCHROOT_DIR}/dev
+ mountpoint -q '${BUILDCHROOT_DIR}/downloads' && \
+ umount ${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/isar-events.bbclass b/meta/classes/isar-events.bbclass
index 4c49635..1deedb9 100644
--- a/meta/classes/isar-events.bbclass
+++ b/meta/classes/isar-events.bbclass
@@ -8,6 +8,7 @@ addhandler build_started
python build_started() {
bb.utils.remove(d.getVar('TMPDIR') + "/work/*/*/*/temp/once.*")
+ os.system('sudo rm %s/work/*/*/*/rootfs/.mount' % d.getVar('TMPDIR'))
}
build_started[eventmask] = "bb.event.BuildStarted"
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 76606f8..01baf89 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
}
--
2.25.1
next prev 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 ` Anton Mikanovich [this message]
2021-04-21 14:58 ` [PATCH v2 3/5] rootfs: Unmount rootfs mounts if not needed Anton Mikanovich
2021-08-12 5:39 ` 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-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