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

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