]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- get version and related LVM2 capabilities only once
authorOndrej Kozina <okozina@redhat.com>
Tue, 13 Aug 2013 09:02:51 +0000 (11:02 +0200)
committerOndrej Kozina <okozina@redhat.com>
Tue, 13 Aug 2013 09:02:51 +0000 (11:02 +0200)
snapper/Lvm.cc
snapper/Lvm.h

index 443cdb8519172c3ee2280b05998cf91d35472125..6012c25eb75689a54c9b23d08c3cfec4b2e2a49e 100644 (file)
@@ -58,7 +58,7 @@ namespace snapper
 
     Lvm::Lvm(const string& subvolume, const string& mount_type)
        : Filesystem(subvolume), mount_type(mount_type),
-       version(getLvmVersion())
+       caps(LvmCapabilities::get_lvm_capabilities())
     {
        if (access(LVCREATEBIN, X_OK) != 0)
        {
@@ -75,12 +75,6 @@ namespace snapper
            throw ProgramNotInstalledException(LVCHANGEBIN " not installed");
        }
 
-       if (!version.valid())
-           y2war("Couldn't get proper LVM version");
-
-       if (version >= lvm_version(2, 2, 99))
-           ignoreactivationskip = " -K";
-
        bool found = false;
        MtabData mtab_data;
 
@@ -331,7 +325,7 @@ namespace snapper
     void
     Lvm::activateSnapshot(const string& vg_name, const string& lv_name) const
     {
-       SystemCmd cmd(LVCHANGEBIN + ignoreactivationskip + " -ay " + quote(vg_name + "/" + lv_name));
+       SystemCmd cmd(LVCHANGEBIN + caps->get_ignoreactivationskip() + " -ay " + quote(vg_name + "/" + lv_name));
        if (cmd.retcode() != 0)
        {
            y2err("Couldn't activate snapshot " << vg_name << "/" << lv_name);
@@ -357,32 +351,41 @@ namespace snapper
     }
 
 
-    lvm_version
-    Lvm::getLvmVersion()
+    LvmCapabilities::LvmCapabilities()
+       : ignoreactivationskip(), time_support(false)
     {
-       uint16_t maj, min, rev;
-
        SystemCmd cmd(string(LVSBIN " --version"));
 
        if (cmd.retcode() != 0)
        {
-           return lvm_version::invalid_version();
+           y2war("Couldn't get LVM version info");
        }
        else
        {
            Regex rx(".*LVM[[:space:]]+version:[[:space:]]+([0-9]+)\\.([0-9]+)\\.([0-9]+).*$");
 
            if (!rx.match(cmd.getLine(0)))
-               return lvm_version::invalid_version();
+           {
+               y2war("LVM version format didn't match");
+           }
            else
            {
+               uint16_t maj, min, rev;
+
                rx.cap(1) >> maj;
                rx.cap(2) >> min;
                rx.cap(3) >> rev;
+
+               lvm_version version(maj, min, rev);
+
+               if (version >= lvm_version(2,2,99))
+               {
+                   ignoreactivationskip = " -K";
+               }
+
+               time_support = (version >= lvm_version(2,2,88));
            }
        }
-
-       return lvm_version(maj, min, rev);
     }
 
 
@@ -392,4 +395,31 @@ namespace snapper
        return a.version >= b.version;
     }
 
+
+    LvmCapabilities*
+    LvmCapabilities::get_lvm_capabilities()
+    {
+       /*
+        * NOTE: verify only one thread can access
+        *       this section at the same time!
+        */
+       static LvmCapabilities caps;
+
+       return &caps;
+    }
+
+
+    string
+    LvmCapabilities::get_ignoreactivationskip() const
+    {
+       return ignoreactivationskip;
+    }
+
+
+    bool
+    LvmCapabilities::get_time_support() const
+    {
+       return time_support;
+    }
+
 }
index 3223e0901d1627c8a6bf6a608bf20c70b59a37f3..462236cb6b6c1acaacf879900ed3225568b1e714 100644 (file)
@@ -23,6 +23,7 @@
 #ifndef SNAPPER_LVM_H
 #define SNAPPER_LVM_H
 
+#include <boost/noncopyable.hpp>
 
 #include "snapper/Filesystem.h"
 
@@ -43,19 +44,31 @@ namespace snapper
     };
 
 
-    class lvm_version
+    struct lvm_version
     {
-    public:
-       static lvm_version invalid_version() { return lvm_version(0, 0, 0); }
-
        lvm_version(uint16_t maj, uint16_t min, uint16_t rev)
-           : version(rev | ((uint32_t) min << 16) | ((uint64_t) maj << 32)) {}
+           : version(rev | ((uint32_t)min << 16)  | ((uint64_t)maj << 32)) {}
+
+       const uint64_t version;
+    };
+
+    bool operator>=(const lvm_version& a, const lvm_version& b);
+
+    class LvmCapabilities : public boost::noncopyable
+    {
+    public:
+       static LvmCapabilities* get_lvm_capabilities();
 
-       bool valid() const { return version != 0; }
+       bool get_time_support() const;
+       string get_ignoreactivationskip() const;
 
-       friend bool operator>=(const lvm_version& a, const lvm_version& b);
     private:
-       const uint64_t version;
+       LvmCapabilities();
+
+       // empty or " -K" if lvm supports ignore activation skip flag
+       string ignoreactivationskip;
+       // true if lvm2 supports time info stored in metadata
+       bool time_support;
     };
 
 
@@ -89,10 +102,8 @@ namespace snapper
 
     private:
 
-       static lvm_version getLvmVersion();
-
        const string mount_type;
-       const lvm_version version;
+       const LvmCapabilities* caps;
 
        bool detectThinVolumeNames(const MtabData& mtab_data);
        void activateSnapshot(const string& vg_name, const string& lv_name) const;
@@ -106,7 +117,6 @@ namespace snapper
 
        vector<string> mount_options;
 
-       string ignoreactivationskip;
     };
 
 }