]> git.datanom.net - omvzfs.git/blame - OMVModuleZPool.php
Latest version
[omvzfs.git] / OMVModuleZPool.php
CommitLineData
76e64919
MR
1<?php
2/*
3 * OMVZModulePool.php
4 *
5 * Copyright 2013 Michael Rasmussen <mir@datanom.net>
6 *
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.
11 *
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.
16 *
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,
20 * MA 02110-1301, USA.
21 */
22
23/**
24 * @class OMVModuleZPool.
25 */
26
27require 'OMVModulePoolType.php';
28
29class OMVModuleZPool {
30
31 private $params;
32 private $log;
33 private $mirror_log;
34 private $cache;
35 private $spare;
36
37 /**
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.
50 * @throws Exception.
51 */
52 public function __construct(array $params = array(), $debug = false) {
53 $this->params = array(
54 'atime' => 'on',
55 'compress' => 'off',
56 'dedub' => 'off',
57 'name' => '',
58 'size' => '0M',
59 'sync' => 'off',
60 'type' => OMVModulePoolType::OMVModulePoolType_TYPE_NONE,
61 'vdevs' => 1
62 );
63
64 foreach ($params as $key => $value) {
65 if (array_key_exists($key, $this->params)) {
66 $this->validate($key, $value);
67 $this->params[$key] = $value;
68 } else {
69 throw new Exception("$key: Not valid parameter");
70 }
71 }
72
73 if ($debug) {
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);
78 }
79 }
80 }
81
82 /**
83 * Create pool.
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.
92 * @throws Exception.
93 */
94 public function createPool(array $disks, array $options = array()) {
95 $disk_num = count($disks);
96 if ($disk_num < 1) {
97 throw new Exception("Pool must have at least 1 disk");
98 };
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)");
104 }
105 foreach ($options as $key => $value) {
106 $this->validate($key, $value, OMVPoolType::OMV_TYPE_NONE);
107 }
108 break;
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");
115 }
116 break;
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");
123 }
124 break;
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");
131 }
132 break;
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");
139 }
140 break;
141 }
142 }
143
144 private function validate($key, $value, $type = -1) {
145 if ($type > -1) {
146 switch ($type) {
147 case OMVModulePoolType::OMVModulePoolType_TYPE_NONE:
148 print "Create basic pool\n";
149 break;
150 case OMVModulePoolType::OMVModulePoolType_TYPE_MIRROR:
151 print "Create mirrored pool\n";
152 break;
153 case OMVModulePoolType::OMVModulePoolType_TYPE_RAIDZ1:
154 print "Create raidz1 pool\n";
155 break;
156 case OMVModulePoolType::OMVModulePoolType_TYPE_RAIDZ2:
157 print "Create raidz2 pool\n";
158 break;
159 case OMVModulePoolType::OMVModulePoolType_TYPE_RAIDZ3:
160 print "Create raidz3 pool\n";
161 break;
162 default:
163 throw new Exception("$type: Unknown OMVModulePoolType");
164 }
165 } else {
166 switch ($key) {
167 case 'atime':
168 case 'compress':
169 case 'dedub':
170 case 'sync':
171 if ($value != 'on' && $value != 'off') {
172 throw new Exception("$key: Value must be 'on' or 'off' found '$value'");
173 }
174 break;
175 case 'size':
176 if (! preg_match('/^\d+(M|G)$/', $value)) {
177 throw new Exception("$key: Value must have format '100M' or '100G' found '$value'");
178 }
179 break;
180 case 'type':
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'");
184 }
185 break;
186 case 'vdevs':
187 if (! preg_match('/^\d+$/', $value) || $value < 1) {
188 throw new Exception("$key: Value must be positive int found '$value'");
189 }
190 }
191 }
192 }
193}
This page took 0.052205 seconds and 5 git commands to generate.