Code

296f83abd538d130afb90dce3468032be4189a7e
[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     print_a($object);
68  
69     // Load attributes from existing partition 
70     if($object){
72       foreach($this->attributes as $attr){
73         if(isset($object[$attr])){
74           $this->$attr = $object[$attr];
75         }
76       }
78       $this->status = $object['status'];
80       if($type == "disk" || $type =="lvm"){
82         /* Prepare size attribute 
83          * This attribute may represent a range, a fixed value 
84          *  or a percentage.
85          * fixed is just a number   * 500MB
86          * range                    * 500MB-1TB
87          * remaining                * -
88          */
89         // Fixed
90         if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
91           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
92           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
93           $this->FAIpartitionSizeType = "fixed";
94         }else
96         // Dynamic range
97         if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)-[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
98           $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\1",$this->FAIpartitionSize);
99           $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\2",$this->FAIpartitionSize);
100           $this->sizeStop = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
101           $this->sizeStop_Type = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
102           $this->FAIpartitionSizeType = "dynamic";
103         }else
105         // Dynamic range
106         if(preg_match("/^\-$/",$this->FAIpartitionSize)){
107           $this->FAIpartitionSizeType = "remaining";
109         }
111         /* Check for encrypted partitions
112          */
113         if(preg_match("/:encrypt$/",$this->FAImountPoint)){
114           $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
115           $this->encrypted = TRUE;
116         }
117     
118       }elseif($type == "raid"){
119     
120         $usedDisks = split(",",$this->FAIpartitionSize);
121         foreach($usedDisks as $disk){
122           $name = preg_replace("/:.*$/","",$disk);
123           $spare = preg_match("/:spare/",$disk);
124           $missing = preg_match("/:missing/",$disk);
125           $this->raidDevices[$name] = 
126             array(
127                 "name" => $name, 
128                 "spare" => $spare, 
129                 "missing" => $missing);
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       $list[$device['name']] = $device['name'];
262       if(isset($objs[$device['name']]['FAIpartitionSize'])){
263         $list[$device['name']].= _("Size").":&nbsp;";
264         $list[$device['name']].= $objs[$device['name']]['FAIpartitionSize'];
265       }
267       $list[$device['name']].= " &nbsp; "._("Options").": ";
268       if($device['spare']){
269         $list[$device['name']].= "&nbsp;"._("spare")."&nbsp;";
270       }
271       if($device['missing']){
272         $list[$device['name']].= "&nbsp;"._("missing")."&nbsp;";
273       }
274     }
275     return($list);
276   }
278   function save_object()
279   {
280     if(isset($_POST['faiPartition'])){
281       foreach($this->attributes as $attr){
282         if(isset($_POST[$attr])){
283           $this->$attr = get_post($attr);
284         }
285       }
286       foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
287         if(isset($_POST[$attr])){
288           $this->$attr = get_post($attr);
289         }
290       }
291       foreach(array("bootable","preserve","resize","encrypted") as $attr){
292         if(isset($_POST[$attr])){
293           $this->$attr = TRUE;
294         }else{
295           $this->$attr = FALSE;
296         }
297       }
299       // Allow user defined partition names for lvm disks.
300       if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
301         $this->cn = get_post('cn');
302       }
304       // Remove partition
305       if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
306         unset($this->raidDevices[$_POST['physicalPartition']]); 
307       }
309       // Toggle spare flag for partition entries
310       if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
311         $this->raidDevices[$_POST['physicalPartition']]['spare'] = 
312             !$this->raidDevices[$_POST['physicalPartition']]['spare'];
313       }
315       // Toggle missing flag for partition entries
316       if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
317         $this->raidDevices[$_POST['physicalPartition']]['missing'] = 
318             !$this->raidDevices[$_POST['physicalPartition']]['missing'];
319       }
320     } 
321   }
324   function check()
325   {
326     $msgs = plugin::check();
327     
328     // Check the given partition size.
329     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
330       if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){ 
331         if(!is_numeric($this->sizeStart)){
332           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
333         }
334       }
335       if($this->FAIpartitionSizeType == "dynamic"){ 
336         if(!is_numeric($this->sizeStop)){
337           $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
338         }
340         $mp = array(
341                     "%"  => 1,
342                     "KB" => pow(1024,0),
343                     "MB" => pow(1024,1),
344                     "GB" => pow(1024,2),
345                     "TB" => pow(1024,3),
346                     "PB" => pow(1024,4));
347         $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
348         $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
349         if($res1 > $res2){
350           $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
351         }
352       }
353     }
354     if($this->FAIdiskType == "raid"){
355       #FIME raid checks missing
356       echo "Add raid checks here, disk combinations are not verified right now.";
357     }
359     // check mount point 
360     if($this->FAIfsType != "swap"){
361       if(!preg_match("#^/#",$this->FAImountPoint)){
362         $msgs[] = msgPool::invalid(_("Mount point"));
363       }
364     }
366     return($msgs);
367   }
370   function save()
371   {
372     $ret = array();
373     foreach($this->attributes as $attr){
374       $ret[$attr] = $this->$attr;
375     }
377     // Save partition size
378     if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){ 
379       switch($this->FAIpartitionSizeType){
380         case 'fixed' : 
381           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break; 
382         case 'dynamic' : 
383           $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".  
384             $this->sizeStop.$this->sizeStop_Type;break; 
385         case 'remaining' : 
386           $ret['FAIpartitionSize'] = "-";break; 
387         default: trigger_error("Unknown partition size!");
388       }
390       // Add encryption flag to partition mount point
391       if($this->encrypted){
392         $ret['FAImountPoint'] .= ":encrypt";
393       }
395     }elseif($this->FAIdiskType == "raid"){
396       $ret['FAIpartitionSize'] = "";
397       foreach($this->raidDevices as $device){
398         $ret['FAIpartitionSize'] .= $device['name'];
399         if($device['spare']){
400           $ret['FAIpartitionSize'] .= ":spare";
401         }
402         if($device['missing']){
403           $ret['FAIpartitionSize'] .= ":missing";
404         }
405         $ret['FAIpartitionSize'] .= ",";
406       }
407       $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
408     }
409     $ret['status'] = $this->status;
411     if($this->FAIfsType == "swap"){
412       $ret['FAImountPoint'] = "swap";
413     }
414  
415     return($ret);
416   }
418 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
419 ?>