1 <?php
3 class faiPartition extends plugin
4 {
5 var $attributes = array("cn","description","FAIfsCreateOptions","FAIfsOptions","FAIfsTuneOptions",
6 "FAIfsType","FAImountOptions","FAImountPoint","FAIpartitionNr","FAIpartitionFlags","FAIdiskType",
7 "FAIpartitionSize","FAIpartitionType","FAIstate", "bootable","resize", "preserve", "preserveType",
8 "encrypted");
10 var $cn = "";
11 var $description = "";
12 var $FAIfsCreateOptions = "";
13 var $FAIfsOptions = "";
14 var $FAIfsTuneOptions = "";
15 var $FAIfsType = "";
16 var $FAImountOptions = "";
17 var $FAImountPoint = "";
18 var $FAIpartitionNr = "undefined"; // Initial value for new partitions
19 var $FAIpartitionSize = "";
20 var $FAIpartitionType = "";
21 var $FAIstate = "";
22 var $FAIpartitionFlags = "";
24 var $FAIdiskType = "disk";
26 // Size options.
27 var $FAIpartitionSizeType = "fixed";
28 var $sizeStart = 0;
29 var $sizeStop = 0;
30 var $sizeStart_Type = "MB";
31 var $sizeStop_Type = "MB";
33 // Flags
34 var $bootable = false;
35 var $resize = false;
36 var $encrypted = false;
37 var $preserve = false;
38 var $preserveType = "always";
40 var $status = "";
41 var $raidDevices = array();
43 // Once we've exceeded the primary partition limit,
44 // hide the the 'primary' option from the select box.
45 var $disablePrimary = FALSE;
47 function __construct($config, $object, $parent,$type)
48 {
50 $this->parent = $parent;
51 $this->status = "new";
52 $this->FAIdiskType = $type;
54 // Check if we should be able to add primary partitions.
55 if(!$object || $object['FAIpartitionType'] == "logical"){
56 if($this->FAIdiskType == "disk"){
57 $types = array('logical' => array(), 'primary' => array());
58 foreach($this->parent->partitions as $key => $part){
59 $types[$part['FAIpartitionType']][$part['FAIpartitionNr']] = 1;
60 }
61 if(count($types['logical']) && count($types['primary']) >= 3){
62 $this->disablePrimary = TRUE;
63 }elseif(count($types['logical']) >= 4){
64 $this->disablePrimary = TRUE;
65 }
66 }
67 }
69 // Load attributes from existing partition
70 if($object){
71 foreach($this->attributes as $attr){
72 if(isset($object[$attr])){
73 $this->$attr = $object[$attr];
74 }
75 }
77 $this->status = $object['status'];
79 if($type == "disk" || $type =="lvm"){
81 /* Prepare size attribute
82 * This attribute may represent a range, a fixed value
83 * or a percentage.
84 * fixed is just a number * 500MB
85 * range * 500MB-1TB
86 * remaining * -
87 */
88 // Fixed
89 if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
90 $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
91 $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
92 $this->FAIpartitionSizeType = "fixed";
93 }else
95 // Dynamic range
96 if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)-[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
97 $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\1",$this->FAIpartitionSize);
98 $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\2",$this->FAIpartitionSize);
99 $this->sizeStop = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
100 $this->sizeStop_Type = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
101 $this->FAIpartitionSizeType = "dynamic";
102 }else
104 // Dynamic range
105 if(preg_match("/^\-$/",$this->FAIpartitionSize)){
106 $this->FAIpartitionSizeType = "remaining";
108 }
110 /* Check for encrypted partitions
111 */
112 if(preg_match("/:encrypt$/",$this->FAImountPoint)){
113 $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
114 $this->encrypted = TRUE;
115 }
117 }elseif($type == "raid"){
119 // Extract raid devices out of the partition size attribute.
120 $usedDisks = split(",",$this->FAIpartitionSize);
121 foreach($usedDisks as $disk){
122 $name = preg_replace("/:.*$/","",$disk);
123 $spare = preg_match("/:spare/",$disk);
124 $missing = preg_match("/:missing/",$disk);
125 if(!empty($name)){
126 $this->raidDevices[$name] =
127 array(
128 "name" => $name,
129 "spare" => $spare,
130 "missing" => $missing);
131 }
132 }
133 }
134 }
135 }
137 function execute()
138 {
139 plugin::execute();
140 $smarty = get_smarty();
142 // Remove partition
143 if(isset($_POST['addPhysicalPartition']) && isset($_POST['physicalPartitionAdd'])){
144 $name = $_POST['physicalPartitionAdd'];
145 $this->raidDevices[$name] = array("name" => $name,"spare"=>false,"missing"=>false);
146 }
148 // Create a list of selectable partition types
149 if($this->FAIdiskType == "disk"){
151 if($this->disablePrimary){
152 $types = array(
153 "logical" => _("Logical"));
154 }else{
155 $types = array(
156 "primary" => _("Primary"),
157 "logical" => _("Logical"));
158 }
160 }elseif($this->FAIdiskType == "raid"){
161 $types = array(
162 "raid0" => _("RAID 0"),
163 "raid1" => _("RAID 1"),
164 "raid5" => _("RAID 5"),
165 "raid6" => _("RAID 6"));
166 }else{
167 $types = "";
168 }
170 // Create a list of all size options
171 $partitionSizeTypes = array(
172 "fixed" => _("fixed"),
173 "dynamic" => _("dynamic"),
174 "remaining" => _("remaining space")
175 );
177 // Create a list of all size options
178 $sizeTypes = array(
179 "KB" => _("KB"),
180 "MB" => _("MB"),
181 "GB" => _("GB"),
182 "TB" => _("TB"),
183 "PB" => _("PB"),
184 "%" => _("%")
185 );
187 // Preserve types
188 $preserveTypes = array(
189 "always" => _("always"),
190 "reinstall" => _("reinstall"));
192 // File system types.
193 $FAIfsTypes = array(
194 "swap" => _("swap space"),
195 "ext2" => _("ext2"),
196 "ext3" => _("ext3"),
197 "ext4" => _("ext4"),
198 "reiserfs" => _("reiser fs"),
199 "xfs" => _("xfs"),
200 "btrfs" => _("btrfs"));
202 $smarty->assign("partitionTypes", $types);
203 $smarty->assign("partitionSizeTypes", $partitionSizeTypes);
204 $smarty->assign("FAIpartitionSizeType", $this->FAIpartitionSizeType);
205 $smarty->assign("sizeTypes", $sizeTypes);
207 $smarty->assign("sizeStart_Type", $this->sizeStart_Type);
208 $smarty->assign("sizeStop_Type", $this->sizeStop_Type);
209 $smarty->assign("sizeStart", $this->sizeStart);
210 $smarty->assign("sizeStop", $this->sizeStop);
212 $smarty->assign("preserveTypes", $preserveTypes);
213 $smarty->assign("preserveType", $this->preserveType);
215 $smarty->assign("FAIfsTypes", $FAIfsTypes);
216 $smarty->assign("cn", $this->cn);
217 $smarty->assign("freeze", preg_match("/freeze/i",$this->parent->FAIstate));
219 $smarty->assign("plist",$this->getRaidlist());
220 $smarty->assign("physicalPartitionList",$this->getPartitionlist());
221 $smarty->assign("disablePrimary", $this->disablePrimary);
223 foreach($this->attributes as $attr){
224 $smarty->assign($attr,$this->$attr);
225 }
226 return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
227 }
230 /* Returns a list of all partitions that are useable
231 * for raid arrays.
232 */
233 function getPartitionList()
234 {
235 $array = array();
236 foreach($this->parent->parent->disks as $disk){
237 if($disk['FAIdiskType'] != "raid"){
238 foreach($disk['partitions'] as $key => $part){
239 $name = $part['cn'];
240 if(!isset($this->raidDevices[$name])){
241 $array[$name] = $name." (".$disk['cn'].")";
242 }
243 }
244 }
245 }
246 return($array);
247 }
250 /* Creates a human readable list of all used partitions
251 * of a raid device.
252 */
253 function getRaidList()
254 {
255 $divlist = new divSelectBox("RaidList");
256 $disks = $this->parent->parent->disks;
257 $objs = array();
258 foreach($disks as $disk){
259 if($disk['FAIdiskType'] != "raid"){
260 foreach($disk['partitions'] as $id => $part){
261 $objs[$part['cn']] = $part;
262 }
263 }
264 }
266 $list = array();
267 foreach($this->raidDevices as $device){
268 $str = $name = $device['name'];
269 $str = str_pad($str,25," ");
270 if(isset($objs[$device['name']]['FAIpartitionSize'])){
271 $str .= _("Size").": ";
272 $str .= $objs[$device['name']]['FAIpartitionSize'];
273 }
274 $str = str_pad($str,60," ");
275 $opt = "";
276 if($device['spare']){
277 $opt.= " "._("spare")." ";
278 }
279 if($device['missing']){
280 $opt.= " "._("missing")." ";
281 }
282 if($opt){
283 $str .= " "._("Options").": ".$opt;
284 }
285 $str = str_pad($str,80," ");
286 $list[$name] = preg_replace("/ /"," ",$str);
287 }
288 return($list);
289 }
291 function save_object()
292 {
293 if(isset($_POST['faiPartition'])){
294 foreach($this->attributes as $attr){
295 if(isset($_POST[$attr])){
296 $this->$attr = get_post($attr);
297 }
298 }
299 foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
300 if(isset($_POST[$attr])){
301 $this->$attr = get_post($attr);
302 }
303 }
304 foreach(array("bootable","preserve","resize","encrypted") as $attr){
305 if(isset($_POST[$attr])){
306 $this->$attr = TRUE;
307 }else{
308 $this->$attr = FALSE;
309 }
310 }
312 // Allow user defined partition names for lvm disks.
313 if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
314 $this->cn = get_post('cn');
315 }
317 // Remove partition
318 if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
319 foreach($_POST['physicalPartition'] as $key){
320 if(isset($this->raidDevices[$key])){
321 unset($this->raidDevices[$key]);
322 }
323 }
324 }
326 // Toggle spare flag for partition entries
327 if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
328 $this->raidDevices[$_POST['physicalPartition']]['spare'] =
329 !$this->raidDevices[$_POST['physicalPartition']]['spare'];
330 }
332 // Toggle missing flag for partition entries
333 if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
334 $this->raidDevices[$_POST['physicalPartition']]['missing'] =
335 !$this->raidDevices[$_POST['physicalPartition']]['missing'];
336 }
337 }
338 }
341 function check()
342 {
343 $msgs = plugin::check();
345 // Check the given partition size.
346 if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
347 if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){
348 if(!is_numeric($this->sizeStart)){
349 $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
350 }
351 }
352 if($this->FAIpartitionSizeType == "dynamic"){
353 if(!is_numeric($this->sizeStop)){
354 $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
355 }
357 $mp = array(
358 "%" => 1,
359 "KB" => pow(1024,0),
360 "MB" => pow(1024,1),
361 "GB" => pow(1024,2),
362 "TB" => pow(1024,3),
363 "PB" => pow(1024,4));
364 $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
365 $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
366 if($res1 > $res2){
367 $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
368 }
369 }
370 }
372 // Add raid checks
373 if($this->FAIdiskType == "raid"){
374 if(count($this->raidDevices) < 2){
375 $msgs[] = _("Raid arrays must contain at least two partitions!");
376 }else if($this->FAIpartitionType == "raid0" && count($this->raidDevices) != 2){
377 $msgs[] = _("Raid 0 arrays can only be realized with a combination of two partitions!");
378 }
379 }
381 // check mount point
382 if($this->FAIfsType != "swap"){
383 if(!preg_match("#^/#",$this->FAImountPoint)){
384 $msgs[] = msgPool::invalid(_("Mount point"));
385 }
386 }
388 return($msgs);
389 }
392 function save()
393 {
394 $ret = array();
395 foreach($this->attributes as $attr){
396 $ret[$attr] = $this->$attr;
397 }
399 // Save partition size
400 if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
401 switch($this->FAIpartitionSizeType){
402 case 'fixed' :
403 $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break;
404 case 'dynamic' :
405 $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".
406 $this->sizeStop.$this->sizeStop_Type;break;
407 case 'remaining' :
408 $ret['FAIpartitionSize'] = "-";break;
409 default: trigger_error("Unknown partition size!");
410 }
412 // Add encryption flag to partition mount point
413 if($this->encrypted){
414 $ret['FAImountPoint'] .= ":encrypt";
415 }
417 }elseif($this->FAIdiskType == "raid"){
419 // Save selected raid partitions in FAIpartitionSize
420 $ret['FAIpartitionSize'] = "";
421 foreach($this->raidDevices as $device){
422 $ret['FAIpartitionSize'] .= $device['name'];
423 if($device['spare']){
424 $ret['FAIpartitionSize'] .= ":spare";
425 }
426 if($device['missing']){
427 $ret['FAIpartitionSize'] .= ":missing";
428 }
429 $ret['FAIpartitionSize'] .= ",";
430 }
431 $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
432 }
433 $ret['status'] = $this->status;
435 if($this->FAIfsType == "swap"){
436 $ret['FAImountPoint'] = "swap";
437 }
439 return($ret);
440 }
441 }
442 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
443 ?>