]>
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, $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");
69 throw new OMVModuleZFSException("$type: Unknown zpool type");
72 $this->disks
= $disks;
77 * Helper function to execute an external program.
78 * @param command The command that will be executed.
79 * @param output If the output argument is present, then the specified
80 * array will be filled with every line of output from the command.
81 * Trailing whitespace, such as \n, is not included in this array.
82 * @return The exit code of the command.
83 * @throws E_EXEC_FAILED
85 private function exec($command, &$output = NULL) {
86 OMVUtil
::exec($command, $output, $result);
90 private function safeExec($disk, $add = true, $change = false) {
94 if ($change ||
$this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
95 $disk1 = $this->disks
[0];
96 $result = exec("zpool attach {$this->pool} $disk1 $disk", $err);
98 $result = exec("zpool add {$this->pool} $disk", $err);
101 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
102 $disk1 = $this->disks
[0];
103 if (($res = exec("zpool offline {$this->pool} $disk", $err)) > 0)
106 $result = exec("zpool detach {$this->pool} $disk", $err);
109 $err = "Cannot remove $disk from {$this->pool}";
113 return ($result) ?
$err : null;
117 * Add a disk to this Vdev
119 * @param $disk the disk
120 * @throws OMVModuleZFSException
123 public function addDisk($disk, $changeType = false) {
124 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN ||
125 $this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
126 throw new OMVModuleZFSException("A Raidz Vdev cannot be changed");
128 if (in_array($disk, $this->disks
))
129 throw new OMVModuleZFSException("$disk: Already part of Vdev");
131 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
&&
132 count($this->disks
) < 2 && $changeType) {
133 $this->type
= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
;
136 if (($err = safeExec($disk, true, $changeType)) != null)
137 throw new OMVModuleZFSException($err);
139 array_push($this->disks
, $disk);
143 * Remove a disk from Vdev
145 * @param $disk disk to remove
146 * @throws OMVModuleZFSException
149 public function removeDisk($disk, $changeType = false) {
150 $new_disks = array();
152 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
153 throw new OMVModuleZFSException("Only inactive hot spares," .
154 "cache, top-level, or log devices can be removed");
156 if (count($this->disks
) < 3 && ! $changeType)
157 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
159 if (! in_array($disk, $this->disks
))
160 throw new OMVModuleZFSException("$disk: Not part of Vdev");
162 if (($err = safeExec($disk, false, $changeType)) != null)
163 throw new OMVModuleZFSException($err);
165 foreach ($this->disks
as $_disk) {
166 if (strcmp($_disk, $disk) != 0)
167 array_push($new_disks, $_disk);
171 $this->disks
= $new_disks;
177 * @return array with disks
180 public function getDisks() {
187 * @return string pool
190 public function getPool() {
197 * @return OMVModuleZFSVdevType
200 public function getType() {
This page took 0.131364 seconds and 6 git commands to generate.