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 = "";
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";
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 // Load attributes from existing partition
68 if($object){
70 foreach($this->attributes as $attr){
71 if(isset($object[$attr])){
72 $this->$attr = $object[$attr];
73 }
74 }
76 $this->status = $object['status'];
78 if($type == "disk" || $type =="lvm"){
80 /* Prepare size attribute
81 * This attribute may represent a range, a fixed value
82 * or a percentage.
83 * fixed is just a number * 500MB
84 * range * 500MB-1TB
85 * remaining * -
86 */
87 // Fixed
88 if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
89 $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
90 $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
91 $this->FAIpartitionSizeType = "fixed";
92 }else
94 // Dynamic range
95 if(preg_match("/^[0-9]*(KB|MB|GB|TB|PB|%)-[0-9]*(KB|MB|GB|TB|PB|%)$/",$this->FAIpartitionSize)){
96 $this->sizeStart = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\1",$this->FAIpartitionSize);
97 $this->sizeStart_Type = preg_replace("/^([0-9]*)(KB|MB|GB|TB|PB|%).*$/","\\2",$this->FAIpartitionSize);
98 $this->sizeStop = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\1",$this->FAIpartitionSize);
99 $this->sizeStop_Type = preg_replace("/^[^\-]*\-([0-9]*)(KB|MB|GB|TB|PB|%)$/","\\2",$this->FAIpartitionSize);
100 $this->FAIpartitionSizeType = "dynamic";
101 }else
103 // Dynamic range
104 if(preg_match("/^\-$/",$this->FAIpartitionSize)){
105 $this->FAIpartitionSizeType = "remaining";
107 }
109 /* Check for encrypted partitions
110 */
111 if(preg_match("/:encrypt$/",$this->FAImountPoint)){
112 $this->FAImountPoint = preg_replace("/:encrypt/","",$this->FAImountPoint);
113 $this->encrypted = TRUE;
114 }
116 }elseif($type == "raid"){
118 $usedDisks = split(",",$this->FAIpartitionSize);
119 foreach($usedDisks as $disk){
120 $name = preg_replace("/:.*$/","",$disk);
121 $spare = preg_match("/:spare/",$disk);
122 $missing = preg_match("/:missing/",$disk);
123 $this->raidDevices[$name] =
124 array(
125 "name" => $name,
126 "spare" => $spare,
127 "missing" => $missing);
128 }
129 }
130 }
131 }
133 function execute()
134 {
135 plugin::execute();
136 $smarty = get_smarty();
138 // Remove partition
139 if(isset($_POST['addPhysicalPartition']) && isset($_POST['physicalPartitionAdd'])){
140 $name = $_POST['physicalPartitionAdd'];
141 $this->raidDevices[$name] = array("name" => $name,"spare"=>false,"missing"=>false);
142 }
144 // Create a list of selectable partition types
145 if($this->FAIdiskType == "disk"){
147 if($this->disablePrimary){
148 $types = array(
149 "logical" => _("Logical"));
150 }else{
151 $types = array(
152 "primary" => _("Primary"),
153 "logical" => _("Logical"));
154 }
156 }elseif($this->FAIdiskType == "raid"){
157 $types = array(
158 "raid0" => _("RAID 0"),
159 "raid1" => _("RAID 1"),
160 "raid5" => _("RAID 5"),
161 "raid6" => _("RAID 6"));
162 }else{
163 $types = "";
164 }
166 // Create a list of all size options
167 $partitionSizeTypes = array(
168 "fixed" => _("fixed"),
169 "dynamic" => _("dynamic"),
170 "remaining" => _("remaining space")
171 );
173 // Create a list of all size options
174 $sizeTypes = array(
175 "KB" => _("KB"),
176 "MB" => _("MB"),
177 "GB" => _("GB"),
178 "TB" => _("TB"),
179 "PB" => _("PB"),
180 "%" => _("%")
181 );
183 // Preserve types
184 $preserveTypes = array(
185 "always" => _("always"),
186 "reinstall" => _("reinstall"));
188 // File system types.
189 $FAIfsTypes = array(
190 "swap" => _("swap space"),
191 "ext2" => _("ext2"),
192 "ext3" => _("ext3"),
193 "ext4" => _("ext4"),
194 "reiserfs" => _("reiser fs"),
195 "xfs" => _("xfs"),
196 "btrfs" => _("btrfs"));
198 $smarty->assign("partitionTypes", $types);
199 $smarty->assign("partitionSizeTypes", $partitionSizeTypes);
200 $smarty->assign("FAIpartitionSizeType", $this->FAIpartitionSizeType);
201 $smarty->assign("sizeTypes", $sizeTypes);
203 $smarty->assign("sizeStart_Type", $this->sizeStart_Type);
204 $smarty->assign("sizeStop_Type", $this->sizeStop_Type);
205 $smarty->assign("sizeStart", $this->sizeStart);
206 $smarty->assign("sizeStop", $this->sizeStop);
208 $smarty->assign("preserveTypes", $preserveTypes);
209 $smarty->assign("preserveType", $this->preserveType);
211 $smarty->assign("FAIfsTypes", $FAIfsTypes);
212 $smarty->assign("cn", $this->cn);
214 $smarty->assign("plist",$this->getRaidlist());
215 $smarty->assign("physicalPartitionList",$this->getPartitionlist());
216 $smarty->assign("disablePrimary", $this->disablePrimary);
218 foreach($this->attributes as $attr){
219 $smarty->assign($attr,$this->$attr);
220 }
221 return($smarty->fetch(get_template_path("faiPartition.tpl", TRUE, dirname(__FILE__))));
222 }
225 function getPartitionList()
226 {
227 $array = array();
228 foreach($this->parent->parent->disks as $disk){
229 if($disk['FAIdiskType'] != "raid"){
230 foreach($disk['partitions'] as $key => $part){
231 $name = $part['cn'];
232 if(!isset($this->raidDevices[$name])){
233 $array[$name] = $name." (".$disk['cn'].")";
234 }
235 }
236 }
237 }
238 return($array);
239 }
242 function getRaidList()
243 {
244 $divlist = new divSelectBox("RaidList");
246 $disks = $this->parent->parent->disks;
247 $objs = array();
248 foreach($disks as $disk){
249 if($disk['FAIdiskType'] != "raid"){
250 foreach($disk['partitions'] as $id => $part){
251 $objs[$part['cn']] = $part;
252 }
253 }
254 }
256 $list = array();
257 foreach($this->raidDevices as $device){
258 $list[$device['name']] = $device['name'];
260 if(isset($objs[$device['name']]['FAIpartitionSize'])){
261 $list[$device['name']].= _("Size").": ";
262 $list[$device['name']].= $objs[$device['name']]['FAIpartitionSize'];
263 }
265 $list[$device['name']].= " "._("Options").": ";
266 if($device['spare']){
267 $list[$device['name']].= " "._("spare")." ";
268 }
269 if($device['missing']){
270 $list[$device['name']].= " "._("missing")." ";
271 }
272 }
273 return($list);
274 }
276 function save_object()
277 {
278 if(isset($_POST['faiPartition'])){
279 foreach($this->attributes as $attr){
280 if(isset($_POST[$attr])){
281 $this->$attr = get_post($attr);
282 }
283 }
284 foreach(array("FAIpartitionSizeType","sizeStart","sizeStop","sizeStart_Type","sizeStop_Type") as $attr){
285 if(isset($_POST[$attr])){
286 $this->$attr = get_post($attr);
287 }
288 }
289 foreach(array("bootable","preserve","resize","encrypted") as $attr){
290 if(isset($_POST[$attr])){
291 $this->$attr = TRUE;
292 }else{
293 $this->$attr = FALSE;
294 }
295 }
297 // Allow user defined partition names for lvm disks.
298 if($this->FAIdiskType == "lvm" && isset($_POST['cn'])){
299 $this->cn = get_post('cn');
300 }
302 // Remove partition
303 if(isset($_POST['delPhysicalPartition']) && isset($_POST['physicalPartition'])){
304 unset($this->raidDevices[$_POST['physicalPartition']]);
305 }
307 // Toggle spare flag for partition entries
308 if(isset($_POST['toggleSpare']) && isset($_POST['physicalPartition'])){
309 $this->raidDevices[$_POST['physicalPartition']]['spare'] =
310 !$this->raidDevices[$_POST['physicalPartition']]['spare'];
311 }
313 // Toggle missing flag for partition entries
314 if(isset($_POST['toggleMissing']) && isset($_POST['physicalPartition'])){
315 $this->raidDevices[$_POST['physicalPartition']]['missing'] =
316 !$this->raidDevices[$_POST['physicalPartition']]['missing'];
317 }
318 }
319 }
322 function check()
323 {
324 $msgs = plugin::check();
326 // Check the given partition size.
327 if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
328 if($this->FAIpartitionSizeType == "fixed" || $this->FAIpartitionSizeType == "dynamic"){
329 if(!is_numeric($this->sizeStart)){
330 $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStart,"/[0-9]/i");
331 }
332 }
333 if($this->FAIpartitionSizeType == "dynamic"){
334 if(!is_numeric($this->sizeStop)){
335 $msgs[] = msgPool::invalid(_("Partition size"),$this->sizeStop,"/[0-9]/i");
336 }
338 $mp = array(
339 "%" => 1,
340 "KB" => pow(1024,0),
341 "MB" => pow(1024,1),
342 "GB" => pow(1024,2),
343 "TB" => pow(1024,3),
344 "PB" => pow(1024,4));
345 $res1 = $this->sizeStart * $mp[$this->sizeStart_Type];
346 $res2 = $this->sizeStop * $mp[$this->sizeStop_Type];
347 if($res1 > $res2){
348 $msgs[] = msgPool::toobig(_("Minimum partition size"), "'"._("Maximum partition size")."'");
349 }
350 }
351 }
352 if($this->FAIdiskType == "raid"){
353 #FIME raid checks missing
354 echo "Add raid checks here, disk combinations are not verified right now.";
355 }
357 // check mount point
358 if($this->FAIfsType != "swap"){
359 if(!preg_match("#^/#",$this->FAImountPoint)){
360 $msgs[] = msgPool::invalid(_("Mount point"));
361 }
362 }
364 return($msgs);
365 }
368 function save()
369 {
370 $ret = array();
371 foreach($this->attributes as $attr){
372 $ret[$attr] = $this->$attr;
373 }
375 // Save partition size
376 if($this->FAIdiskType == "disk" || $this->FAIdiskType == "lvm"){
377 switch($this->FAIpartitionSizeType){
378 case 'fixed' :
379 $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type;break;
380 case 'dynamic' :
381 $ret['FAIpartitionSize'] = $this->sizeStart.$this->sizeStart_Type."-".
382 $this->sizeStop.$this->sizeStop_Type;break;
383 case 'remaining' :
384 $ret['FAIpartitionSize'] = "-";break;
385 default: trigger_error("Unknown partition size!");
386 }
388 // Add encryption flag to partition mount point
389 if($this->encrypted){
390 $ret['FAImountPoint'] .= ":encrypt";
391 }
393 }elseif($this->FAIdiskType == "raid"){
394 $ret['FAIpartitionSize'] = "";
395 foreach($this->raidDevices as $device){
396 $ret['FAIpartitionSize'] .= $device['name'];
397 if($device['spare']){
398 $ret['FAIpartitionSize'] .= ":spare";
399 }
400 if($device['missing']){
401 $ret['FAIpartitionSize'] .= ":missing";
402 }
403 $ret['FAIpartitionSize'] .= ",";
404 }
405 $ret['FAIpartitionSize'] = trim($ret['FAIpartitionSize'],",");
406 }
407 $ret['status'] = $this->status;
409 if($this->FAIfsType == "swap"){
410 $ret['FAImountPoint'] = "swap";
411 }
413 return($ret);
414 }
415 }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>