util/chroot: add run() method to context class

Rename the ChrootProcDevSys class to just Chroot and add a run() method.
Calls now can be made using:

  with Chroot(root) as chroot:
      chroot.run(command)
This commit is contained in:
Achilleas Koutsou 2024-08-21 19:03:35 +02:00 committed by Brian C. Lane
parent 931e832944
commit 3dbf389ebf
3 changed files with 13 additions and 16 deletions

View file

@ -2,7 +2,7 @@ import os
import subprocess import subprocess
class ChrootProcDevSys: class Chroot:
""" """
Sets up mounts for the virtual filesystems inside a root tree, preparing it for running commands using chroot. This Sets up mounts for the virtual filesystems inside a root tree, preparing it for running commands using chroot. This
should be used whenever a stage needs to run a command against the root tree but doesn't support a --root option or should be used whenever a stage needs to run a command against the root tree but doesn't support a --root option or
@ -43,3 +43,8 @@ class ChrootProcDevSys:
failed_umounts.append(d) failed_umounts.append(d)
if failed_umounts: if failed_umounts:
print(f"Error unmounting paths from chroot: {failed_umounts}") print(f"Error unmounting paths from chroot: {failed_umounts}")
def run(self, cmd, **kwargs):
cmd = ["/usr/sbin/chroot", self.root] + cmd
# pylint: disable=subprocess-run-check
subprocess.run(cmd, **kwargs) # noqa: PLW1510

View file

@ -1,9 +1,8 @@
#!/usr/bin/python3 #!/usr/bin/python3
import subprocess
import sys import sys
import osbuild.api import osbuild.api
from osbuild.util.chroot import ChrootProcDevSys from osbuild.util.chroot import Chroot
def yesno(name: str, value: bool) -> str: def yesno(name: str, value: bool) -> str:
@ -83,12 +82,8 @@ def main(tree, options):
if initoverlayfs: if initoverlayfs:
initfs_bin = "/usr/bin/initoverlayfs-install" initfs_bin = "/usr/bin/initoverlayfs-install"
with ChrootProcDevSys(tree): with Chroot(tree) as chroot:
subprocess.run(["/usr/sbin/chroot", tree, initfs_bin, chroot.run([initfs_bin, "--no-hostonly", "--kver", kver] + opts, check=True)
"--no-hostonly",
"--kver", kver]
+ opts,
check=True)
return 0 return 0

View file

@ -1,23 +1,20 @@
#!/usr/bin/python3 #!/usr/bin/python3
import subprocess
import sys import sys
from osbuild import api from osbuild import api
from osbuild.util.chroot import ChrootProcDevSys from osbuild.util.chroot import Chroot
def main(tree, options): def main(tree, options):
policy = options["policy"] policy = options["policy"]
with ChrootProcDevSys(tree): with Chroot(tree) as chroot:
# update-crypto-polciies uses /proc/self/mountinfo to find and verify that fips paths have been mounted to their # update-crypto-polciies uses /proc/self/mountinfo to find and verify that fips paths have been mounted to their
# expected locations by searching for the following path suffixes: # expected locations by searching for the following path suffixes:
# /crypto-policies/default-fips-config # /crypto-policies/default-fips-config
# /crypto-policies/back-ends/FIPS # /crypto-policies/back-ends/FIPS
cmd = ["/usr/sbin/chroot", tree, cmd = ["/usr/bin/update-crypto-policies", "--set", policy]
"/usr/bin/update-crypto-policies", "--set", policy] chroot.run(cmd, check=True)
subprocess.run(cmd, check=True)
return 0 return 0