[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH] Use libblkid instead of custom filesystem probing



This patch gets rid of all of our custom filesystem type and label probing
and switches it all over to using libblkid instead.  In some very basic
testing, it seems fine but some of the fun cases of mdraid, dmraid and lvm
need verification.

What do people think about it?

Jeremy

---
 fsset.py       |  127 +-----------------------------------------
 isys/Makefile  |    2 +-
 isys/isys.c    |   32 +++++++++++
 isys/isys.py   |  169 ++------------------------------------------------------
 partedUtils.py |   73 ++----------------------
 5 files changed, 45 insertions(+), 358 deletions(-)

diff --git a/fsset.py b/fsset.py
index 5dee46f..518aa34 100644
--- a/fsset.py
+++ b/fsset.py
@@ -1164,7 +1164,8 @@ class AutoFileSystem(PsudoFileSystem):
             ret = isys.resetFileContext(mountpoint, instroot)
             log.info("set SELinux context for mountpoint %s to %s" %(mountpoint, ret))
 
-        for fs in getFStoTry (device):
+        fs = isys.readFSType(device)
+        if fs is not None:
             try:
                 isys.mount (device, mountpoint, fstype = fs, readOnly =
                             readOnly, bindMount = bindMount)
@@ -1172,7 +1173,6 @@ class AutoFileSystem(PsudoFileSystem):
             except SystemError, (num, msg):
                 errNum = num
                 errMsg = msg
-                continue
 
         raise SystemError (errNum, errMsg)
 
@@ -2756,129 +2756,6 @@ def readFstab (anaconda):
         fsset.add(entry)
     return fsset
 
-def getDevFD(device):
-    try:
-        fd = os.open(device, os.O_RDONLY)
-    except:
-        try:
-            file = isys.makeDevInode(device)
-            fd = os.open(file, os.O_RDONLY)
-        except:
-            return -1
-    return fd
-
-def isValidExt2(device):
-    fd = getDevFD(device)
-    if fd == -1:
-        return 0
-
-    buf = os.read(fd, 2048)
-    os.close(fd)
-
-    if len(buf) != 2048:
-        return 0
-
-    if struct.unpack("<H", buf[1080:1082]) == (0xef53,):
-        return 1
-
-    return 0
-
-def isValidXFS(device):
-    fd = getDevFD(device)
-    if fd == -1:
-        return 0
-
-    buf = os.read(fd, 4)
-    os.close(fd)
-
-    if len(buf) != 4:
-        return 0
-
-    if buf == "XFSB":
-        return 1
-
-    return 0
-
-def isValidReiserFS(device):
-    fd = getDevFD(device)
-    if fd == -1:
-        return 0
-
-    '''
-    ** reiserfs 3.5.x super block begins at offset 8K
-    ** reiserfs 3.6.x super block begins at offset 64K
-    All versions have a magic value of "ReIsEr" at
-    offset 0x34 from start of super block
-    '''
-    reiserMagicVal = "ReIsEr"
-    reiserMagicOffset = 0x34
-    reiserSBStart = [64*1024, 8*1024]
-    bufSize = 0x40  # just large enough to include the magic value
-    for SBOffset in reiserSBStart:
-        try:
-            os.lseek(fd, SBOffset, 0)
-            buf = os.read(fd, bufSize)
-        except:
-            buf = ""
-
-        if len(buf) < bufSize:
-            continue
-
-        if (buf[reiserMagicOffset:reiserMagicOffset+len(reiserMagicVal)] ==
-            reiserMagicVal):
-            os.close(fd)
-            return 1
-
-    os.close(fd)
-    return 0    
-
-def isValidJFS(device):
-    fd = getDevFD(device)
-    if fd == -1:
-        return 0
-
-    try:
-        os.lseek(fd, 32768, 0)
-        buf = os.read(fd, 128)
-    except:
-        buf = ""
-
-    os.close(fd)
-    if len(buf) < 4:
-        return 0
-
-    if (buf[0:4] == "JFS1"):
-        return 1
-
-    return 0    
-
-# this will return a list of types of filesystems which device
-# looks like it could be to try mounting as
-def getFStoTry(device):
-    rc = []
-
-    if isValidXFS(device):
-        rc.append("xfs")
-
-    if isValidReiserFS(device):
-        rc.append("reiserfs")
-
-    if isValidJFS(device):
-        rc.append("jfs")
-
-    if isValidExt2(device):
-        if os.access(device, os.O_RDONLY):
-            create = 0
-        else:
-            create = 1
-        if isys.ext2HasJournal(device, makeDevNode = create):
-            rc.append("ext3")
-        rc.append("ext2")
-
-    # FIXME: need to check for swap
-
-    return rc
-
 def allocateLoopback(file):
     found = 1
     for i in range(8):
diff --git a/isys/Makefile b/isys/Makefile
index 7b38cef..1ab65da 100644
--- a/isys/Makefile
+++ b/isys/Makefile
@@ -8,7 +8,7 @@ OBJECTS = nfsmount.o nfsmount_clnt.o nfsmount_xdr.o imount.o \
           ethtool.o wireless.o eddsupport.o nl.o str.o auditd.o
 SOBJECTS = $(patsubst %.o,%.lo,$(OBJECTS))
 SOURCES = $(patsubst %.o,%.c,$(OBJECTS)) isys.c
-LOADLIBES = -lresolv -lpci -lpopt -lext2fs -lz -lkudzu -lpci -ldevmapper
+LOADLIBES = -lresolv -lpci -lpopt -lext2fs -lz -lkudzu -lpci -ldevmapper -lblkid
 LOADLIBES += $(SELINUXLIBES)
 ifeq ($(USESELINUX),1)
 LOADLIBES += -laudit
diff --git a/isys/isys.c b/isys/isys.c
index dd22669..eed25e5 100644
--- a/isys/isys.c
+++ b/isys/isys.c
@@ -49,6 +49,8 @@
 #include <libdhcp/ip_addr.h>
 #include <libdhcp/pump.h>
 
+#include <blkid/blkid.h>
+
 #include "nl.h"
 #include "imount.h"
 #include "isys.h"
@@ -115,6 +117,7 @@ static PyObject * doGetBiosDisk(PyObject * s, PyObject * args);
 static PyObject * doSegvHandler(PyObject *s, PyObject *args);
 static PyObject * doAuditDaemon(PyObject *s);
 static PyObject * doPrefixToNetmask(PyObject *s, PyObject *args);
+static PyObject * doGetBlkidData(PyObject * s, PyObject * args);
 
 static PyMethodDef isysModuleMethods[] = {
     { "ejectcdrom", (PyCFunction) doEjectCdrom, METH_VARARGS, NULL },
@@ -167,6 +170,7 @@ static PyMethodDef isysModuleMethods[] = {
     { "handleSegv", (PyCFunction) doSegvHandler, METH_VARARGS, NULL },
     { "auditdaemon", (PyCFunction) doAuditDaemon, METH_NOARGS, NULL },
     { "prefix2netmask", (PyCFunction) doPrefixToNetmask, METH_VARARGS, NULL },
+    { "getblkid", (PyCFunction) doGetBlkidData, METH_VARARGS, NULL },
     { NULL, NULL, 0, NULL }
 } ;
 
@@ -1195,4 +1199,32 @@ static PyObject * doAuditDaemon(PyObject *s) {
     return Py_None;
 }
 
+static PyObject * doGetBlkidData(PyObject * s, PyObject * args) {
+    char * dev, * key;
+    blkid_cache cache;
+    blkid_dev bdev = NULL;
+    blkid_tag_iterate titer;
+    const char * type, * data;
+
+    if (!PyArg_ParseTuple(args, "ss", &dev, &key)) return NULL;
+
+    blkid_get_cache(&cache, NULL);
+
+    bdev = blkid_get_dev(cache, dev, BLKID_DEV_NORMAL);
+    if (bdev == NULL)
+        goto out;
+    titer = blkid_tag_iterate_begin(bdev);
+    while (blkid_tag_next(titer, &type, &data) >= 0) {
+        if (!strcmp(type, key)) {
+            blkid_tag_iterate_end(titer);
+            return Py_BuildValue("s", data);
+        }
+    }
+    blkid_tag_iterate_end(titer);
+
+ out:
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4: */
diff --git a/isys/isys.py b/isys/isys.py
index eb0b4cb..691c1d5 100644
--- a/isys/isys.py
+++ b/isys/isys.py
@@ -769,176 +769,17 @@ def dhcpNetDevice(device):
 
     return _isys.dhcpnetdevice(devname, v4, v4method, v6, v6method, klass)
 
-def _readXFSLabel_int(device):
-    try:
-        fd = os.open(device, os.O_RDONLY)
-    except:
-        return None
-
-    try:
-        buf = os.read(fd, 128)
-        os.close(fd)
-    except OSError, e:
-        log.debug("error reading xfs label on %s: %s" %(device, e))
-        try:
-            os.close(fd)
-        except:
-            pass
-        return None
-
-    xfslabel = None
-    if len(buf) == 128 and buf[0:4] == "XFSB":
-        xfslabel = string.rstrip(buf[108:120],"\0x00")
-
-    return xfslabel
-
-def readXFSLabel(device, makeDevNode = 1):
-    if makeDevNode:
-        makeDevInode(device, "/tmp/disk")
-	label = _readXFSLabel_int("/tmp/disk")
-	os.unlink("/tmp/disk")
-    else:
-        label = _readXFSLabel_int(device)
-    return label
-
-def _readJFSLabel_int(device):
-    jfslabel = None
-    try:
-        fd = os.open(device, os.O_RDONLY)
-    except:
-        return jfslabel
-
-    try:
-        os.lseek(fd, 32768, 0)
-        buf = os.read(fd, 180)
-        os.close(fd)
-    except OSError, e:
-        log.debug("error reading jfs label on %s: %s" %(device, e))
-        try:
-            os.close(fd)
-        except:
-            pass
-        return jfslabel
-
-    if (len(buf) == 180 and buf[0:4] == "JFS1"):
-        jfslabel = string.rstrip(buf[152:168],"\0x00")
-
-    return jfslabel
-
-def readJFSLabel(device, makeDevNode = 1):
-    if makeDevNode:
-        makeDevInode(device, "/tmp/disk")
-	label = _readJFSLabel_int("/tmp/disk")
-	os.unlink("/tmp/disk")
-    else:
-        label = _readJFSLabel_int(device)
-    return label
-
-def _readSwapLabel_int(device):
-    label = None
-    try:
-        fd = os.open(device, os.O_RDONLY)
-    except:
-        return label
-
-    pagesize = resource.getpagesize()
-    try:
-        buf = os.read(fd, pagesize)
-        os.close(fd)
-    except OSError, e:
-        log.debug("error reading swap label on %s: %s" %(device, e))
-        try:
-            os.close(fd)
-        except:
-            pass
-        return label
-
-    if ((len(buf) == pagesize) and (buf[pagesize - 10:] == "SWAPSPACE2")):
-        label = string.rstrip(buf[1052:1068], "\0x00")
-    return label
-
-def readSwapLabel(device, makeDevNode = 1):
+def readFSLabel(device, makeDevNode = 1):
     if makeDevNode:
         makeDevInode(device, "/tmp/disk")
-        label = _readSwapLabel_int("/tmp/disk")
-        os.unlink("/tmp/disk")
-    else:
-        label = _readSwapLabel_int(device)
-    return label
-
-def readExt2Label(device, makeDevNode = 1):
+        device = "/tmp/disk"
+    label = _isys.getblkid(device, "LABEL")
     if makeDevNode:
-        makeDevInode(device, "/tmp/disk")
-        label = _isys.e2fslabel("/tmp/disk");
         os.unlink("/tmp/disk")
-    else:
-        label = _isys.e2fslabel(device)
     return label
 
-def _readReiserFSLabel_int(device):
-    label = None
-
-    try:
-        fd = os.open(device, os.O_RDONLY)
-    except OSError, e:
-        log.debug("error opening device %s: %s" % (device, e))
-        return label
-
-    # valid block sizes in reiserfs are 512 - 8192, powers of 2
-    # we put 4096 first, since it's the default
-    # reiserfs superblock occupies either the 2nd or 16th block
-    for blksize in (4096, 512, 1024, 2048, 8192):
-        for start in (blksize, (blksize*16)):
-            try:
-                os.lseek(fd, start, 0)
-                # read 120 bytes to get s_magic and s_label
-                buf = os.read(fd, 120)
-
-                # see if this block is the superblock
-                # this reads reiserfs_super_block_v1.s_magic as defined
-                # in include/reiserfs_fs.h in the reiserfsprogs source
-                m = string.rstrip(buf[52:61], "\0x00")
-                if m == "ReIsErFs" or m == "ReIsEr2Fs" or m == "ReIsEr3Fs":
-                    # this reads reiserfs_super_block.s_label as
-                    # defined in include/reiserfs_fs.h
-                    label = string.rstrip(buf[100:116], "\0x00")
-                    os.close(fd)
-                    return label
-            except OSError, e:
-                # [Error 22] probably means we're trying to read an
-                # extended partition. 
-                log.debug("error reading reiserfs label on %s: %s" %(device, e))
-
-                try:
-                    os.close(fd)
-                except:
-                    pass
-
-                return label
-
-    os.close(fd)
-    return label
-
-def readReiserFSLabel(device, makeDevNode = 1):
-    if makeDevNode:
-        makeDevInode(device, "/tmp/disk")
-	label = _readReiserFSLabel_int("/tmp/disk")
-        os.unlink("/tmp/disk")
-    else:
-        label = _readReiserFSLabel_int(device)
-    return label
-
-def readFSLabel(device, makeDevNode = 1):
-    label = readExt2Label(device, makeDevNode)
-    if label is None:
-        label = readSwapLabel(device, makeDevNode)
-    if label is None:
-        label = readXFSLabel(device, makeDevNode)
-    if label is None:
-        label = readJFSLabel(device, makeDevNode)
-    if label is None:
-        label = readReiserFSLabel(device, makeDevNode)
-    return label
+def readFSType(device):
+    return _isys.getblkid(device, "TYPE")
 
 def ext2Clobber(device, makeDevNode = 1):
     _isys.e2fsclobber(device)
diff --git a/partedUtils.py b/partedUtils.py
index 4adc0b9..57f1eda 100644
--- a/partedUtils.py
+++ b/partedUtils.py
@@ -465,72 +465,7 @@ def sniffFilesystemType(device):
     else:
         dev = isys.makeDevInode(device)
 
-    pagesize = resource.getpagesize()
-    if pagesize > 2048:
-        num = pagesize
-    else:
-        num = 2048
-
-    try:
-        fd = os.open(dev, os.O_RDONLY)
-        buf = os.read(fd, num)
-    except:
-        return None
-    finally:
-        try:
-            os.close(fd)
-        except:
-            pass
-
-    if len(buf) < pagesize:
-        try:
-            log.error("Tried to read pagesize for %s in sniffFilesystemType and only read %s", dev, len(buf))
-        except:
-            pass
-
-        return None
-
-    # physical volumes start with HM (see linux/lvm.h
-    # and LVM/ver/tools/lib/pv_copy.c)
-    if buf.startswith("HM"):
-        return "physical volume (LVM)"
-    # sniff for LVM2 label.  see LVM/ver/lib/label/label.[ch] for a
-    # description of the label and LVM/ver/lib/format_text/layout.h 
-    for sec in range(0, 4):
-        off = (sec * 512) + 24
-        if buf[off:].startswith("LVM2"):
-            return "physical volume (LVM)"
-
-    try:
-        isys.raidsbFromDevice(dev)
-        return "software RAID"
-    except:
-        pass
-
-    # ext2 check
-    if struct.unpack("<H", buf[1080:1082]) == (0xef53,):
-        if isys.ext2HasJournal(dev, makeDevNode = 0):
-            return "ext3"
-        else:
-            return "ext2"
-
-    # xfs signature
-    if buf.startswith("XFSB"):
-        return "xfs"
-
-    # 2.6 doesn't support version 0, so we don't like SWAP-SPACE
-    if (buf[pagesize - 10:] == "SWAPSPACE2"):
-        return "swap"
-
-    if fsset.isValidReiserFS(dev):
-        return "reiserfs"
-
-    if fsset.isValidJFS(dev):
-        return "jfs"
-
-    # FIXME:  we don't look for vfat
-
-    return None
+    return isys.readFSType(device)
 
 def getReleaseString(mountpoint):
     if os.access(mountpoint + "/etc/redhat-release", os.R_OK):
@@ -796,7 +731,8 @@ class DiskSet:
         for dev, devices, level, numActive in self.mdList:
             (errno, msg) = (None, None)
             found = 0
-            for fs in fsset.getFStoTry(dev):
+            fs = isys.readFSType(dev)
+            if fs is not None:
                 try:
                     isys.mount(dev, self.anaconda.rootPath, fs, readOnly = 1)
                     found = 1
@@ -827,7 +763,8 @@ class DiskSet:
                 continue
             dev = "/dev/%s/%s" %(vg, lv)
             found = 0
-            for fs in fsset.getFStoTry(dev):
+            fs = isys.readFSType(dev)
+            if fs is not None:
                 try:
                     isys.mount(dev, self.anaconda.rootPath, fs, readOnly = 1)
                     found = 1
-- 
1.5.3.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]