summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0012-virtual-media.3-Add-NfsShare-based-on-NetDevShare.patch
blob: a3a64087b85b1687660f3bd04d28f4221c4955b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
From 190e85aaf6cfa553ce31467a7ef57a1ae13305a5 Mon Sep 17 00:00:00 2001
From: Alexandr Ilenko <AIlenko@IBS.RU>
Date: Mon, 20 Jun 2022 09:46:43 +0300
Subject: [PATCH 12/20] virtual-media.3: Add: "NfsShare", based on
 "NetDevShare"

---
 src/netdev.hpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/src/netdev.hpp b/src/netdev.hpp
index 47d39a1..fc1b907 100644
--- a/src/netdev.hpp
+++ b/src/netdev.hpp
@@ -115,3 +115,69 @@ class SmbShare : public NetDevShare
         return ec;
     }
 };
+
+class NfsShare final: public NetDevShare
+{
+    static constexpr const char* mountType = "nfs";
+  public:
+    explicit NfsShare(const fs::path& mountDir): NetDevShare(mountDir)
+    {
+    }
+
+    virtual const char* getMountType() const override
+    {
+        return mountType;
+    }
+
+    bool mount(const fs::path& remote, bool rw,
+               const std::unique_ptr<utils::CredentialsProvider>& credentials) override
+    {
+        LogMsg(Logger::Debug, "Trying to mount remote : ", remote);
+
+        try {
+            auto mountUri = parseMountUri(remote);
+            const std::string params = "nolock";
+            const std::string& mountAddress = mountUri.first;
+            const std::string& mountPath = mountUri.second;
+            auto options = params + ",addr=" + mountAddress;
+
+            LogMsg(Logger::Debug, "Mounting URI: ", mountPath);
+            LogMsg(Logger::Debug, "With options: ", options);
+
+            auto ec = ::mount((":" + mountPath).c_str(), mountDir.c_str(),
+                              mountType, 0, options.c_str());
+            utils::secureCleanup(options);
+
+            if (ec)
+            {
+                LogMsg(Logger::Error, "Mount failed with ec = ", ec,
+                       " errno = ", errno);
+                return false;
+            }
+        } catch (const std::exception& ex) {
+
+            LogMsg(Logger::Error, "Mount failed: ", ex.what());
+            return false;
+        }
+        return true;
+    }
+  private:
+    using MountAddress = std::string;
+    using MountPath = std::string;
+
+    static std::pair<MountAddress, MountPath> parseMountUri(const std::string uri)
+    {
+        // All paths start with '//', so seek a cursor to the 2 positions
+        // forward.
+        constexpr unsigned char startAddressPos = 2U;
+        auto hostnameEndPos = uri.find('/', startAddressPos);
+        if (hostnameEndPos == std::string::npos) {
+            throw std::invalid_argument("Mount Uri");
+        }
+        return {
+            uri.substr(startAddressPos, hostnameEndPos - startAddressPos),
+            uri.substr(hostnameEndPos),
+        };
+    }
+
+};
-- 
2.35.1