neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

Blame SOURCES/0001-Split-ROOT_PATH-usage-into-getTargetPhysicalRoot-get.patch

151055
From 6f1a21e4c2dde6436886e9c3c3f9305d5bc6d13a Mon Sep 17 00:00:00 2001
151055
From: Colin Walters <walters@verbum.org>
151055
Date: Wed, 19 Mar 2014 10:56:24 -0400
151055
Subject: [PATCH] Split ROOT_PATH usage into
151055
 getTargetPhysicalRoot()/getSysroot()
151055
151055
This mirrors the change to Anaconda, in order to enable OSTree
151055
support.
151055
151055
For OSTree, the location of the OS checkout (and e.g. /etc/fstab) is
151055
really in /ostree/deploy/$osname/deploy/$revision/etc/fstab.
151055
151055
In order to properly support OSTree, Blivet will need to gain an
151055
understanding of the separation between the physical system / and the
151055
target root.
151055
151055
This patch will be used in Anaconda, which will call setSysroot()
151055
attribute after the root being installed is laid out.
151055
151055
After that, when we call write(), the fstab data will be correctly
151055
written into the target root.
151055
151055
Related: rhbz#1113535
151055
Port of rpmostreepayload patches from master
151055
commit 5b39c90ae582a8fb008c3633954a33b58394802c
151055
---
151055
 blivet/__init__.py        | 102 +++++++++++++++++++++++++++++++++-------------
151055
 blivet/devicelibs/dasd.py |   4 +-
151055
 blivet/fcoe.py            |   8 ++--
151055
 blivet/iscsi.py           |  14 +++----
151055
 blivet/util.py            |   4 +-
151055
 blivet/zfcp.py            |   8 ++--
151055
 6 files changed, 92 insertions(+), 48 deletions(-)
151055
151055
diff --git a/blivet/__init__.py b/blivet/__init__.py
151055
index 85b67da..6b127d7 100644
151055
--- a/blivet/__init__.py
151055
+++ b/blivet/__init__.py
151055
@@ -30,7 +30,10 @@ __version__ = '0.18.34'
151055
 ## enable_installer_mode is called.
151055
 ##
151055
 isys = None
151055
+iutil = None
151055
 ROOT_PATH = '/'
151055
+_storageRoot = ROOT_PATH
151055
+_sysroot = ROOT_PATH
151055
 shortProductName = 'blivet'
151055
 productName = 'blivet'
151055
 bootLoaderError = Exception
151055
@@ -99,7 +102,10 @@ log = logging.getLogger("blivet")
151055
 
151055
 def enable_installer_mode():
151055
     global isys
151055
+    global iutil
151055
     global ROOT_PATH
151055
+    global _storageRoot
151055
+    global _sysroot
151055
     global shortProductName
151055
     global productName
151055
     global get_bootloader
151055
@@ -108,6 +114,7 @@ def enable_installer_mode():
151055
     global ERROR_RAISE
151055
 
151055
     from pyanaconda import isys
151055
+    from pyanaconda import iutil
151055
     from pyanaconda.constants import ROOT_PATH
151055
     from pyanaconda.constants import shortProductName
151055
     from pyanaconda.constants import productName
151055
@@ -116,11 +123,48 @@ def enable_installer_mode():
151055
     from pyanaconda.errors import errorHandler
151055
     from pyanaconda.errors import ERROR_RAISE
151055
 
151055
+    if hasattr(iutil, 'getTargetPhysicalRoot'):
151055
+        # Introduced in newer Anaconda
151055
+        _storageRoot = iutil.getTargetPhysicalRoot()
151055
+        _sysroot = iutil.getSysroot()
151055
+    else:
151055
+        _storageRoot = _sysroot = ROOT_PATH
151055
+
151055
     from pyanaconda.anaconda_log import program_log_lock
151055
     util.program_log_lock = program_log_lock
151055
 
151055
     flags.installer_mode = True
151055
 
151055
+def getSysroot():
151055
+    """Returns the path to the target OS installation.
151055
+
151055
+    For traditional installations, this is the same as the physical
151055
+    storage root.
151055
+    """
151055
+    return _sysroot
151055
+
151055
+def getTargetPhysicalRoot():
151055
+    """Returns the path to the "physical" storage root.
151055
+
151055
+    This may be distinct from the sysroot, which could be a
151055
+    chroot-type subdirectory of the physical root.  This is used for
151055
+    example by all OSTree-based installations.
151055
+    """
151055
+    return _storageRoot
151055
+
151055
+def setSysroot(storageRoot, sysroot=None):
151055
+    """Change the OS root path.
151055
+       :param storageRoot: The root of physical storage
151055
+       :param sysroot: An optional chroot subdirectory of storageRoot
151055
+
151055
+    Change the
151055
+    """
151055
+    global _storageRoot
151055
+    global _sysroot
151055
+    _storageRoot = _sysroot = storageRoot
151055
+    if sysroot is not None:
151055
+        _sysroot = sysroot
151055
+
151055
 def storageInitialize(storage, ksdata, protected):
151055
     """ Perform installer-specific storage initialization. """
151055
     from pyanaconda.flags import flags as anaconda_flags
151055
@@ -202,7 +246,7 @@ def writeEscrowPackets(storage):
151055
     backupPassphrase = generateBackupPassphrase()
151055
 
151055
     try:
151055
-        escrowDir = ROOT_PATH + "/root"
151055
+        escrowDir = _sysroot + "/root"
151055
         log.debug("escrow: writing escrow packets to %s", escrowDir)
151055
         util.makedirs(escrowDir)
151055
         for device in escrowDevices:
151055
@@ -1665,22 +1709,22 @@ class Blivet(object):
151055
         return list(pkgs)
151055
 
151055
     def write(self):
151055
-        if not os.path.isdir("%s/etc" % ROOT_PATH):
151055
-            os.mkdir("%s/etc" % ROOT_PATH)
151055
+        if not os.path.isdir("%s/etc" % _sysroot):
151055
+            os.mkdir("%s/etc" % _sysroot)
151055
 
151055
         self.fsset.write()
151055
         self.makeMtab()
151055
-        self.iscsi.write(ROOT_PATH, self)
151055
-        self.fcoe.write(ROOT_PATH)
151055
-        self.zfcp.write(ROOT_PATH)
151055
-        write_dasd_conf(self.dasd, ROOT_PATH)
151055
+        self.iscsi.write(_sysroot, self)
151055
+        self.fcoe.write(_sysroot)
151055
+        self.zfcp.write(_sysroot)
151055
+        write_dasd_conf(self.dasd, _sysroot)
151055
 
151055
     def turnOnSwap(self, upgrading=None):
151055
-        self.fsset.turnOnSwap(rootPath=ROOT_PATH,
151055
+        self.fsset.turnOnSwap(rootPath=_sysroot,
151055
                               upgrading=upgrading)
151055
 
151055
     def mountFilesystems(self, raiseErrors=None, readOnly=None, skipRoot=False):
151055
-        self.fsset.mountFilesystems(rootPath=ROOT_PATH,
151055
+        self.fsset.mountFilesystems(rootPath=_sysroot,
151055
                                     raiseErrors=raiseErrors,
151055
                                     readOnly=readOnly, skipRoot=skipRoot)
151055
 
151055
@@ -1797,7 +1841,7 @@ class Blivet(object):
151055
     def makeMtab(self):
151055
         path = "/etc/mtab"
151055
         target = "/proc/self/mounts"
151055
-        path = os.path.normpath("%s/%s" % (ROOT_PATH, path))
151055
+        path = os.path.normpath("%s/%s" % (_sysroot, path))
151055
 
151055
         if os.path.islink(path):
151055
             # return early if the mtab symlink is already how we like it
151055
@@ -2130,7 +2174,7 @@ def mountExistingSystem(fsset, rootDevice,
151055
                         allowDirty=None, dirtyCB=None,
151055
                         readOnly=None):
151055
     """ Mount filesystems specified in rootDevice's /etc/fstab file. """
151055
-    rootPath = ROOT_PATH
151055
+    rootPath = _sysroot
151055
     if dirtyCB is None:
151055
         dirtyCB = lambda l: False
151055
 
151055
@@ -2172,7 +2216,7 @@ def mountExistingSystem(fsset, rootDevice,
151055
     if dirtyDevs and (not allowDirty or dirtyCB(dirtyDevs)):
151055
         raise DirtyFSError("\n".join(dirtyDevs))
151055
 
151055
-    fsset.mountFilesystems(rootPath=ROOT_PATH, readOnly=readOnly, skipRoot=True)
151055
+    fsset.mountFilesystems(rootPath=_sysroot, readOnly=readOnly, skipRoot=True)
151055
 
151055
 
151055
 class BlkidTab(object):
151055
@@ -2529,7 +2573,7 @@ class FSSet(object):
151055
                 loop mounts?
151055
         """
151055
         if not chroot or not os.path.isdir(chroot):
151055
-            chroot = ROOT_PATH
151055
+            chroot = _sysroot
151055
 
151055
         path = "%s/etc/fstab" % chroot
151055
         if not os.access(path, os.R_OK):
151055
@@ -2703,10 +2747,10 @@ class FSSet(object):
151055
         self.active = False
151055
 
151055
     def createSwapFile(self, device, size):
151055
-        """ Create and activate a swap file under ROOT_PATH. """
151055
+        """ Create and activate a swap file under storage root. """
151055
         filename = "/SWAP"
151055
         count = 0
151055
-        basedir = os.path.normpath("%s/%s" % (ROOT_PATH,
151055
+        basedir = os.path.normpath("%s/%s" % (getTargetPhysicalRoot(),
151055
                                               device.format.mountpoint))
151055
         while os.path.exists("%s/%s" % (basedir, filename)) or \
151055
               self.devicetree.getDeviceByName(filename):
151055
@@ -2727,10 +2771,10 @@ class FSSet(object):
151055
 
151055
     def mkDevRoot(self):
151055
         root = self.rootDevice
151055
-        dev = "%s/%s" % (ROOT_PATH, root.path)
151055
-        if not os.path.exists("%s/dev/root" %(ROOT_PATH,)) and os.path.exists(dev):
151055
+        dev = "%s/%s" % (_sysroot, root.path)
151055
+        if not os.path.exists("%s/dev/root" %(_sysroot,)) and os.path.exists(dev):
151055
             rdev = os.stat(dev).st_rdev
151055
-            os.mknod("%s/dev/root" % (ROOT_PATH,), stat.S_IFBLK | 0600, rdev)
151055
+            os.mknod("%s/dev/root" % (_sysroot,), stat.S_IFBLK | 0600, rdev)
151055
 
151055
     @property
151055
     def swapDevices(self):
151055
@@ -2742,7 +2786,7 @@ class FSSet(object):
151055
 
151055
     @property
151055
     def rootDevice(self):
151055
-        for path in ["/", ROOT_PATH]:
151055
+        for path in ["/", getTargetPhysicalRoot()]:
151055
             for device in self.devices:
151055
                 try:
151055
                     mountpoint = device.format.mountpoint
151055
@@ -2755,19 +2799,19 @@ class FSSet(object):
151055
     def write(self):
151055
         """ write out all config files based on the set of filesystems """
151055
         # /etc/fstab
151055
-        fstab_path = os.path.normpath("%s/etc/fstab" % ROOT_PATH)
151055
+        fstab_path = os.path.normpath("%s/etc/fstab" % _sysroot)
151055
         fstab = self.fstab()
151055
         open(fstab_path, "w").write(fstab)
151055
 
151055
         # /etc/crypttab
151055
-        crypttab_path = os.path.normpath("%s/etc/crypttab" % ROOT_PATH)
151055
+        crypttab_path = os.path.normpath("%s/etc/crypttab" % _sysroot)
151055
         crypttab = self.crypttab()
151055
         origmask = os.umask(0077)
151055
         open(crypttab_path, "w").write(crypttab)
151055
         os.umask(origmask)
151055
 
151055
         # /etc/mdadm.conf
151055
-        mdadm_path = os.path.normpath("%s/etc/mdadm.conf" % ROOT_PATH)
151055
+        mdadm_path = os.path.normpath("%s/etc/mdadm.conf" % _sysroot)
151055
         mdadm_conf = self.mdadmConf()
151055
         if mdadm_conf:
151055
             open(mdadm_path, "w").write(mdadm_conf)
151055
@@ -2900,11 +2944,11 @@ def getReleaseString():
151055
     relVer = None
151055
 
151055
     try:
151055
-        relArch = util.capture_output(["arch"], root=ROOT_PATH).strip()
151055
+        relArch = util.capture_output(["arch"], root=_sysroot).strip()
151055
     except:
151055
         relArch = None
151055
 
151055
-    filename = "%s/etc/redhat-release" % ROOT_PATH
151055
+    filename = "%s/etc/redhat-release" % getSysroot()
151055
     if os.access(filename, os.R_OK):
151055
         with open(filename) as f:
151055
             try:
151055
@@ -2923,8 +2967,8 @@ def getReleaseString():
151055
     return (relArch, relName, relVer)
151055
 
151055
 def findExistingInstallations(devicetree):
151055
-    if not os.path.exists(ROOT_PATH):
151055
-        util.makedirs(ROOT_PATH)
151055
+    if not os.path.exists(getTargetPhysicalRoot()):
151055
+        util.makedirs(getTargetPhysicalRoot())
151055
 
151055
     roots = []
151055
     for device in devicetree.leaves:
151055
@@ -2940,7 +2984,7 @@ def findExistingInstallations(devicetree):
151055
 
151055
         options = device.format.options + ",ro"
151055
         try:
151055
-            device.format.mount(options=options, mountpoint=ROOT_PATH)
151055
+            device.format.mount(options=options, mountpoint=getSysroot())
151055
         except Exception as e:
151055
             log.warning("mount of %s as %s failed: %s" % (device.name,
151055
                                                           device.format.type,
151055
@@ -2948,7 +2992,7 @@ def findExistingInstallations(devicetree):
151055
             device.teardown()
151055
             continue
151055
 
151055
-        if not os.access(ROOT_PATH + "/etc/fstab", os.R_OK):
151055
+        if not os.access(getSysroot() + "/etc/fstab", os.R_OK):
151055
             device.teardown(recursive=True)
151055
             continue
151055
 
151055
@@ -3000,7 +3044,7 @@ class Root(object):
151055
 def parseFSTab(devicetree, chroot=None):
151055
     """ parse /etc/fstab and return a tuple of a mount dict and swap list """
151055
     if not chroot or not os.path.isdir(chroot):
151055
-        chroot = ROOT_PATH
151055
+        chroot = _sysroot
151055
 
151055
     mounts = {}
151055
     swaps = []
151055
diff --git a/blivet/devicelibs/dasd.py b/blivet/devicelibs/dasd.py
151055
index 7eb2551..5beb11d 100644
151055
--- a/blivet/devicelibs/dasd.py
151055
+++ b/blivet/devicelibs/dasd.py
151055
@@ -110,14 +110,14 @@ def dasd_needs_format(dasd):
151055
     return False
151055
 
151055
 
151055
-def write_dasd_conf(disks, ROOT_PATH):
151055
+def write_dasd_conf(disks, root):
151055
     """ Write /etc/dasd.conf to target system for all DASD devices
151055
         configured during installation.
151055
     """
151055
     if not (arch.isS390() or disks):
151055
         return
151055
 
151055
-    with open(os.path.realpath(ROOT_PATH + "/etc/dasd.conf"), "w") as f:
151055
+    with open(os.path.realpath(root + "/etc/dasd.conf"), "w") as f:
151055
         for dasd in sorted(disks, key=lambda d: d.name):
151055
             fields = [dasd.busid] + dasd.getOpts()
151055
             f.write("%s\n" % " ".join(fields),)
151055
diff --git a/blivet/fcoe.py b/blivet/fcoe.py
151055
index 42d7550..71ffbcb 100644
151055
--- a/blivet/fcoe.py
151055
+++ b/blivet/fcoe.py
151055
@@ -149,15 +149,15 @@ class fcoe(object):
151055
 
151055
         return error_msg
151055
 
151055
-    def write(self, ROOT_PATH):
151055
+    def write(self, root):
151055
         if not self.nics:
151055
             return
151055
 
151055
-        if not os.path.isdir(ROOT_PATH + "/etc/fcoe"):
151055
-            os.makedirs(ROOT_PATH + "/etc/fcoe", 0755)
151055
+        if not os.path.isdir(root + "/etc/fcoe"):
151055
+            os.makedirs(root + "/etc/fcoe", 0755)
151055
 
151055
         for nic, dcb, auto_vlan in self.nics:
151055
-            fd = os.open(ROOT_PATH + "/etc/fcoe/cfg-" + nic,
151055
+            fd = os.open(root + "/etc/fcoe/cfg-" + nic,
151055
                          os.O_RDWR | os.O_CREAT)
151055
             os.write(fd, '# Created by anaconda\n')
151055
             os.write(fd, '# Enable/Disable FCoE service at the Ethernet port\n')
151055
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
151055
index fceb881..eb640cd 100644
151055
--- a/blivet/iscsi.py
151055
+++ b/blivet/iscsi.py
151055
@@ -360,7 +360,7 @@ class iscsi(object):
151055
 
151055
         self.stabilize()
151055
 
151055
-    def write(self, ROOT_PATH, storage):
151055
+    def write(self, root, storage):
151055
         if not self.initiatorSet:
151055
             return
151055
 
151055
@@ -377,17 +377,17 @@ class iscsi(object):
151055
             if autostart:
151055
                 node.setParameter("node.startup", "automatic")
151055
 
151055
-        if not os.path.isdir(ROOT_PATH + "/etc/iscsi"):
151055
-            os.makedirs(ROOT_PATH + "/etc/iscsi", 0755)
151055
-        fd = os.open(ROOT_PATH + INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
151055
+        if not os.path.isdir(root + "/etc/iscsi"):
151055
+            os.makedirs(root + "/etc/iscsi", 0755)
151055
+        fd = os.open(root + INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
151055
         os.write(fd, "InitiatorName=%s\n" %(self.initiator))
151055
         os.close(fd)
151055
 
151055
         # copy "db" files.  *sigh*
151055
-        if os.path.isdir(ROOT_PATH + "/var/lib/iscsi"):
151055
-            shutil.rmtree(ROOT_PATH + "/var/lib/iscsi")
151055
+        if os.path.isdir(root + "/var/lib/iscsi"):
151055
+            shutil.rmtree(root + "/var/lib/iscsi")
151055
         if os.path.isdir("/var/lib/iscsi"):
151055
-            shutil.copytree("/var/lib/iscsi", ROOT_PATH + "/var/lib/iscsi",
151055
+            shutil.copytree("/var/lib/iscsi", root + "/var/lib/iscsi",
151055
                             symlinks=True)
151055
 
151055
     def getNode(self, name, address, port, iface):
151055
diff --git a/blivet/util.py b/blivet/util.py
151055
index d43b252..505acc6 100644
151055
--- a/blivet/util.py
151055
+++ b/blivet/util.py
151055
@@ -277,13 +277,13 @@ def makedirs(path):
151055
 
151055
 def copy_to_system(source):
151055
     # do the import now because enable_installer_mode() has finally been called.
151055
-    from . import ROOT_PATH
151055
+    from . import getSysroot
151055
 
151055
     if not os.access(source, os.R_OK):
151055
         log.info("copy_to_system: source '%s' does not exist." % source)
151055
         return False
151055
 
151055
-    target = ROOT_PATH + source
151055
+    target = getSysroot() + source
151055
     target_dir = os.path.dirname(target)
151055
     log.debug("copy_to_system: '%s' -> '%s'." % (source, target))
151055
     if not os.path.isdir(target_dir):
151055
diff --git a/blivet/zfcp.py b/blivet/zfcp.py
151055
index cf8ec40..565c201 100644
151055
--- a/blivet/zfcp.py
151055
+++ b/blivet/zfcp.py
151055
@@ -409,15 +409,15 @@ class ZFCP:
151055
             except ValueError as e:
151055
                 log.warn(str(e))
151055
 
151055
-    def write(self, ROOT_PATH):
151055
+    def write(self, root):
151055
         if len(self.fcpdevs) == 0:
151055
             return
151055
-        f = open(ROOT_PATH + zfcpconf, "w")
151055
+        f = open(root + zfcpconf, "w")
151055
         for d in self.fcpdevs:
151055
             f.write("%s\n" %(d,))
151055
         f.close()
151055
-        
151055
-        f = open(ROOT_PATH + "/etc/modprobe.conf", "a")
151055
+
151055
+        f = open(root + "/etc/modprobe.conf", "a")
151055
         f.write("alias scsi_hostadapter zfcp\n")
151055
         f.close()
151055
 
151055
-- 
151055
1.9.3
151055