Handle hostnames with upper-case letters
[webmin.git] / fdisk / edit_part.cgi
1 #!/usr/local/bin/perl
2 # edit_part.cgi
3 # Edit an existing partition, or create a new one
4
5 require './fdisk-lib.pl';
6 &ReadParse();
7 @dlist = &list_disks_partitions();
8 $dinfo = $dlist[$in{'disk'}];
9 &can_edit_disk($dinfo->{'device'}) ||
10         &error($text{'edit_ecannot'});
11 if ($in{'new'}) {
12         &ui_print_header($dinfo->{'desc'}, $text{'create_title'}, "");
13         }
14 else {
15         &ui_print_header($dinfo->{'desc'}, $text{'edit_title'}, "");
16         }
17
18 print &ui_form_start("save_part.cgi");
19 print &ui_table_start($text{'edit_details'}, "width=100%", 4);
20 print &ui_hidden("disk", $in{'disk'});
21 print &ui_hidden("part", $in{'part'});
22 print &ui_hidden("new", $in{'new'});
23
24 # Work out the start and end for the new partition
25 @plist = @{$dinfo->{'parts'}};
26 if ($in{'new'}) {
27         if ($in{'new'} == 1 || $in{'new'} == 3) {
28                 # Adding a new primary or extended partition
29                 $np = 1;
30                 for($i=0; $i<@plist; $i++) {
31                         if ($plist[$i]->{'number'} == $np) { $np++; }
32                         push(@start, $plist[$i]->{'start'});
33                         push(@end, $plist[$i]->{'end'});
34                         }
35                 $min = 1;
36                 $max = $dinfo->{'cylinders'};
37                 }
38         else {
39                 # Adding a new logical partition (inside the extended partition)
40                 $np = 5;
41                 for($i=0; $i<@plist; $i++) {
42                         if ($plist[$i]->{'number'} == $np) { $np++; }
43                         if ($plist[$i]->{'extended'}) {
44                                 $min = $plist[$i]->{'start'};
45                                 $max = $plist[$i]->{'end'};
46                                 }
47                         else {
48                                 push(@start, $plist[$i]->{'start'});
49                                 push(@end, $plist[$i]->{'end'});
50                                 }
51                         }
52                 }
53         print &ui_hidden("newpart", $np);
54         print &ui_hidden("min", $min);
55         print &ui_hidden("max", $max);
56
57         # find a gap in the partition map
58         for($start=$min; $start<=$max; $start++) {
59                 $found = 1;
60                 for($i=0; $i<@start; $i++) {
61                         if ($start >= $start[$i] && $start <= $end[$i]) {
62                                 $found = 0;
63                                 last;
64                                 }
65                         }
66                 if ($found) { last; }
67                 }
68         if ($found) {
69                 # starting place found.. find the end
70                 $found = 0;
71                 for($end=$start; $end<=$max; $end++) {
72                         for($i=0; $i<@start; $i++) {
73                                 if ($end >= $start[$i] && $end <= $end[$i]) {
74                                         $found = 1;
75                                         last;
76                                         }
77                                 }
78                         if ($found) { last; }
79                         }
80                 $end--;
81                 }
82         else {
83                 # no place for new partition!
84                 $start = $end = 0;
85                 }
86         }
87 else { 
88         # Just editing an existing partition
89         $pinfo = $plist[$in{'part'}];
90         $np = $pinfo->{'number'};
91         }
92 print &ui_hidden("np", $np);
93
94 # Describe partition
95 print &ui_table_row($text{'edit_location'},
96              $dinfo->{'device'} =~ /^\/dev\/(s|h)d([a-z])$/ ?
97                 &text('select_part', $1 eq 's' ? 'SCSI' : 'IDE', uc($2), $np) :
98              $dinfo->{'device'} =~ /rd\/c(\d+)d(\d+)$/ ?
99                 &text('select_mpart', "$1", "$2", $np) :
100              $dinfo->{'device'} =~ /ida\/c(\d+)d(\d+)$/ ?
101                 &text('select_cpart', "$1", "$2", $np) :
102              $dinfo->{'device'} =~ /scsi\/host(\d+)\/bus(\d+)\/target(\d+)\/lun(\d+)\/disc/ ?
103                 &text('select_spart', "$1", "$2", "$3", "$4", $np) :
104              $dinfo->{'device'} =~ /ide\/host(\d+)\/bus(\d+)\/target(\d+)\/lun(\d+)\/disc/ ?
105                 &text('select_snewide', "$1", "$2", "$3", "$4", $np) :
106                 $dinfo->{'device'});
107
108 # Device name
109 $dev = $dinfo->{'prefix'}.$np;
110 print &ui_table_row($text{'edit_device'}, $dev);
111
112 # Partition type
113 if ($pinfo->{'extended'} || $in{'new'} == 3) {
114         # Extended, cannot change
115         print &ui_table_row($text{'edit_type'}, $text{'extended'});
116         }
117 elsif ($pinfo->{'edittype'} || $in{'new'}) {
118         # Can change
119         print &ui_table_row($text{'edit_type'},
120                 &ui_select("type",
121                            $in{'new'} ? &default_tag() : $pinfo->{'type'},
122                            [ map { [ $_, &tag_name($_) ] }
123                                  (sort { &tag_name($a) cmp &tag_name($b) }
124                                        &list_tags()) ]));
125         }
126 else {
127         # Tool doesn't allow change
128         print &ui_table_row($text{'edit_type'},
129                             &tag_name($pinfo->{'type'}));
130                 
131         }
132
133 # Extent and cylinders
134 if ($in{'new'}) {
135         $ext = &ui_textbox("start", $start, 4)." - ".&ui_textbox("end", $end, 4);
136         }
137 else {
138         $ext = "$pinfo->{'start'} - $pinfo->{'end'}";
139         }
140 $ext .= " ".$text{'edit_of'}." ".$dinfo->{'cylinders'};
141 print &ui_table_row($text{'edit_extent'}, $ext);
142
143 # Current status
144 if ($pinfo->{'extended'}) {
145         foreach $p (@plist) {
146                 $ecount++ if ($p->{'number'} > 4);
147                 }
148         if ($ecount == 1) {
149                 $stat = $text{'edit_cont1'};
150                 }
151         else {
152                 if ($ecount > 4) {
153                         $stat = &text('edit_cont5', $ecount);
154                         }
155                 else {
156                         $stat = &text('edit_cont234', $ecount);
157                         }
158                 }
159         }
160 elsif (!$in{'new'}) {
161         @stat = &device_status($dev);
162         if (@stat) {
163                 $msg = $stat[2] ? 'edit_mount' : 'edit_umount';
164                 $msg .= 'vm' if ($stat[1] eq 'swap');
165                 $msg .= 'raid' if ($stat[1] eq 'raid');
166                 $msg .= 'lvm' if ($stat[1] eq 'lvm');
167                 $stat = &text($msg, "<tt>$stat[0]</tt>",
168                                     "<tt>$stat[1]</tt>");
169                 }
170         else {
171                 $stat = $text{'edit_notused'};
172                 }
173         }
174 if ($stat) {
175         print &ui_table_row($text{'edit_status'}, $stat);
176         }
177
178 # Partition size
179 if (!$in{'new'}) {
180         print &ui_table_row($text{'edit_size'},
181                 $dinfo->{'cylsize'} ? &nice_size(($pinfo->{'end'} - $pinfo->{'start'} + 1) * $dinfo->{'cylsize'}) : &text('edit_blocks', $pinfo->{'blocks'}));
182         }
183
184 # Show field for editing filesystem label
185 if (($has_e2label || $has_xfs_db) && &supports_label($pinfo) && !$in{'new'}) {
186         local $label = $in{'new'} ? undef : &get_label($pinfo->{'device'});
187         if (@stat) {
188                 print &ui_table_row($text{'edit_label'},
189                         $label ? "<tt>$label</tt>" : $text{'edit_none'});
190                 }
191         else {
192                 print &ui_table_row($text{'edit_label'},
193                         &ui_textbox("label", $label, 16));
194                 }
195         }
196
197 # Show field for partition name
198 if (&supports_name($dinfo)) {
199         print &ui_table_row($text{'edit_name'},
200                         &ui_textbox("name", $pinfo->{'name'}, 20));
201         }
202
203 # Show current UUID
204 if ($has_volid && !$in{'new'}) {
205         local $volid = &get_volid($pinfo->{'device'});
206         print &ui_table_row($text{'edit_volid'}, "<tt>$volid</tt>", 3);
207         }
208
209 print &ui_table_end();
210 if ($in{'new'}) {
211         print &ui_form_end([ [ undef, $text{'create'} ] ]);
212         }
213 elsif (@stat && $stat[2]) {
214         print &ui_form_end();
215         print "<b>$text{'edit_inuse'}</b><p>\n";
216         }
217 else {
218         print &ui_form_end([ $pinfo->{'extended'} ? ( ) :
219                                 ( [ undef, $text{'save'} ] ),
220                              [ 'delete', $text{'delete'} ] ]);
221         }
222
223 if (!$in{'new'} && !$pinfo->{'extended'}) {
224         print &ui_hr();
225         print &ui_buttons_start();
226
227         if (!@stat || $stat[2] == 0) {
228                 # Show form for creating filesystem
229                 local $rt = @stat ? $stat[1] : &conv_type($pinfo->{'type'});
230                 print &ui_buttons_row("mkfs_form.cgi",
231                         $text{'edit_mkfs2'}, $text{'edit_mkfsmsg2'},
232                         &ui_hidden("dev", $dev),
233                         &ui_select("type", $rt,
234                                 [ map { [ $_, $fdisk::text{"fs_".$_}." ($_)" ] }
235                                       &fdisk::supported_filesystems() ]));
236                 }
237
238         if (!$in{'new'} && @stat && $stat[2] == 0 && &can_fsck($stat[1])) {
239                 # Show form to fsck filesystem
240                 print &ui_buttons_row("fsck_form.cgi",
241                         $text{'edit_fsck'},&text('edit_fsckmsg', "<tt>fsck</tt>"),
242                         &ui_hidden("dev", $dev)." ".
243                         &ui_hidden("type", $stat[1]));
244                 }
245
246         if (!$in{'new'} && @stat && $stat[2] == 0 && &can_tune($stat[1])) {
247                 # Show form to tune filesystem
248                 print &ui_buttons_row("tunefs_form.cgi",
249                         $text{'edit_tune'}, $text{'edit_tunemsg'},
250                         &ui_hidden("dev", $dev)." ".
251                         &ui_hidden("type", $stat[1]));
252                 }
253
254         @types = &conv_type($pinfo->{'type'});
255         if (!$in{'new'} && !@stat && @types) {
256                 # Show form to mount filesystem
257                 if ($types[0] eq "swap") {
258                         # Swap partition
259                         print &ui_buttons_row("../mount/edit_mount.cgi",
260                                 $text{'edit_newmount2'}, $text{'edit_mountmsg2'},
261                                 &ui_hidden("type", $types[0]));
262                         }
263                 else {
264                         # For some filesystem
265                         $dirsel = &ui_textbox("newdir", undef, 20);
266                         if (@types > 1) {
267                                 $dirsel .= $text{'edit_mountas'}." ".
268                                         &ui_select("type", undef, \@types);
269                                 }
270                         else {
271                                 $dirsel .= &ui_hidden("type", $types[0]);
272                                 }
273                         print &ui_buttons_row("../mount/edit_mount.cgi",
274                                 $text{'edit_newmount'}, $text{'edit_mountmsg'},
275                                 undef, $dirsel);
276                         }
277                 }
278
279         print &ui_buttons_end();
280         }
281
282 &ui_print_footer("edit_disk.cgi?device=$dinfo->{'device'}",
283                  $text{'disk_return'});
284