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(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 }
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[$id])){
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[$presflag])){
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 // Add partition to lvm compilation.
179 if(isset($_POST['addLvmPartition']) && isset($_POST['lvmPartitionAdd'])){
180 $name = get_post('lvmPartitionAdd');
181 $this->lvmDevices[$name] = $name;
182 }
184 // Remove partition from lvm compilation.
185 if(isset($_POST['delLvmPartition']) && isset($_POST['physicalPartition'])){
186 $names = $_POST['physicalPartition'];
187 foreach($names as $name){
188 if(isset($this->lvmDevices[$name])){
189 unset($this->lvmDevices[$name]);
190 }
191 }
192 }
194 /* Check all Posts if there is something usefull for us,
195 * For example : Delete is posted as Delete_1
196 * The number specifies the index we want to delete
197 */
198 foreach($_POST as $name => $value){
199 if((preg_match("/RemovePartition_/",$name)) &&
200 $this->acl_is_removeable() &&
201 !preg_match("/freeze/i",$this->FAIstate)){
202 $tmp = split("_",$name);
203 $this->removePartition($tmp[1]);
204 break;
205 }
206 if(preg_match("/^EditPartition_/",$name)){
207 $id = preg_replace("/^EditPartition_/","",$name);
208 $id = preg_replace("/_.*$/","",$id);
209 if(isset($this->partitions[$id])){
210 $this->dialog = new faiPartition($this->config,$this->partitions[$id], $this,$this->FAIdiskType);
211 break;
212 }
213 }
214 }
216 /* Create a new partition for this disk.
217 */
218 if(isset($_POST['AddPartition']) && !preg_match("/freeze/i",$this->FAIstate)){
219 $this->dialog = new faiPartition($this->config, array(), $this,$this->FAIdiskType);
220 }
222 /* Handle partition dialogs.
223 */
224 if($this->dialog instanceOf plugin && isset($_POST['PartitionCancel'])){
225 $this->dialog = null;
226 }
227 if($this->dialog instanceOf plugin && isset($_POST['PartitionSave'])){
228 $this->dialog->save_object();
230 $new_partition = $this->dialog->save();
231 $msgs = $this->dialog->check();
232 $msgs = array_merge($this->check_disks($new_partition));
234 if(!count($msgs)){
235 $this->updatePartition($new_partition);
236 $this->dialog = null;
237 }else{
238 msg_dialog::displayChecks($msgs);
239 }
240 }
241 if($this->dialog instanceOf plugin){
242 $this->dialog->save_object();
243 return($this->dialog->execute());
244 }
246 // Assign checkbox related values.
247 foreach($this->attributes as $attrs){
248 $smarty->assign($attrs,$this->$attrs);
249 if($this->$attrs){
250 $smarty->assign($attrs."CHK"," ");
251 }else{
252 $smarty->assign($attrs."CHK"," disabled ");
253 }
254 }
256 // Assign disk info to smarty.
257 $smarty->assign("setup", $this->generateParts());
258 $smarty->assign("sub_object_is_createable",$this->acl_is_createable());
259 $smarty->assign("freeze",preg_match("/freeze/i",$this->FAIstate));
260 $smarty->assign("fstabkeys", array("device" => _("Device"), "label" => _("Label"), "uuid" => _("UUID")));
261 $smarty->assign("disklabels", array("msdos" => "MSDOS", "gpt" => "GPT"));
262 $smarty->assign("fstabkey", $this->fstabkey);
263 $smarty->assign("disklabel", $this->disklabel);
264 $smarty->assign("FAIdiskType", $this->FAIdiskType);
265 $smarty->assign("plist", $this->getPartitionList());
266 $smarty->assign("physicalPartitionList", $this->getAvailablePartitions());
268 foreach($this->attributes as $attr){
269 $smarty->assign($attr,$this->$attr);
270 }
272 // Assign partitions
273 $tmp = $this->plInfo();
274 $sacl = "";
275 foreach($tmp['plProvidedAcls'] as $name => $translated){
276 $acl = $this->getacl($name, preg_match("/freeze/i",$this->FAIstate));
277 $smarty->assign($name."ACL",$acl);
278 }
280 $display.= $smarty->fetch(get_template_path('faiDiskEntry.tpl', TRUE));
281 return($display);
282 }
284 function getPartitionList()
285 {
286 $divlist = new divSelectBox("RaidList");
288 /* Create a list of all available disks and partitions.
289 * This list will then be used to display detailed info.
290 */
291 $disks = $this->parent->disks;
292 foreach($disks as $dname => $disk){
294 // Skip currently edited disk
295 if($disk['cn'] == $this->old_cn) continue;
297 // Add disk
298 $objs[$dname] = $disk;
300 // Add disk partitions
301 foreach($disk['partitions'] as $id => $part){
302 $part['parentDisk'] = $disk;
303 $objs[$part['cn']] = $part;
304 }
305 }
307 // Attach current disk setup to the details list.
308 $data = $this->save();
309 $objs[$data['cn']] = $data;
310 foreach($data['partitions'] as $part){
311 $part['parentDisk'] = $data;
312 $objs[$part['cn']] = $part;
313 }
315 // Walk through physical partition combinations and build up
316 // user friendly list with partition details.
317 $list = array();
318 foreach($this->lvmDevices as $device){
320 // We've a html select box here, add spaces for better readability
321 $str = $device;
322 $str = preg_replace("/ /"," ",str_pad($str,20," "));
324 // Add disk/partition details.
325 if(isset($objs[$device])){
326 if(isset($objs[$device]['FAIpartitionSize'])){
327 if($objs[$device]['parentDisk']['FAIdiskType'] == "raid"){
328 $str .= _("Disks").": ";
329 $str .= $objs[$device]['FAIpartitionSize'];
330 }else{
331 $str .= _("Size").": ";
332 $str .= $objs[$device]['FAIpartitionSize'];
333 }
334 }
335 }
336 $list[$device] = $str;
337 }
338 return($list);
339 }
342 /* Returns a list of available partitions that are useable in this
343 * lvm disk setup.
344 */
345 function getAvailablePartitions()
346 {
347 $array = array();
348 foreach($this->parent->disks as $disk){
350 // We can't add ourselves to the available partitions
351 if($disk['cn'] == $this->old_cn) continue;
353 // Add partitions to the list.
354 foreach($disk['partitions'] as $key => $part){
355 $name = $part['cn'];
356 if(!isset($this->lvmDevices[$name])){
357 $array[$name] = $name;
358 }
359 }
360 }
361 return($array);
363 }
366 /* Remove the selected partition and shift the following partitions
367 * to fill gap.
368 * Additionally update the partition numbers correspondingly.
369 */
370 function removePartition($id)
371 {
372 $start = false;
373 foreach($this->partitions as $key => $part){
374 if($id == $key){
375 $start = true;
376 }
377 if($start){
378 if($this->partitions[$key]['status'] == "edited"){
379 $this->deletePartitions[$key]= $this->partitions[$key];
380 $this->deletePartitions[$key]['FAIpartitionNr']=$key;
381 unset($this->partitions[$key]);
382 }else{
383 unset($this->partitions[$key]);
384 }
385 if(isset($this->partitions[($key+1)])){
386 if(isset($this->deletePartitions[$key])){
387 unset($this->deletePartitions[$key]);
388 }
389 $this->partitions[$key] = $this->partitions[($key+1)];
390 $this->partitions[$key]['FAIpartitionNr'] = $key;
391 $this->partitions[$key]['status'] = "new";
392 }
393 }
394 }
395 $tmp= array();
396 foreach($this->partitions as $part){
397 $tmp[count($tmp)+1]=$part;
398 }
399 $this->partitions = $tmp;
400 }
403 /* Add or update a partition
404 */
405 function updatePartition($part)
406 {
407 if(!isset($part['FAIpartitionNr']) || $part['FAIpartitionNr'] == "undefined"){
408 $part['FAIpartitionNr'] = count($this->partitions) + 1;
409 }
411 /* Update the disk cn -
412 * Do NOT touch the partition 'cn' in case of lvm devices.
413 */
414 if($this->FAIdiskType == "disk"){
415 $part['cn'] = $this->DISKcn.$part['FAIpartitionNr'];
416 }
418 /* Check if we've to update lvm compilations.
419 */
420 if($this->FAIdiskType == "lvm"){
421 if(isset($this->partitions[$part['FAIpartitionNr']])){
422 $old_cn = $this->partitions[$part['FAIpartitionNr']]['cn'];
423 $new_cn = $part['cn'];
424 if(isset($this->lvmDevices[$old_cn])){
425 unset($this->lvmDevices[$old_cn]);
426 $this->lvmDevices[$new_cn] = $new_cn;
427 }
428 }
429 }
431 $this->partitions[$part['FAIpartitionNr']] = $part;
432 }
435 function generateParts()
436 {
438 $divlist = new divSelectBox("DiskEntries");
439 foreach($this->partitions as $key => $part){
440 $cn =array(
441 "string" => $part['cn'],
442 "attach" => "style='width:20px;'");
443 $number =array(
444 "string" => $part['FAIpartitionNr'],
445 "attach" => "style='width:20px;'");
446 $size =array(
447 "string" => $part['FAIpartitionSize'],
448 "attach" => "style='width:100px;'");
449 $fstype =array(
450 "string" => $part['FAIfsType'],
451 "attach" => "style='width:60px;'");
452 $type =array(
453 "string" => $part['FAIpartitionType'],
454 "attach" => "style='width:80px;'");
455 $opt =array(
456 "string" => $part['FAImountOptions'],
457 "attach" => "style='width:80px;'");
458 $fsopt =array(
459 "string" => $part['FAIfsOptions'],
460 "attach" => "style='width:80px;'");
461 $flags =array(
462 "string" => $part['FAIpartitionFlags'],
463 "attach" => "style='width:80px;'");
464 $mntp =array("string" => $part['FAImountPoint']);
467 $action =array(
468 "string" => "<input type='image' src='images/lists/edit.png' name='EditPartition_".$key."'>".
469 "<input type='image' src='images/lists/trash.png' name='RemovePartition_".$key."'>",
470 "attach" => "style='width:40px; border-right: 0px;'");
472 if($this->FAIdiskType == "lvm"){
473 $fields = array($cn,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
474 }else{
475 $fields = array($number,$type,$mntp,$size,$fstype, $opt,$fsopt,$flags,$action);
476 }
477 $divlist->AddEntry($fields);
478 }
479 return($divlist->DrawList());
480 }
483 function save()
484 {
485 $tmp = array();
486 $tmp['cn'] = $this->DISKcn;
488 /* Attach partitions */
489 foreach($this->partitions as $key=>$val) {
490 $this->partitions[$key]['FAIpartitionNr']=$key;
492 if($this->FAIdiskType == "disk"){
493 $this->partitions[$key]['cn'] = $this->DISKcn.$key;
494 }elseif($this->FAIdiskType == "lvm"){
495 $this->partitions[$key]['FAIpartitionType'] = 'lvm';
496 }
497 }
499 /* Attach deleted */
500 foreach($this->deletePartitions as $key=>$val) {
501 $this->partitions[$key."-delete"]=$val;
502 $this->partitions[$key."-delete"]['status']="delete";
503 }
505 $tmp['description'] = $this->DISKdescription;
506 $tmp['partitions'] = $this->partitions;
507 $tmp['status'] = $this->status;
508 $tmp['FAIdiskType'] = $this->FAIdiskType;
510 $tmp['FAIlvmDevice'] = array();
511 foreach($this->lvmDevices as $dev){
512 $tmp['FAIlvmDevice'][] = $dev;
513 }
515 /* Assemble flags */
516 $tmp['FAIdiskOption'] = array("fstabkey:".$this->fstabkey, "disklabel:".$this->disklabel);
518 /* If hdd name has changed, tell partitionTable to rename it */
519 if(($this->is_edit)&&($this->old_cn != $this->DISKcn)){
520 $tmp['rename']['from'] = $this->old_cn;
521 $tmp['rename']['to'] = $this->DISKcn;
522 }
524 // Build up disk options
525 $bootable = "";
526 $resize = "";
527 $preserve_always = "";
528 $preserve_reinstall = "";
530 foreach($tmp['partitions'] as $id => $part){
531 if(isset($part['bootable']) && $part['bootable']){
532 $bootable .= $id.",";
533 }
534 if(isset($part['resize']) && $part['resize']){
535 $resize .= $id.",";
536 }
537 if(isset($part['preserve']) && $part['preserve']){
538 if($part['preserveType'] == "always"){
539 $preserve_always .= $id.",";
540 }else{
541 $preserve_reinstall .= $id.",";
542 }
543 }
544 $tmp['partitions'][$id]['status'] = $part['status'];
546 // Unset non valid attributes
547 foreach(array("bootable","encrypted","preserve","preserveType","resize","FAIdiskType") as $attr){
548 if(isset($tmp['partitions'][$id][$attr])){
549 unset($tmp['partitions'][$id][$attr]);
550 }
551 }
552 }
554 if(!empty($bootable)){
555 $tmp['FAIdiskOption'][] = "bootable:".trim($bootable,",");
556 }
557 if(!empty($resize)){
558 $tmp['FAIdiskOption'][] = "resize:".trim($resize,",");
559 }
560 if(!empty($preserve_always)){
561 $tmp['FAIdiskOption'][] = "preserve_always:".trim($preserve_always,",");
562 }
563 if(!empty($preserve_reinstall)){
564 $tmp['FAIdiskOption'][] = "preserve_reinstall:".trim($preserve_reinstall,",");
565 }
567 $tmp['status'] = $this->status;
568 return($tmp);
569 }
572 /* Save data to object */
573 function save_object()
574 {
575 if((isset($_POST['TableEntryFrameSubmitted'])) && !preg_match("/freeze/", $this->FAIstate) ){
576 plugin::save_object();
578 // Save posted disk label and fstab key
579 if (isset($_POST['disklabel']) && preg_match("/^(msdos|gpt)$/", $_POST['disklabel'])){
580 $this->disklabel= $_POST['disklabel'];
581 }
582 if (isset($_POST['fstabkey']) && preg_match("/^(device|label|uuid)$/", $_POST['fstabkey'])){
583 $this->fstabkey= $_POST['fstabkey'];
584 }
585 }
586 }
589 /* Check supplied data */
590 function check()
591 {
592 /* Call common method to give check the hook */
593 $message= plugin::check();
595 /* Check for an empty disk name */
596 $d = trim($this->DISKcn);
597 if($d == "" ){
598 $message[] = msgPool::required(_("Name"));
599 }
600 if(preg_match("/[^a-z0-9_\-]/i",$d)){
601 $message[] = msgPool::invalid(_("Name"),$d,"/[a-z0-9_\-]/i");
602 }
603 return ($message);
604 }
607 /* Checks the disk combinations.
608 *
609 */
610 function check_disks($disk_to_add = array())
611 {
612 $msgs = array();
614 /* Check 'disk' combinations.
615 * - There can be four primary partitions.
616 * - If there is at least one 'logical' partition, then there can be only
617 * three 'primary' partitions.
618 */
619 if($this->FAIdiskType == "disk"){
621 $types = array('logical' => array(), 'primary' => array());
622 $types[$disk_to_add['FAIpartitionType']][$disk_to_add['FAIpartitionNr']] = 1;
623 foreach($this->partitions as $key => $part){
624 $types[$part['FAIpartitionType']][$part['FAIpartitionNr']] = 1;
625 }
627 // There can only be four primary partitions per disk - without any logical partition.
628 if(count($types['logical']) == 0){
629 if(count($types['primary']) > 4){
630 $msgs[] = _("You have more than four primary partition table entries in your configuration, please check your configuration twice.");
631 }
632 }else{
633 if(count($types['primary']) > 3){
634 $msgs[] = _("You cannot have more than three primary partition while using logical partitions, please check your configuration twice.");
635 }
636 }
637 }
639 return($msgs);
640 }
643 /* Return plugin informations for acl handling */
644 static function plInfo()
645 {
646 return (array(
647 "plShortName" => _("Partition table entry"),
648 "plDescription" => _("FAI partition table entry"),
649 "plSelfModify" => FALSE,
650 "plDepends" => array(),
651 "plPriority" => 27,
652 "plSection" => array("administration"),
653 "plCategory" => array("fai"),
654 "plProvidedAcls" => array(
655 "DISKcn" => _("Name"),
656 "DISKdescription" => _("Description"),
657 "DISKFAIdiskOption" => _("Disk options"),
658 "FAIpartitionType" => _("Partition type"),
659 "FAIpartitionNr" => _("Partition no."),
660 "FAIfsType" => _("File system type"),
661 "FAImountPoint" => _("Mount point"),
662 "FAIpartitionSize" => _("Partition size"),
663 "FAImountOptions" => _("Mount options"),
664 "FAIfsOptions" => _("File system options"),
665 "FAIpartitionFlags" => _("Partition flags"))
666 ));
667 }
670 }
672 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
673 ?>