source: MondoRescue/branches/3.2/mindi/mr-parted2fdisk

Last change on this file was 3591, checked in by Bruno Cornec, 8 years ago

disable fdisk output buffering in mr-parted2fdisk (William Montgomery)

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