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

Last change on this file since 3355 was 3355, checked in by Bruno Cornec, 9 years ago
  • Fix parted2fdisk for RHEL5 support with regex for fdisk version adapted
  • parted2fdisk now supports being called with -l option without device. It then calls fdisk directly for this case to be fully compatible.
  • Property svn:keywords set to Id
File size: 18.7 KB
Line 
1#!/usr/bin/perl -w
2#
3# $Id: parted2fdisk.pl 3355 2015-03-05 12:25:41Z 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 $device =~ s/[0-9]+$//;
185}
186
187if ($args =~ /-n/) {
188 print FLOG "Fake mode. Nothing will be really done\n";
189 $fake = 1;
190}
191
192print FLOG "Called with device $device and arg $args\n";
193
194if (($args =~ /-l/) && ($device eq "")) {
195 # Pass to real fdisk directly
196 local_fdisk($args,$device);
197 myexit(0);
198}
199
200# util-linux/fdisk version
201open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
202my $version = <CMD>;
203close(CMD);
204chomp($version);
205# RHEL 5 has fdisk (util-linux 2.13-pre7)
206# Mageia 4 has fdisk from util-linux 2.24.2
207$version =~ s/[^0-9\.]*([0-9a-z\.-]+)[\)]*$/$1/;
208my ($v,$maj,$min) = split(/\./,$version);
209
210# Consider pre version the same as the following for formats
211if ((defined $maj) && ($maj =~ /-pre/)) {
212 $maj =~ s/-pre.*$//;
213 $maj++;
214}
215if ((defined $min) && ($min =~ /-pre/)) {
216 $min =~ s/-pre.*$//;
217 $min++;
218}
219
220if (($v == 1) || (($v == 2) && ($maj < 22))) {
221 # Check partition table type
222 print FLOG "We use an old fdisk, activating replacement code...\n";
223 $parted = is_lsb($parted);
224 $type = which_type($device);
225 if ($type ne "msdos") {
226 print FLOG "Not an msdos type of disk label\n";
227 if ($args =~ /-l/) {
228 fdisk_list($device,undef,\%start,\%end, 1);
229 } elsif ($args =~ /-s/) {
230 fdisk_list($device,$wpart,\%start,\%end, 1);
231 } elsif (($args =~ /-/) and ($fake == 0)) {
232 printf FLOG "Option not supported ($args) ...\n";
233 printf FLOG "Please report to the author\n";
234 mysyn();
235 } else {
236 # Read fdisk orders on stdin and pass them to parted
237 # on the command line as parted doesn't read on stdin
238 print FLOG "Translating fdisk command to parted\n";
239 while ($i = <STDIN>) {
240 if ($i =~ /^p$/) {
241 fdisk_list($device,undef,\%start,\%end, 1);
242 print "command (m for help) send back to fake fdisk for mondorestore\n";
243 } elsif ($i =~ /^n$/) {
244 fdisk_list($device,undef,\%start,\%end, 0);
245 if ($type ne "gpt") {
246 print FLOG "Forcing GPT type of disk label\n";
247 print FLOG "mklabel gpt\n";
248 system "$parted -s $device mklabel gpt\n" if ($fake == 0);
249 $type = "gpt";
250 }
251 $l = <STDIN>;
252 if (not (defined $l)) {
253 print FLOG "no primary/extended arg given for creation... assuming primary\n";
254 $l = "p";
255 }
256 chomp($l);
257 $part = <STDIN>;
258 if ((not (defined $part)) || ($part eq "")) {
259 print FLOG "no partition given for creation... skipping\n";
260 next;
261 }
262 chomp($part);
263 $cylstart = <STDIN>;
264 chomp($cylstart);
265 if ((not (defined $cylstart)) || ($cylstart eq "")) {
266 if (defined $start{$part-1}) {
267 # in MB => cyl
268 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
269 print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
270 } else {
271 print FLOG "no start cyl given for creation... assuming the following 1\n";
272 $cylstart = 1;
273 }
274 }
275 $cylstart = 1 if ($cylstart < 1);
276 print FLOG "start cyl : $cylstart\n";
277 $un = get_un($device, "", 0);
278 # parted needs MB
279 if ($cylstart == 1) {
280 $start = 0.01;
281 } else {
282 $start = $cylstart* $un / $mega + 0.001;
283 }
284 # this is a size in B/KB/MB/GB
285
286 $endmax = get_max($device);
287 $cylend = <STDIN>;
288 chomp($cylend);
289 if ((not (defined $cylend)) || ($cylend eq "")) {
290 print FLOG "no end cyl given for creation... assuming full disk)\n";
291 $cylend = $endmax;
292 }
293 # Handles end syntaxes (+, K, M, ...)
294 # to give cylinders
295 if ($cylend =~ /^\+/) {
296 $cylend =~ s/^\+//;
297 # Handles suffixes; return bytes
298 $cylend = decode_Bsuf($cylend,1);
299 # This gives the number of cyl
300 $cylend /= $un;
301 $cylend = sprintf("%d",$cylend);
302 $cylend += $cylstart - 0.001;
303 # We now have the end cyl
304 }
305 $cylend = $endmax if ($cylend > $endmax);
306 print FLOG "end cyl : $cylend\n";
307 # parted needs MB
308 $end = $cylend * $un / $mega;
309 print FLOG "n $l $part $cylstart $cylend => mkpart primary $start $end\n";
310 system "$parted -s $device mkpart primary ext2 $start $end\n" if ($fake == 0);
311 print "command (m for help) send back to fake fdisk for mondorestore\n";
312 } elsif ($i =~ /^d$/) {
313 $part = <STDIN>;
314 if (not (defined $part)) {
315 print FLOG "no partition given for deletion... skipping\n";
316 next;
317 }
318 chomp($part);
319 print FLOG "d $part => rm $part\n";
320 system "$parted -s $device rm $part\n" if ($fake == 0);
321 get_parted($device,undef,\%start,\%end,undef);
322 print "command (m for help) send back to fake fdisk for mondorestore\n";
323 } elsif ($i =~ /^w$/) {
324 print FLOG "w => quit\n";
325 } elsif ($i =~ /^t$/) {
326 $part = <STDIN>;
327 if (not (defined $part)) {
328 print FLOG "no partition given for tagging... skipping\n";
329 next;
330 }
331 chomp($part);
332 # If no partition number given it's 1, and we received the type
333 if ($part !~ /\d+/) {
334 $l = $part;
335 $part = 1
336 } else {
337 $l = <STDIN>;
338 }
339 if (not (defined $l)) {
340 print FLOG "no type given for tagging partition $part... skipping\n";
341 next;
342 }
343 chomp($l);
344 if (not (defined $pnum{$l})) {
345 print FLOG "no partition number given for $l... please report to the author\n";
346 next;
347 }
348
349 if ($pnum{$l} eq "lvm") {
350 # In that case this is a flag set, not a mkfs
351 print FLOG "t $part $l => set $part $pnum{$l} on\n";
352 system "$parted -s $device set $part $pnum{$l} on\n" if ($fake == 0);
353 } else {
354 print FLOG "t $part $l => mkfs $part $pnum{$l}\n";
355 system "$parted -s $device mkfs $part $pnum{$l}\n" if ($fake == 0);
356 }
357 print "command (m for help) send back to fake fdisk for mondorestore\n";
358 } elsif ($i =~ /^a$/) {
359 $part = <STDIN>;
360 if (not (defined $part)) {
361 print FLOG "no partition given for tagging... skipping\n";
362 next;
363 }
364 chomp($part);
365
366 # Partition shouldn't be negative or null. Then take the first one.
367 $part = 1 if ($part le 0);
368
369 print FLOG "a $part => set $part boot on\n";
370 system "$parted -s $device set $part boot on\n" if ($fake == 0);
371 print "command (m for help) send back to fake fdisk for mondorestore\n";
372 } elsif ($i =~ /^q$/) {
373 print FLOG "q => quit\n";
374 } else {
375 print FLOG "Unknown command: $i\n";
376 print "command (m for help) send back to fake fdisk for mondorestore\n";
377 next;
378 }
379
380 }
381 }
382 myexit(0);
383 }
384}
385
386#
387# Else everything is for fdisk
388#
389# Print only mode
390local_fdisk($args,$device);
391myexit(0);
392
393sub local_fdisk {
394
395my $args=shift;
396my $device=shift;
397
398print FLOG "Passing everything to the real fdisk with $args $device\n";
399
400if ($args =~ /^-/) {
401 # -l or -s
402 open (FDISK, "$fdisk $args $device 2>/dev/null |") || die "Unable to read from $fdisk";
403 while (<FDISK>) {
404 print $_;
405 }
406 close(FDISK);
407} else {
408 # Modification mode
409 open (FDISK, "| $fdisk $args $device 2>/dev/null") || die "Unable to modify through $fdisk";
410 while (<STDIN>) {
411 print FDISK $_;
412 }
413 close(FDISK);
414 close(STDIN);
415}
416return;
417}
418
419# Is your system LSB ?
420sub is_lsb {
421
422my $cmd = shift;
423my $basename = basename($cmd);
424
425if (not (-x $cmd)) {
426 print FLOG "Your system is not LSB/mondo compliant: $basename was not found as $cmd\n";
427 print FLOG "Searching elswhere...";
428 foreach $i (split(':',$ENV{PATH})) {
429 if (-x "$i/$basename") {
430 $cmd = "$i/$basename";
431 print FLOG "Found $cmd, using it !\n";
432 last;
433 }
434 }
435 if (not (-x $cmd)) {
436 print FLOG "Your system doesn't provide $basename in the PATH\n";
437 print FLOG "Please correct it before relaunching\n";
438 myexit(-1);
439 }
440}
441return($cmd);
442}
443
444# Unused for now - Kept for reference in case there is a need later on
445sub fdisk_list_all {
446my $device = shift;
447my $wpart = shift;
448my $start = shift;
449my $end = shift;
450my $verbose = shift;
451
452return fdisk_list($device,$wpart,$start,$end,$verbose) if ((defined $device) && ($device ne ""));
453
454# If no device given loop on the list of devices found in /proc/partitions
455open(PART,"/proc/partitions") || die "Unable to open /proc/partitions";
456while (<PART>) {
457 my ($maj,$min,$blocks,$dev) = split(/\s+/);
458 next if ($dev =~ /^fd|^sr/);
459 next if ($min != 0);
460 fdisk_list("/dev/$dev",$wpart,$start,$end,$verbose);
461}
462close(PART);
463}
464
465
466sub fdisk_list {
467
468my $device = shift;
469my $wpart = shift;
470my $start = shift;
471my $end = shift;
472my $verbose = shift;
473
474my $un;
475my $endmax;
476my $d;
477my $n;
478
479my %cmt = ( "FAT" => "FAT",
480 "ext2" => "Linux",
481 "ext3" => "Linux",
482 "ext4" => "Linux",
483 "xfs" => "Linux",
484 "reiserfs" => "Linux",
485 "linux-swap" => "Linux swap",
486 "lvm" => "Linux LVM",
487 "raid" => "RAID Linux auto",
488 "fat16" => "fat16",
489 "fat32" => "fat32",
490 "" => "Linux",
491);
492
493my $part;
494my $mstart;
495my $mend;
496my $length;
497my $pid;
498my $cmt;
499format FLOG1 =
500@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
501$part, $mstart, $mend, $length, $pid, $cmt
502.
503format FLOG2 =
504@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
505$part,
506 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
507 $mstart, $mend, $length, $pid, $cmt
508.
509format STDOUT1 =
510@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
511$part, $mstart, $mend, $length, $pid, $cmt
512.
513format STDOUT2 =
514@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
515$part,
516 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
517 $mstart, $mend, $length, $pid, $cmt
518.
519# Device Boot Start End Blocks Id System
520#/dev/hda1 1 77579 39099374+ ee EFI GPT
521
522
523#
524# Keep Fdisk headers
525#
526# this will return bytes
527$un = get_un ($device,$wpart,$verbose);
528
529$endmax = get_max($device);
530
531# This will return MB
532get_parted($device,$start,$end,\%type);
533
534while (($n,$d) = each %type) {
535 # Print infos fdisk like
536 $part = ${device}.$n;
537 # start and end are in cylinder in fdisk format
538 # so return in MB * 1MB / what represents 1 cyl in B
539 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
540 $mstart = 1 if ($mstart < 1);
541 $mstart = $endmax if ($mstart > $endmax);
542 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
543 $mend = $endmax if ($mend > $endmax);
544 $mend = 1 if ($mend < 1);
545 # length is in 1K blocks
546 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
547 $pid = $pid{$type{$n}};
548 $cmt = $cmt{$type{$n}};
549 #print FLOG "$part - $mstart - $mend - $length\n";
550
551 if ($verbose == 1) {
552 if (not (defined $wpart)) {
553 if (length($part) > 13) {
554 open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
555 select(STDOUT2);
556 write;
557 open(FLOG2,">&FLOG") || die "Unable to open FLOG2";
558 select(FLOG2);
559 write;
560 select(STDOUT);
561 close(FLOG2);
562 close(STDOUT2);
563 } else {
564 open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1";
565 select(STDOUT1);
566 write;
567 open(FLOG1,">&FLOG") || die "Unable to open FLOG1";
568 select(FLOG1);
569 write;
570 select(STDOUT);
571 close(FLOG1);
572 close(STDOUT1);
573 }
574 } else {
575 # manage the -s option of fdisk here
576 print "$length\n" if ($part eq $wpart);
577 print FLOG "$part has $length KBytes\n" if ($part eq $wpart);
578 }
579 }
580}
581close(FDISK);
582close(PARTED);
583}
584
585#
586# Get max size from fdisk
587#
588sub get_max {
589
590my $device = shift;
591my $max = 0;
592my $foo;
593
594open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
595while (<FDISK>) {
596 if ($_ =~ /heads/) {
597 chomp;
598 $max = $_;
599 $max =~ s/.* ([0-9]+) cylinders/$1/;
600 }
601}
602close(FDISK);
603print FLOG "get_max returns $max\n";
604return($max);
605}
606
607#
608# Get units from fdisk (cylinder size)
609#
610sub get_un {
611
612my $device = shift;
613my $wpart = shift;
614my $verbose = shift;
615my $un = 0;
616my $foo;
617
618open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
619while (<FDISK>) {
620 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
621 if ($_ =~ /^Units/) {
622 ($foo, $un , $foo) = split /=/;
623 $un =~ s/[A-z\s=]//g;
624 $un = eval($un);
625 }
626}
627close(FDISK);
628print FLOG "get_un returns $un\n";
629return($un);
630}
631
632#
633# Parted gives info in MB
634# (depending on versions - 1.6.25.1 provides suffixes)
635#
636sub get_parted {
637
638my $device = shift;
639my $start = shift;
640my $end = shift;
641my $type = shift;
642my $void;
643my $d;
644my $n;
645my $ret;
646my $mode;
647my $size;
648my $unit;
649
650open (PARTED, "$parted -v |") || die "Unable to read from $parted";
651$d = <PARTED>;
652print FLOG "$d";
653close(PARTED);
654chomp($d);
655# parted version
656$d =~ s/[^0-9\.]*([0-9\.]+)$/$1/;
657my ($v,$maj,$min) = split(/\./,$d);
658# depending on parted version, information given change:
659if ($v == 2) {
660 # RHEL 6 parted 2.1
661 $mode=2;
662} elsif ($v == 1) {
663 if (($maj <= 5) || (($maj == 6) && (defined $min) && ($min < 25))) {
664 # RHEL 3 parted 1.6.3
665 # RHEL 4 parted 1.6.19
666 $mode=0;
667 } else {
668 # SLES 10 parted >= 1.6.25
669 $mode=1;
670 }
671} else {
672 $mode=-1;
673}
674print FLOG "mode: $mode\n";
675
676open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
677# Skip 3 first lines
678$d = <PARTED>;
679$d = <PARTED>;
680$d = <PARTED>;
681
682if ($mode == 2) {
683 $d = <PARTED>;
684 $d = <PARTED>;
685 $d = <PARTED>;
686}
687print FLOG "Got from parted: \n";
688print FLOG "Minor Start End Filesystem\n";
689# Get info from each partition line
690while (($n,$d) = split(/\s/, <PARTED>,2)) {
691 chomp($d);
692 # v2 of parted ends with empty line
693 next if (($mode == 2) && ($n eq "") && ($d eq ""));
694 # v2 of parted starts with space potentially
695 ($n,$d) = split(/\s/, $d,2) if (($mode == 2) && ($n eq ""));
696 next if ($n !~ /^[1-9]/);
697 $d =~ s/^\s*//;
698 $d =~ s/\s+/ /g;
699 if ($mode == 0) {
700 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
701 $unit = 1;
702 } elsif ($mode == 1) {
703 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
704 $unit = $mega;
705 } elsif ($mode == 2) {
706 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
707 $unit = $mega;
708 } else {
709 die "Undefined mode $mode";
710 }
711 $$start{$n} = "" if (not defined $$start{$n});
712 $$end{$n} = "" if (not defined $$end{$n});
713 $$type{$n} = "" if (not defined $$type{$n});
714 # Handles potential suffixes in latest parted version. Return MB
715 $ret = decode_Bsuf($$start{$n},$unit);
716 $$start{$n} = $ret;
717 $ret = decode_Bsuf($$end{$n},$unit);
718 $$end{$n} = $ret;
719 print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
720}
721close(PARTED);
722}
723
724sub decode_Bsuf {
725
726my $size = shift;
727my $unit = shift;
728my $ret = 0;
729
730#print FLOG "decode_Bsuf input: $size / $unit ";
731if ($size =~ /K[B]*$/i) {
732 $size =~ s/K[B]*$//i;
733 $size *= 1024;
734} elsif ($size =~ /M[B]*$/i) {
735 $size =~ s/M[B]*$//i;
736 $size *= 1048576;
737} elsif ($size =~ /G[B]*$/i) {
738 $size =~ s/G[B]*$//i;
739 $size *= 1073741824;
740} elsif ($size =~ /T[B]*$/i) {
741 $size =~ s/T[B]*$//i;
742 $size *= 1099511627776;
743} else {
744 # Nothing to do
745}
746$ret = $size / $unit;
747#print FLOG " - output : $size => $ret\n";
748return($ret);
749}
750
751sub myexit {
752
753my $val=shift;
754
755close(FLOG);
756exit($val);
757}
758
759sub which_type {
760
761my $device = shift;
762my $type = "";
763
764open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
765while (<FDISK>) {
766 if ($_ =~ /EFI GPT/) {
767 $type= "gpt";
768 print FLOG "Found a GPT partition format\n";
769 last;
770 }
771}
772close(FDISK);
773open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
774while (<PARTED>) {
775 if ($_ =~ /Disk label type: msdos/) {
776 $type= "msdos";
777 print FLOG "Found a msdos partition format\n";
778 last;
779 }
780}
781close(FDISK);
782return ($type);
783}
784
785sub mysyn {
786 print "Syntax: $0 [-l] device | [-s] partition\n";
787 myexit(-1);
788}
Note: See TracBrowser for help on using the repository browser.