Code

059ebda2b7162e13b3b0b4b5c6c27d56ba9d4e60
[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 $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]{1,}(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]{1,}(KB|MB|GB|TB|PB|%|)-[0-9]{1,}(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("/^(0|)\-$/",$this->FAIpartitionSize)){
106           $this->FAIpartitionSizeType = "remaining";
107         }
109         // Ensure that we've a valid value type selected.
110         if(!preg_match("/(KB|MB|GB|TB|PB|%)/",$this->sizeStart_Type)){
111           $this->sizeStart_Type = "MB";
112         }
113         if(!preg_match("/(KB|MB|GB|TB|PB|%)/",$this->sizeStop_Type)){
114           $this->sizeStop_Type = "MB";
115         }
117         /* Check for encrypted partitions
118          */
119         if(preg_match("/:encrypt$/",$this->FAImountPoint)){
120           $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
121           $this->encrypted = TRUE;
122         }
123     
124       }elseif($type == "raid"){
125    
126         // Extract raid devices out of the partition size attribute. 
127         $usedDisks = split(",",$this->FAIpartitionSize);
128         foreach($usedDisks as $disk){
129           $name = preg_replace("/:.*$/","",$disk);
130           $spare = preg_match("/:spare/",$disk);
131           $missing = preg_match("/:missing/",$disk);
132           if(!empty($name)){
133             $this->raidDevices[$name] = 
134               array(
135                   "name" => $name, 
136                   "spare" => $spare, 
137                   "missing" => $missing);
138           }
139         }
140       }
141     }
142   }
144   function execute()
145   {
146     plugin::execute();
147     $smarty = get_smarty();
149     // Remove partition
150     if(isset($_POST['addPhysicalPartition']) && isset($_POST['physicalPartitionAdd'])){
151       $name = $_POST['physicalPartitionAdd'];
152       $this->raidDevices[$name] = array("name" => $name,"spare"=>false,"missing"=>false);     
153     }
155     // Create a list of selectable partition types
156     if($this->FAIdiskType == "disk"){
158       if($this->disablePrimary){
159         $types  = array(
160             "logical" => _("Logical"));
161       }else{
162         $types  = array(
163             "primary" => _("Primary"),
164             "logical" => _("Logical"));
165       }
167     }elseif($this->FAIdiskType == "raid"){
168       $types  = array(
169           "raid0" => _("RAID 0"),
170           "raid1" => _("RAID 1"),
171           "raid5" => _("RAID 5"),
172           "raid6" => _("RAID 6"));
173     }else{
174       $types = "";
175     }
176      
177     // Create a list of all size options
178     $partitionSizeTypes  = array(
179         "fixed"     => _("fixed"),
180         "dynamic"     => _("dynamic"),
181         "remaining" => _("remaining space")
182         );
184     // Create a list of all size options
185     $sizeTypes  = array(
186         "KB"      => _("KB"),
187         "MB"      => _("MB"),
188         "GB"      => _("GB"),
189         "TB"      => _("TB"),
190         "PB"      => _("PB"),
191         "%"      => _("%")
192         );
194     // Preserve types 
195     $preserveTypes = array(
196         "always" => _("always"),
197         "reinstall" => _("reinstall"));
199     // File system types.  
200     $FAIfsTypes = array(
201         "swap" => _("swap space"),
202         "ext2" => _("ext2"),
203         "ext3" => _("ext3"),
204         "ext4" => _("ext4"),
205         "reiserfs" => _("reiser fs"),
206         "xfs" => _("xfs"),
207         "btrfs" => _("btrfs"));
209     $smarty->assign("partitionTypes", $types);
210     $smarty->assign("partitionSizeTypes", $partitionSizeTypes);
211     $smarty->assign("FAIpartitionSizeType", $this->FAIpartitionSizeType);
212     $smarty->assign("sizeTypes", $sizeTypes);
214     $smarty->assign("sizeStart_Type", $this->sizeStart_Type);
215     $smarty->assign("sizeStop_Type", $this->sizeStop_Type);
216     $smarty->assign("sizeStart", $this->sizeStart);
217     $smarty->assign("sizeStop", $this->sizeStop);
219     $smarty->assign("preserveTypes", $preserveTypes);
220     $smarty->assign("preserveType", $this->preserveType);
222     $smarty->assign("FAIfsTypes", $FAIfsTypes);
223     $smarty->assign("cn", $this->cn);
224     $smarty->assign("freeze", preg_match("/freeze/i",$this->parent->FAIstate));
226     $smarty->assign("plist",$this->getRaidlist());
227     $smarty->assign("physicalPartitionList",$this->getPartitionlist());
228     $smarty->assign("disablePrimary", $this->disablePrimary);
230     foreach($this->attributes as $attr){
231       $smarty->assign($attr,$this->$attr);
232     }
233     return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
234   }
236   
237   /* Returns a list of all partitions that are useable 
238    *  for raid arrays.
239    */
240   function getPartitionList()
241   {
242     $array = array();  
243     foreach($this->parent->parent->disks as $disk){
244       if($disk['FAIdiskType'] != "raid"){
245         foreach($disk['partitions'] as $key => $part){
246           $name = $part['cn'];
247           if(!isset($this->raidDevices[$name])){
248             $array[$name] = $name." (".$disk['cn'].")";
249           }
250         }
251       }
252     }
253     return($array);
254   }
257   /* Creates a human readable list of all used partitions 
258    *  of a raid device.
259    */
260   function getRaidList()
261   {
262     $divlist = new divSelectBox("RaidList");
263     $disks = $this->parent->parent->disks;
264     $objs = array();
265     foreach($disks as $disk){
266       if($disk['FAIdiskType'] != "raid"){
267         foreach($disk['partitions'] as $id => $part){
268           $objs[$part['cn']] = $part;
269         }
270       }
271     }
272   
273     $list = array();
274     foreach($this->raidDevices as $device){
275       $str = $name = $device['name'];
276       $str = str_pad($str,25," ");
277       if(isset($objs[$device['name']]['FAIpartitionSize'])){
278         $str .= _("Size").":&nbsp;";
279         $str .= $objs[$device['name']]['FAIpartitionSize'];
280       }
281       $str = str_pad($str,60," ");
282       $opt = "";
283       if($device['spare']){
284         $opt.= "&nbsp;"._("spare")."&nbsp;";
285       }
286       if($device['missing']){
287         $opt.= "&nbsp;"._("missing")."&nbsp;";
288       }
289       if($opt){
290         $str .= " &nbsp; "._("Options").": ".$opt;
291       }
292       $str = str_pad($str,80," ");
293       $list[$name] = preg_replace("/ /","&nbsp;",$str);
294     }
295     return($list);
296   }
298   function save_object()
299   {
300     if(isset($_POST['faiPartition'])){
301       foreach($this->attributes as $attr){
302         if(isset($_POST[$attr])){
303           $this->$attr = get_post($attr);
304         }
305       }
306       foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
307         if(isset($_POST[$attr])){
308           $this->$attr = get_post($attr);
309         }
310       }
311       foreach(array("bootable","preserve","resize","encrypted") as $attr){
312         if(isset($_POST[$attr])){
313           $this->$attr = TRUE;
314         }else{
315           $this->$attr = FALSE;
316         }
317       }
319       // Allow user defined partition names for lvm disks.
320       if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
321         $this->cn = get_post('cn');
322       }
324       // Remove partition
325       if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
326         foreach($_POST['physicalPartition'] as $key){
327           if(isset($this->raidDevices[$key])){
328             unset($this->raidDevices[$key]);
329           }
330         }
331       }
333       // Toggle spare flag for partition entries
334       if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
335         $this->raidDevices[$_POST['physicalPartition']]['spare'] = 
336             !$this->raidDevices[$_POST['physicalPartition']]['spare'];
337       }
339       // Toggle missing flag for partition entries
340       if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
341         $this->raidDevices[$_POST['physicalPartition']]['missing'] = 
342             !$this->raidDevices[$_POST['physicalPartition']]['missing'];
343       }
344     } 
345   }
348   function check()
349   {
350     $msgs = plugin::check();
351     
352     // Check the given partition size.
353     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
354       if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){ 
355         if(!is_numeric($this->sizeStart)){
356           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
357         }
358       }
359       if($this->FAIpartitionSizeType == "dynamic"){ 
360         if(!is_numeric($this->sizeStop)){
361           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
362         }
364         $mp = array(
365                     "%"  => 1,
366                     "KB" => pow(1024,0),
367                     "MB" => pow(1024,1),
368                     "GB" => pow(1024,2),
369                     "TB" => pow(1024,3),
370                     "PB" => pow(1024,4));
371         $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
372         $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
373         if($res1 > $res2){
374           $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
375         }
376       }
377     }
379     // Add raid checks
380     if($this->FAIdiskType == "raid"){
381       if(count($this->raidDevices) < 2){
382         $msgs[] = _("Raid arrays must contain at least two partitions!");
383       }else if($this->FAIpartitionType == "raid0" && count($this->raidDevices) != 2){
384         $msgs[] = _("Raid 0 arrays can only be realized with a combination of two partitions!");
385       }
386     }
388     // check mount point 
389     if($this->FAIfsType != "swap"){
390       if(!preg_match("#^/#",$this->FAImountPoint)){
391         $msgs[] = msgPool::invalid(_("Mount point"));
392       }
393     }
395     return($msgs);
396   }
399   function save()
400   {
401     $ret = array();
402     foreach($this->attributes as $attr){
403       $ret[$attr] = $this->$attr;
404     }
406     // Save partition size
407     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){ 
408       switch($this->FAIpartitionSizeType){
409         case 'fixed' : 
410           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break; 
411         case 'dynamic' : 
412           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".  
413             $this->sizeStop.$this->sizeStop_Type;break; 
414         case 'remaining' : 
415           $ret['FAIpartitionSize'] = "0-";break; 
416         default: trigger_error("Unknown partition size!");
417       }
419       // Add encryption flag to partition mount point
420       if($this->encrypted){
421         $ret['FAImountPoint'] .= ":encrypt";
422       }
424     }elseif($this->FAIdiskType == "raid"){
426       // Save selected raid partitions in FAIpartitionSize
427       $ret['FAIpartitionSize'] = "";
428       foreach($this->raidDevices as $device){
429         $ret['FAIpartitionSize'] .= $device['name'];
430         if($device['spare']){
431           $ret['FAIpartitionSize'] .= ":spare";
432         }
433         if($device['missing']){
434           $ret['FAIpartitionSize'] .= ":missing";
435         }
436         $ret['FAIpartitionSize'] .= ",";
437       }
438       $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
439     }
440     $ret['status'] = $this->status;
442     if($this->FAIfsType == "swap"){
443       $ret['FAImountPoint'] = "swap";
444     }
445  
446     return($ret);
447   }
449 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
450 ?>