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_bootable = FALSE;
53 public $p_fsType = 'ext3';
54 public $p_mountPoint = '';
55 public $p_used_disk = array();
56 public $p_forcePrimary = FALSE;
57 public $p_encrypt = FALSE;
58 public $p_size_max_value = 1000;
59 public $p_size_options = 0;
61 // Attributes managed by this plugin.
62 public $attributes = array("selected_type");
64 // Partitions attributes
65 public $p_attributes = array("p_size", "p_fsType", "p_mountPoint", "p_forcePrimary", "p_encrypt",
66 "p_size_options", "p_size_max_value", "p_used_disk", "p_format", "p_bootable");
68 // Raid device attributes
69 public $r_attributes = array("r_fsType", "r_mountPoint", "r_raidLevel", "r_partitions",
70 "r_spares","r_encrypt", "r_fsOptions");
72 // Volume group attributes
73 public $vg_attributes = array("vg_name", "vg_partitions");
75 // Volume attributes
76 public $v_attributes = array("v_name", "v_group", "v_mountPoint", "v_fsType",
77 "v_size", "v_encrypt", "v_fsOptions");
79 // Disk attributes
80 public $d_attributes = array("d_name");
82 /*! \brief Constructs the Dialog and loads all required informations
83 * to be able to add partitions, raid devices, volumes groups ...
84 * @param Config The GOsa configuration object.
85 * @param remoteObject The remote partition object.
86 */
87 function __construct($config, $partitionObject)
88 {
89 $this->partitionObject = &$partitionObject;
90 $this->config = &$config;
92 // Prepare filesystem types
93 $this->fsTypes = array();
94 foreach($this->partitionObject->getFsTypes() as $type){
95 $this->fsTypes[$type] = $type;
96 }
97 $this->fsTypes['pv'] = _('Physical volume (LVM)');
98 $this->fsTypes['raid'] = _('Software raid');
100 // Prepare list of available raid level
101 $this->raidLevelList = array();
102 foreach($this->partitionObject->getRaidLevels() as $lvl){
103 $this->raidLevelList[$lvl] = sprintf(_("Raid %s"), $lvl);
104 }
106 // Load values from backend
107 $this->disks = $this->partitionObject->getDisks();
108 $this->partitions = $this->partitionObject->getPartitions();
109 $this->raidDevices = $this->partitionObject->getRaidDevices();
110 $this->volumeGroups = $this->partitionObject->getVolumeGroups();
111 $this->freeLvmPartitions = $this->partitionObject->getUnassignedPhysicalVolumes();
112 $this->freeRaidPartitions = $this->partitionObject->getUnassignedRaidPartitions();
113 foreach($this->volumeGroups as $vg){
114 $this->volumeGroupList[$vg['name']] = $vg['name'];
115 }
116 $this->diskList = array();
117 foreach($this->disks as $disk){
118 $this->diskList[$disk['device']] = $disk['device'];
119 }
121 // Select first disk as default.
122 $this->p_used_disk = key($this->diskList);
124 // Preselect partition creation if we've already created a disk
125 if(count($this->disks)){
126 $this->selected_type = PARTITION;
127 }
128 }
131 /*! \brief Generates the HTML output for this plugin.
132 * @return String HTML content of the plugin.
133 */
134 function execute()
135 {
136 $smarty = get_smarty();
138 // Assign base attributes
139 foreach($this->attributes as $attr){
140 $smarty->assign($attr, $this->$attr);
141 }
143 // Assign partition attributes.
144 $fsTypes = $this->fsTypes;
145 $attrs = $bool_attrs = array();
146 switch($this->selected_type){
147 case PARTITION: {
148 $attrs = $this->p_attributes;
149 $bool_attrs = array("p_forcePrimary", "p_encrypt", "p_format", "p_bootable");
150 break;
151 }
152 case RAID_DEVICE: {
153 $attrs = $this->r_attributes;
155 // Do not allow to create raid devices in raid devices.
156 unset($fsTypes['raid']);
157 $bool_attrs = array("r_encrypt");
158 break;
159 }
160 case VOLUME_GROUP: {
161 $attrs = $this->vg_attributes;
162 break;
163 }
164 case VOLUME: {
165 $attrs = $this->v_attributes;
166 unset($fsTypes['raid']);
167 unset($fsTypes['pv']);
168 $bool_attrs = array("v_encrypt");
169 break;
170 }
171 case DISK: {
172 $attrs = $this->d_attributes;
173 break;
174 }
175 }
177 // Assign properties to smarty.
178 foreach($attrs as $attr){
179 $smarty->assign($attr, $this->$attr);
180 }
181 foreach($bool_attrs as $attr){
182 $smarty->assign("{$attr}_selected", $this->$attr != FALSE);
183 }
185 $smarty->assign("deviceUsage", $this->partitionObject->getDeviceUsage());
186 $smarty->assign('fsTypes', $fsTypes);
187 $smarty->assign('raidLevelList', $this->raidLevelList);
188 $smarty->assign('freeRaidPartitions', $this->freeRaidPartitions);
189 $smarty->assign('disks', $this->diskList);
190 $smarty->assign('volumeGroupList', $this->volumeGroupList);
191 $smarty->assign('freeLvmPartitions', $this->freeLvmPartitions);
192 return($smarty->fetch(get_template_path("goto/Device/AddPartitionDialog.tpl", TRUE)));
193 }
196 /*! \brief Saves posted values.
197 */
198 function save_object()
199 {
200 // Assign partition attributes.
201 $attrs = $bool_attrs = array();
202 switch($this->selected_type){
203 case PARTITION: {
204 $attrs = $this->p_attributes;
205 $bool_attrs = array("p_forcePrimary", "p_encrypt", "p_format", "p_bootable");
206 break;
207 }
208 case RAID_DEVICE: {
209 $attrs = $this->r_attributes;
210 $bool_attrs = array("r_encrypt");
211 $this->r_partitions = array();
212 foreach($this->freeRaidPartitions as $key => $part){
213 if(isset($_POST['r_partition_'.$key])){
214 $this->r_partitions[] = $part;
215 }
216 }
217 break;
218 }
219 case VOLUME_GROUP: {
220 $attrs = $this->vg_attributes;
221 $this->vg_partitions = array();
222 foreach($this->freeLvmPartitions as $key => $part){
223 if(isset($_POST['vg_partition_'.$key])){
224 $this->vg_partitions[] = $part;
225 }
226 }
227 break;
228 }
229 case VOLUME: {
230 $attrs = $this->v_attributes;
231 $bool_attrs = array("v_encrypt");
232 break;
233 }
234 case DISK: {
235 $attrs = $this->d_attributes;
236 break;
237 }
238 }
240 // Get posted string values
241 $attrs = array_merge($attrs, $this->attributes);
242 foreach($attrs as $attr){
243 if(isset($_POST[$attr])){
244 $this->$attr = get_post($attr);
245 }
246 }
248 // Get boolean values
249 foreach($bool_attrs as $attr){
250 $this->$attr = isset($_POST[$attr]);
251 }
252 }
255 /*! \brief Stores the changes back to the remote table model.
256 * @return TRUE on success else false.
257 */
258 function save()
259 {
260 if($this->selected_type == DISK){
262 // Get volume group properties
263 $name = $this->d_name;
264 $this->partitionObject->addDisk($name);
266 // Display potential errors
267 if(!$this->partitionObject->success()){
268 $msg = sprintf(_("Failed to add '%s': %s"), _("Disk"), $this->partitionObject->getError());
269 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
270 }
271 return($this->partitionObject->success());
273 }elseif($this->selected_type == VOLUME_GROUP){
275 // Get volume group properties
276 $devices = $this->vg_partitions;
277 $name = $this->vg_name;
278 $this->partitionObject->addVolumeGroup($name, $devices, $format=TRUE, $use_existing=FALSE, NULL);
280 // Display potential errors
281 if(!$this->partitionObject->success()){
282 $msg = sprintf(_("Failed to add '%s': %s"), _("Volume group"), $this->partitionObject->getError());
283 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
284 }
285 return($this->partitionObject->success());
287 }elseif($this->selected_type == VOLUME){
289 // Get volume properties
290 $name = $this->v_name;
291 $fsType = $this->v_fsType;
292 $fsOptions = $this->v_fsOptions;
293 $group = $this->v_group;
294 $target = $this->v_mountPoint;
295 $size = intval($this->v_size);
296 $encrypt = $this->v_encrypt;
298 $maxSize = NULL;
299 $grow = FALSE;
300 $format = TRUE;
301 $use_existing = FALSE;
303 if($fsType == "swap"){
304 $target = "swap";
305 }
307 $this->partitionObject->addVolume($target, $name, $group, $size, $maxSize,
308 $grow, $format, $use_existing, $fsType, $fsOptions);
310 // Display potential errors
311 if(!$this->partitionObject->success()){
312 $msg = sprintf(_("Failed to add '%s': %s"), _("Volume"), $this->partitionObject->getError());
313 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
314 }
315 return($this->partitionObject->success());
317 }elseif($this->selected_type == RAID_DEVICE){
319 // Get raid device properties
320 $devices = $this->r_partitions;
321 $fsType = $this->r_fsType;
322 $raidLevel = $this->r_raidLevel;
323 $spares = $this->r_spares;
324 $encrypt = $this->r_encrypt;
326 $name = $this->partitionObject->getNextRaidDevice();
328 // Check selected target
329 $partitions = $this->partitions;
330 $raids = $this->raidDevices;
331 $usedTargets = array();
332 foreach($partitions as $part){
333 $usedTargets[] = $part['target'];
334 }
335 foreach($raids as $part){
336 $usedTargets[] = $part['target'];
337 }
338 if($fsType == "pv"){
339 $target = $this->partitionObject->getNextPhysicalVolumeName();
340 $fsType = $fsOptions = NULL;
341 }else{
342 $target = $this->r_mountPoint;
343 $fsType = $this->r_fsType;
344 $fsOptions = $this->r_fsOptions;
345 }
346 if($fsType == "swap"){
347 $target = "swap";
348 }
350 $this->partitionObject->addRaidDevice($target, $name, $raidLevel, $spares, $fsType,$fsOptions,
351 TRUE, FALSE, $devices);
353 // Display potential errors
354 if(!$this->partitionObject->success()){
355 $msg = sprintf(_("Failed to add '%s': %s"), _("Raid device"), $this->partitionObject->getError());
356 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
357 }
358 return($this->partitionObject->success());
360 }elseif($this->selected_type == PARTITION){
362 // Get all currently used partitions
363 $partitions = $this->partitions;
364 $raids = $this->raidDevices;
365 $usedTargets = array();
366 foreach($partitions as $part){
367 $usedTargets[] = $part['target'];
368 }
369 foreach($raids as $part){
370 $usedTargets[] = $part['target'];
371 }
373 // Collect options
374 $size = intval($this->p_size);
375 $maxSize = NULL;
376 if($this->p_size_options == 2){
377 $maxSize = $this->p_size_max_value;
378 }
379 $grow = $this->p_size_options == 1;
380 $format = $this->p_format == 1;
381 $boot = $this->p_bootable == 1;
382 $primary = $this->p_forcePrimary;
383 $fsType = $this->p_fsType;
384 $fsOptions = "";
385 $encrypt = $this->p_encrypt;
386 $passphrase = "";
387 $disk = $this->p_used_disk;
389 // We've to create a raid disk
390 if($this->p_fsType == "raid"){
391 $target = $this->partitionObject->getNextRaidName();
392 $fsType = $fsOptions = NULL;
393 $encrypt = $format = FALSE;
394 }else
396 // We've to create a raid disk
397 if($this->p_fsType == "pv"){
398 $target = $this->partitionObject->getNextPhysicalVolumeName();
399 $fsType = $fsOptions = NULL;
400 $encrypt = $format = FALSE;
401 }else{
403 // Add normal-physical partition
404 $target = $this->p_mountPoint;
405 if($fsType == "swap"){
406 $target = "swap";
407 $encrypt = $format = FALSE;
408 }
409 }
411 // Add partition to remote model
412 $this->partitionObject->addPartition($target,$size, $maxSize, $grow, $format, $boot, $primary,
413 $fsType, $fsOptions , $encrypt, $passphrase, $disk);
415 // Display potential errors
416 if(!$this->partitionObject->success()){
417 $msg = sprintf(_("Failed to add '%s': %s"), _("Partition"), $this->partitionObject->getError());
418 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
419 }
420 return($this->partitionObject->success());
421 }
422 }
423 }
424 ?>