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