1 <?php
4 define('DISK' , 0);
5 define('PARTITION' , 1);
6 define('RAID_DEVICE' , 2);
7 define('VOLUME_GROUP' , 3);
8 define('VOLUME' , 4);
10 class AddPartitionDialog
11 {
12 public $partitionObject;
13 public $disk = array();
14 public $diskList = array();
15 public $paritions = array();
16 public $selected_type = DISK;
18 // Disk properties
19 public $d_name = "";
21 // LVM Volume group properties
22 public $vg_name = "";
23 public $vg_partitions = array();
24 public $volumeGroups = array();
25 public $freeLvmPartitions = array();
27 // Volume properties
28 public $v_name = "";
29 public $v_group = "";
30 public $v_mountPoint = "";
31 public $v_fsType = "ext3";
32 public $v_fsOptions = "";
33 public $v_size = 1000;
34 public $v_encrypt = FALSE;
35 public $volumeGroupList = array();
37 // Raid device properties
38 public $r_fsType = 'ext3';
39 public $r_fsOptions = '';
40 public $r_mountPoint = '';
41 public $r_raidLevel = 0;
42 public $raidLevelList = array();
43 public $raidDevices = array();
44 public $freeRaidPartitions = array();
45 public $r_partitions = array();
46 public $r_spares = 0;
47 public $r_encrypt = FALSE;
49 // Partition properties
50 public $p_size = 1000;
51 public $p_format = FALSE;
52 public $p_fsType = 'ext3';
53 public $p_mountPoint = '';
54 public $p_used_disk = array();
55 public $p_forcePrimary = FALSE;
56 public $p_encrypt = FALSE;
57 public $p_size_max_value = 1000;
58 public $p_size_options = 0;
60 // Attributes managed by this plugin.
61 public $attributes = array("selected_type");
63 // Partitions attributes
64 public $p_attributes = array("p_size", "p_fsType", "p_mountPoint", "p_forcePrimary", "p_encrypt",
65 "p_size_options", "p_size_max_value", "p_used_disk", "p_format");
67 // Raid device attributes
68 public $r_attributes = array("r_fsType", "r_mountPoint", "r_raidLevel", "r_partitions",
69 "r_spares","r_encrypt", "r_fsOptions");
71 // Volume group attributes
72 public $vg_attributes = array("vg_name", "vg_partitions");
74 // Volume attributes
75 public $v_attributes = array("v_name", "v_group", "v_mountPoint", "v_fsType",
76 "v_size", "v_encrypt", "v_fsOptions");
78 // Disk attributes
79 public $d_attributes = array("d_name");
81 /*! \brief Constructs the Dialog and loads all required informations
82 * to be able to add partitions, raid devices, volumes groups ...
83 * @param Config The GOsa configuration object.
84 * @param remoteObject The remote partition object.
85 */
86 function __construct($config, $partitionObject)
87 {
88 $this->partitionObject = &$partitionObject;
89 $this->config = &$config;
91 // Prepare filesystem types
92 $this->fsTypes = array();
93 foreach($this->partitionObject->getFsTypes() as $type){
94 $this->fsTypes[$type] = $type;
95 }
96 $this->fsTypes['pv'] = _('Physical volume (LVM)');
97 $this->fsTypes['raid'] = _('Software raid');
99 // Prepare list of available raid level
100 $this->raidLevelList = array();
101 foreach($this->partitionObject->getRaidLevels() as $lvl){
102 $this->raidLevelList[$lvl] = sprintf(_("Raid %s"), $lvl);
103 }
105 // Load values from backend
106 $this->disks = $this->partitionObject->getDisks();
107 $this->partitions = $this->partitionObject->getPartitions();
108 $this->raidDevices = $this->partitionObject->getRaidDevices();
109 $this->volumeGroups = $this->partitionObject->getVolumeGroups();
110 $this->freeLvmPartitions = $this->partitionObject->getUnassignedPhysicalVolumes();
111 $this->freeRaidPartitions = $this->partitionObject->getUnassignedRaidPartitions();
112 foreach($this->volumeGroups as $vg){
113 $this->volumeGroupList[$vg['name']] = $vg['name'];
114 }
115 $this->diskList = array();
116 foreach($this->disks as $disk){
117 $this->diskList[$disk['device']] = $disk['device'];
118 }
120 // Select first disk as default.
121 $this->p_used_disk = key($this->diskList);
123 // Preselect partition creation if we've already created a disk
124 if(count($this->disks)){
125 $this->selected_type = PARTITION;
126 }
127 }
130 /*! \brief Generates the HTML output for this plugin.
131 * @return String HTML content of the plugin.
132 */
133 function execute()
134 {
135 $smarty = get_smarty();
137 // Assign base attributes
138 foreach($this->attributes as $attr){
139 $smarty->assign($attr, $this->$attr);
140 }
142 // Assign partition attributes.
143 $fsTypes = $this->fsTypes;
144 $attrs = $bool_attrs = array();
145 switch($this->selected_type){
146 case PARTITION: {
147 $attrs = $this->p_attributes;
148 $bool_attrs = array("p_forcePrimary", "p_encrypt", "p_format");
149 break;
150 }
151 case RAID_DEVICE: {
152 $attrs = $this->r_attributes;
154 // Do not allow to create raid devices in raid devices.
155 unset($fsTypes['raid']);
156 $bool_attrs = array("r_encrypt");
157 break;
158 }
159 case VOLUME_GROUP: {
160 $attrs = $this->vg_attributes;
161 break;
162 }
163 case VOLUME: {
164 $attrs = $this->v_attributes;
165 unset($fsTypes['raid']);
166 unset($fsTypes['pv']);
167 $bool_attrs = array("v_encrypt");
168 break;
169 }
170 case DISK: {
171 $attrs = $this->d_attributes;
172 break;
173 }
174 }
176 // Assign properties to smarty.
177 foreach($attrs as $attr){
178 $smarty->assign($attr, $this->$attr);
179 }
180 foreach($bool_attrs as $attr){
181 $smarty->assign("{$attr}_selected", $this->$attr != FALSE);
182 }
183 $smarty->assign('fsTypes', $fsTypes);
184 $smarty->assign('raidLevelList', $this->raidLevelList);
185 $smarty->assign('freeRaidPartitions', $this->freeRaidPartitions);
186 $smarty->assign('disks', $this->diskList);
187 $smarty->assign('volumeGroupList', $this->volumeGroupList);
188 $smarty->assign('freeLvmPartitions', $this->freeLvmPartitions);
189 return($smarty->fetch(get_template_path("goto/Device/AddPartitionDialog.tpl", TRUE)));
190 }
193 /*! \brief Saves posted values.
194 */
195 function save_object()
196 {
197 // Assign partition attributes.
198 $attrs = $bool_attrs = array();
199 switch($this->selected_type){
200 case PARTITION: {
201 $attrs = $this->p_attributes;
202 $bool_attrs = array("p_forcePrimary", "p_encrypt", "p_format");
203 break;
204 }
205 case RAID_DEVICE: {
206 $attrs = $this->r_attributes;
207 $bool_attrs = array("r_encrypt");
208 $this->r_partitions = array();
209 foreach($this->freeRaidPartitions as $key => $part){
210 if(isset($_POST['r_partition_'.$key])){
211 $this->r_partitions[] = $part;
212 }
213 }
214 break;
215 }
216 case VOLUME_GROUP: {
217 $attrs = $this->vg_attributes;
218 $this->vg_partitions = array();
219 foreach($this->freeLvmPartitions as $key => $part){
220 if(isset($_POST['vg_partition_'.$key])){
221 $this->vg_partitions[] = $part;
222 }
223 }
224 break;
225 }
226 case VOLUME: {
227 $attrs = $this->v_attributes;
228 $bool_attrs = array("v_encrypt");
229 break;
230 }
231 case DISK: {
232 $attrs = $this->d_attributes;
233 break;
234 }
235 }
237 // Get posted string values
238 $attrs = array_merge($attrs, $this->attributes);
239 foreach($attrs as $attr){
240 if(isset($_POST[$attr])){
241 $this->$attr = get_post($attr);
242 }
243 }
245 // Get boolean values
246 foreach($bool_attrs as $attr){
247 $this->$attr = isset($_POST[$attr]);
248 }
249 }
252 /*! \brief Stores the changes back to the remote table model.
253 * @return TRUE on success else false.
254 */
255 function save()
256 {
257 if($this->selected_type == DISK){
259 // Get volume group properties
260 $name = $this->d_name;
261 $this->partitionObject->addDisk($name);
262 return($this->partitionObject->success());
264 }elseif($this->selected_type == VOLUME_GROUP){
266 // Get volume group properties
267 $devices = $this->vg_partitions;
268 $name = $this->vg_name;
269 $this->partitionObject->addVolumeGroup($name, $devices, $format=TRUE, $use_existing=FALSE, NULL);
270 return($this->partitionObject->success());
272 }elseif($this->selected_type == VOLUME){
274 // Get volume properties
275 $name = $this->v_name;
276 $fsType = $this->v_fsType;
277 $fsOptions = $this->v_fsOptions;
278 $group = $this->v_group;
279 $target = $this->v_mountPoint;
280 $size = $this->v_size;
281 $encrypt = $this->v_encrypt;
283 $maxSize = NULL;
284 $grow = FALSE;
285 $format = TRUE;
286 $use_existing = FALSE;
288 if($fsType == "swap"){
289 $target = "swap";
290 }
292 $this->partitionObject->addVolume($target, $name, $group, $size, $maxSize,
293 $grow, $format, $use_existing, $fsType, $fsOptions);
295 return($this->partitionObject->success());
297 }elseif($this->selected_type == RAID_DEVICE){
299 // Get raid device properties
300 $devices = $this->r_partitions;
301 $fsType = $this->r_fsType;
302 $raidLevel = $this->r_raidLevel;
303 $spares = $this->r_spares;
304 $encrypt = $this->r_encrypt;
306 // Get used raid-device names and then calculate the next free name
307 $usedNames = array();
308 foreach($this->raidDevices as $raid){
309 $usedNames[] = $raid['name'];
310 }
311 $name = "md";
312 $id = 0;
313 while($id <= 16 && in_array($name.$id, $usedNames)){
314 $id ++;
315 }
316 $name = $name.$id;
318 // No free space for a raid device
319 if($id == 16){
320 echo "No more raid device ids";
321 return(FALSE);
322 }
324 // Check selected target
325 $partitions = $this->partitions;
326 $raids = $this->raidDevices;
327 $usedTargets = array();
328 foreach($partitions as $part){
329 $usedTargets[] = $part['target'];
330 }
331 foreach($raids as $part){
332 $usedTargets[] = $part['target'];
333 }
334 if($fsType == "pv"){
335 $target="pv.";
336 $id = 0;
337 while($id < 100 && in_array($target.str_pad($id, 2, '0', STR_PAD_LEFT) , $usedTargets)){
338 $id ++;
339 }
340 $target = $target.str_pad($id, 2, '0', STR_PAD_LEFT);
341 $fsType = $fsOptions = NULL;
342 }else{
343 $target = $this->r_mountPoint;
344 $fsType = $this->r_fsType;
345 $fsOptions = $this->r_fsOptions;
346 }
347 if($fsType == "swap"){
348 $target = "swap";
349 }
351 $this->partitionObject->addRaidDevice($target, $name, $raidLevel, $spares, $fsType,$fsOptions,
352 TRUE, FALSE, $devices);
354 return($this->partitionObject->success());
356 }elseif($this->selected_type == PARTITION){
358 // Get all currently used partitions
359 $partitions = $this->partitions;
360 $raids = $this->raidDevices;
361 $usedTargets = array();
362 foreach($partitions as $part){
363 $usedTargets[] = $part['target'];
364 }
365 foreach($raids as $part){
366 $usedTargets[] = $part['target'];
367 }
369 // Collect options
370 $size = $this->p_size;
371 $maxSize = NULL;
372 if($this->p_size_options == 2){
373 $maxSize = $this->p_size_max_value;
374 }
375 $grow = $this->p_size_options == 1;
376 $format = $this->p_format == 1;
377 $boot = FALSE;
378 $primary = $this->p_forcePrimary;
379 $fsType = $this->p_fsType;
380 $fsOptions = "";
381 $encrypt = $this->p_encrypt;
382 $passphrase = "";
383 $disk = $this->p_used_disk;
385 // We've to create a raid disk
386 if($this->p_fsType == "raid"){
387 $target="raid.";
388 $id = 0;
389 while($id < 100 && in_array($target.str_pad($id, 2, '0', STR_PAD_LEFT) , $usedTargets)){
390 $id ++;
391 }
392 $target = $target.str_pad($id, 2, '0', STR_PAD_LEFT);
393 $fsType = $fsOptions = NULL;
394 $encrypt = $format = FALSE;
395 }else
397 // We've to create a raid disk
398 if($this->p_fsType == "pv"){
399 $target="pv.";
400 $id = 0;
401 while($id < 100 && in_array($target.str_pad($id, 2, '0', STR_PAD_LEFT) , $usedTargets)){
402 $id ++;
403 }
404 $target = $target.str_pad($id, 2, '0', STR_PAD_LEFT);
405 $fsType = $fsOptions = NULL;
406 $encrypt = $format = FALSE;
407 }else{
409 // Add normal-physical partition
410 $target = $this->p_mountPoint;
411 if($fsType == "swap"){
412 $target = "swap";
413 $encrypt = $format = FALSE;
414 }
415 }
417 // Add partition to remote model
418 $this->partitionObject->addPartition($target,$size, $maxSize, $grow, $format, $boot, $primary,
419 $fsType, $fsOptions , $encrypt, $passphrase, $disk);
420 return($this->partitionObject->success());
421 }
422 }
423 }
424 ?>