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 28497138359 for ; Thu, 29 Oct 2020 16:16:43 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 77085E097C; Thu, 29 Oct 2020 16:16:42 +0000 (UTC) Received: from mail-qt1-f196.google.com (mail-qt1-f196.google.com [209.85.160.196]) (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 7123FE097C for ; Thu, 29 Oct 2020 16:16:42 +0000 (UTC) Received: by mail-qt1-f196.google.com with SMTP id h19so2177908qtq.4 for ; Thu, 29 Oct 2020 09:16:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P0X57R1aHfKThv6Ijs0eHRUN5QVioBLMWZ5pkjmHDIk=; b=g3Rc04FSf673ei0vDQ+U+sqNdeG3K790UeZFLCdfl8aqi5gpiHk3nnoDM98d9/zP5w gCw2LM80Vtm/RlgIuK4+gEyFTAMUGLB0joluqKEdI32O4ttb4qwS9rpqe49ZfcsyfFhg 1BDfreEYav2AsfCk/nXEQt4OQKjaAovwr8mUSMvIC74m8i5Iv+aN48a4brZigoHkAmHy v0amWgHfJjO/1TVI4lgejweIa358SeKueBFGqbD0POMBNhgRpK4M3PvbR1ZOu4hixFXZ DqZyxh1I5dag6vnYICSBypyao8rVObmwzM5bhq4N+qVn67JmNXElK4GUjkHx61WIO0/5 5obw== X-Gm-Message-State: AOAM533L1yLt6MwnLREgdGFEZk6u5STjDBmDZdBdVz3meaVQjy5cOnpY 7EH4BMoayZxLvoUioJbtYlVLinv7hpY= X-Google-Smtp-Source: ABdhPJygjduLVe8vALfAMQzdx4msGDW5o890cGk2XUEvvSAnd0gKL+rV6918P3pNaTM4akYQJzePYA== X-Received: by 2002:ac8:4e1c:: with SMTP id c28mr4216098qtw.98.1603988201172; Thu, 29 Oct 2020 09:16:41 -0700 (PDT) Received: from localhost (2606-a000-131c-10bb-0000-0000-0000-1fc3.inf6.spectrum.com. [2606:a000:131c:10bb::1fc3]) by smtp.gmail.com with ESMTPSA id d14sm1273021qtr.62.2020.10.29.09.16.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 09:16:40 -0700 (PDT) From: Matt Turner To: gentoo-catalyst@lists.gentoo.org Cc: Matt Turner Subject: [gentoo-catalyst] [PATCH 03/12] catalyst: Use libmount for handling mounts Date: Thu, 29 Oct 2020 12:16:23 -0400 Message-Id: <20201029161632.146732-3-mattst88@gentoo.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201029161632.146732-1-mattst88@gentoo.org> References: <20201029161632.146732-1-mattst88@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-catalyst@lists.gentoo.org Reply-to: gentoo-catalyst@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archives-Salt: a867b7ed-8cb2-460f-b67c-df1d13ea4ca4 X-Archives-Hash: 10805d96449f706f371e0a56697794e9 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 --- 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