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