5 * Copyright 2013 Michael Rasmussen <mir@datanom.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 * @class OMVModuleZPool.
27 require 'OMVModulePoolType.php';
29 class OMVModuleZPool
{
38 * Constructor of class OMVModuleZPool.
39 * @param params Array which can contain all or a subset of
40 * the following fields:
41 * \em atime Either true or false. <b>Default is true</b>.
42 * \em compress Either true or false. <b>Default is false</b>.
43 * \em dedub Either true or false. <b>Default is false</b>.
44 * \em name Name for pool
45 * \em size Size of pool in either MB or GB. 100M or 100G.
46 * \em sync Either true or false. <b>Default is false</b>.
47 * \em type See OMVModulePoolType.
48 * \em vdevs Number of vdevs in pool. <b>Default is 1</b>.
49 * @param debug Print debug information to STDERR.
52 public function __construct(array $params = array(), $debug = false) {
53 $this->params
= array(
60 'type' => OMVModulePoolType
::OMVModulePoolType_TYPE_NONE
,
64 foreach ($params as $key => $value) {
65 if (array_key_exists($key, $this->params
)) {
66 $this->validate($key, $value);
67 $this->params
[$key] = $value;
69 throw new Exception("$key: Not valid parameter");
74 fprintf(STDERR
, "OMVModuleZPool instantiate with the following params\n");
75 foreach ($this->params
as $key => $value) {
76 $param = ($key == 'type') ? OMVModulePoolType
::toString($value) : $value;
77 fprintf(STDERR
, " %-8s => %s\n", $key, $param);
84 * @param disks Array of disks. Format: /dev/sdx or /dev/vdx.
85 * @param options Array which can contain all or a subset of
86 * the following fields:
87 * \em log Array which contains the following fields:
88 * \em disks Array of disks. Format: /dev/sdx or /dev/vdx.
89 * \em mirror Either true or false. <b>Default is false</b>.
90 * \em cache Disk to use for cache. <b>Default use internal</b>.
91 * \em spare Array of disks. Format: /dev/sdx or /dev/vdx.
94 public function createPool(array $disks, array $options = array()) {
95 $disk_num = count($disks);
97 throw new Exception("Pool must have at least 1 disk");
99 switch ($this->params
['type']) {
100 case OMVModulePoolType
::OMVModulePoolType_TYPE_NONE
:
101 print "Create basic pool\n";
102 if ($disk_num %
$this->params
['vdevs']) {
103 throw new Exception("$disk_num disk(s) != ".$this->params
['vdevs']." vdev(s)");
105 foreach ($options as $key => $value) {
106 $this->validate($key, $value, OMVPoolType
::OMV_TYPE_NONE
);
109 case OMVModulePoolType
::OMVModulePoolType_TYPE_MIRROR
:
110 print "Create mirrored pool\n";
111 if ($disk_num / $this->params
['vdevs'] < 2 ||
112 ($disk_num / $this->params
['vdevs']) %
$this->params
['vdevs']) {
113 throw new Exception("$disk_num disk(s) cannot be evenly distributed to ".
114 $this->params
['vdevs']." vdev(s) and form a proper mirror");
117 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ1
:
118 print "Create raidz1 pool\n";
119 if ($disk_num / $this->params
['vdevs'] < 3 ||
120 ($disk_num / $this->params
['vdevs']) %
$this->params
['vdevs']) {
121 throw new Exception("$disk_num disk and ".$this->params
['vdevs'].
122 " vdev(s) cannot provide mininum 3 disks per vdev for raidz1");
125 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ2
:
126 print "Create raidz2 pool\n";
127 if ($disk_num / $this->params
['vdevs'] < 5 ||
128 ($disk_num / $this->params
['vdevs']) %
$this->params
['vdevs']) {
129 throw new Exception("$disk_num disk and ".$this->params
['vdevs'].
130 " vdev(s) cannot provide mininum 5 disks per vdev for raidz2");
133 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ3
:
134 print "Create raidz3 pool\n";
135 if ($disk_num / $this->params
['vdevs'] < 8 ||
136 ($disk_num / $this->params
['vdevs']) %
$this->params
['vdevs']) {
137 throw new Exception("$disk_num disk and ".$this->params
['vdevs'].
138 " vdev(s) cannot provide mininum 8 disks per vdev for raidz3");
144 private function validate($key, $value, $type = -1) {
147 case OMVModulePoolType
::OMVModulePoolType_TYPE_NONE
:
148 print "Create basic pool\n";
150 case OMVModulePoolType
::OMVModulePoolType_TYPE_MIRROR
:
151 print "Create mirrored pool\n";
153 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ1
:
154 print "Create raidz1 pool\n";
156 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ2
:
157 print "Create raidz2 pool\n";
159 case OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ3
:
160 print "Create raidz3 pool\n";
163 throw new Exception("$type: Unknown OMVModulePoolType");
171 if ($value != 'on' && $value != 'off') {
172 throw new Exception("$key: Value must be 'on' or 'off' found '$value'");
176 if (! preg_match('/^\d+(M|G)$/', $value)) {
177 throw new Exception("$key: Value must have format '100M' or '100G' found '$value'");
181 if (OMVModulePoolType
::OMVModulePoolType_TYPE_NONE
> $value ||
182 OMVModulePoolType
::OMVModulePoolType_TYPE_RAIDZ3
< $value) {
183 throw new Exception("$key: Value must be type of OMVPoolType found '$value'");
187 if (! preg_match('/^\d+$/', $value) ||
$value < 1) {
188 throw new Exception("$key: Value must be positive int found '$value'");