1 | #!/usr/bin/perl -w |
---|
2 | # |
---|
3 | # $Id: parted2fdisk.pl 90 2005-10-27 22:11:35Z bcornec $ |
---|
4 | # |
---|
5 | # parted2fdisk: fdisk like interface for parted |
---|
6 | # [developped for mindi/mondo http://mondorescue.berlios.de] |
---|
7 | # |
---|
8 | # Aims to be architecture independant (i386/ia64) |
---|
9 | # Tested on RHAS 2.1 ia64 - Mandrake 9.0 ia64 - RHAS 3.0 ia64 |
---|
10 | # |
---|
11 | # (c) Bruno Cornec <Bruno.Cornec@hp.com> |
---|
12 | # Licensed under the GPL |
---|
13 | |
---|
14 | use strict; |
---|
15 | |
---|
16 | $ENV{LANG} = "C"; |
---|
17 | $ENV{LANGUAGE} = "C"; |
---|
18 | $ENV{LC_ALL} = "C"; |
---|
19 | |
---|
20 | # Log |
---|
21 | my $flog = "/tmp/parted2fdisk.log"; |
---|
22 | open(FLOG, "> $flog") || die "Unable to open $flog"; |
---|
23 | |
---|
24 | my $fdisk = "/sbin/fdisk"; |
---|
25 | my $parted = "/sbin/parted"; |
---|
26 | |
---|
27 | my $i; |
---|
28 | my $l; |
---|
29 | my $part; |
---|
30 | my $wpart; |
---|
31 | my $start = ""; |
---|
32 | my $end = ""; |
---|
33 | my $cylstart; |
---|
34 | my $cylend; |
---|
35 | my %start; |
---|
36 | my %end; |
---|
37 | my %type; |
---|
38 | my %flags; |
---|
39 | my $arch; |
---|
40 | my $fake = 0; |
---|
41 | |
---|
42 | # Determine on which arch we're running |
---|
43 | if (defined ($ENV{ARCH})) { |
---|
44 | $arch = $ENV{ARCH}; |
---|
45 | } else { |
---|
46 | $arch = `"/bin/arch"`; |
---|
47 | chomp($arch); |
---|
48 | } |
---|
49 | |
---|
50 | # |
---|
51 | # Looking for fdisk |
---|
52 | # |
---|
53 | $fdisk = is_lsb($fdisk); |
---|
54 | # |
---|
55 | # We always use fdisk except on ia64 with GPT types of |
---|
56 | # partition tables where we need parted |
---|
57 | # All should return fdisk like format so that callers |
---|
58 | # think they have called fdisk directly |
---|
59 | # |
---|
60 | my $un; |
---|
61 | my $type; |
---|
62 | my $args = ""; |
---|
63 | my $device = ""; |
---|
64 | my $endmax = ""; |
---|
65 | |
---|
66 | if ($#ARGV < 0) { |
---|
67 | printf FLOG "No arguments given exiting ...\n"; |
---|
68 | mysyn(); |
---|
69 | } |
---|
70 | |
---|
71 | my %pid = ( "FAT" => "6", |
---|
72 | "fat32" => "b", |
---|
73 | "fat16" => "e", |
---|
74 | "ext2" => "83", |
---|
75 | "ext3" => "83", |
---|
76 | "linux-swap" => "82", |
---|
77 | "LVM" => "8e", |
---|
78 | "" => "", |
---|
79 | ); |
---|
80 | my %pnum; |
---|
81 | |
---|
82 | # Reverse table of pid |
---|
83 | while (($i,$l) = each %pid) { |
---|
84 | next if ($i eq "ext2"); |
---|
85 | $pnum{$l} = $i; |
---|
86 | } |
---|
87 | |
---|
88 | foreach $i (@ARGV) { |
---|
89 | # We support at most one option and one device |
---|
90 | print FLOG "Parameter found : $i\n"; |
---|
91 | if ($i =~ /^\/dev\//) { |
---|
92 | $device = $i; |
---|
93 | next; |
---|
94 | } elsif ($i =~ /^-/) { |
---|
95 | $args = $i; |
---|
96 | next; |
---|
97 | } else { |
---|
98 | mysyn(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if (($args ne "") and ($device eq "")) { |
---|
103 | mysyn(); |
---|
104 | } |
---|
105 | |
---|
106 | # -s takes a partition as arg |
---|
107 | if ($args =~ /-s/) { |
---|
108 | $wpart = $device; |
---|
109 | $device =~ s/[0-9]+$//; |
---|
110 | } |
---|
111 | |
---|
112 | if ($args =~ /-n/) { |
---|
113 | print FLOG "Fake mode. Nothing will be really done\n"; |
---|
114 | $fake = 1; |
---|
115 | } |
---|
116 | |
---|
117 | print FLOG "Called with device $device and arg $args\n"; |
---|
118 | |
---|
119 | if ($arch =~ /^ia64/) { |
---|
120 | # Check partition table type |
---|
121 | print FLOG "We're on ia64 ...\n"; |
---|
122 | $parted = is_lsb($parted); |
---|
123 | $type = which_type($device); |
---|
124 | if ($type ne "msdos") { |
---|
125 | print FLOG "Not an msdos type of disk label\n"; |
---|
126 | if ($args =~ /-l/) { |
---|
127 | fdisk_list($device,undef,\%start,\%end, 1); |
---|
128 | } elsif ($args =~ /-s/) { |
---|
129 | fdisk_list($device,$wpart,\%start,\%end, 1); |
---|
130 | } elsif (($args =~ /-/) and ($fake == 0)) { |
---|
131 | printf FLOG "Option not supported ($args) ...\n"; |
---|
132 | printf FLOG "Please report to the author\n"; |
---|
133 | mysyn(); |
---|
134 | } else { |
---|
135 | # Read fdisk orders on stdin and pass them to parted |
---|
136 | # on the command line as parted doesn't read on stdin |
---|
137 | print FLOG "Translating fdisk command to parted\n"; |
---|
138 | while ($i = <STDIN>) { |
---|
139 | if ($i =~ /^p$/) { |
---|
140 | fdisk_list($device,undef,\%start,\%end, 1); |
---|
141 | } |
---|
142 | elsif ($i =~ /^n$/) { |
---|
143 | fdisk_list($device,undef,\%start,\%end, 0); |
---|
144 | if ($type ne "gpt") { |
---|
145 | print FLOG "Forcing GPT type of disk label\n"; |
---|
146 | print FLOG "mklabel gpt\n"; |
---|
147 | system "$parted -s $device mklabel gpt\n" if ($fake == 0); |
---|
148 | $type = "gpt"; |
---|
149 | } |
---|
150 | $l = <STDIN>; |
---|
151 | if (not (defined $l)) { |
---|
152 | print FLOG "no primary/extended arg given for creation... assuming primary\n"; |
---|
153 | $l = "p"; |
---|
154 | } |
---|
155 | chomp($l); |
---|
156 | $part = <STDIN>; |
---|
157 | if ((not (defined $part)) || ($part eq "")) { |
---|
158 | print FLOG "no partition given for creation... skipping\n"; |
---|
159 | next; |
---|
160 | } |
---|
161 | chomp($part); |
---|
162 | $cylstart = <STDIN>; |
---|
163 | chomp($cylstart); |
---|
164 | if ((not (defined $cylstart)) || ($cylstart eq "")) { |
---|
165 | if (defined $start{$part-1}) { |
---|
166 | # in MB => cyl |
---|
167 | $cylstart = sprintf("%d",$end{$part-1}*1048576/$un + 1); |
---|
168 | print FLOG "no start cyl given for creation... assuming the following $cylstart\n"; |
---|
169 | } else { |
---|
170 | print FLOG "no start cyl given for creation... assuming the following 1\n"; |
---|
171 | $cylstart = 1; |
---|
172 | } |
---|
173 | } |
---|
174 | $cylstart = 1 if ($cylstart < 1); |
---|
175 | print FLOG "start cyl : $cylstart\n"; |
---|
176 | $un = get_un($device, "", 0); |
---|
177 | # parted needs MB |
---|
178 | if ($cylstart == 1) { |
---|
179 | $start = 0.01; |
---|
180 | } else { |
---|
181 | $start = $cylstart* $un / 1048576 +0.001; |
---|
182 | } |
---|
183 | # this is a size in B/KB/MB/GB |
---|
184 | |
---|
185 | $endmax = get_max($device); |
---|
186 | $cylend = <STDIN>; |
---|
187 | chomp($cylend); |
---|
188 | if ((not (defined $cylend)) || ($cylend eq "")) { |
---|
189 | print FLOG "no end cyl given for creation... assuming full disk)\n"; |
---|
190 | $cylend = $endmax; |
---|
191 | } |
---|
192 | # Handles end syntaxes (+, K, M, ...) |
---|
193 | # to give cylinders |
---|
194 | if ($cylend =~ /^\+/) { |
---|
195 | $cylend =~ s/^\+//; |
---|
196 | if ($cylend =~ /K$/) { |
---|
197 | $cylend =~ s/K$//; |
---|
198 | $cylend *= 1024; |
---|
199 | } elsif ($cylend =~ /M$/) { |
---|
200 | $cylend =~ s/M$//; |
---|
201 | $cylend *= 1048576; |
---|
202 | } elsif ($cylend =~ /G$/) { |
---|
203 | $cylend =~ s/G$//; |
---|
204 | $cylend *= 1073741824; |
---|
205 | } |
---|
206 | # This gives the number of cyl |
---|
207 | $cylend /= $un; |
---|
208 | $cylend = sprintf("%d",$cylend); |
---|
209 | $cylend += $cylstart - 0.001; |
---|
210 | # We now have the end cyl |
---|
211 | } |
---|
212 | $cylend = $endmax if ($cylend > $endmax); |
---|
213 | print FLOG "end cyl : $cylend\n"; |
---|
214 | # parted needs MB |
---|
215 | $end = $cylend * $un / 1048576; |
---|
216 | print FLOG "n $l $part $cylstart $cylend => mkpart primary $start $end\n"; |
---|
217 | system "$parted -s $device mkpart primary ext2 $start $end\n" if ($fake == 0); |
---|
218 | } |
---|
219 | elsif ($i =~ /^d$/) { |
---|
220 | $part = <STDIN>; |
---|
221 | if (not (defined $part)) { |
---|
222 | print FLOG "no partition given for deletion... skipping\n"; |
---|
223 | next; |
---|
224 | } |
---|
225 | chomp($part); |
---|
226 | print FLOG "d $part => rm $part\n"; |
---|
227 | system "$parted -s $device rm $part\n" if ($fake == 0); |
---|
228 | get_parted($device,undef,\%start,\%end,undef); |
---|
229 | } |
---|
230 | elsif ($i =~ /^w$/) { |
---|
231 | print FLOG "w => quit\n"; |
---|
232 | } |
---|
233 | elsif ($i =~ /^t$/) { |
---|
234 | $part = <STDIN>; |
---|
235 | if (not (defined $part)) { |
---|
236 | print FLOG "no partition given for tagging... skipping\n"; |
---|
237 | next; |
---|
238 | } |
---|
239 | chomp($part); |
---|
240 | $l = <STDIN>; |
---|
241 | if (not (defined $l)) { |
---|
242 | print FLOG "no type given for tagging partition $part... skipping\n"; |
---|
243 | next; |
---|
244 | } |
---|
245 | chomp($l); |
---|
246 | if (not (defined $pnum{$l})) { |
---|
247 | print FLOG "no partition number given for $l... please report to the author\n"; |
---|
248 | next; |
---|
249 | } |
---|
250 | print FLOG "t $part $l => mkfs $part $pnum{$l}\n"; |
---|
251 | system "$parted -s $device mkfs $part $pnum{$l}\n" if ($fake == 0); |
---|
252 | } |
---|
253 | elsif ($i =~ /^a$/) { |
---|
254 | $part = <STDIN>; |
---|
255 | if (not (defined $part)) { |
---|
256 | print FLOG "no partition given for tagging... skipping\n"; |
---|
257 | next; |
---|
258 | } |
---|
259 | chomp($part); |
---|
260 | print FLOG "a $part => set $part boot on\n"; |
---|
261 | system "$parted -s $device set $part boot on\n" if ($fake == 0); |
---|
262 | } |
---|
263 | elsif ($i =~ /^q$/) { |
---|
264 | print FLOG "q => quit\n"; |
---|
265 | } |
---|
266 | else { |
---|
267 | print FLOG "Unknown command: $i\n"; |
---|
268 | next; |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|
272 | } |
---|
273 | myexit(0); |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | # |
---|
278 | # Else everything is for fdisk |
---|
279 | # |
---|
280 | # Print only mode |
---|
281 | print FLOG "Passing everything to the real fdisk\n"; |
---|
282 | my $fargs = join(@ARGV); |
---|
283 | |
---|
284 | if ($args =~ /^-/) { |
---|
285 | # -l or -s |
---|
286 | open (FDISK, "$fdisk $fargs |") || die "Unable to read from $fdisk"; |
---|
287 | while (<FDISK>) { |
---|
288 | print; |
---|
289 | } |
---|
290 | close(FDISK); |
---|
291 | } else { |
---|
292 | # Modification mode |
---|
293 | open (FDISK, "| $fdisk $fargs") || die "Unable to modify through $fdisk"; |
---|
294 | while (<STDIN>) { |
---|
295 | print FDISK; |
---|
296 | } |
---|
297 | close(FDISK); |
---|
298 | close(STDIN); |
---|
299 | } |
---|
300 | myexit(0); |
---|
301 | |
---|
302 | |
---|
303 | # Is your system LSB ? |
---|
304 | sub is_lsb { |
---|
305 | |
---|
306 | my $cmd = shift; |
---|
307 | my $basename = basename($cmd); |
---|
308 | my $mindifdisk="/usr/local/bin/fdisk"; |
---|
309 | |
---|
310 | if ($cmd =~ /fdisk/) { |
---|
311 | if ($arch =~ /^ia64/) { |
---|
312 | if (-l $cmd) { |
---|
313 | print FLOG "Your system is ready for mondo-archive on ia64\n"; |
---|
314 | } else { |
---|
315 | print FLOG "Your system is ready for mondo-restore on ia64\n"; |
---|
316 | } |
---|
317 | if (-x $mindifdisk) { |
---|
318 | $cmd = $mindifdisk; |
---|
319 | } else { |
---|
320 | print FLOG "Your system doesn't provide $mindifdisk\n"; |
---|
321 | print FLOG "Please use mindi-x.yz-ia64.rpm on the system to backup\n"; |
---|
322 | myexit(-1); |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | if (not (-x $cmd)) { |
---|
327 | print FLOG "Your system is not LSB/mondo compliant: $basename was not found as $cmd\n"; |
---|
328 | print FLOG "Searching elswhere..."; |
---|
329 | foreach $i (split(':',$ENV{PATH})) { |
---|
330 | if (-x "$i/$basename") { |
---|
331 | $cmd = "$i/$basename"; |
---|
332 | print FLOG "Found $cmd, using it !\n"; |
---|
333 | last; |
---|
334 | } |
---|
335 | } |
---|
336 | if (not (-x $cmd)) { |
---|
337 | print FLOG "Your system doesn't provide $basename in the PATH\n"; |
---|
338 | print FLOG "Please correct it before relaunching\n"; |
---|
339 | myexit(-1); |
---|
340 | } |
---|
341 | } |
---|
342 | return($cmd); |
---|
343 | } |
---|
344 | |
---|
345 | sub fdisk_list { |
---|
346 | |
---|
347 | my $device = shift; |
---|
348 | my $wpart = shift; |
---|
349 | my $start = shift; |
---|
350 | my $end = shift; |
---|
351 | my $verbose = shift; |
---|
352 | |
---|
353 | my $un; |
---|
354 | my $endmax; |
---|
355 | my $d; |
---|
356 | my $n; |
---|
357 | |
---|
358 | my %cmt = ( "FAT" => "FAT", |
---|
359 | "ext2" => "Linux", |
---|
360 | "ext3" => "Linux", |
---|
361 | "linux-swap" => "Linux swap", |
---|
362 | "LVM" => "Linux LVM", |
---|
363 | "fat16" => "fat16", |
---|
364 | "fat32" => "fat32", |
---|
365 | "" => "Linux", |
---|
366 | ); |
---|
367 | |
---|
368 | my $part; |
---|
369 | my $mstart; |
---|
370 | my $mend; |
---|
371 | my $length; |
---|
372 | my $pid; |
---|
373 | my $cmt; |
---|
374 | format FLOG1 = |
---|
375 | @<<<<<<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
376 | $part, $mstart, $mend, $length, $pid, $cmt |
---|
377 | . |
---|
378 | format FLOG2 = |
---|
379 | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
380 | $part, |
---|
381 | @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
382 | $mstart, $mend, $length, $pid, $cmt |
---|
383 | . |
---|
384 | format STDOUT1 = |
---|
385 | @<<<<<<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
386 | $part, $mstart, $mend, $length, $pid, $cmt |
---|
387 | . |
---|
388 | format STDOUT2 = |
---|
389 | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
390 | $part, |
---|
391 | @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
---|
392 | $mstart, $mend, $length, $pid, $cmt |
---|
393 | . |
---|
394 | #/dev/hda1 1 77579 39099374+ ee EFI GPT |
---|
395 | |
---|
396 | # |
---|
397 | # Keep Fdisk headers |
---|
398 | # |
---|
399 | # this will return bytes |
---|
400 | $un = get_un ($device,$wpart,$verbose); |
---|
401 | |
---|
402 | $endmax = get_max($device); |
---|
403 | |
---|
404 | # This will return MB |
---|
405 | get_parted ($device,$start,$end,\%type,\%flags); |
---|
406 | |
---|
407 | while (($n,$d) = each %type) { |
---|
408 | # Print infos fdisk like |
---|
409 | $part = ${device}.$n; |
---|
410 | # start and end are in cylinder in fdisk format |
---|
411 | # so return in MB * 1MB / what represents 1 cyl in B |
---|
412 | $mstart = sprintf("%d",$$start{$n}*1048576/$un); |
---|
413 | $mstart = 1 if ($mstart < 1); |
---|
414 | $mend = sprintf("%d",$$end{$n}*1048576/$un - 1); |
---|
415 | $mend = $endmax if ($mend > $endmax); |
---|
416 | # length is in 1K blocks |
---|
417 | $length = sprintf("%d",($mend-$mstart+1)*$un/1024); |
---|
418 | $pid = $pid{$type{$n}}; |
---|
419 | $cmt = $cmt{$type{$n}}; |
---|
420 | #print FLOG "$part - $mstart - $mend - $length\n"; |
---|
421 | |
---|
422 | if ($verbose == 1) { |
---|
423 | if (not (defined $wpart)) { |
---|
424 | if (length($part) > 13) { |
---|
425 | open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2"; |
---|
426 | select(STDOUT2); |
---|
427 | write; |
---|
428 | open(FLOG2,">&FLOG") || die "Unable to open FLOG2"; |
---|
429 | select(FLOG2); |
---|
430 | write; |
---|
431 | select(STDOUT); |
---|
432 | close(FLOG2); |
---|
433 | close(STDOUT2); |
---|
434 | } else { |
---|
435 | open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1"; |
---|
436 | select(STDOUT1); |
---|
437 | write; |
---|
438 | open(FLOG1,">&FLOG") || die "Unable to open FLOG1"; |
---|
439 | select(FLOG1); |
---|
440 | write; |
---|
441 | select(STDOUT); |
---|
442 | close(FLOG1); |
---|
443 | close(STDOUT1); |
---|
444 | } |
---|
445 | } else { |
---|
446 | # manage the -s option of fdisk here |
---|
447 | print "$length\n" if ($part eq $wpart); |
---|
448 | print FLOG "$part has $length KBytes\n" if ($part eq $wpart); |
---|
449 | } |
---|
450 | } |
---|
451 | } |
---|
452 | close(FDISK); |
---|
453 | close(PARTED); |
---|
454 | } |
---|
455 | |
---|
456 | # |
---|
457 | # Get max size from fdisk |
---|
458 | # |
---|
459 | sub get_max { |
---|
460 | |
---|
461 | my $device = shift; |
---|
462 | my $max = 0; |
---|
463 | my $foo; |
---|
464 | |
---|
465 | open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk"; |
---|
466 | while (<FDISK>) { |
---|
467 | if ($_ =~ /heads/) { |
---|
468 | chomp; |
---|
469 | $max = $_; |
---|
470 | $max =~ s/.* ([0-9]+) cylinders/$1/; |
---|
471 | } |
---|
472 | } |
---|
473 | close(FDISK); |
---|
474 | print FLOG "get_max returns $max\n"; |
---|
475 | return($max); |
---|
476 | } |
---|
477 | |
---|
478 | # |
---|
479 | # Get units from fdisk (cylinder size) |
---|
480 | # |
---|
481 | sub get_un { |
---|
482 | |
---|
483 | my $device = shift; |
---|
484 | my $wpart = shift; |
---|
485 | my $verbose = shift; |
---|
486 | my $un = 0; |
---|
487 | my $foo; |
---|
488 | |
---|
489 | open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk"; |
---|
490 | while (<FDISK>) { |
---|
491 | print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1)); |
---|
492 | if ($_ =~ /^Units/) { |
---|
493 | ($foo, $un , $foo) = split /=/; |
---|
494 | $un =~ s/[A-z\s=]//g; |
---|
495 | $un = eval($un); |
---|
496 | } |
---|
497 | } |
---|
498 | close(FDISK); |
---|
499 | print FLOG "get_un returns $un\n"; |
---|
500 | return($un); |
---|
501 | } |
---|
502 | |
---|
503 | # |
---|
504 | # Parted gives info in MB |
---|
505 | # |
---|
506 | sub get_parted { |
---|
507 | |
---|
508 | my $device = shift; |
---|
509 | my $start = shift; |
---|
510 | my $end = shift; |
---|
511 | my $type = shift; |
---|
512 | my $flags = shift; |
---|
513 | my $d; |
---|
514 | my $n; |
---|
515 | |
---|
516 | open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted"; |
---|
517 | # Skip 3 first lines |
---|
518 | $d = <PARTED>; |
---|
519 | $d = <PARTED>; |
---|
520 | $d = <PARTED>; |
---|
521 | print FLOG "Got from parted: \n"; |
---|
522 | print FLOG "Minor Start End Filesystem Flags\n"; |
---|
523 | # Get info from each partition line |
---|
524 | while (($n,$d) = split(/\s/, <PARTED>,2)) { |
---|
525 | chomp($d); |
---|
526 | next if ($n !~ /^[1-9]/); |
---|
527 | $d =~ s/^\s*//; |
---|
528 | $d =~ s/\s+/ /g; |
---|
529 | ($$start{$n},$$end{$n},$$type{$n},$$flags{$n}) = split(/ /,$d); |
---|
530 | $$start{$n} = "" if (not defined $$start{$n}); |
---|
531 | $$end{$n} = "" if (not defined $$end{$n}); |
---|
532 | $$type{$n} = "" if (not defined $$type{$n}); |
---|
533 | $$flags{$n} = "" if (not defined $$flags{$n}); |
---|
534 | print FLOG "$n $$start{$n} $$end{$n} $$type{$n} $$flags{$n}\n"; |
---|
535 | } |
---|
536 | close(PARTED); |
---|
537 | } |
---|
538 | |
---|
539 | # Based on Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu |
---|
540 | # in Basename.pm |
---|
541 | |
---|
542 | sub basename { |
---|
543 | |
---|
544 | my($fullname) = shift; |
---|
545 | |
---|
546 | my($dirpath,$basename); |
---|
547 | |
---|
548 | ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s); |
---|
549 | |
---|
550 | return($basename); |
---|
551 | } |
---|
552 | |
---|
553 | sub myexit { |
---|
554 | |
---|
555 | my $val=shift; |
---|
556 | |
---|
557 | close(FLOG); |
---|
558 | exit($val); |
---|
559 | } |
---|
560 | |
---|
561 | sub which_type { |
---|
562 | |
---|
563 | my $device = shift; |
---|
564 | my $type = ""; |
---|
565 | |
---|
566 | open (FDISK, "$fdisk -l $device |") || die "Unable to read from $fdisk"; |
---|
567 | while (<FDISK>) { |
---|
568 | if ($_ =~ /EFI GPT/) { |
---|
569 | $type= "gpt"; |
---|
570 | print FLOG "Found a GPT partition format\n"; |
---|
571 | last; |
---|
572 | } |
---|
573 | } |
---|
574 | close(FDISK); |
---|
575 | open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk"; |
---|
576 | while (<PARTED>) { |
---|
577 | if ($_ =~ /Disk label type: msdos/) { |
---|
578 | $type= "msdos"; |
---|
579 | print FLOG "Found a msdos partition format\n"; |
---|
580 | last; |
---|
581 | } |
---|
582 | } |
---|
583 | close(FDISK); |
---|
584 | return ($type); |
---|
585 | } |
---|
586 | |
---|
587 | sub mysyn { |
---|
588 | print "Syntax: $0 [-l] device | [-s] partition\n"; |
---|
589 | myexit(-1); |
---|
590 | } |
---|