Code

Updated FAI partition stuff.
[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   var $FAIpartitionSizeType = "fixed";
27   var $sizeStart = 0;
28   var $sizeStop  = 0;
29   var $sizeStart_Type = "MB";
30   var $sizeStop_Type  = "MB";
31    
32   var $bootable = false; 
33   var $resize = false;
34   var $preserve = false;
35   var $preserveType = "always";
36   var $encrypted = false;
38   var $status = "";
39   var $raidDevices = array();
41   // Once we've exceeded the primary partition limit,
42   //  hide the the 'primary' option from the select box.
43   var $disablePrimary = FALSE; 
45   function __construct($config, $object, $parent,$type)
46   {
48     $this->parent = $parent;
49     $this->status = "new";
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     } 
66  
67     // Load attributes from existing partition 
68     if($object){
70       foreach($this->attributes as $attr){
71         if(isset($object[$attr])){
72           $this->$attr = $object[$attr];
73         }
74       }
76       $this->status = $object['status'];
78       if($type == "disk" || $type =="lvm"){
80         /* Prepare size attribute 
81          * This attribute may represent a range, a fixed value 
82          *  or a percentage.
83          * fixed is just a number   * 500MB
84          * range                    * 500MB-1TB
85          * remaining                * -
86          */
87         // Fixed
88         if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
89           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
90           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
91           $this->FAIpartitionSizeType = "fixed";
92         }else
94         // Dynamic range
95         if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)-[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
96           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\1",$this->FAIpartitionSize);
97           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\2",$this->FAIpartitionSize);
98           $this->sizeStop = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
99           $this->sizeStop_Type = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
100           $this->FAIpartitionSizeType = "dynamic";
101         }else
103         // Dynamic range
104         if(preg_match("/^\-$/",$this->FAIpartitionSize)){
105           $this->FAIpartitionSizeType = "remaining";
107         }
109         /* Check for encrypted partitions
110          */
111         if(preg_match("/:encrypt$/",$this->FAImountPoint)){
112           $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
113           $this->encrypted = TRUE;
114         }
115     
116       }elseif($type == "raid"){
117     
118         $usedDisks = split(",",$this->FAIpartitionSize);
119         foreach($usedDisks as $disk){
120           $name = preg_replace("/:.*$/","",$disk);
121           $spare = preg_match("/:spare/",$disk);
122           $missing = preg_match("/:missing/",$disk);
123           $this->raidDevices[$name] = 
124             array(
125                 "name" => $name, 
126                 "spare" => $spare, 
127                 "missing" => $missing);
128         }
129       }
130     }
131   }
133   function execute()
134   {
135     plugin::execute();
136     $smarty = get_smarty();
138     // Remove partition
139     if(isset($_POST['addPhysicalPartition']) && isset($_POST['physicalPartitionAdd'])){
140       $name = $_POST['physicalPartitionAdd'];
141       $this->raidDevices[$name] = array("name" => $name,"spare"=>false,"missing"=>false);     
142     }
144     // Create a list of selectable partition types
145     if($this->FAIdiskType == "disk"){
147       if($this->disablePrimary){
148         $types  = array(
149             "logical" => _("Logical"));
150       }else{
151         $types  = array(
152             "primary" => _("Primary"),
153             "logical" => _("Logical"));
154       }
156     }elseif($this->FAIdiskType == "raid"){
157       $types  = array(
158           "raid0" => _("RAID 0"),
159           "raid1" => _("RAID 1"),
160           "raid5" => _("RAID 5"),
161           "raid6" => _("RAID 6"));
162     }else{
163       $types = "";
164     }
165      
166     // Create a list of all size options
167     $partitionSizeTypes  = array(
168         "fixed"     => _("fixed"),
169         "dynamic"     => _("dynamic"),
170         "remaining" => _("remaining space")
171         );
173     // Create a list of all size options
174     $sizeTypes  = array(
175         "KB"      => _("KB"),
176         "MB"      => _("MB"),
177         "GB"      => _("GB"),
178         "TB"      => _("TB"),
179         "PB"      => _("PB"),
180         "%"      => _("%")
181         );
183     // Preserve types 
184     $preserveTypes = array(
185         "always" => _("always"),
186         "reinstall" => _("reinstall"));
188     // File system types.  
189     $FAIfsTypes = array(
190         "swap" => _("swap space"),
191         "ext2" => _("ext2"),
192         "ext3" => _("ext3"),
193         "ext4" => _("ext4"),
194         "reiserfs" => _("reiser fs"),
195         "xfs" => _("xfs"),
196         "btrfs" => _("btrfs"));
198     $smarty->assign("partitionTypes", $types);
199     $smarty->assign("partitionSizeTypes", $partitionSizeTypes);
200     $smarty->assign("FAIpartitionSizeType", $this->FAIpartitionSizeType);
201     $smarty->assign("sizeTypes", $sizeTypes);
203     $smarty->assign("sizeStart_Type", $this->sizeStart_Type);
204     $smarty->assign("sizeStop_Type", $this->sizeStop_Type);
205     $smarty->assign("sizeStart", $this->sizeStart);
206     $smarty->assign("sizeStop", $this->sizeStop);
208     $smarty->assign("preserveTypes", $preserveTypes);
209     $smarty->assign("preserveType", $this->preserveType);
211     $smarty->assign("FAIfsTypes", $FAIfsTypes);
212     $smarty->assign("cn", $this->cn);
214     $smarty->assign("plist",$this->getRaidlist());
215     $smarty->assign("physicalPartitionList",$this->getPartitionlist());
216     $smarty->assign("disablePrimary", $this->disablePrimary);
218     foreach($this->attributes as $attr){
219       $smarty->assign($attr,$this->$attr);
220     }
221     return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
222   }
224     
225   function getPartitionList()
226   {
227     $array = array();  
228     foreach($this->parent->parent->disks as $disk){
229       if($disk['FAIdiskType'] != "raid"){
230         foreach($disk['partitions'] as $key => $part){
231           $name = $part['cn'];
232           if(!isset($this->raidDevices[$name])){
233             $array[$name] = $name." (".$disk['cn'].")";
234           }
235         }
236       }
237     }
238     return($array);
239   }
242   function getRaidList()
243   {
244     $divlist = new divSelectBox("RaidList");
245    
246     $disks = $this->parent->parent->disks;
247     $objs = array();
248     foreach($disks as $disk){
249       if($disk['FAIdiskType'] != "raid"){
250         foreach($disk['partitions'] as $id => $part){
251           $objs[$part['cn']] = $part;
252         }
253       }
254     }
255   
256     $list = array();
257     foreach($this->raidDevices as $device){
258       $list[$device['name']] = $device['name'];
260       if(isset($objs[$device['name']]['FAIpartitionSize'])){
261         $list[$device['name']].= _("Size").":&nbsp;";
262         $list[$device['name']].= $objs[$device['name']]['FAIpartitionSize'];
263       }
265       $list[$device['name']].= " &nbsp; "._("Options").": ";
266       if($device['spare']){
267         $list[$device['name']].= "&nbsp;"._("spare")."&nbsp;";
268       }
269       if($device['missing']){
270         $list[$device['name']].= "&nbsp;"._("missing")."&nbsp;";
271       }
272     }
273     return($list);
274   }
276   function save_object()
277   {
278     if(isset($_POST['faiPartition'])){
279       foreach($this->attributes as $attr){
280         if(isset($_POST[$attr])){
281           $this->$attr = get_post($attr);
282         }
283       }
284       foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
285         if(isset($_POST[$attr])){
286           $this->$attr = get_post($attr);
287         }
288       }
289       foreach(array("bootable","preserve","resize","encrypted") as $attr){
290         if(isset($_POST[$attr])){
291           $this->$attr = TRUE;
292         }else{
293           $this->$attr = FALSE;
294         }
295       }
297       // Remove partition
298       if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
299         unset($this->raidDevices[$_POST['physicalPartition']]); 
300       }
302       // Toggle spare flag for partition entries
303       if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
304         $this->raidDevices[$_POST['physicalPartition']]['spare'] = 
305             !$this->raidDevices[$_POST['physicalPartition']]['spare'];
306       }
308       // Toggle missing flag for partition entries
309       if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
310         $this->raidDevices[$_POST['physicalPartition']]['missing'] = 
311             !$this->raidDevices[$_POST['physicalPartition']]['missing'];
312       }
313     } 
314   }
317   function check()
318   {
319     $msgs = plugin::check();
320     
321     // Check the given partition size.
322     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
323       if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){ 
324         if(!is_numeric($this->sizeStart)){
325           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
326         }
327       }
328       if($this->FAIpartitionSizeType == "dynamic"){ 
329         if(!is_numeric($this->sizeStop)){
330           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
331         }
333         $mp = array(
334                     "%"  => 1,
335                     "KB" => pow(1024,0),
336                     "MB" => pow(1024,1),
337                     "GB" => pow(1024,2),
338                     "TB" => pow(1024,3),
339                     "PB" => pow(1024,4));
340         $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
341         $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
342         if($res1 > $res2){
343           $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
344         }
345       }
346     }
347     if($this->FAIdiskType == "raid"){
348       #FIME raid checks missing
349       echo "Add raid checks here, disk combinations are not verified right now.";
350     }
352     // check mount point 
353     if($this->FAIfsType != "swap"){
354       if(!preg_match("#^/#",$this->FAImountPoint)){
355         $msgs[] = msgPool::invalid(_("Mount point"));
356       }
357     }
359     return($msgs);
360   }
363   function save()
364   {
365     $ret = array();
366     foreach($this->attributes as $attr){
367       $ret[$attr] = $this->$attr;
368     }
370     // Save partition size
371     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){ 
372       switch($this->FAIpartitionSizeType){
373         case 'fixed' : 
374           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break; 
375         case 'dynamic' : 
376           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".  
377             $this->sizeStop.$this->sizeStop_Type;break; 
378         case 'remaining' : 
379           $ret['FAIpartitionSize'] = "-";break; 
380         default: trigger_error("Unknown partition size!");
381       }
383       // Add encryption flag to partition mount point
384       if($this->encrypted){
385         $ret['FAImountPoint'] .= ":encrypt";
386       }
388     }elseif($this->FAIdiskType == "raid"){
389       $ret['FAIpartitionSize'] = "";
390       foreach($this->raidDevices as $device){
391         $ret['FAIpartitionSize'] .= $device['name'];
392         if($device['spare']){
393           $ret['FAIpartitionSize'] .= ":spare";
394         }
395         if($device['missing']){
396           $ret['FAIpartitionSize'] .= ":missing";
397         }
398         $ret['FAIpartitionSize'] .= ",";
399       }
400       $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
401     }
402     $ret['status'] = $this->status;
404     if($this->FAIfsType == "swap"){
405       $ret['FAImountPoint'] = "swap";
406     }
407   
408     return($ret);
409   }
411 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
412 ?>