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 = "";
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";
29 var $lvmDevices = array();
31 function faiDiskEntry (&$config, $dn= NULL,$parent,$disk,$type)
32 {
33 plugin::plugin ($config, $dn);
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($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 }
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] = array("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();
219 $this->updatePartition($this->dialog->save());
220 $this->dialog = null;
221 }
222 if($this->dialog instanceOf plugin){
223 $this->dialog->save_object();
224 return($this->dialog->execute());
225 }
227 // Assign checkbox related values.
228 foreach($this->attributes as $attrs){
229 $smarty->assign($attrs,$this->$attrs);
230 if($this->$attrs){
231 $smarty->assign($attrs."CHK"," ");
232 }else{
233 $smarty->assign($attrs."CHK"," disabled ");
234 }
235 }
237 // Assign disk info to smarty.
238 $smarty->assign("setup", $this->generateParts());
239 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
240 $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
241 $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
242 $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
243 $smarty->assign("fstabkey", $this->fstabkey);
244 $smarty->assign("disklabel", $this->disklabel);
245 $smarty->assign("FAIdiskType", $this->FAIdiskType);
246 $smarty->assign("plist", $this->getPartitionList());
247 $smarty->assign("physicalPartitionList", $this->getAvailablePartitions());
249 foreach($this->attributes as $attr){
250 $smarty->assign($attr,$this->$attr);
251 }
253 // Assign partitions
254 $tmp = $this->plInfo();
255 $sacl = "";
256 foreach($tmp['plProvidedAcls'] as $name => $translated){
257 $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
258 $smarty->assign($name."ACL",$acl);
259 }
261 $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
262 return($display);
263 }
265 function getPartitionList()
266 {
267 $divlist = new divSelectBox("RaidList");
268 $disks = $this->parent->disks;
269 $objs = $disks;
270 foreach($disks as $disk){
271 foreach($disk['partitions'] as $id => $part){
272 $objs[$disk['cn'].$id] = $part;
273 }
274 }
276 $list = array();
277 foreach($this->lvmDevices as $device){
278 $list[$device] = $device;
280 if(isset($objs[$device]['FAIpartitionSize'])){
281 $list[$device].= _("Size").": ";
282 $list[$device].= $objs[$device]['FAIpartitionSize'];
283 }
284 }
285 return($list);
286 }
289 function getAvailablePartitions(){
290 $array = array();
291 foreach($this->parent->disks as $disk){
292 foreach($disk['partitions'] as $key => $part){
293 $name = $disk['cn'].$key;
294 if(!isset($this->lvmDevices[$name])){
295 $array[$name] = $name;
296 }
297 }
298 }
299 return($array);
301 }
304 /* Remove the selected partition and shift the following partitions
305 * to fill gap.
306 * Additionally update the partition numbers correspondingly.
307 */
308 function removePartition($id)
309 {
310 $start = false;
311 foreach($this->partitions as $key => $part){
312 if($id == $key){
313 $start = true;
314 }
315 if($start){
316 if($this->partitions[$key]['status'] == "edited"){
317 $this->deletePartitions[$key]= $this->partitions[$key];
318 $this->deletePartitions[$key]['FAIpartitionNr']=$key;
319 unset($this->partitions[$key]);
320 }else{
321 unset($this->partitions[$key]);
322 }
323 if(isset($this->partitions[($key+1)])){
324 if(isset($this->deletePartitions[$key])){
325 unset($this->deletePartitions[$key]);
326 }
327 $this->partitions[$key] = $this->partitions[($key+1)];
328 $this->partitions[$key]['FAIpartitionNr'] = $key;
329 $this->partitions[$key]['status'] = "new";
330 }
331 }
332 }
333 $tmp= array();
334 foreach($this->partitions as $part){
335 $tmp[count($tmp)+1]=$part;
336 }
337 $this->partitions = $tmp;
338 }
341 /* Add or update a partition
342 */
343 function updatePartition($part)
344 {
345 if(!isset($part['FAIpartitionNr']) || $part['FAIpartitionNr'] == "undefined"){
346 $part['FAIpartitionNr'] = count($this->partitions) + 1;
347 $part['cn'] = count($this->partitions) + 1;
348 }
349 $this->partitions[$part['FAIpartitionNr']] = $part;
350 }
353 function generateParts()
354 {
356 $divlist = new divSelectBox("DiskEntries");
357 foreach($this->partitions as $key => $part){
358 $number =array(
359 "string" => $part['FAIpartitionNr'],
360 "attach" => "style='width:20px;'");
361 $size =array(
362 "string" => $part['FAIpartitionSize'],
363 "attach" => "style='width:100px;'");
364 $fstype =array(
365 "string" => $part['FAIfsType'],
366 "attach" => "style='width:60px;'");
367 $type =array(
368 "string" => $part['FAIpartitionType'],
369 "attach" => "style='width:80px;'");
370 $opt =array(
371 "string" => $part['FAImountOptions'],
372 "attach" => "style='width:80px;'");
373 $fsopt =array(
374 "string" => $part['FAIfsOptions'],
375 "attach" => "style='width:80px;'");
376 $flags =array(
377 "string" => $part['FAIpartitionFlags'],
378 "attach" => "style='width:80px;'");
379 $mntp =array("string" => $part['FAImountPoint']);
382 $action =array(
383 "string" => "<input type='image' src='images/lists/edit.png' name='EditPartition_".$key."'>".
384 "<input type='image' src='images/lists/trash.png' name='RemovePartition_".$key."'>",
385 "attach" => "style='width:40px;'");
388 $fields = array($number,$type,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
389 $divlist->AddEntry($fields);
390 }
391 return($divlist->DrawList());
392 }
395 function save()
396 {
397 $tmp = array();
398 $tmp['cn'] = $this->DISKcn;
400 /* Attach partitions */
401 foreach($this->partitions as $key=>$val) {
402 $this->partitions[$key]['FAIpartitionNr']=$key;
403 $this->partitions[$key]['cn']=$key;
404 }
406 /* Attach deleted */
407 foreach($this->deletePartitions as $key=>$val) {
408 $this->partitions[$key."-delete"]=$val;
409 $this->partitions[$key."-delete"]['status']="delete";
410 }
412 $tmp['description'] = $this->DISKdescription;
413 $tmp['partitions'] = $this->partitions;
414 $tmp['status'] = $this->status;
415 $tmp['FAIdiskType'] = $this->FAIdiskType;
417 $tmp['FAIlvmDevice'] = array();
418 foreach($this->lvmDevices as $dev){
419 $tmp['FAIlvmDevice'][] = $dev;
420 }
422 /* Assemble flags */
423 $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
425 /* If hdd name has changed, tell partitionTable to rename it */
426 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
427 $tmp['rename']['from'] = $this->old_cn;
428 $tmp['rename']['to'] = $this->DISKcn;
429 }
431 // Build up disk options
432 $bootable = "";
433 $resize = "";
434 $preserve_always = "";
435 $preserve_reinstall = "";
437 foreach($tmp['partitions'] as $id => $part){
438 if(isset($part['bootable']) && $part['bootable']){
439 $bootable .= $id.",";
440 }
441 if(isset($part['resize']) && $part['resize']){
442 $resize .= $id.",";
443 }
444 if(isset($part['preserve']) && $part['preserve']){
445 if($part['preserveType'] == "always"){
446 $preserve_always .= $id.",";
447 }else{
448 $preserve_reinstall .= $id.",";
449 }
450 }
451 $tmp['partitions'][$id]['status'] = $part['status'];
453 // Unset non valid attributes
454 foreach(array("bootable","encrypted","preserve","preserveType","resize","FAIdiskType") as $attr){
455 if(isset($tmp['partitions'][$id][$attr])){
456 unset($tmp['partitions'][$id][$attr]);
457 }
458 }
459 }
461 if(!empty($bootable)){
462 $tmp['FAIdiskOption'][] = "bootable:".trim($bootable,",");
463 }
464 if(!empty($resize)){
465 $tmp['FAIdiskOption'][] = "resize:".trim($resize,",");
466 }
467 if(!empty($preserve_always)){
468 $tmp['FAIdiskOption'][] = "preserve_always:".trim($preserve_always,",");
469 }
470 if(!empty($preserve_reinstall)){
471 $tmp['FAIdiskOption'][] = "preserve_reinstall:".trim($preserve_reinstall,",");
472 }
474 $tmp['status'] = $this->status;
475 return($tmp);
476 }
479 /* Save data to object */
480 function save_object()
481 {
482 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
483 plugin::save_object();
485 // Save posted disk label and fstab key
486 if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
487 $this->disklabel= $_POST['disklabel'];
488 }
489 if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
490 $this->fstabkey= $_POST['fstabkey'];
491 }
492 }
493 }
496 /* Check supplied data */
497 function check()
498 {
499 /* Call common method to give check the hook */
500 $message= plugin::check();
502 /* Check for an empty disk name */
503 $d = trim($this->DISKcn);
504 if($d == "" ){
505 $message[] = msgPool::required(_("Name"));
506 }
507 if(preg_match("/[^a-z0-9_\-]/i",$d)){
508 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
509 }
511 /* check every partition.
512 * if there is an invalid value defined, append an errorstr to message
513 */
515 /* Array that contain every partitionname mountpoint etc already assigned */
516 $alreadyUsed = array();
517 foreach($this->UsedAttrs as $attrs){
518 $alreadyUsed[$attrs] = array();
519 }
521 $cnt = 0;
522 foreach($this->partitions as $key => $part){
523 if($part['FAIpartitionType'] == "primary"){
524 $cnt ++ ;
525 }
526 }
527 if($cnt > 3){
528 $message[] = _("You have more than 3 primary partition table entries in your configuration, please check your configuration twice.");
529 }
531 return ($message);
532 }
536 /* Return plugin informations for acl handling */
537 static function plInfo()
538 {
539 return (array(
540 "plShortName" => _("Partition table entry"),
541 "plDescription" => _("FAI partition table entry"),
542 "plSelfModify" => FALSE,
543 "plDepends" => array(),
544 "plPriority" => 27,
545 "plSection" => array("administration"),
546 "plCategory" => array("fai"),
547 "plProvidedAcls" => array(
548 "DISKcn" => _("Name"),
549 "DISKdescription" => _("Description"),
550 "DISKFAIdiskOption" => _("Disk options"),
551 "FAIpartitionType" => _("Partition type"),
552 "FAIpartitionNr" => _("Partition no."),
553 "FAIfsType" => _("File system type"),
554 "FAImountPoint" => _("Mount point"),
555 "FAIpartitionSize" => _("Partition size"),
556 "FAImountOptions" => _("Mount options"),
557 "FAIfsOptions" => _("File system options"),
558 "FAIpartitionFlags" => _("Partition flags"))
559 ));
560 }
563 }
565 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
566 ?>