]>
git.datanom.net - omvzfs.git/blob - src/Vdev.php
2 require_once("Exception.php");
3 require_once("VdevType.php");
4 require_once("openmediavault/util.inc");
9 * @author Michael Rasmussen
11 * @copyright Michael Rasmussen <mir@datanom.net>
13 class OMVModuleZFSVdev
{
26 * @var string $pool pool name
34 * @var OMVModuleZFSVdevType $type Vdev type
44 * @param $pool pool this mirror belongs to
45 * @throws OMVModuleZFSException
48 public function __construct($pool, OMVModuleZFSVdevType
$type, array $disks) {
50 case OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
:
52 case OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
:
53 if (count($disks) < 2)
54 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
56 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ1
:
57 if (count($disks) < 3)
58 throw new OMVModuleZFSException("A Raidz1 must contain at least 3 disks");
60 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ2
:
61 if (count($disks) < 4)
62 throw new OMVModuleZFSException("A Raidz2 must contain at least 4 disks");
64 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ3
:
65 if (count($disks) < 5)
66 throw new OMVModuleZFSException("A Raidz3 must contain at least 5 disks");
70 $this->disks
= $disks;
75 * Helper function to execute an external program.
76 * @param command The command that will be executed.
77 * @param output If the output argument is present, then the specified
78 * array will be filled with every line of output from the command.
79 * Trailing whitespace, such as \n, is not included in this array.
80 * @return The exit code of the command.
81 * @throws E_EXEC_FAILED
83 private function exec($command, &$output = NULL) {
84 OMVUtil
::exec($command, $output, $result);
88 private function safeExec($disk, $add = true, $change = false) {
92 if ($change ||
$this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
93 $disk1 = $this->disks
[0];
94 $result = exec("zpool attach {$this->pool} $disk1 $disk", $err);
96 $result = exec("zpool add {$this->pool} $disk", $err);
99 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
100 $disk1 = $this->disks
[0];
101 if (($res = exec("zpool offline {$this->pool} $disk", $err)) > 0)
104 $result = exec("zpool detach {$this->pool} $disk", $err);
107 $err = "Cannot remove $disk from {$this->pool}";
111 return ($result) ?
$err : null;
115 * Add a disk to this Vdev
117 * @param $disk the disk
118 * @throws OMVModuleZFSException
121 public function addDisk($disk, $changeType = false) {
122 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN ||
123 $this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
124 throw new OMVModuleZFSException("A Raidz Vdev cannot be changed");
126 if (in_array($disk, $this->disks
))
127 throw new OMVModuleZFSException("$disk: Already part of Vdev");
129 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
&&
130 count($this->disks
) < 2 && $changeType) {
131 $this->type
= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
;
134 if (($err = safeExec($disk, true, $changeType)) != null)
135 throw new OMVModuleZFSException($err);
137 array_push($this->disks
, $disk);
141 * Remove a disk from Vdev
143 * @param $disk disk to remove
144 * @throws OMVModuleZFSException
147 public function removeDisk($disk, $changeType = false) {
148 $new_disks = array();
150 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
151 throw new OMVModuleZFSException("Only inactive hot spares," .
152 "cache, top-level, or log devices can be removed");
154 if (count($this->disks
) < 3 && ! $changeType)
155 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
157 if (! in_array($disk, $this->disks
))
158 throw new OMVModuleZFSException("$disk: Not part of Vdev");
160 if (($err = safeExec($disk, false, $changeType)) != null)
161 throw new OMVModuleZFSException($err);
163 foreach ($this->disks
as $_disk) {
164 if (strcmp($_disk, $disk) != 0)
165 array_push($new_disks, $_disk);
169 $this->disks
= $new_disks;
175 * @return array with disks
178 public function getDisks() {
185 * @return string pool
188 public function getPool() {
195 * @return OMVModuleZFSVdevType
198 public function getType() {
This page took 0.118641 seconds and 6 git commands to generate.