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:
parent
931e832944
commit
3dbf389ebf
3 changed files with 13 additions and 16 deletions
|
|
@ -2,7 +2,7 @@ import os
|
|||
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
|
||||
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)
|
||||
if 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
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
#!/usr/bin/python3
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import osbuild.api
|
||||
from osbuild.util.chroot import ChrootProcDevSys
|
||||
from osbuild.util.chroot import Chroot
|
||||
|
||||
|
||||
def yesno(name: str, value: bool) -> str:
|
||||
|
|
@ -83,12 +82,8 @@ def main(tree, options):
|
|||
if initoverlayfs:
|
||||
initfs_bin = "/usr/bin/initoverlayfs-install"
|
||||
|
||||
with ChrootProcDevSys(tree):
|
||||
subprocess.run(["/usr/sbin/chroot", tree, initfs_bin,
|
||||
"--no-hostonly",
|
||||
"--kver", kver]
|
||||
+ opts,
|
||||
check=True)
|
||||
with Chroot(tree) as chroot:
|
||||
chroot.run([initfs_bin, "--no-hostonly", "--kver", kver] + opts, check=True)
|
||||
|
||||
return 0
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,20 @@
|
|||
#!/usr/bin/python3
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from osbuild import api
|
||||
from osbuild.util.chroot import ChrootProcDevSys
|
||||
from osbuild.util.chroot import Chroot
|
||||
|
||||
|
||||
def main(tree, options):
|
||||
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
|
||||
# expected locations by searching for the following path suffixes:
|
||||
# /crypto-policies/default-fips-config
|
||||
# /crypto-policies/back-ends/FIPS
|
||||
cmd = ["/usr/sbin/chroot", tree,
|
||||
"/usr/bin/update-crypto-policies", "--set", policy]
|
||||
|
||||
subprocess.run(cmd, check=True)
|
||||
cmd = ["/usr/bin/update-crypto-policies", "--set", policy]
|
||||
chroot.run(cmd, check=True)
|
||||
|
||||
return 0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue