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