]>
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
47 public function __construct($pool, OMVModuleZFSVdevType
$type, array $disks) {
49 case OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
:
51 case OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
:
52 if (count($disks) < 2)
53 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
55 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ1
:
56 if (count($disks) < 3)
57 throw new OMVModuleZFSException("A Raidz1 must contain at least 3 disks");
59 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ2
:
60 if (count($disks) < 4)
61 throw new OMVModuleZFSException("A Raidz2 must contain at least 4 disks");
63 case OMVModuleZFSVdevType
::OMVMODULEZFSRAIDZ3
:
64 if (count($disks) < 5)
65 throw new OMVModuleZFSException("A Raidz3 must contain at least 5 disks");
69 $this->disks
= $disks;
74 * Helper function to execute an external program.
75 * @param command The command that will be executed.
76 * @param output If the output argument is present, then the specified
77 * array will be filled with every line of output from the command.
78 * Trailing whitespace, such as \n, is not included in this array.
79 * @return The exit code of the command.
80 * @throws E_EXEC_FAILED
82 private function exec($command, &$output = NULL) {
83 OMVUtil
::exec($command, $output, $result);
87 private function safeExec($disk, $add = true, $change = false) {
91 if ($change ||
$this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
92 $disk1 = $this->disks
[0];
93 $result = exec("zpool attach {$this->pool} $disk1 $disk", $err);
95 $result = exec("zpool add {$this->pool} $disk", $err);
98 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
) {
99 $disk1 = $this->disks
[0];
100 if (($res = exec("zpool offline {$this->pool} $disk", $err)) > 0)
103 $result = exec("zpool detach {$this->pool} $disk", $err);
106 $err = "Cannot remove $disk from {$this->pool}";
110 return ($result) ?
$err : null;
114 * Add a disk to this Vdev
116 * @param $disk the disk
117 * @throws OMVModuleZFSException
120 public function addDisk($disk, $changeType = false) {
121 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN ||
122 $this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
123 throw new OMVModuleZFSException("A Raidz Vdev cannot be changed");
125 if (in_array($disk, $this->disks
))
126 throw new OMVModuleZFSException("$disk: Already part of Vdev");
128 if ($this->type
== OMVModuleZFSVdevType
::OMVMODULEZFSPLAIN
&&
129 count($this->disks
) < 2 && $changeType) {
130 $this->type
= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
;
133 if (($err = safeExec($disk, true, $changeType)) != null)
134 throw new OMVModuleZFSException($err);
136 array_push($this->disks
, $disk);
140 * Remove a disk from Vdev
142 * @param $disk disk to remove
143 * @throws OMVModuleZFSException
146 public function removeDisk($disk, $changeType = false) {
147 $new_disks = array();
149 if ($this->type
!= OMVModuleZFSVdevType
::OMVMODULEZFSMIRROR
)
150 throw new OMVModuleZFSException("Only inactive hot spares," .
151 "cache, top-level, or log devices can be removed");
153 if (count($this->disks
) < 3 && ! $changeType)
154 throw new OMVModuleZFSException("A mirror must contain at least 2 disks");
156 if (! in_array($disk, $this->disks
))
157 throw new OMVModuleZFSException("$disk: Not part of Vdev");
159 if (($err = safeExec($disk, false, $changeType)) != null)
160 throw new OMVModuleZFSException($err);
162 foreach ($this->disks
as $_disk) {
163 if (strcmp($_disk, $disk) != 0)
164 array_push($new_disks, $_disk);
168 $this->disks
= $new_disks;
174 * @return array with disks
177 public function getDisks() {
184 * @return string pool
187 public function getPool() {
This page took 0.215064 seconds and 6 git commands to generate.