source: MondoRescue/branches/3.2/mindi/parted2fdisk.pl@ 3367

Last change on this file since 3367 was 3367, checked in by Bruno Cornec, 9 years ago
  • Add support for msdos disk type reporting by parted with -s option (2.3 version of parted)
  • Property svn:keywords set to Id
File size: 18.9 KB
Line 
1#!/usr/bin/perl -w
2#
3# $Id: parted2fdisk.pl 3367 2015-03-20 07:20:16Z bruno $
4#
5# parted2fdisk: fdisk like interface for parted
6# [developed for mindi/mondo http://www.mondorescue.org]
7#
8# Aims to be architecture independant (i386/ia64)
9# Tested on ia64 with RHAS 2.1 - Mandrake 9.0 - RHEL 3.0 - SLES 10 - RHEL 5 -
10#
11# Copyright B. Cornec 2000-2015
12# Provided under the GPL v2
13
14use strict;
15use File::Basename;
16
17
18=pod
19
20=head1 NAME
21
22parted2fdisk is a fdisk like command using parted internally.
23
24=head1 DESCRIPTION
25
26parted2fdisk behaves like the fdisk command, but dialog internally with parted in order to manipulate partition tables, which allow it to support GPT partition format as well as MBR, contrary to fdisk. It aims at providing compatible external interface with fdisk. Developed initialy for ia64 Linux, it is also useful now on x86 systems using GPT partition format (for large HDDs).
27
28=head1 SYNOPSIS
29
30parted2fdisk -s partition
31parted2fdisk -l [device]
32parted2fdisk [-n] device
33
34=head1 OPTIONS
35
36=over 4
37
38=item B<-s>
39
40Print the size (in blocks) of the given partition.
41
42=item B<-n>
43
44Fake mode. Doesn't pass the commands just simulate.
45
46=item B<-l>
47
48List the partition tables for the specified device (or all if none specified) and then exit.
49
50=item B<no option>
51
52Allow the creation and manipulation of partition tables.
53
54=back
55
56=head1 ARGUMENTS
57
58=over 4
59
60=item B<partition>
61
62partition device file (only used with -s option).
63
64=item B<device>
65
66device file to work on.
67
68=back
69
70=head1 WEB SITES
71
72The main Web site of the project is available at L<http://www.mondorescue.org>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.
73
74=head1 USER MAILING LIST
75
76For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
77
78=head1 AUTHORS
79
80The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
81
82=head1 COPYRIGHT
83
84MondoRescue is distributed under the GPL v2.0 license or later,
85described in the file C<COPYING> included with the distribution.
86
87=cut
88
89
90$ENV{LANG} = "C";
91$ENV{LANGUAGE} = "C";
92$ENV{LC_ALL} = "C";
93
94# Log
95my $flog = "/var/log/parted2fdisk.log";
96open(FLOG, "> $flog") || die "Unable to open $flog";
97
98my $fdisk = "/sbin/fdisk";
99$fdisk = "/usr/sbin/fdisk" if (not -x "/sbin/fdisk");
100my $parted = "/sbin/parted";
101$parted = "/usr/sbin/parted" if (not -x "/sbin/parted");
102
103my $i;
104my $l;
105my $part;
106my $wpart;
107my $start = "";
108my $end = "";
109my $cylstart;
110my $cylend;
111my %start;
112my %end;
113my %type;
114my $fake = 0;
115my $mega = 1048576;
116
117# Immediate flushing to avoids read error from mondorestore in log files
118$| = 1;
119
120#
121# Looking for fdisk
122#
123$fdisk = is_lsb($fdisk);
124#
125# We always use fdisk except with GPT types of
126# partition tables where we need parted
127# All should return fdisk like format so that callers
128# think they have called fdisk directly
129#
130my $un;
131my $type;
132my $args = "";
133my $device = "";
134my $endmax = "";
135
136if ($#ARGV < 0) {
137 printf FLOG "No arguments given exiting ...\n";
138 mysyn();
139}
140
141my %pid = ( "FAT" => "6",
142 "fat32" => "b",
143 "fat16" => "e",
144 "ext2" => "83",
145 "ext3" => "83",
146 "ext4" => "83",
147 "xfs" => "83",
148 "btrfs" => "83",
149 "reiserfs" => "83",
150 "linux-swap" => "82",
151 "lvm" => "8e",
152 "raid" => "fd",
153 "" => "",
154 );
155my %pnum;
156
157# Reverse table of pid
158while (($i,$l) = each %pid) {
159 next if ($i eq "ext2");
160 $pnum{$l} = $i;
161}
162
163foreach $i (@ARGV) {
164 # We support at most one option and one device
165 print FLOG "Parameter found : $i\n";
166 if ($i =~ /^\/dev\//) {
167 $device = $i;
168 next;
169 } elsif ($i =~ /^-/) {
170 $args = $i;
171 next;
172 } else {
173 mysyn();
174 }
175}
176
177if (($args ne "") && ($args ne "-l") && ($device eq "")) {
178 mysyn();
179}
180
181# -s takes a partition as arg
182if ($args =~ /-s/) {
183 $wpart = $device;
184 # To support dev like cciss/c0d0p1
185 if ($device =~ /([0-9]+)p[0-9]+$/) {
186 $device =~ s/([0-9]+)p[0-9]+$/$1/;
187 } else {
188 $device =~ s/[0-9]+$//;
189 }
190}
191
192if ($args =~ /-n/) {
193 print FLOG "Fake mode. Nothing will be really done\n";
194 $fake = 1;
195}
196
197print FLOG "Called with device $device and arg $args\n";
198
199if (($args =~ /-l/) && ($device eq "")) {
200 # Pass to real fdisk directly
201 local_fdisk($args,$device);
202 myexit(0);
203}
204
205# util-linux/fdisk version
206open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
207my $version = <CMD>;
208close(CMD);
209chomp($version);
210# RHEL 5 has fdisk (util-linux 2.13-pre7)
211# Mageia 4 has fdisk from util-linux 2.24.2
212$version =~ s/[^0-9\.]*([0-9a-z\.-]+)[\)]*$/$1/;
213my ($v,$maj,$min) = split(/\./,$version);
214
215# Consider pre version the same as the following for formats
216if ((defined $maj) && ($maj =~ /-pre/)) {
217 $maj =~ s/-pre.*$//;
218 $maj++;
219}
220if ((defined $min) && ($min =~ /-pre/)) {
221 $min =~ s/-pre.*$//;
222 $min++;
223}
224
225if (($v == 1) || (($v == 2) && ($maj < 22))) {
226 # Check partition table type
227 print FLOG "We use an old fdisk, activating replacement code...\n";
228 $parted = is_lsb($parted);
229 $type = which_type($device);
230 if ($type ne "msdos") {
231 print FLOG "Not an msdos type of disk label\n";
232 if ($args =~ /-l/) {
233 fdisk_list($device,undef,\%start,\%end, 1);
234 } elsif ($args =~ /-s/) {
235 fdisk_list($device,$wpart,\%start,\%end, 1);
236 } elsif (($args =~ /-/) and ($fake == 0)) {
237 printf FLOG "Option not supported ($args) ...\n";
238 printf FLOG "Please report to the author\n";
239 mysyn();
240 } else {
241 # Read fdisk orders on stdin and pass them to parted
242 # on the command line as parted doesn't read on stdin
243 print FLOG "Translating fdisk command to parted\n";
244 while ($i = <STDIN>) {
245 if ($i =~ /^p$/) {
246 fdisk_list($device,undef,\%start,\%end, 1);
247 print "command (m for help) send back to fake fdisk for mondorestore\n";
248 } elsif ($i =~ /^n$/) {
249 fdisk_list($device,undef,\%start,\%end, 0);
250 if ($type ne "gpt") {
251 print FLOG "Forcing GPT type of disk label\n";
252 print FLOG "mklabel gpt\n";
253 system "$parted -s $device mklabel gpt\n" if ($fake == 0);
254 $type = "gpt";
255 }
256 $l = <STDIN>;
257 if (not (defined $l)) {
258 print FLOG "no primary/extended arg given for creation... assuming primary\n";
259 $l = "p";
260 }
261 chomp($l);
262 $part = <STDIN>;
263 if ((not (defined $part)) || ($part eq "")) {
264 print FLOG "no partition given for creation... skipping\n";
265 next;
266 }
267 chomp($part);
268 $cylstart = <STDIN>;
269 chomp($cylstart);
270 if ((not (defined $cylstart)) || ($cylstart eq "")) {
271 if (defined $start{$part-1}) {
272 # in MB => cyl
273 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
274 print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
275 } else {
276 print FLOG "no start cyl given for creation... assuming the following 1\n";
277 $cylstart = 1;
278 }
279 }
280 $cylstart = 1 if ($cylstart < 1);
281 print FLOG "start cyl : $cylstart\n";
282 $un = get_un($device, "", 0);
283 # parted needs MB
284 if ($cylstart == 1) {
285 $start = 0.01;
286 } else {
287 $start = $cylstart* $un / $mega + 0.001;
288 }
289 # this is a size in B/KB/MB/GB
290
291 $endmax = get_max($device);
292 $cylend = <STDIN>;
293 chomp($cylend);
294 if ((not (defined $cylend)) || ($cylend eq "")) {
295 print FLOG "no end cyl given for creation... assuming full disk)\n";
296 $cylend = $endmax;
297 }
298 # Handles end syntaxes (+, K, M, ...)
299 # to give cylinders
300 if ($cylend =~ /^\+/) {
301 $cylend =~ s/^\+//;
302 # Handles suffixes; return bytes
303 $cylend = decode_Bsuf($cylend,1);
304 # This gives the number of cyl
305 $cylend /= $un;
306 $cylend = sprintf("%d",$cylend);
307 $cylend += $cylstart - 0.001;
308 # We now have the end cyl
309 }
310 $cylend = $endmax if ($cylend > $endmax);
311 print FLOG "end cyl : $cylend\n";
312 # parted needs MB
313 $end = $cylend * $un / $mega;
314 print FLOG "n $l $part $cylstart $cylend => mkpart primary $start $end\n";
315 system "$parted -s $device mkpart primary ext2 $start $end\n" if ($fake == 0);
316 print "command (m for help) send back to fake fdisk for mondorestore\n";
317 } elsif ($i =~ /^d$/) {
318 $part = <STDIN>;
319 if (not (defined $part)) {
320 print FLOG "no partition given for deletion... skipping\n";
321 next;
322 }
323 chomp($part);
324 print FLOG "d $part => rm $part\n";
325 system "$parted -s $device rm $part\n" if ($fake == 0);
326 get_parted($device,undef,\%start,\%end,undef);
327 print "command (m for help) send back to fake fdisk for mondorestore\n";
328 } elsif ($i =~ /^w$/) {
329 print FLOG "w => quit\n";
330 } elsif ($i =~ /^t$/) {
331 $part = <STDIN>;
332 if (not (defined $part)) {
333 print FLOG "no partition given for tagging... skipping\n";
334 next;
335 }
336 chomp($part);
337 # If no partition number given it's 1, and we received the type
338 if ($part !~ /\d+/) {
339 $l = $part;
340 $part = 1
341 } else {
342 $l = <STDIN>;
343 }
344 if (not (defined $l)) {
345 print FLOG "no type given for tagging partition $part... skipping\n";
346 next;
347 }
348 chomp($l);
349 if (not (defined $pnum{$l})) {
350 print FLOG "no partition number given for $l... please report to the author\n";
351 next;
352 }
353
354 if ($pnum{$l} eq "lvm") {
355 # In that case this is a flag set, not a mkfs
356 print FLOG "t $part $l => set $part $pnum{$l} on\n";
357 system "$parted -s $device set $part $pnum{$l} on\n" if ($fake == 0);
358 } else {
359 print FLOG "t $part $l => mkfs $part $pnum{$l}\n";
360 system "$parted -s $device mkfs $part $pnum{$l}\n" if ($fake == 0);
361 }
362 print "command (m for help) send back to fake fdisk for mondorestore\n";
363 } elsif ($i =~ /^a$/) {
364 $part = <STDIN>;
365 if (not (defined $part)) {
366 print FLOG "no partition given for tagging... skipping\n";
367 next;
368 }
369 chomp($part);
370
371 # Partition shouldn't be negative or null. Then take the first one.
372 $part = 1 if ($part le 0);
373
374 print FLOG "a $part => set $part boot on\n";
375 system "$parted -s $device set $part boot on\n" if ($fake == 0);
376 print "command (m for help) send back to fake fdisk for mondorestore\n";
377 } elsif ($i =~ /^q$/) {
378 print FLOG "q => quit\n";
379 } else {
380 print FLOG "Unknown command: $i\n";
381 print "command (m for help) send back to fake fdisk for mondorestore\n";
382 next;
383 }
384
385 }
386 }
387 myexit(0);
388 }
389}
390
391#
392# Else everything is for fdisk
393#
394# Print only mode
395local_fdisk($args,$device);
396myexit(0);
397
398sub local_fdisk {
399
400my $args=shift;
401my $device=shift;
402
403print FLOG "Passing everything to the real fdisk with $args $device\n";
404
405if ($args =~ /^-/) {
406 # -l or -s
407 open (FDISK, "$fdisk $args $device 2>/dev/null |") || die "Unable to read from $fdisk";
408 while (<FDISK>) {
409 print $_;
410 }
411 close(FDISK);
412} else {
413 # Modification mode
414 open (FDISK, "| $fdisk $args $device 2>/dev/null") || die "Unable to modify through $fdisk";
415 while (<STDIN>) {
416 print FDISK $_;
417 }
418 close(FDISK);
419 close(STDIN);
420}
421return;
422}
423
424# Is your system LSB ?
425sub is_lsb {
426
427my $cmd = shift;
428my $basename = basename($cmd);
429
430if (not (-x $cmd)) {
431 print FLOG "Your system is not LSB/mondo compliant: $basename was not found as $cmd\n";
432 print FLOG "Searching elswhere...";
433 foreach $i (split(':',$ENV{PATH})) {
434 if (-x "$i/$basename") {
435 $cmd = "$i/$basename";
436 print FLOG "Found $cmd, using it !\n";
437 last;
438 }
439 }
440 if (not (-x $cmd)) {
441 print FLOG "Your system doesn't provide $basename in the PATH\n";
442 print FLOG "Please correct it before relaunching\n";
443 myexit(-1);
444 }
445}
446return($cmd);
447}
448
449# Unused for now - Kept for reference in case there is a need later on
450sub fdisk_list_all {
451my $device = shift;
452my $wpart = shift;
453my $start = shift;
454my $end = shift;
455my $verbose = shift;
456
457return fdisk_list($device,$wpart,$start,$end,$verbose) if ((defined $device) && ($device ne ""));
458
459# If no device given loop on the list of devices found in /proc/partitions
460open(PART,"/proc/partitions") || die "Unable to open /proc/partitions";
461while (<PART>) {
462 my ($maj,$min,$blocks,$dev) = split(/\s+/);
463 next if ($dev =~ /^fd|^sr/);
464 next if ($min != 0);
465 fdisk_list("/dev/$dev",$wpart,$start,$end,$verbose);
466}
467close(PART);
468}
469
470
471sub fdisk_list {
472
473my $device = shift;
474my $wpart = shift;
475my $start = shift;
476my $end = shift;
477my $verbose = shift;
478
479my $un;
480my $endmax;
481my $d;
482my $n;
483
484my %cmt = ( "FAT" => "FAT",
485 "ext2" => "Linux",
486 "ext3" => "Linux",
487 "ext4" => "Linux",
488 "xfs" => "Linux",
489 "reiserfs" => "Linux",
490 "linux-swap" => "Linux swap",
491 "lvm" => "Linux LVM",
492 "raid" => "RAID Linux auto",
493 "fat16" => "fat16",
494 "fat32" => "fat32",
495 "" => "Linux",
496);
497
498my $part;
499my $mstart;
500my $mend;
501my $length;
502my $pid;
503my $cmt;
504format FLOG1 =
505@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
506$part, $mstart, $mend, $length, $pid, $cmt
507.
508format FLOG2 =
509@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
510$part,
511 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
512 $mstart, $mend, $length, $pid, $cmt
513.
514format STDOUT1 =
515@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
516$part, $mstart, $mend, $length, $pid, $cmt
517.
518format STDOUT2 =
519@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
520$part,
521 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
522 $mstart, $mend, $length, $pid, $cmt
523.
524# Device Boot Start End Blocks Id System
525#/dev/hda1 1 77579 39099374+ ee EFI GPT
526
527
528#
529# Keep Fdisk headers
530#
531# this will return bytes
532$un = get_un($device,$wpart,$verbose);
533
534$endmax = get_max($device);
535
536# This will return MB
537get_parted($device,$start,$end,\%type);
538
539while (($n,$d) = each %type) {
540 # Print infos fdisk like
541 $part = ${device}.$n;
542 # start and end are in cylinder in fdisk format
543 # so return in MB * 1MB / what represents 1 cyl in B
544 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
545 $mstart = 1 if ($mstart < 1);
546 $mstart = $endmax if ($mstart > $endmax);
547 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
548 $mend = $endmax if ($mend > $endmax);
549 $mend = 1 if ($mend < 1);
550 # length is in 1K blocks
551 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
552 $pid = $pid{$type{$n}};
553 $cmt = $cmt{$type{$n}};
554 #print FLOG "$part - $mstart - $mend - $length\n";
555
556 if ($verbose == 1) {
557 if (not (defined $wpart)) {
558 if (length($part) > 13) {
559 open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
560 select(STDOUT2);
561 write;
562 open(FLOG2,">&FLOG") || die "Unable to open FLOG2";
563 select(FLOG2);
564 write;
565 select(STDOUT);
566 close(FLOG2);
567 close(STDOUT2);
568 } else {
569 open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1";
570 select(STDOUT1);
571 write;
572 open(FLOG1,">&FLOG") || die "Unable to open FLOG1";
573 select(FLOG1);
574 write;
575 select(STDOUT);
576 close(FLOG1);
577 close(STDOUT1);
578 }
579 } else {
580 # manage the -s option of fdisk here
581 print "$length\n" if ($part eq $wpart);
582 print FLOG "$part has $length KBytes\n" if ($part eq $wpart);
583 }
584 }
585}
586close(FDISK);
587close(PARTED);
588}
589
590#
591# Get max size from fdisk
592#
593sub get_max {
594
595my $device = shift;
596my $max = 0;
597my $foo;
598
599open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
600while (<FDISK>) {
601 if ($_ =~ /heads/) {
602 chomp;
603 $max = $_;
604 $max =~ s/.* ([0-9]+) cylinders/$1/;
605 }
606}
607close(FDISK);
608print FLOG "get_max returns $max\n";
609return($max);
610}
611
612#
613# Get units from fdisk (cylinder size)
614#
615sub get_un {
616
617my $device = shift;
618my $wpart = shift;
619my $verbose = shift;
620my $un = 0;
621my $foo;
622
623open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
624while (<FDISK>) {
625 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
626 if ($_ =~ /^Units/) {
627 ($foo, $un , $foo) = split /=/;
628 $un =~ s/[A-z\s=]//g;
629 $un = eval($un);
630 }
631}
632close(FDISK);
633print FLOG "get_un returns $un\n";
634return($un);
635}
636
637#
638# Parted gives info in MB
639# (depending on versions - 1.6.25.1 provides suffixes)
640#
641sub get_parted {
642
643my $device = shift;
644my $start = shift;
645my $end = shift;
646my $type = shift;
647my $void;
648my $d;
649my $n;
650my $ret;
651my $mode;
652my $size;
653my $unit;
654
655open (PARTED, "$parted -v |") || die "Unable to read from $parted";
656$d = <PARTED>;
657print FLOG "$d";
658close(PARTED);
659chomp($d);
660# parted version
661$d =~ s/[^0-9\.]*([0-9\.]+)$/$1/;
662my ($v,$maj,$min) = split(/\./,$d);
663# depending on parted version, information given change:
664if ($v == 2) {
665 # RHEL 6 parted 2.1
666 $mode=2;
667} elsif ($v == 1) {
668 if (($maj <= 5) || (($maj == 6) && (defined $min) && ($min < 25))) {
669 # RHEL 3 parted 1.6.3
670 # RHEL 4 parted 1.6.19
671 $mode=0;
672 } else {
673 # SLES 10 parted >= 1.6.25
674 $mode=1;
675 }
676} else {
677 $mode=-1;
678}
679print FLOG "mode: $mode\n";
680
681open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
682# Skip 3 first lines
683$d = <PARTED>;
684$d = <PARTED>;
685$d = <PARTED>;
686
687if ($mode == 2) {
688 $d = <PARTED>;
689 $d = <PARTED>;
690 $d = <PARTED>;
691}
692print FLOG "Got from parted: \n";
693print FLOG "Minor Start End Filesystem\n";
694# Get info from each partition line
695while (($n,$d) = split(/\s/, <PARTED>,2)) {
696 chomp($d);
697 # v2 of parted ends with empty line
698 next if (($mode == 2) && ($n eq "") && ($d eq ""));
699 # v2 of parted starts with space potentially
700 ($n,$d) = split(/\s/, $d,2) if (($mode == 2) && ($n eq ""));
701 next if ($n !~ /^[1-9]/);
702 $d =~ s/^\s*//;
703 $d =~ s/\s+/ /g;
704 if ($mode == 0) {
705 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
706 $unit = 1;
707 } elsif ($mode == 1) {
708 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
709 $unit = $mega;
710 } elsif ($mode == 2) {
711 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
712 $unit = $mega;
713 } else {
714 die "Undefined mode $mode";
715 }
716 $$start{$n} = "" if (not defined $$start{$n});
717 $$end{$n} = "" if (not defined $$end{$n});
718 $$type{$n} = "" if (not defined $$type{$n});
719 # Handles potential suffixes in latest parted version. Return MB
720 $ret = decode_Bsuf($$start{$n},$unit);
721 $$start{$n} = $ret;
722 $ret = decode_Bsuf($$end{$n},$unit);
723 $$end{$n} = $ret;
724 print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
725}
726close(PARTED);
727}
728
729sub decode_Bsuf {
730
731my $size = shift;
732my $unit = shift;
733my $ret = 0;
734
735#print FLOG "decode_Bsuf input: $size / $unit ";
736if ($size =~ /K[B]*$/i) {
737 $size =~ s/K[B]*$//i;
738 $size *= 1024;
739} elsif ($size =~ /M[B]*$/i) {
740 $size =~ s/M[B]*$//i;
741 $size *= 1048576;
742} elsif ($size =~ /G[B]*$/i) {
743 $size =~ s/G[B]*$//i;
744 $size *= 1073741824;
745} elsif ($size =~ /T[B]*$/i) {
746 $size =~ s/T[B]*$//i;
747 $size *= 1099511627776;
748} else {
749 # Nothing to do
750}
751$ret = $size / $unit;
752#print FLOG " - output : $size => $ret\n";
753return($ret);
754}
755
756sub myexit {
757
758my $val=shift;
759
760close(FLOG);
761exit($val);
762}
763
764sub which_type {
765
766my $device = shift;
767my $type = "";
768
769open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
770while (<FDISK>) {
771 if ($_ =~ /EFI GPT/) {
772 $type= "gpt";
773 print FLOG "Found a GPT partition format\n";
774 last;
775 }
776}
777close(FDISK);
778open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
779while (<PARTED>) {
780 if (($_ =~ /Disk label type: msdos/) || ($_ =~ /Partition Table: msdos/)) {
781 $type= "msdos";
782 print FLOG "Found a msdos partition format\n";
783 last;
784 }
785}
786close(FDISK);
787return ($type);
788}
789
790sub mysyn {
791 print "Syntax: $0 [-l] device | [-s] partition\n";
792 myexit(-1);
793}
Note: See TracBrowser for help on using the repository browser.