Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap the dnf transaction in ProcMount to keep systemd happy #1333

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/pylorax/imgutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,20 +473,20 @@ def __exit__(self, exc_type, exc_value, tracebk):
execWithRedirect("kpartx", ["-d", "-s", self.disk_img])


class DracutChroot(object):
"""Setup the chroot for running dracut inside it, cleanup when done
class ProcMount(object):
"""Setup /proc, /dev, and optional bind mounts. Cleanup when done.

This mount /proc, /dev, and /var/tmp plus optional bind mounted directories
This mount /proc, /dev, and optional bind mounted directories
as a list of (source, destination) tuples where destination is relative to the chroot.
"""
def __init__(self, root, bind=None):
self.root = root
self.bind = [("/var/tmp", "/var/tmp")] + (bind if bind else [])
self.bind = bind if bind else []

def __enter__(self):
for d in [d for _, d in self.bind] + ["/proc", "/dev", "/etc/dracut.conf.d"]:
for d in [d for _, d in self.bind] + ["/proc", "/dev"]:
if not os.path.exists(self.root + d):
logger.warning("Making missing dracut chroot directory: %s", d)
logger.warning("Making missing mount directory: %s", d)
os.makedirs(self.root + d)

runcmd(["mount", "-t", "proc", "-o", "nosuid,noexec,nodev", "proc", self.root + "/proc" ])
Expand All @@ -507,6 +507,13 @@ def __exit__(self, exc_type, exc_value, tracebk):
# some mounts in /var/tmp/lorax can be busy at the moment of unmounting
umount(self.root + d, maxretry=10, retrysleep=5, delete=False)

class DracutChroot(ProcMount):
def __init__(self, root, bind=None):
super(DracutChroot, self).__init__(root, [("/var/tmp", "/var/tmp")] + (bind if bind else []))

if not os.path.exists(self.root + "/etc/dracut.conf.d/"):
os.makedirs(self.root + "/etc/dracut.conf.d/")

def _copy_conf(self, args):
"""Check the arguments for --conf, copy the file into the chroot under
/etc/dracut.conf.d/, and rewrite the file path to point to the new location.
Expand Down
15 changes: 8 additions & 7 deletions src/pylorax/ltmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pylorax.dnfhelper import LoraxDownloadCallback, LoraxRpmCallback
from pylorax.base import DataHolder
from pylorax.executils import runcmd, runcmd_output
from pylorax.imgutils import mkcpio
from pylorax.imgutils import mkcpio, ProcMount

from mako.lookup import TemplateLookup
from mako.exceptions import text_error_template
Expand Down Expand Up @@ -719,12 +719,13 @@ def run_pkg_transaction(self):
raise

logger.info("Preparing transaction from installation source")
try:
display = LoraxRpmCallback()
self.dbo.do_transaction(display=display)
except BaseException as e:
logger.error("The transaction process has ended abruptly: %s", e)
raise
with ProcMount(self.outroot):
try:
display = LoraxRpmCallback()
self.dbo.do_transaction(display=display)
except BaseException as e:
logger.error("The transaction process has ended abruptly: %s", e)
raise

# Reset the package sack to pick up the installed packages
self.dbo.reset(repos=False)
Expand Down
2 changes: 1 addition & 1 deletion tests/image-minimizer/test_minimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class MinimizerTestCase(unittest.TestCase):
def test_minimizer_ok(self):
with tempfile.TemporaryDirectory(prefix="minimize.test.") as rootdir:
check_call(["dnf", "--releasever=/", "--use-host-config", "--installroot", rootdir, "install", "-y", \
check_call(["dnf", "--use-host-config", "--installroot", rootdir, "install", "-y", \
"filesystem", "tzdata"])

im = ImageMinimizer("./tests/image-minimizer/im-script.txt", rootdir, False, False)
Expand Down
3 changes: 3 additions & 0 deletions tests/pylint/runpylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self):
FalsePositive(r"raise-missing-from"),
FalsePositive(r"redundant-u-string-prefix"),
FalsePositive(r"unspecified-encoding"),
# Python 3.12 problems with pylint/astroid
# see https://github.com/pylint-dev/pylint/issues/8852
FalsePositive(r"Class 'datetime' has no '(now|utcfromtimestamp)' member"),
]

@property
Expand Down
1 change: 1 addition & 0 deletions tests/pylorax/templates/install-cmd.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%page />
mkdir /etc
append /etc/lorax-test "TESTING LORAX TEMPLATES"
install /etc/lorax-test /etc/lorax-test-dest
2 changes: 1 addition & 1 deletion tests/pylorax/test_imgutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_mkhfsimg(self):
mkhfsimg(work_dir, disk_img.name, label="test")
self.assertTrue(os.path.exists(disk_img.name))
file_details = get_file_magic(disk_img.name)
self.assertTrue("Macintosh HFS" in file_details, file_details)
self.assertTrue(any(s in file_details for s in ("Macintosh HFS", "Apple HFS")), file_details)

def test_default_image_name(self):
"""Test default_image_name function"""
Expand Down
1 change: 1 addition & 0 deletions tests/pylorax/test_ltmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def nevra(pkg):
else:
self.assertEqual(r, [], t[0])

@unittest.skipUnless(os.geteuid() == 0 and not os.path.exists("/.in-container"), "requires root privileges, and no containers")
def test_01_runner_multi_repo(self):
"""Test installing packages with updates in a 2nd repo"""
# If this does not raise an error it means that:
Expand Down