]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | require_once("Exception.php"); | |
3 | require_once("openmediavault/util.inc"); | |
4 | ||
5 | /** | |
6 | * XXX detailed description | |
7 | * | |
8 | * @author XXX | |
9 | * @version XXX | |
10 | * @copyright XXX | |
11 | */ | |
12 | class OMVModuleZFSDataset { | |
13 | // Attributes | |
14 | /** | |
15 | * Name of Dataset | |
16 | * | |
17 | * @var string $name | |
18 | * @access private | |
19 | */ | |
20 | private $name; | |
21 | /** | |
22 | * Mountpoint of the Dataset | |
23 | * | |
24 | * @var string $mountPoint | |
25 | * @access private | |
26 | */ | |
27 | private $mountPoint; | |
28 | ||
29 | /** | |
30 | * List of features assigned to the Dataset | |
31 | * | |
32 | * @var array $features | |
33 | * @access private | |
34 | */ | |
35 | private $features; | |
36 | ||
37 | // Associations | |
38 | // Operations | |
39 | ||
40 | /** | |
41 | * Constructor | |
42 | * | |
43 | * @param string $name Name of the new Dataset | |
44 | * @param array $features An array of features (strings) in the form <key>=<value> to set when creating the Dataset | |
45 | * @throws OMVModuleZFSException | |
46 | * | |
47 | */ | |
48 | public function __construct($name, array $features = null) { | |
49 | $cmd = "zfs create "; | |
50 | if (isset($features)) { | |
51 | foreach ($features as $feature) { | |
52 | $cmd .= "-o " . $feature . " "; | |
53 | } | |
54 | } | |
55 | $cmd .= $name . " 2>&1"; | |
56 | OMVUtil::exec($cmd,$out,$res); | |
57 | if ($res == 1) { | |
58 | throw new OMVModuleZFSException(implode("\n", $out)); | |
59 | } | |
60 | unset($res); | |
61 | $this->name = $name; | |
62 | if (isset($features)) { | |
63 | $this->features = $features; | |
64 | foreach ($features as $feature) { | |
65 | if (preg_match('/^mountpoint\=(.*)$/', $feature, $res)) { | |
66 | $this->mountPoint = $res[1]; | |
67 | continue; | |
68 | } | |
69 | } | |
70 | } else { | |
71 | $this->features = array(); | |
72 | $this->mountPoint = "/" . $name; | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
77 | * Return name of the Dataset | |
78 | * | |
79 | * @return string $name | |
80 | * @access public | |
81 | */ | |
82 | public function getName() { | |
83 | return $this->name; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Get the mountpoint of the Dataset | |
88 | * | |
89 | * @return string $mountPoint | |
90 | * @access public | |
91 | */ | |
92 | public function getMountPoint() { | |
93 | return $this->mountPoint; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Get an array of features associated with the Dataset | |
98 | * | |
99 | * @return array $features | |
100 | * @access public | |
101 | */ | |
102 | public function getFeatures() { | |
103 | return $this->features; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Sets a number of Dataset properties. If a property is already set it will be updated with the new value. | |
108 | * | |
109 | * @param array $features An array of strings in format <key>=<value> | |
110 | * @return void | |
111 | * @access public | |
112 | */ | |
113 | public function setFeatures($features) { | |
114 | foreach ($features as $newfeature) { | |
115 | $cmd = "zfs set " . $newfeature . " " . $this->name; | |
116 | OMVUtil::exec($cmd,$out,$res); | |
117 | if ($res == 1) { | |
118 | throw new OMVModuleZFSException(implode("\n", $out)); | |
119 | } | |
120 | $tmp = explode("=", $newfeature); | |
121 | $newfeaturek = $tmp[0]; | |
122 | $found = false; | |
123 | for ($i=0; $i<count($this->features); $i++) { | |
124 | $tmp = explode("=", $this->features[$i]); | |
125 | $oldfeaturek = $tmp[0]; | |
126 | if (strcmp($newfeaturek, $oldfeaturek) == 0) { | |
127 | $this->features[$i] = $newfeature; | |
128 | $found = true; | |
129 | continue; | |
130 | } | |
131 | } | |
132 | if (!$found) { | |
133 | array_push($this->features, $newfeature); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Destroy the Dataset. | |
140 | * | |
141 | */ | |
142 | public function destroy() { | |
143 | $cmd = "zfs destroy " . $this->name; | |
144 | OMVUtil::exec($cmd,$out,$res); | |
145 | if ($res == 1) { | |
146 | throw new OMVModuleZFSException(implode("\n", $out)); | |
147 | } | |
148 | } | |
149 | ||
150 | } | |
151 | ||
152 | ?> |