Code

9d83e2bb510af0e90310d3aad58bc938d2dde1ee
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPartition.inc
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 = "";
23   
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";
32   
33   //  Flags 
34   var $bootable = false; 
35   var $resize = false;
36   var $encrypted = false;
37   var $preserve = false;
38   var $preserveType = "always";
40   var $raidDevices = array();
42   // Once we've exceeded the primary partition limit,
43   //  hide the the 'primary' option from the select box.
44   var $disablePrimary = FALSE; 
46   function __construct($config, $object, $parent,$type)
47   {
49     $this->parent = $parent;
50     $this->FAIdiskType = $type;
52     // Check if we should be able to add primary partitions.
53     if(!$object || $object['FAIpartitionType'] == "logical"){
54       if($this->FAIdiskType == "disk"){
55         $types = array('logical' => array(), 'primary' => array());
56         foreach($this->parent->partitions as $key => $part){
57           $types[$part['FAIpartitionType']][$part['FAIpartitionNr']] = 1;
58         }
59         if(count($types['logical']) && count($types['primary']) >= 3){
60           $this->disablePrimary = TRUE;
61         }elseif(count($types['logical']) >= 4){
62           $this->disablePrimary = TRUE;
63         }
64       } 
65     } 
67     // Load attributes from existing partition 
68     if($object){
69       foreach($this->attributes as $attr){
70         if(isset($object[$attr])){
71           $this->$attr = $object[$attr];
72         }
73       }
75       if($type == "disk" || $type =="lvm"){
77         /* Prepare size attribute 
78          * This attribute may represent a range, a fixed value 
79          *  or a percentage.
80          * fixed is just a number   * 500MB
81          * range                    * 500MB-1TB
82          * remaining                * -
83          */
84         // Fixed
85         if(preg_match("/^[0-9]{1,}(KB|MB|GB|TB|PB|%|)$/",$this->FAIpartitionSize)){
86           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
87           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
88           $this->FAIpartitionSizeType = "fixed";
89         }else
91         // Dynamic range
92         if(preg_match("/^[0-9]{1,}(KB|MB|GB|TB|PB|%|)-[0-9]{1,}(KB|MB|GB|TB|PB|%|)$/",$this->FAIpartitionSize)){
93           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\1",$this->FAIpartitionSize);
94           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\2",$this->FAIpartitionSize);
95           $this->sizeStop = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
96           $this->sizeStop_Type = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
97           $this->FAIpartitionSizeType = "dynamic";
98         }else
100         // Dynamic range
101         if(preg_match("/^(0|)\-$/",$this->FAIpartitionSize)){
102           $this->FAIpartitionSizeType = "remaining";
103         }
105         // Ensure that we've a valid value type selected.
106         if(!preg_match("/(KB|MB|GB|TB|PB|%)/",$this->sizeStart_Type)){
107           $this->sizeStart_Type = "MB";
108         }
109         if(!preg_match("/(KB|MB|GB|TB|PB|%)/",$this->sizeStop_Type)){
110           $this->sizeStop_Type = "MB";
111         }
113         /* Check for encrypted partitions
114          */
115         if(preg_match("/:encrypt$/",$this->FAImountPoint)){
116           $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
117           $this->encrypted = TRUE;
118         }
119     
120       }elseif($type == "raid"){
121    
122         // Extract raid devices out of the partition size attribute. 
123         $usedDisks = explode(",",$this->FAIpartitionSize);
124         foreach($usedDisks as $disk){
125           $name = preg_replace("/:.*$/","",$disk);
126           $spare = preg_match("/:spare/",$disk);
127           $missing = preg_match("/:missing/",$disk);
128           if(!empty($name)){
129             $this->raidDevices[$name] = 
130               array(
131                   "name" => $name, 
132                   "spare" => $spare, 
133                   "missing" => $missing);
134           }
135         }
136       }
137     }
138   }
140   function execute()
141   {
142     plugin::execute();
143     $smarty = get_smarty();
145     // Remove partition
146     if(isset($_POST['addPhysicalPartition']) && isset($_POST['physicalPartitionAdd'])){
147       $name = $_POST['physicalPartitionAdd'];
148       $this->raidDevices[$name] = array("name" => $name,"spare"=>false,"missing"=>false);     
149     }
151     // Create a list of selectable partition types
152     if($this->FAIdiskType == "disk"){
154       if($this->disablePrimary){
155         $types  = array(
156             "logical" => _("Logical"));
157       }else{
158         $types  = array(
159             "primary" => _("Primary"),
160             "logical" => _("Logical"));
161       }
163     }elseif($this->FAIdiskType == "raid"){
164       $types  = array(
165           "raid0" => _("RAID 0"),
166           "raid1" => _("RAID 1"),
167           "raid5" => _("RAID 5"),
168           "raid6" => _("RAID 6"));
169     }else{
170       $types = "";
171     }
172      
173     // Create a list of all size options
174     $partitionSizeTypes  = array(
175         "fixed"     => _("fixed"),
176         "dynamic"     => _("dynamic"),
177         "remaining" => _("remaining space")
178         );
180     // Create a list of all size options
181     $sizeTypes  = array(
182         "KB"      => _("KB"),
183         "MB"      => _("MB"),
184         "GB"      => _("GB"),
185         "TB"      => _("TB"),
186         "PB"      => _("PB"),
187         "%"      => _("%")
188         );
190     // Preserve types 
191     $preserveTypes = array(
192         "always" => _("always"),
193         "reinstall" => _("reinstall"));
195     // File system types.  
196     $FAIfsTypes = array(
197         "swap" => _("swap space"),
198         "ext2" => _("ext2"),
199         "ext3" => _("ext3"),
200         "ext4" => _("ext4"),
201         "reiserfs" => _("reiser fs"),
202         "xfs" => _("xfs"),
203         "btrfs" => _("btrfs"));
205     $smarty->assign("partitionTypes", $types);
206     $smarty->assign("partitionSizeTypes", $partitionSizeTypes);
207     $smarty->assign("FAIpartitionSizeType", $this->FAIpartitionSizeType);
208     $smarty->assign("sizeTypes", $sizeTypes);
210     $smarty->assign("sizeStart_Type", $this->sizeStart_Type);
211     $smarty->assign("sizeStop_Type", $this->sizeStop_Type);
212     $smarty->assign("sizeStart", $this->sizeStart);
213     $smarty->assign("sizeStop", $this->sizeStop);
215     $smarty->assign("preserveTypes", $preserveTypes);
216     $smarty->assign("preserveType", $this->preserveType);
218     $smarty->assign("FAIfsTypes", $FAIfsTypes);
219     $smarty->assign("cn", $this->cn);
220     $smarty->assign("freeze", preg_match("/freeze/i",$this->parent->FAIstate));
222     $smarty->assign("plist",$this->getRaidlist());
223     $smarty->assign("physicalPartitionList",$this->getPartitionlist());
224     $smarty->assign("disablePrimary", $this->disablePrimary);
226     foreach($this->attributes as $attr){
227       $smarty->assign($attr,$this->$attr);
228     }
229     return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
230   }
232   
233   /* Returns a list of all partitions that are useable 
234    *  for raid arrays.
235    */
236   function getPartitionList()
237   {
238     $may = $used = array();  
239     foreach($this->parent->parent->disks as $disk){
241       // Skip ourselves 
242       if($disk['cn'] == $this->parent->DISKcn) continue;
244       // Add partition from lvm combinations 
245       if($disk['FAIdiskType'] == "lvm"){
246         $used = array_merge($used,$disk['FAIlvmDevice']);
247       }
249       foreach($disk['partitions'] as $key => $part){
251         // Add disks of raid arrays, to the used list.
252         if($disk['FAIdiskType'] == "raid"){
253           foreach(explode(",",$part['FAIpartitionSize']) as $rDevice){
254             $used[] = preg_replace("/:.*$/i","",$rDevice);
255           }
256         }
258         // Collect all available disks 
259         if($disk['FAIdiskType'] == "disk"){
260           $name = $part['cn'];
261           if(!isset($this->raidDevices[$name])){
262             $may[] = $name;
263           }
264         }
265       }
266     }
267  
268     // Check which of the available disks are unused. 
269     $ret = array();
270     foreach($may as $val){
271       if(!in_array($val,$used)){
272         $ret[$val] = $val;
273       }
274     }
275     return($ret);
276   }
279   /* Creates a human readable list of all used partitions 
280    *  of a raid device.
281    */
282   function getRaidList()
283   {
284     $divlist = new divSelectBox("RaidList");
285     $disks = $this->parent->parent->disks;
286     $objs = array();
287     foreach($disks as $disk){
288       if($disk['FAIdiskType'] != "raid"){
289         foreach($disk['partitions'] as $id => $part){
290           $objs[$part['cn']] = $part;
291         }
292       }
293     }
294   
295     $list = array();
296     foreach($this->raidDevices as $device){
297       $str = $name = $device['name'];
298       $str = str_pad($str,25," ");
299       if(isset($objs[$device['name']]['FAIpartitionSize'])){
300         $str .= _("Size").":&nbsp;";
301         $str .= $objs[$device['name']]['FAIpartitionSize'];
302       }
303       $str = str_pad($str,60," ");
304       $opt = "";
305       if($device['spare']){
306         $opt.= "&nbsp;"._("spare")."&nbsp;";
307       }
308       if($device['missing']){
309         $opt.= "&nbsp;"._("missing")."&nbsp;";
310       }
311       if($opt){
312         $str .= " &nbsp; "._("Options").": ".$opt;
313       }
314       $str = str_pad($str,80," ");
315       $list[$name] = preg_replace("/ /","&nbsp;",$str);
316     }
317     return($list);
318   }
320   function save_object()
321   {
322     if(isset($_POST['faiPartition'])){
323       foreach($this->attributes as $attr){
324         if(isset($_POST[$attr])){
325           $this->$attr = get_post($attr);
326         }
327       }
328       foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
329         if(isset($_POST[$attr])){
330           $this->$attr = get_post($attr);
331         }
332       }
333       foreach(array("bootable","preserve","resize","encrypted") as $attr){
334         if(isset($_POST[$attr])){
335           $this->$attr = TRUE;
336         }else{
337           $this->$attr = FALSE;
338         }
339       }
341       // Allow user defined partition names for lvm disks.
342       if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
343         $this->cn = get_post('cn');
344       }
346       // Remove partition
347       if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
348         foreach($_POST['physicalPartition'] as $key){
349           if(isset($this->raidDevices[$key])){
350             unset($this->raidDevices[$key]);
351           }
352         }
353       }
355       // Toggle spare flag for partition entries
356       if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
357         $this->raidDevices[$_POST['physicalPartition']]['spare'] = 
358             !$this->raidDevices[$_POST['physicalPartition']]['spare'];
359       }
361       // Toggle missing flag for partition entries
362       if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
363         $this->raidDevices[$_POST['physicalPartition']]['missing'] = 
364             !$this->raidDevices[$_POST['physicalPartition']]['missing'];
365       }
366     } 
367   }
370   function check()
371   {
372     $msgs = plugin::check();
373     
374     // Check the given partition size.
375     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
376       if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){ 
377         if(!is_numeric($this->sizeStart)){
378           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
379         }
380       }
381       if($this->FAIpartitionSizeType == "dynamic"){ 
382         if(!is_numeric($this->sizeStop)){
383           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
384         }
386         $mp = array(
387                     "%"  => 1,
388                     "KB" => pow(1024,0),
389                     "MB" => pow(1024,1),
390                     "GB" => pow(1024,2),
391                     "TB" => pow(1024,3),
392                     "PB" => pow(1024,4));
393         $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
394         $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
395         if($res1 > $res2){
396           $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
397         }
398       }
399     }
401     // Add raid checks
402     if($this->FAIdiskType == "raid"){
403       if(count($this->raidDevices) < 2){
404         $msgs[] = _("Raid arrays must contain at least two partitions!");
405       }else if($this->FAIpartitionType == "raid0" && count($this->raidDevices) != 2){
406         $msgs[] = _("Raid 0 arrays can only be realized with a combination of two partitions!");
407       }
408     }
410     // check mount point 
411     if($this->FAIfsType != "swap"){
412       if(!preg_match("#^/#",$this->FAImountPoint)){
413         $msgs[] = msgPool::invalid(_("Mount point"));
414       }
415     }
417     return($msgs);
418   }
421   function save()
422   {
423     $ret = array();
424     foreach($this->attributes as $attr){
425       $ret[$attr] = $this->$attr;
426     }
428     // Save partition size
429     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){ 
430       switch($this->FAIpartitionSizeType){
431         case 'fixed' : 
432           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break; 
433         case 'dynamic' : 
434           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".  
435             $this->sizeStop.$this->sizeStop_Type;break; 
436         case 'remaining' : 
437           $ret['FAIpartitionSize'] = "0-";break; 
438         default: trigger_error("Unknown partition size!");
439       }
441       // Add encryption flag to partition mount point
442       if($this->encrypted){
443         $ret['FAImountPoint'] .= ":encrypt";
444       }
446     }elseif($this->FAIdiskType == "raid"){
448       // Save selected raid partitions in FAIpartitionSize
449       $ret['FAIpartitionSize'] = "";
450       foreach($this->raidDevices as $device){
451         $ret['FAIpartitionSize'] .= $device['name'];
452         if($device['spare']){
453           $ret['FAIpartitionSize'] .= ":spare";
454         }
455         if($device['missing']){
456           $ret['FAIpartitionSize'] .= ":missing";
457         }
458         $ret['FAIpartitionSize'] .= ",";
459       }
460       $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
461     }
463     if($this->FAIfsType == "swap"){
464       $ret['FAImountPoint'] = "swap";
465     }
466  
467     return($ret);
468   }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>