From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 990BD1381F3 for ; Thu, 22 Oct 2020 18:59:36 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id BC99AE084A; Thu, 22 Oct 2020 18:59:35 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 90967E084A for ; Thu, 22 Oct 2020 18:59:35 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 17ED1340E39 for ; Thu, 22 Oct 2020 18:59:34 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 81DB73AB for ; Thu, 22 Oct 2020 18:59:32 +0000 (UTC) From: "Matt Turner" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Matt Turner" Message-ID: <1603393063.45a0e0b08affca6615ed16de29bf9a8454be414f.mattst88@gentoo> Subject: [gentoo-commits] proj/catalyst:pending/mattst88 commit in: catalyst/base/ X-VCS-Repository: proj/catalyst X-VCS-Files: catalyst/base/stagebase.py X-VCS-Directories: catalyst/base/ X-VCS-Committer: mattst88 X-VCS-Committer-Name: Matt Turner X-VCS-Revision: 45a0e0b08affca6615ed16de29bf9a8454be414f X-VCS-Branch: pending/mattst88 Date: Thu, 22 Oct 2020 18:59:32 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 6bcdfeda-e6bd-43f3-baae-3f6c23e1bdb2 X-Archives-Hash: de222e39f69010ffd1f01928a3a42cfc commit: 45a0e0b08affca6615ed16de29bf9a8454be414f Author: Matt Turner gentoo org> AuthorDate: Sat May 16 21:44:37 2020 +0000 Commit: Matt Turner gentoo org> CommitDate: Thu Oct 22 18:57:43 2020 +0000 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=45a0e0b0 catalyst: Use libmount for handling mounts Handle the mounts/unmounts in all in process rather than shelling out (pun intended!) to an external program. While we're here, change some log.notice to log.debug since those cases are normal and expected. Signed-off-by: Matt Turner gentoo.org> catalyst/base/stagebase.py | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py index 06919fb5..95cb2912 100644 --- a/catalyst/base/stagebase.py +++ b/catalyst/base/stagebase.py @@ -6,6 +6,7 @@ import sys from pathlib import Path +import libmount import toml from snakeoil import fileutils @@ -842,7 +843,8 @@ class StageBase(TargetBase, ClearBase, GenBase): source = str(self.mount[x]['source']) target = self.settings['chroot_path'] + str(self.mount[x]['target']) - mount = ['mount'] + fstype = '' + options = '' log.debug('bind %s: "%s" -> "%s"', x, source, target) @@ -850,18 +852,20 @@ class StageBase(TargetBase, ClearBase, GenBase): if 'var_tmpfs_portage' not in self.settings: continue - mount += ['-t', 'tmpfs', '-o', - f"size={self.settings['var_tmpfs_portage']}G"] + fstype = 'tmpfs' + options = f"size={self.settings['var_tmpfs_portage']}G" elif source == 'tmpfs': - mount += ['-t', 'tmpfs'] + fstype = 'tmpfs' elif source == 'shm': - mount += ['-t', 'tmpfs', '-o', 'noexec,nosuid,nodev'] + fstype = 'tmpfs' + options = "noexec,nosuid,nodev" else: source_path = Path(self.mount[x]['source']) if source_path.suffix == '.sqfs': - mount += ['-o', 'ro'] + fstype = "squashfs" + options = "ro,loop" else: - mount.append('--bind') + options = "bind" # We may need to create the source of the bind mount. E.g., in the # case of an empty package cache we must create the directory that @@ -870,25 +874,33 @@ class StageBase(TargetBase, ClearBase, GenBase): Path(target).mkdir(mode=0o755, parents=True, exist_ok=True) - cmd(mount + [source, target], env=self.env, fail_func=self.unbind) + try: + cxt = libmount.Context(source=source, target=target, + fstype=fstype, options=options) + cxt.mount() + except OSError as e: + self.unbind() + raise CatalystError(f"Counldn't mount: {source}, {e.strerror}") def unbind(self): - mypath = self.settings["chroot_path"] + chroot_path = self.settings["chroot_path"] umount_failed = False # Unmount in reverse order - for x in [x for x in reversed(self.mount) if self.mount[x]['enable']]: - target = normpath(mypath + self.mount[x]['target']) - if not os.path.exists(target): - log.notice('%s does not exist. Skipping', target) + for target in [Path(chroot_path + self.mount[x]['target']) + for x in reversed(self.mount) + if self.mount[x]['enable']]: + if not target.exists(): + log.debug('%s does not exist. Skipping', target) continue if not ismount(target): - log.notice('%s is not a mount point. Skipping', target) + log.debug('%s is not a mount point. Skipping', target) continue try: - cmd(['umount', target], env=self.env) + cxt = libmount.Context(target=str(target)) + cxt.umount() except OSError as e: log.warning("Couldn't umount: %s, %s", target, e.strerror)