From: Matt Turner <mattst88@gentoo.org>
To: gentoo-catalyst@lists.gentoo.org
Cc: Matt Turner <mattst88@gentoo.org>
Subject: [gentoo-catalyst] [PATCH 03/12] catalyst: Use libmount for handling mounts
Date: Thu, 29 Oct 2020 12:16:23 -0400 [thread overview]
Message-ID: <20201029161632.146732-3-mattst88@gentoo.org> (raw)
In-Reply-To: <20201029161632.146732-1-mattst88@gentoo.org>
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 <mattst88@gentoo.org>
---
catalyst/base/stagebase.py | 57 +++++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 22 deletions(-)
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index e71ce344..73eacfbe 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
@@ -853,7 +854,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)
@@ -861,18 +863,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
@@ -881,38 +885,47 @@ 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"Couldn't mount: {source}, {e.strerror}")
def unbind(self):
- ouch = 0
- 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)
- except CatalystError:
+ cxt = libmount.Context(target=str(target))
+ cxt.umount()
+ except OSError:
log.warning('First attempt to unmount failed: %s', target)
log.warning('Killing any pids still running in the chroot')
self.kill_chroot_pids()
try:
- cmd(['umount', target], env=self.env)
- except CatalystError:
- ouch = 1
- log.warning("Couldn't umount bind mount: %s", target)
+ cxt.umount()
+ except OSError as e:
+ umount_failed = True
+ log.warning("Couldn't umount: %s, %s", target,
+ e.strerror)
- if ouch:
+ if umount_failed:
# if any bind mounts really failed, then we need to raise
# this to potentially prevent an upcoming bash stage cleanup script
# from wiping our bind mounts.
--
2.26.2
next prev parent reply other threads:[~2020-10-29 16:16 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-29 16:16 [gentoo-catalyst] [PATCH 01/12] catalyst: Replace pathcompare() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 02/12] catalyst: Rewrite ismount() to use libmount Matt Turner
2020-10-29 16:16 ` Matt Turner [this message]
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 04/12] catalyst: Move action_sequence out of self.settings[] Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 05/12] catalyst: Use .extend() and .append() for action_sequence Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 06/12] catalyst: Split action_sequence into prepare/build/finish Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 07/12] catalyst: Factor out run_sequence() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 08/12] catalyst: Add and use namespace context manager Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 09/12] catalyst: Run the build sequence in new mount namespace Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 10/12] catalyst: Remove kill_support_pids() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 11/12] catalyst: Remove mount_safety_check() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 12/12] catalyst: Drop unbind() Matt Turner
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=20201029161632.146732-3-mattst88@gentoo.org \
--to=mattst88@gentoo.org \
--cc=gentoo-catalyst@lists.gentoo.org \
/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