Code

Updated FAI partition stuff.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiDiskEntry.inc
1 <?php
3 class faiDiskEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("DISKcn","DISKdescription");
8   var $UsedAttrs            = array("cn","description","FAIpartitionType",
9       "FAIpartitionNr","FAIfsType","FAImountPoint","FAIpartitionSize",
10       "FAIfsTuneOptions", "FAIfsCreateOptions", "FAImountOptions",
11       "FAIfsOptions","FAIpartitionFlags","FAIdiskOption");
13   var $objectclasses= array();
15   var $DISKcn              = "";
16   var $DISKdescription     = "";
17   var $DISKFAIdiskOptions  = "";
18   
19   var $partitions          = array();
20   var $is_edit             = false;
21   var $old_cn              = "";
22   var $status              = "new";
23   var $deletePartitions    = array();
24   var $fstabkey            = "device";
25   var $disklabel           = "msdos";
26   var $FAIstate            = "";
27   var $FAIdiskType         = "disk";
28   
29   var $lvmDevices          = array();
31   function faiDiskEntry (&$config, $dn= NULL,$parent,$disk,$type)
32   {
33     plugin::plugin ($config, $dn);
34   
35     // Set default attributes 
36     $this->parent = $parent;
37     $this->FAIdiskType = $type;
38     $this->status = "new";    
40     // If disk is not empty, then we are going to edit 
41     //  an existing disk, load disk info now. 
42     if($disk){
44       // Get devices used in volume base disks.
45       if(isset($disk['FAIlvmDevice'])){
46         $this->lvmDevices = $disk['FAIlvmDevice'];
47       }
49       // Get disk status
50       $this->status = $disk['status'];
52       // Load default attributes 
53       $this->DISKcn          = $disk['cn'];
54       $this->DISKdescription = $disk['description'];
55       $this->is_edit         = true;
56       $this->old_cn          = $disk['cn'];
58       // Load partition info 
59       foreach($disk['partitions'] as $name => $values){
61         // Do not load removed partitions 
62         if($values['status'] == "delete"){
63           unset($disk['partitions'][$name]);
64           $this->deletePartitions[]=$values;
65         }else{
67           /* If status is not new, set to edit mode. 
68            * New means that this partition currently wasn't saved to ldap.
69            */
70           if($disk['partitions'][$name]['status']!="new"){
71             $disk['partitions'][$name]['status']="edited";
72           }
73     
74           // Load partition attributes  
75           $disk['partitions'][$name]['old_cn']= $disk['partitions'][$name]['cn'];
76           $disk['partitions'][$name]['FAIdiskType']= $this->FAIdiskType;
77           foreach($this->UsedAttrs as $attr){
78             if(!isset($values[$attr])){
79               $disk['partitions'][$name][$attr]="";  
80             }
81           }
82           if (preg_match('/^_/', $disk['partitions'][$name]['FAIfsType'])){
83             $disk['partitions'][$name]['FAIfsType']= 
84               preg_replace('/^_/', '', $disk['partitions'][$name]['FAIfsType']);
85           }
86         }
87       }
89       $this->partitions      = $disk['partitions'];
91       /* Load FAIdiskOptions.
92        * Some options are disk related and some are used for partitions.
93        *  - fstabkey    -> disk
94        *  - disklabel   -> disk
95        *  - bootable    -> partition
96        *  - preserve    -> partition
97        *  - resize      -> partition
98        */
99       if (isset($disk['FAIdiskOption'])){
100         foreach($disk['FAIdiskOption'] as $option) {
102           // Get fstab key  
103           if (preg_match("/fstabkey:(device|label|uuid)/", $option)){
104             $this->fstabkey= preg_replace("/^.*fstabkey:(device|label|uuid).*$/", "$1", $option);
105             continue;
106           }
108           // Get disk label
109           if (preg_match("/disklabel:(msdos|gpt)/", $option)){
110             $this->disklabel= preg_replace("/^.*disklabel:(msdos|gpt).*$/", "$1", $option);
111             continue;
112           }
114           // Load bootable flag for partitions 
115           if (preg_match("/^bootable:/", $option)){
116             $bootable = split(",", trim(preg_replace("/^bootable:/","",$option),","));
117             foreach($bootable as $bootflag){
118               if(isset($this->partitions[$bootflag])){
119                 $this->partitions[$bootflag]['bootable'] = TRUE;  
120               }
121             }
122             continue;
123           }
125           // Load resize flag for partitions 
126           if (preg_match("/^resize:/", $option)){
127             $resize = split(",", trim(preg_replace("/^resize:/","",$option),","));
128             foreach($resize as $id){
129               if(isset($this->partitions[$bootflag])){
130                 $this->partitions[$id]['resize'] = TRUE;  
131               }
132             }
133             continue;
134           }
136           // Load preserve_always flag for partitions 
137           if (preg_match("/^preserve_always:/", $option)){
138             $preserve = split(",", trim(preg_replace("/^preserve_always:/","",$option),","));
139             foreach($preserve as $presflag){
140               if(isset($this->partitions[$bootflag])){
141                 $this->partitions[$presflag]['preserve'] = TRUE;  
142                 $this->partitions[$presflag]['preserveType'] = 'always';  
143               }
144             }
145             continue;
146           }
148           // Load preserve_reinstall flag for partitions 
149           if (preg_match("/^preserve_reinstall:/", $option)){
150             $preserve = split(",", trim(preg_replace("/^preserve_reinstall:/","",$option),","));
151             foreach($preserve as $presflag){
152               if(isset($this->partitions[$bootflag])){
153                 $this->partitions[$presflag]['preserve'] = TRUE;  
154                 $this->partitions[$presflag]['preserveType'] = 'reinstall'; 
155               } 
156             }
157             continue;
158           }
159         }
160       } else {
161         $this->fstabkey= "device";
162       }
163     }
164   }
167   function execute()
168   {
169     /* Call parent execute */
170     plugin::execute();
172     // Fill templating stuff
173     $smarty     = get_smarty();
174     $s_action   = "";
175     $s_entry    = "";
176     $display    = "";
178     // Remove partition
179     if(isset($_POST['addLvmPartition']) && isset($_POST['lvmPartitionAdd'])){
180       $name = $_POST['lvmPartitionAdd'];
181       $this->lvmDevices[$name] = $name;
182     }
184     /* Check all Posts if there is something usefull for us,
185      * For example : Delete is posted as Delete_1 
186      * The number specifies the index we want to delete
187      */
188     foreach($_POST as $name => $value){
189       if((preg_match("/RemovePartition_/",$name)) && 
190           $this->acl_is_removeable() && 
191           !preg_match("/freeze/i",$this->FAIstate)){
192         $tmp = split("_",$name);
193         $this->removePartition($tmp[1]);
194         break;
195       }
196       if(preg_match("/^EditPartition_/",$name)){
197         $id = preg_replace("/^EditPartition_/","",$name);
198         $id = preg_replace("/_.*$/","",$id);
199         if(isset($this->partitions[$id])){
200           $this->dialog = new faiPartition($this->config,$this->partitions[$id], $this,$this->FAIdiskType);
201           break;
202         }
203       } 
204     }
206     /* Create a new partition for this disk.
207      */
208     if(isset($_POST['AddPartition']) && !preg_match("/freeze/i",$this->FAIstate)){
209       $this->dialog = new faiPartition($this->config, array(), $this,$this->FAIdiskType);
210     }
212     /* Handle partition dialogs.
213      */
214     if($this->dialog instanceOf plugin && isset($_POST['PartitionCancel'])){
215       $this->dialog = null;
216     }
217     if($this->dialog instanceOf plugin && isset($_POST['PartitionSave'])){
218       $this->dialog->save_object();
220       $new_partition = $this->dialog->save(); 
221       $msgs = $this->dialog->check();
222       $msgs = array_merge($this->check_disks($new_partition));
224       if(!count($msgs)){
225         $this->updatePartition($new_partition);
226         $this->dialog = null;
227       }else{
228         msg_dialog::displayChecks($msgs);
229       }
230     }
231     if($this->dialog instanceOf plugin){
232       $this->dialog->save_object();
233       return($this->dialog->execute());
234     }
236     // Assign checkbox related values.
237     foreach($this->attributes as $attrs){
238       $smarty->assign($attrs,$this->$attrs);
239       if($this->$attrs){
240         $smarty->assign($attrs."CHK"," ");
241       }else{
242         $smarty->assign($attrs."CHK"," disabled ");
243       }
244     }
246     // Assign disk info to smarty.
247     $smarty->assign("setup", $this->generateParts());
248     $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
249     $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
250     $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
251     $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
252     $smarty->assign("fstabkey", $this->fstabkey);
253     $smarty->assign("disklabel", $this->disklabel);
254     $smarty->assign("FAIdiskType", $this->FAIdiskType);
255     $smarty->assign("plist", $this->getPartitionList());
256     $smarty->assign("physicalPartitionList", $this->getAvailablePartitions());
258     foreach($this->attributes as $attr){
259       $smarty->assign($attr,$this->$attr);
260     }
262     // Assign partitions
263     $tmp = $this->plInfo();
264     $sacl = "";
265     foreach($tmp['plProvidedAcls'] as $name => $translated){
266       $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
267       $smarty->assign($name."ACL",$acl);
268     }
269    
270     $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
271     return($display);
272   }
274   function getPartitionList()
275   {
276     $divlist = new divSelectBox("RaidList");
277     $disks = $this->parent->disks;
278     $objs = $disks;
279     foreach($disks as $disk){
280       foreach($disk['partitions'] as $id => $part){
281         $objs[$disk['cn']] = $part;
282       }
283     }
285     $list = array();
286     foreach($this->lvmDevices as $device){
287       $list[$device] = $device;
289       if(isset($objs[$device]['FAIpartitionSize'])){
290         $list[$device].= _("Size").":&nbsp;";
291         $list[$device].= $objs[$device]['FAIpartitionSize'];
292       }
293     }
294     return($list);
295   }
297   
298   function getAvailablePartitions(){
299     $array = array();
300     foreach($this->parent->disks as $disk){
301       foreach($disk['partitions'] as $key => $part){
302         $name = $disk['cn'];
303         if(!isset($this->lvmDevices[$name])){
304           $array[$name] = $name;
305         }
306       }
307     }
308     return($array);
310   }
313   /* Remove the selected partition and shift the following partitions 
314    *  to fill gap.
315    * Additionally update the partition numbers correspondingly.
316    */
317   function removePartition($id)
318   {
319     $start = false;
320     foreach($this->partitions as $key => $part){
321       if($id == $key){
322         $start = true;
323       }
324       if($start){
325         if($this->partitions[$key]['status'] == "edited"){
326           $this->deletePartitions[$key]= $this->partitions[$key];
327           $this->deletePartitions[$key]['FAIpartitionNr']=$key;
328           unset($this->partitions[$key]);
329         }else{
330           unset($this->partitions[$key]);
331         }
332         if(isset($this->partitions[($key+1)])){
333           if(isset($this->deletePartitions[$key])){
334             unset($this->deletePartitions[$key]);
335           }
336           $this->partitions[$key] = $this->partitions[($key+1)];
337           $this->partitions[$key]['FAIpartitionNr'] = $key;
338           $this->partitions[$key]['status'] = "new";
339         }
340       }
341     }
342     $tmp= array();
343     foreach($this->partitions as $part){
344       $tmp[count($tmp)+1]=$part;
345     }
346     $this->partitions = $tmp;
347   }
350   /* Add or update a partition 
351    */
352   function updatePartition($part)
353   {
354     if(!isset($part['FAIpartitionNr']) || $part['FAIpartitionNr'] == "undefined"){
355       $part['FAIpartitionNr'] = count($this->partitions) + 1;
356       $part['cn'] = $this->DISKcn.$part['FAIpartitionNr'];
357     }
358     $part['cn'] = $this->DISKcn.$part['FAIpartitionNr'];
359     $this->partitions[$part['FAIpartitionNr']] = $part;
360   }
363   function generateParts()
364   {
366     $divlist = new divSelectBox("DiskEntries"); 
367     foreach($this->partitions as $key => $part){
368       $number =array(
369           "string" => $part['FAIpartitionNr'],
370           "attach" => "style='width:20px;'");
371       $size   =array(
372           "string" => $part['FAIpartitionSize'],
373           "attach" => "style='width:100px;'");
374       $fstype =array(
375           "string" => $part['FAIfsType'],
376           "attach" => "style='width:60px;'");
377       $type   =array(
378           "string" => $part['FAIpartitionType'],
379           "attach" => "style='width:80px;'");
380       $opt    =array(
381           "string" => $part['FAImountOptions'],
382           "attach" => "style='width:80px;'");
383       $fsopt  =array(
384           "string" => $part['FAIfsOptions'],
385           "attach" => "style='width:80px;'");
386       $flags  =array(
387           "string" => $part['FAIpartitionFlags'],
388           "attach" => "style='width:80px;'");
389       $mntp   =array("string" => $part['FAImountPoint']);
391    
392       $action =array(
393           "string" => "<input type='image' src='images/lists/edit.png' name='EditPartition_".$key."'>".
394                       "<input type='image' src='images/lists/trash.png' name='RemovePartition_".$key."'>",
395           "attach" => "style='width:40px;'");
397  
398       $fields = array($number,$type,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
399       $divlist->AddEntry($fields);
400     }
401     return($divlist->DrawList());    
402   }
405   function save()
406   {
407     $tmp = array();
408     $tmp['cn']          = $this->DISKcn;
410     /* Attach partitions */
411     foreach($this->partitions as $key=>$val) {
412       $this->partitions[$key]['FAIpartitionNr']=$key;
413       $this->partitions[$key]['cn'] = $this->DISKcn.$key;
414     }
416     /* Attach deleted */
417     foreach($this->deletePartitions as $key=>$val) {
418       $this->partitions[$key."-delete"]=$val;
419       $this->partitions[$key."-delete"]['status']="delete";
420     }
422     $tmp['description'] = $this->DISKdescription;
423     $tmp['partitions']  = $this->partitions;
424     $tmp['status']      = $this->status;
425     $tmp['FAIdiskType'] = $this->FAIdiskType;
427     $tmp['FAIlvmDevice'] = array();
428     foreach($this->lvmDevices as $dev){
429       $tmp['FAIlvmDevice'][] = $dev;
430     } 
432     /* Assemble flags */
433     $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
435     /* If hdd name has changed, tell partitionTable to rename it */
436     if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
437       $tmp['rename']['from']  = $this->old_cn;
438       $tmp['rename']['to']    = $this->DISKcn;
439     }
441     // Build up disk options 
442     $bootable = "";
443     $resize = "";
444     $preserve_always = "";
445     $preserve_reinstall = "";
447     foreach($tmp['partitions'] as $id => $part){
448       if(isset($part['bootable']) && $part['bootable']){
449         $bootable .= $id.",";
450       }
451       if(isset($part['resize']) && $part['resize']){
452         $resize .= $id.",";
453       }
454       if(isset($part['preserve']) && $part['preserve']){
455         if($part['preserveType'] == "always"){
456           $preserve_always .= $id.",";
457         }else{
458           $preserve_reinstall .= $id.",";
459         }
460       }
461       $tmp['partitions'][$id]['status'] = $part['status'];
463       // Unset non valid attributes 
464       foreach(array("bootable","encrypted","preserve","preserveType","resize","FAIdiskType") as $attr){
465         if(isset($tmp['partitions'][$id][$attr])){
466           unset($tmp['partitions'][$id][$attr]);
467         }
468       }
469     }    
471     if(!empty($bootable)){
472       $tmp['FAIdiskOption'][] = "bootable:".trim($bootable,",");
473     }
474     if(!empty($resize)){
475       $tmp['FAIdiskOption'][] = "resize:".trim($resize,",");
476     }
477     if(!empty($preserve_always)){
478       $tmp['FAIdiskOption'][] = "preserve_always:".trim($preserve_always,",");
479     }
480     if(!empty($preserve_reinstall)){
481       $tmp['FAIdiskOption'][] = "preserve_reinstall:".trim($preserve_reinstall,",");
482     }
484     $tmp['status'] = $this->status;
485     return($tmp);
486   }
489   /* Save data to object */
490   function save_object()
491   {
492     if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
493       plugin::save_object();
495       // Save posted disk label and fstab key
496       if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
497         $this->disklabel= $_POST['disklabel'];
498       }
499       if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
500         $this->fstabkey= $_POST['fstabkey'];
501       }
502     }
503   }
506   /* Check supplied data */
507   function check()
508   {
509     /* Call common method to give check the hook */
510     $message= plugin::check();
511   
512     /* Check for an empty disk name */
513     $d = trim($this->DISKcn);
514     if($d == "" ){
515       $message[] = msgPool::required(_("Name"));
516     }
517     if(preg_match("/[^a-z0-9_\-]/i",$d)){
518       $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
519     }
520     return ($message);
521   }
524   /* Checks the disk combinations.
525    * 
526    */  
527   function check_disks($disk_to_add = array())
528   {
529     $msgs = array();
531     /* Check 'disk' combinations. 
532      *  - There can be four primary partitions.
533      *  - If there is at least one 'logical' partition, then there can be only 
534      *     three 'primary' partitions.
535      */    
536     if($this->FAIdiskType == "disk"){
537      
538       $types = array('logical' => array(), 'primary' => array());
539       $types[$disk_to_add['FAIpartitionType']][$disk_to_add['FAIpartitionNr']] = 1;
540       foreach($this->partitions as $key => $part){
541         $types[$part['FAIpartitionType']][$part['FAIpartitionNr']] = 1;
542       }
544       // There can only be four primary partitions per disk - without any logical partition.
545       if(count($types['logical']) == 0){
546         if(count($types['primary']) > 4){
547           $msgs[] = _("You have more than four primary partition table entries in your configuration, please check your configuration twice.");
548         }
549       }else{
550         if(count($types['primary']) > 3){
551           $msgs[] = _("You cannot have more than three primary partition while using logical partitions, please check your configuration twice.");
552         }
553       }
554     }
556     return($msgs);
557   }  
560   /* Return plugin informations for acl handling */
561   static function plInfo()
562   {
563     return (array(
564           "plShortName" => _("Partition table entry"),
565           "plDescription" => _("FAI partition table entry"),
566           "plSelfModify"  => FALSE,
567           "plDepends"     => array(),
568           "plPriority"    => 27,
569           "plSection"     => array("administration"),
570           "plCategory"    => array("fai"),
571           "plProvidedAcls" => array(
572             "DISKcn"           => _("Name"),
573             "DISKdescription"  => _("Description"),
574             "DISKFAIdiskOption"  => _("Disk options"),
575             "FAIpartitionType"  => _("Partition type"),
576             "FAIpartitionNr"    => _("Partition no."),
577             "FAIfsType"         => _("File system type"),
578             "FAImountPoint"     => _("Mount point"),
579             "FAIpartitionSize"  => _("Partition size"),
580             "FAImountOptions"   => _("Mount options"),
581             "FAIfsOptions"      => _("File system options"),
582             "FAIpartitionFlags" => _("Partition flags"))
583           ));
584   }
586  
589 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
590 ?>