Code

Updated FAI partition handling.
[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);
216     $smarty->assign("plist",$this->getRaidlist());
217     $smarty->assign("physicalPartitionList",$this->getPartitionlist());
218     $smarty->assign("disablePrimary", $this->disablePrimary);
220     foreach($this->attributes as $attr){
221       $smarty->assign($attr,$this->$attr);
222     }
223     return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
224   }
226     
227   function getPartitionList()
228   {
229     $array = array();  
230     foreach($this->parent->parent->disks as $disk){
231       if($disk['FAIdiskType'] != "raid"){
232         foreach($disk['partitions'] as $key => $part){
233           $name = $part['cn'];
234           if(!isset($this->raidDevices[$name])){
235             $array[$name] = $name." (".$disk['cn'].")";
236           }
237         }
238       }
239     }
240     return($array);
241   }
244   function getRaidList()
245   {
246     $divlist = new divSelectBox("RaidList");
247    
248     $disks = $this->parent->parent->disks;
249     $objs = array();
250     foreach($disks as $disk){
251       if($disk['FAIdiskType'] != "raid"){
252         foreach($disk['partitions'] as $id => $part){
253           $objs[$part['cn']] = $part;
254         }
255       }
256     }
257   
258     $list = array();
259     foreach($this->raidDevices as $device){
260       $str = $name = $device['name'];
261       $str = str_pad($str,25," ");
262       if(isset($objs[$device['name']]['FAIpartitionSize'])){
263         $str .= _("Size").":&nbsp;";
264         $str .= $objs[$device['name']]['FAIpartitionSize'];
265       }
266       $str = str_pad($str,60," ");
267       $opt = "";
268       if($device['spare']){
269         $opt.= "&nbsp;"._("spare")."&nbsp;";
270       }
271       if($device['missing']){
272         $opt.= "&nbsp;"._("missing")."&nbsp;";
273       }
274       if($opt){
275         $str .= " &nbsp; "._("Options").": ".$opt;
276       }
277       $str = str_pad($str,80," ");
278       $list[$name] = preg_replace("/ /","&nbsp;",$str);
279     }
280     return($list);
281   }
283   function save_object()
284   {
285     if(isset($_POST['faiPartition'])){
286       foreach($this->attributes as $attr){
287         if(isset($_POST[$attr])){
288           $this->$attr = get_post($attr);
289         }
290       }
291       foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
292         if(isset($_POST[$attr])){
293           $this->$attr = get_post($attr);
294         }
295       }
296       foreach(array("bootable","preserve","resize","encrypted") as $attr){
297         if(isset($_POST[$attr])){
298           $this->$attr = TRUE;
299         }else{
300           $this->$attr = FALSE;
301         }
302       }
304       // Allow user defined partition names for lvm disks.
305       if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
306         $this->cn = get_post('cn');
307       }
309       // Remove partition
310       if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
311         unset($this->raidDevices[$_POST['physicalPartition']]); 
312       }
314       // Toggle spare flag for partition entries
315       if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
316         $this->raidDevices[$_POST['physicalPartition']]['spare'] = 
317             !$this->raidDevices[$_POST['physicalPartition']]['spare'];
318       }
320       // Toggle missing flag for partition entries
321       if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
322         $this->raidDevices[$_POST['physicalPartition']]['missing'] = 
323             !$this->raidDevices[$_POST['physicalPartition']]['missing'];
324       }
325     } 
326   }
329   function check()
330   {
331     $msgs = plugin::check();
332     
333     // Check the given partition size.
334     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
335       if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){ 
336         if(!is_numeric($this->sizeStart)){
337           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
338         }
339       }
340       if($this->FAIpartitionSizeType == "dynamic"){ 
341         if(!is_numeric($this->sizeStop)){
342           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
343         }
345         $mp = array(
346                     "%"  => 1,
347                     "KB" => pow(1024,0),
348                     "MB" => pow(1024,1),
349                     "GB" => pow(1024,2),
350                     "TB" => pow(1024,3),
351                     "PB" => pow(1024,4));
352         $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
353         $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
354         if($res1 > $res2){
355           $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
356         }
357       }
358     }
360     // Add raid checks
361     if($this->FAIdiskType == "raid"){
362       if(count($this->raidDevices) < 2){
363         $msgs[] = _("Raid arrays must contain at least two partitions!");
364       }else if($this->FAIpartitionType == "raid0" && count($this->raidDevices) != 2){
365         $msgs[] = _("Raid 0 arrays can only be realized with a combination of two partitions!");
366       }
367     }
369     // check mount point 
370     if($this->FAIfsType != "swap"){
371       if(!preg_match("#^/#",$this->FAImountPoint)){
372         $msgs[] = msgPool::invalid(_("Mount point"));
373       }
374     }
376     return($msgs);
377   }
380   function save()
381   {
382     $ret = array();
383     foreach($this->attributes as $attr){
384       $ret[$attr] = $this->$attr;
385     }
387     // Save partition size
388     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){ 
389       switch($this->FAIpartitionSizeType){
390         case 'fixed' : 
391           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break; 
392         case 'dynamic' : 
393           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".  
394             $this->sizeStop.$this->sizeStop_Type;break; 
395         case 'remaining' : 
396           $ret['FAIpartitionSize'] = "-";break; 
397         default: trigger_error("Unknown partition size!");
398       }
400       // Add encryption flag to partition mount point
401       if($this->encrypted){
402         $ret['FAImountPoint'] .= ":encrypt";
403       }
405     }elseif($this->FAIdiskType == "raid"){
406       $ret['FAIpartitionSize'] = "";
407       foreach($this->raidDevices as $device){
408         $ret['FAIpartitionSize'] .= $device['name'];
409         if($device['spare']){
410           $ret['FAIpartitionSize'] .= ":spare";
411         }
412         if($device['missing']){
413           $ret['FAIpartitionSize'] .= ":missing";
414         }
415         $ret['FAIpartitionSize'] .= ",";
416       }
417       $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
418     }
419     $ret['status'] = $this->status;
421     if($this->FAIfsType == "swap"){
422       $ret['FAImountPoint'] = "swap";
423     }
424  
425     return($ret);
426   }
428 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
429 ?>