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

Last change on this file since 3383 was 3383, checked in by Bruno Cornec, 9 years ago
  • Fix mindi-get-perl-modules to get all modules in dependency as well (pb with Expoerter module missing during UEFI tests)
  • Property svn:keywords set to Id
File size: 18.3 KB
RevLine 
[1]1#!/usr/bin/perl -w
2#
[88]3# $Id: mr-parted2fdisk 3383 2015-05-12 00:16:38Z bruno $
4#
[3377]5# mr-parted2fdisk: fdisk like interface for parted
[3343]6# [developed for mindi/mondo http://www.mondorescue.org]
[1]7#
8# Aims to be architecture independant (i386/ia64)
[3355]9# Tested on ia64 with RHAS 2.1 - Mandrake 9.0 - RHEL 3.0 - SLES 10 - RHEL 5 -
[1]10#
[3343]11# Copyright B. Cornec 2000-2015
[3143]12# Provided under the GPL v2
[1]13
14use strict;
[3343]15use File::Basename;
[3370]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;
[1]24
[3143]25=pod
26
27=head1 NAME
28
[3377]29mr-parted2fdisk is a fdisk like command using parted internally for analysing GPT labelled disks
[3143]30
31=head1 DESCRIPTION
32
[3377]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).
[3143]34
35=head1 SYNOPSIS
36
[3377]37mr-parted2fdisk -s partition
[3370]38
[3377]39mr-parted2fdisk -l [device]
[3370]40
[3377]41mr-parted2fdisk [-n] device
[3143]42
43=head1 OPTIONS
44
45=over 4
46
47=item B<-s>
48
49Print the size (in blocks) of the given partition.
50
[3370]51=item B<-l>
52
53List the partition tables for the specified device (or all if none specified) and then exit.
54
[3143]55=item B<-n>
56
57Fake mode. Doesn't pass the commands just simulate.
58
[3377]59=item B<-v>
60
61Verbose mode. Used to help debugging issues.
62
[3143]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
[1]103$ENV{LANG} = "C";
104$ENV{LANGUAGE} = "C";
105$ENV{LC_ALL} = "C";
106
107# Log
[3377]108my $flog = "/var/log/mr-parted2fdisk.log";
[1]109open(FLOG, "> $flog") || die "Unable to open $flog";
110
111my $i;
112my $l;
113my $part;
114my $wpart;
115my $start = "";
116my $end = "";
[90]117my $cylstart;
118my $cylend;
[1]119my %start;
120my %end;
121my %type;
[88]122my $fake = 0;
[1801]123my $mega = 1048576;
[3370]124my %opts;
[1]125
[2163]126# Immediate flushing to avoids read error from mondorestore in log files
127$| = 1;
128
[1]129#
[3343]130# We always use fdisk except with GPT types of
[1]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;
[3370]137my $device;
[88]138my $endmax = "";
[3377]139my $appname = "mr-parted2fdisk";
[3370]140my ($mrver,$mrrev) = mr_version_init();
[1]141
[3370]142pb_syntax_init("$appname Version $mrver-$mrrev\n");
143
[1]144if ($#ARGV < 0) {
[3370]145 pb_syntax(-1,0);
[1]146}
147
[3370]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
[1]197my %pid = ( "FAT" => "6",
198 "fat32" => "b",
199 "fat16" => "e",
200 "ext2" => "83",
201 "ext3" => "83",
[2087]202 "ext4" => "83",
203 "xfs" => "83",
[3330]204 "btrfs" => "83",
[1565]205 "reiserfs" => "83",
[1]206 "linux-swap" => "82",
[2158]207 "lvm" => "8e",
[3343]208 "raid" => "fd",
[1]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
[3343]219# util-linux/fdisk version
[3370]220my $fdisk = pb_check_req("fdisk",0);
[3343]221open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
222my $version = <CMD>;
223close(CMD);
224chomp($version);
[3383]225# RHEL 5 has fdisk from util-linux 2.13-pre7
[3355]226# Mageia 4 has fdisk from util-linux 2.24.2
227$version =~ s/[^0-9\.]*([0-9a-z\.-]+)[\)]*$/$1/;
[3343]228my ($v,$maj,$min) = split(/\./,$version);
229
[3355]230# Consider pre version the same as the following for formats
231if ((defined $maj) && ($maj =~ /-pre/)) {
232 $maj =~ s/-pre.*$//;
233 $maj++;
234}
235if ((defined $min) && ($min =~ /-pre/)) {
236 $min =~ s/-pre.*$//;
237 $min++;
238}
239
[3370]240# Check partition table type
241$type = mr_disk_type($device);
242# Replacement code only for GPT disks
243if ((($v == 1) || (($v == 2) && ($maj < 22))) && ($type ne "msdos")) {
244 pb_log(1,"This distribution uses an old fdisk, activating replacement code for GPT disk label...\n");
245 my $parted = pb_check_req("parted",0);
246 if (defined $opts{'l'}) {
247 fdisk_list($device,undef,\%start,\%end, 1);
248 } elsif (defined $opts{'s'}) {
249 fdisk_list($device,$wpart,\%start,\%end, 1);
250 } else {
251 # Read fdisk orders on stdin and pass them to parted
252 # on the command line as parted doesn't read on stdin
253 pb_log(1,"Translating fdisk command to parted\n");
254 while ($i = <STDIN>) {
255 if ($i =~ /^p$/) {
256 fdisk_list($device,undef,\%start,\%end, 1);
257 print "command (m for help) sent back to fake fdisk for mondorestore\n";
258 } elsif ($i =~ /^n$/) {
259 fdisk_list($device,undef,\%start,\%end, 0);
260 if ($type ne "gpt") {
261 pb_log(1,"Forcing GPT type of disk label\n");
262 pb_log(1,"mklabel gpt\n");
263 pb_system("$parted -s $device mklabel gpt\n") if ($fake == 0);
264 $type = "gpt";
265 }
266 $l = <STDIN>;
267 if (not defined $l) {
268 pb_log(1,"no primary/extended arg given for creation... assuming primary\n");
269 $l = "p";
270 }
271 chomp($l);
272 $part = <STDIN>;
273 if ((not defined $part) || ($part eq "")) {
274 pb_log(1,"no partition given for creation... skipping\n");
275 next;
276 }
277 chomp($part);
278 $cylstart = <STDIN>;
279 chomp($cylstart);
280 if ((not defined $cylstart) || ($cylstart eq "")) {
281 if (defined $start{$part-1}) {
282 # in MB => cyl
283 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
[90]284 } else {
[3370]285 $cylstart = 1;
[90]286 }
[3370]287 pb_log(1,"no start cyl given for creation... assuming the following: $cylstart\n");
288 } else {
289 pb_log(1,"start cyl: $cylstart\n");
290 }
291 $cylstart = 1 if ($cylstart < 1);
292 $un = get_un($device, "", 0);
293 # parted needs MB
294 if ($cylstart == 1) {
295 $start = 0.01;
296 } else {
297 $start = $cylstart* $un / $mega + 0.001;
298 }
299 # this is a size in B/KB/MB/GB
[90]300
[3370]301 $endmax = get_max($device);
302 $cylend = <STDIN>;
303 chomp($cylend);
304 if ((not defined $cylend) || ($cylend eq "")) {
305 pb_log(1,"no end cyl given for creation... assuming full disk)\n");
306 $cylend = $endmax;
307 }
308 # Handles end syntaxes (+, K, M, ...)
309 # to give cylinders
310 if ($cylend =~ /^\+/) {
311 $cylend =~ s/^\+//;
312 # Handles suffixes; return bytes
313 $cylend = decode_Bsuf($cylend,1);
314 # This gives the number of cyl
315 $cylend /= $un;
316 $cylend = sprintf("%d",$cylend);
317 $cylend += $cylstart - 0.001;
318 # We now have the end cyl
319 }
320 $cylend = $endmax if ($cylend > $endmax);
321 pb_log(1,"end cyl: $cylend\n");
322 # parted needs MB
323 $end = $cylend * $un / $mega;
324 pb_log(1,"n $l $part $cylstart $cylend => mkpart primary $start $end\n");
325 pb_system("$parted -s $device mkpart primary ext2 $start $end\n") if ($fake == 0);
326 print "command (m for help) sent back to fake fdisk for mondorestore\n";
327 } elsif ($i =~ /^d$/) {
328 $part = <STDIN>;
329 if (not defined $part) {
330 pb_log(1,"no partition given for deletion... skipping\n");
331 next;
332 }
333 chomp($part);
334 pb_log(1,"d $part => rm $part\n");
335 pb_system("$parted -s $device rm $part\n") if ($fake == 0);
336 get_parted($device,undef,\%start,\%end,undef);
337 print "command (m for help) sent back to fake fdisk for mondorestore\n";
338 } elsif ($i =~ /^w$/) {
339 pb_log(1,"w => quit\n");
340 } elsif ($i =~ /^t$/) {
341 $part = <STDIN>;
342 if (not defined $part) {
343 pb_log(1,"no partition given for tagging... skipping\n");
344 next;
345 }
346 chomp($part);
347 # If no partition number given it's 1, and we received the type
348 if ($part !~ /\d+/) {
349 $l = $part;
350 $part = 1
351 } else {
352 $l = <STDIN>;
353 }
354 if (not defined $l) {
355 pb_log(1,"no type given for tagging partition $part... skipping\n");
356 next;
357 }
358 chomp($l);
359 if (not defined $pnum{$l}) {
360 pb_log(1,"no partition number given for $l... please report to the author\n");
361 next;
362 }
[2158]363
[3370]364 if ($pnum{$l} eq "lvm") {
365 # In that case this is a flag set, not a mkfs
366 pb_log(1,"t $part $l => set $part $pnum{$l} on\n");
367 pb_system("$parted -s $device set $part $pnum{$l} on\n") if ($fake == 0);
[2154]368 } else {
[3370]369 pb_log(1,"t $part $l => mkfs $part $pnum{$l}\n");
370 pb_system("$parted -s $device mkfs $part $pnum{$l}\n") if ($fake == 0);
371 }
372 print "command (m for help) sent back to fake fdisk for mondorestore\n";
373 } elsif ($i =~ /^a$/) {
374 $part = <STDIN>;
375 if (not defined $part) {
376 pb_log(1,"no partition given for tagging... skipping\n");
[1]377 next;
378 }
[3370]379 chomp($part);
380
381 # Partition shouldn't be negative or null. Then take the first one.
382 $part = 1 if ($part le 0);
383
384 pb_log(1,"a $part => set $part boot on\n");
385 pb_system("$parted -s $device set $part boot on\n") if ($fake == 0);
386 print "command (m for help) sent back to fake fdisk for mondorestore\n";
387 } elsif ($i =~ /^q$/) {
388 pb_log(1,"q => quit\n");
389 } else {
390 pb_log(1,"Unknown command: $i\n");
391 print "command (m for help) sent back to fake fdisk for mondorestore\n";
392 next;
[1]393 }
[3370]394
[1]395 }
396 }
[3370]397 exit(0);
[1]398}
399
400#
401# Else everything is for fdisk
402#
403# Print only mode
[3370]404local_fdisk(\%opts,$device);
405exit(0);
[3355]406
[3370]407# End of main
408
409
[3355]410sub local_fdisk {
411
[3370]412my $opts=shift;
[3355]413my $device=shift;
414
[3370]415pb_log(1,"Passing everything to the real fdisk with device: $device\n");
416pb_log(1,"and the -s $wpart option\n") if (defined $opts{'s'});
[1]417
[3370]418if ((defined $opts->{'l'}) || (defined $opts->{'s'})) {
419 my $args = "-l $device" if (defined $opts->{'l'});
420 $args = "-s $wpart" if (defined $opts->{'s'});
421 open (FDISK, "$fdisk $args 2>/dev/null |") || die "Unable to read from $fdisk";
[1]422 while (<FDISK>) {
[3344]423 print $_;
[1]424 }
425 close(FDISK);
426} else {
427 # Modification mode
[3370]428 open (FDISK, "| $fdisk $device 2>/dev/null") || die "Unable to modify through $fdisk";
[1]429 while (<STDIN>) {
[3344]430 print FDISK $_;
[1]431 }
432 close(FDISK);
433 close(STDIN);
434}
[3355]435return;
436}
[1]437
[3355]438# Unused for now - Kept for reference in case there is a need later on
439sub fdisk_list_all {
[3370]440
[3355]441my $device = shift;
442my $wpart = shift;
443my $start = shift;
444my $end = shift;
445my $verbose = shift;
446
447return fdisk_list($device,$wpart,$start,$end,$verbose) if ((defined $device) && ($device ne ""));
448
449# If no device given loop on the list of devices found in /proc/partitions
450open(PART,"/proc/partitions") || die "Unable to open /proc/partitions";
451while (<PART>) {
452 my ($maj,$min,$blocks,$dev) = split(/\s+/);
453 next if ($dev =~ /^fd|^sr/);
454 next if ($min != 0);
455 fdisk_list("/dev/$dev",$wpart,$start,$end,$verbose);
456}
457close(PART);
458}
459
460
[1]461sub fdisk_list {
462
463my $device = shift;
464my $wpart = shift;
465my $start = shift;
466my $end = shift;
[90]467my $verbose = shift;
[1]468
469my $un;
[88]470my $endmax;
[1]471my $d;
472my $n;
473
474my %cmt = ( "FAT" => "FAT",
475 "ext2" => "Linux",
476 "ext3" => "Linux",
[2087]477 "ext4" => "Linux",
478 "xfs" => "Linux",
[1565]479 "reiserfs" => "Linux",
[1]480 "linux-swap" => "Linux swap",
[2158]481 "lvm" => "Linux LVM",
[3343]482 "raid" => "RAID Linux auto",
[1]483 "fat16" => "fat16",
484 "fat32" => "fat32",
485 "" => "Linux",
486);
487
488my $part;
489my $mstart;
490my $mend;
491my $length;
492my $pid;
493my $cmt;
494format FLOG1 =
[1565]495@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]496$part, $mstart, $mend, $length, $pid, $cmt
497.
498format FLOG2 =
499@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
500$part,
[1565]501 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]502 $mstart, $mend, $length, $pid, $cmt
503.
504format STDOUT1 =
[1565]505@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]506$part, $mstart, $mend, $length, $pid, $cmt
507.
508format STDOUT2 =
509@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
510$part,
[1565]511 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]512 $mstart, $mend, $length, $pid, $cmt
513.
[1565]514# Device Boot Start End Blocks Id System
515#/dev/hda1 1 77579 39099374+ ee EFI GPT
[1]516
[1565]517
[1]518#
519# Keep Fdisk headers
520#
[88]521# this will return bytes
[3364]522$un = get_un($device,$wpart,$verbose);
[90]523
[88]524$endmax = get_max($device);
[90]525
[88]526# This will return MB
[3355]527get_parted($device,$start,$end,\%type);
[1]528
529while (($n,$d) = each %type) {
530 # Print infos fdisk like
531 $part = ${device}.$n;
[88]532 # start and end are in cylinder in fdisk format
533 # so return in MB * 1MB / what represents 1 cyl in B
[1801]534 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
[88]535 $mstart = 1 if ($mstart < 1);
[1565]536 $mstart = $endmax if ($mstart > $endmax);
[1801]537 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
[88]538 $mend = $endmax if ($mend > $endmax);
[1565]539 $mend = 1 if ($mend < 1);
[88]540 # length is in 1K blocks
541 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
[1]542 $pid = $pid{$type{$n}};
543 $cmt = $cmt{$type{$n}};
[90]544 #print FLOG "$part - $mstart - $mend - $length\n";
[1]545
[90]546 if ($verbose == 1) {
[3370]547 if (not defined $wpart) {
[90]548 if (length($part) > 13) {
549 open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
550 select(STDOUT2);
551 write;
552 open(FLOG2,">&FLOG") || die "Unable to open FLOG2";
553 select(FLOG2);
554 write;
555 select(STDOUT);
556 close(FLOG2);
557 close(STDOUT2);
558 } else {
559 open(STDOUT1,">&STDOUT") || die "Unable to open STDOUT1";
560 select(STDOUT1);
561 write;
562 open(FLOG1,">&FLOG") || die "Unable to open FLOG1";
563 select(FLOG1);
564 write;
565 select(STDOUT);
566 close(FLOG1);
567 close(STDOUT1);
568 }
[1]569 } else {
[90]570 # manage the -s option of fdisk here
571 print "$length\n" if ($part eq $wpart);
[3370]572 pb_log(1,"$part has $length KBytes\n") if ($part eq $wpart);
[1]573 }
574 }
575}
576close(FDISK);
577close(PARTED);
578}
579
580#
581# Get max size from fdisk
582#
583sub get_max {
584
585my $device = shift;
586my $max = 0;
587my $foo;
588
[1857]589open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]590while (<FDISK>) {
[88]591 if ($_ =~ /heads/) {
592 chomp;
593 $max = $_;
594 $max =~ s/.* ([0-9]+) cylinders/$1/;
[1]595 }
596}
597close(FDISK);
[3370]598pb_log(2,"get_max returns $max\n");
[1]599return($max);
600}
601
602#
[88]603# Get units from fdisk (cylinder size)
[1]604#
605sub get_un {
606
607my $device = shift;
608my $wpart = shift;
[90]609my $verbose = shift;
[1]610my $un = 0;
611my $foo;
612
[1857]613open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]614while (<FDISK>) {
[90]615 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
[1]616 if ($_ =~ /^Units/) {
617 ($foo, $un , $foo) = split /=/;
618 $un =~ s/[A-z\s=]//g;
619 $un = eval($un);
620 }
621}
622close(FDISK);
[3370]623pb_log(2,"get_un returns $un\n");
[1]624return($un);
625}
626
[88]627#
628# Parted gives info in MB
[1565]629# (depending on versions - 1.6.25.1 provides suffixes)
[88]630#
[1]631sub get_parted {
632
633my $device = shift;
634my $start = shift;
635my $end = shift;
636my $type = shift;
[1565]637my $void;
[1]638my $d;
639my $n;
[1565]640my $ret;
641my $mode;
642my $size;
[1801]643my $unit;
[1]644
[3370]645my $parted = pb_check_req("parted",0);
[1565]646open (PARTED, "$parted -v |") || die "Unable to read from $parted";
647$d = <PARTED>;
[3370]648pb_log(2,"$d");
[1565]649close(PARTED);
[3343]650chomp($d);
651# parted version
652$d =~ s/[^0-9\.]*([0-9\.]+)$/$1/;
653my ($v,$maj,$min) = split(/\./,$d);
654# depending on parted version, information given change:
655if ($v == 2) {
656 # RHEL 6 parted 2.1
657 $mode=2;
658} elsif ($v == 1) {
659 if (($maj <= 5) || (($maj == 6) && (defined $min) && ($min < 25))) {
660 # RHEL 3 parted 1.6.3
661 # RHEL 4 parted 1.6.19
662 $mode=0;
663 } else {
664 # SLES 10 parted >= 1.6.25
665 $mode=1;
666 }
667} else {
668 $mode=-1;
669}
[3370]670pb_log(2,"parted mode: $mode\n");
[1565]671
[3370]672open(PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
[1]673# Skip 3 first lines
674$d = <PARTED>;
675$d = <PARTED>;
676$d = <PARTED>;
[1565]677
[3343]678if ($mode == 2) {
679 $d = <PARTED>;
680 $d = <PARTED>;
681 $d = <PARTED>;
[1565]682}
[3370]683pb_log(2,"Got from parted: \n");
684pb_log(2,"Minor Start End Filesystem\n");
[1]685# Get info from each partition line
686while (($n,$d) = split(/\s/, <PARTED>,2)) {
687 chomp($d);
[3343]688 # v2 of parted ends with empty line
689 next if (($mode == 2) && ($n eq "") && ($d eq ""));
690 # v2 of parted starts with space potentially
691 ($n,$d) = split(/\s/, $d,2) if (($mode == 2) && ($n eq ""));
[1]692 next if ($n !~ /^[1-9]/);
693 $d =~ s/^\s*//;
694 $d =~ s/\s+/ /g;
[1565]695 if ($mode == 0) {
696 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
[1801]697 $unit = 1;
[1565]698 } elsif ($mode == 1) {
699 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
[1801]700 $unit = $mega;
[3343]701 } elsif ($mode == 2) {
702 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
703 $unit = $mega;
[1565]704 } else {
705 die "Undefined mode $mode";
706 }
[1]707 $$start{$n} = "" if (not defined $$start{$n});
708 $$end{$n} = "" if (not defined $$end{$n});
709 $$type{$n} = "" if (not defined $$type{$n});
[1565]710 # Handles potential suffixes in latest parted version. Return MB
[1801]711 $ret = decode_Bsuf($$start{$n},$unit);
[1565]712 $$start{$n} = $ret;
[1801]713 $ret = decode_Bsuf($$end{$n},$unit);
[1565]714 $$end{$n} = $ret;
[3370]715 pb_log(2,"$n $$start{$n} $$end{$n} $$type{$n}\n");
[1]716}
717close(PARTED);
718}
719
[1565]720sub decode_Bsuf {
721
722my $size = shift;
723my $unit = shift;
724my $ret = 0;
725
[3370]726pb_log(2,"decode_Bsuf input: $size / $unit ");
[1837]727if ($size =~ /K[B]*$/i) {
728 $size =~ s/K[B]*$//i;
[1565]729 $size *= 1024;
[1837]730} elsif ($size =~ /M[B]*$/i) {
731 $size =~ s/M[B]*$//i;
[1565]732 $size *= 1048576;
[1837]733} elsif ($size =~ /G[B]*$/i) {
734 $size =~ s/G[B]*$//i;
[1565]735 $size *= 1073741824;
[1837]736} elsif ($size =~ /T[B]*$/i) {
737 $size =~ s/T[B]*$//i;
[1565]738 $size *= 1099511627776;
739} else {
740 # Nothing to do
741}
742$ret = $size / $unit;
[3370]743pb_log(2," - output : $size => $ret\n");
[1565]744return($ret);
745}
Note: See TracBrowser for help on using the repository browser.