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
RevLine 
[1]1#!/usr/bin/perl -w
2#
[88]3# $Id: parted2fdisk.pl 3367 2015-03-20 07:20:16Z 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;
[1]16
[3143]17
18=pod
19
20=head1 NAME
21
[3343]22parted2fdisk is a fdisk like command using parted internally.
[3143]23
24=head1 DESCRIPTION
25
[3343]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).
[3143]27
28=head1 SYNOPSIS
29
30parted2fdisk -s partition
[3355]31parted2fdisk -l [device]
[3143]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
[3355]48List the partition tables for the specified device (or all if none specified) and then exit.
[3143]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
[1]90$ENV{LANG} = "C";
91$ENV{LANGUAGE} = "C";
92$ENV{LC_ALL} = "C";
93
94# Log
[2192]95my $flog = "/var/log/parted2fdisk.log";
[1]96open(FLOG, "> $flog") || die "Unable to open $flog";
97
98my $fdisk = "/sbin/fdisk";
[3343]99$fdisk = "/usr/sbin/fdisk" if (not -x "/sbin/fdisk");
[1]100my $parted = "/sbin/parted";
[3343]101$parted = "/usr/sbin/parted" if (not -x "/sbin/parted");
[1]102
103my $i;
104my $l;
105my $part;
106my $wpart;
107my $start = "";
108my $end = "";
[90]109my $cylstart;
110my $cylend;
[1]111my %start;
112my %end;
113my %type;
[88]114my $fake = 0;
[1801]115my $mega = 1048576;
[1]116
[2163]117# Immediate flushing to avoids read error from mondorestore in log files
118$| = 1;
119
[1]120#
121# Looking for fdisk
122#
123$fdisk = is_lsb($fdisk);
124#
[3343]125# We always use fdisk except with GPT types of
[1]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 = "";
[88]134my $endmax = "";
[1]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",
[2087]146 "ext4" => "83",
147 "xfs" => "83",
[3330]148 "btrfs" => "83",
[1565]149 "reiserfs" => "83",
[1]150 "linux-swap" => "82",
[2158]151 "lvm" => "8e",
[3343]152 "raid" => "fd",
[1]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
[3355]177if (($args ne "") && ($args ne "-l") && ($device eq "")) {
[1]178 mysyn();
179}
180
181# -s takes a partition as arg
182if ($args =~ /-s/) {
183 $wpart = $device;
[3364]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 }
[1]190}
191
[88]192if ($args =~ /-n/) {
193 print FLOG "Fake mode. Nothing will be really done\n";
194 $fake = 1;
195}
196
[1]197print FLOG "Called with device $device and arg $args\n";
198
[3355]199if (($args =~ /-l/) && ($device eq "")) {
200 # Pass to real fdisk directly
201 local_fdisk($args,$device);
202 myexit(0);
203}
204
[3343]205# util-linux/fdisk version
206open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
207my $version = <CMD>;
208close(CMD);
209chomp($version);
[3355]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/;
[3343]213my ($v,$maj,$min) = split(/\./,$version);
214
[3355]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
[3343]225if (($v == 1) || (($v == 2) && ($maj < 22))) {
[1]226 # Check partition table type
[3343]227 print FLOG "We use an old fdisk, activating replacement code...\n";
[1]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/) {
[90]233 fdisk_list($device,undef,\%start,\%end, 1);
[1]234 } elsif ($args =~ /-s/) {
[90]235 fdisk_list($device,$wpart,\%start,\%end, 1);
[88]236 } elsif (($args =~ /-/) and ($fake == 0)) {
[1]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$/) {
[90]246 fdisk_list($device,undef,\%start,\%end, 1);
[2163]247 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]248 } elsif ($i =~ /^n$/) {
[90]249 fdisk_list($device,undef,\%start,\%end, 0);
[1]250 if ($type ne "gpt") {
251 print FLOG "Forcing GPT type of disk label\n";
252 print FLOG "mklabel gpt\n";
[90]253 system "$parted -s $device mklabel gpt\n" if ($fake == 0);
[1]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);
[90]268 $cylstart = <STDIN>;
269 chomp($cylstart);
270 if ((not (defined $cylstart)) || ($cylstart eq "")) {
[1]271 if (defined $start{$part-1}) {
[90]272 # in MB => cyl
[1801]273 $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
[90]274 print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
[1]275 } else {
276 print FLOG "no start cyl given for creation... assuming the following 1\n";
[90]277 $cylstart = 1;
[1]278 }
279 }
[90]280 $cylstart = 1 if ($cylstart < 1);
281 print FLOG "start cyl : $cylstart\n";
282 $un = get_un($device, "", 0);
[88]283 # parted needs MB
[90]284 if ($cylstart == 1) {
285 $start = 0.01;
286 } else {
[1801]287 $start = $cylstart* $un / $mega + 0.001;
[90]288 }
[88]289 # this is a size in B/KB/MB/GB
[90]290
[88]291 $endmax = get_max($device);
[90]292 $cylend = <STDIN>;
293 chomp($cylend);
294 if ((not (defined $cylend)) || ($cylend eq "")) {
[1]295 print FLOG "no end cyl given for creation... assuming full disk)\n";
[90]296 $cylend = $endmax;
[1]297 }
298 # Handles end syntaxes (+, K, M, ...)
[88]299 # to give cylinders
[90]300 if ($cylend =~ /^\+/) {
301 $cylend =~ s/^\+//;
[1565]302 # Handles suffixes; return bytes
303 $cylend = decode_Bsuf($cylend,1);
[88]304 # This gives the number of cyl
[90]305 $cylend /= $un;
306 $cylend = sprintf("%d",$cylend);
307 $cylend += $cylstart - 0.001;
[88]308 # We now have the end cyl
[1]309 }
[90]310 $cylend = $endmax if ($cylend > $endmax);
311 print FLOG "end cyl : $cylend\n";
[88]312 # parted needs MB
[1801]313 $end = $cylend * $un / $mega;
[90]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);
[2158]316 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]317 } elsif ($i =~ /^d$/) {
[1]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";
[90]325 system "$parted -s $device rm $part\n" if ($fake == 0);
326 get_parted($device,undef,\%start,\%end,undef);
[2158]327 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]328 } elsif ($i =~ /^w$/) {
[1]329 print FLOG "w => quit\n";
[2154]330 } elsif ($i =~ /^t$/) {
[1]331 $part = <STDIN>;
332 if (not (defined $part)) {
333 print FLOG "no partition given for tagging... skipping\n";
334 next;
335 }
336 chomp($part);
[2158]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 }
[1]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 }
[2158]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";
[2154]363 } elsif ($i =~ /^a$/) {
[1]364 $part = <STDIN>;
365 if (not (defined $part)) {
366 print FLOG "no partition given for tagging... skipping\n";
367 next;
368 }
369 chomp($part);
[2158]370
371 # Partition shouldn't be negative or null. Then take the first one.
372 $part = 1 if ($part le 0);
373
[1]374 print FLOG "a $part => set $part boot on\n";
[90]375 system "$parted -s $device set $part boot on\n" if ($fake == 0);
[2158]376 print "command (m for help) send back to fake fdisk for mondorestore\n";
[2154]377 } elsif ($i =~ /^q$/) {
[1]378 print FLOG "q => quit\n";
[2154]379 } else {
[1]380 print FLOG "Unknown command: $i\n";
[2163]381 print "command (m for help) send back to fake fdisk for mondorestore\n";
[1]382 next;
383 }
384
385 }
386 }
387 myexit(0);
388 }
389}
390
391#
392# Else everything is for fdisk
393#
394# Print only mode
[3355]395local_fdisk($args,$device);
396myexit(0);
397
398sub local_fdisk {
399
400my $args=shift;
401my $device=shift;
402
[3344]403print FLOG "Passing everything to the real fdisk with $args $device\n";
[1]404
405if ($args =~ /^-/) {
406 # -l or -s
[3344]407 open (FDISK, "$fdisk $args $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]408 while (<FDISK>) {
[3344]409 print $_;
[1]410 }
411 close(FDISK);
412} else {
413 # Modification mode
[3344]414 open (FDISK, "| $fdisk $args $device 2>/dev/null") || die "Unable to modify through $fdisk";
[1]415 while (<STDIN>) {
[3344]416 print FDISK $_;
[1]417 }
418 close(FDISK);
419 close(STDIN);
420}
[3355]421return;
422}
[1]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
[3355]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
[1]471sub fdisk_list {
472
473my $device = shift;
474my $wpart = shift;
475my $start = shift;
476my $end = shift;
[90]477my $verbose = shift;
[1]478
479my $un;
[88]480my $endmax;
[1]481my $d;
482my $n;
483
484my %cmt = ( "FAT" => "FAT",
485 "ext2" => "Linux",
486 "ext3" => "Linux",
[2087]487 "ext4" => "Linux",
488 "xfs" => "Linux",
[1565]489 "reiserfs" => "Linux",
[1]490 "linux-swap" => "Linux swap",
[2158]491 "lvm" => "Linux LVM",
[3343]492 "raid" => "RAID Linux auto",
[1]493 "fat16" => "fat16",
494 "fat32" => "fat32",
495 "" => "Linux",
496);
497
498my $part;
499my $mstart;
500my $mend;
501my $length;
502my $pid;
503my $cmt;
504format FLOG1 =
[1565]505@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]506$part, $mstart, $mend, $length, $pid, $cmt
507.
508format FLOG2 =
509@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
510$part,
[1565]511 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]512 $mstart, $mend, $length, $pid, $cmt
513.
514format STDOUT1 =
[1565]515@<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]516$part, $mstart, $mend, $length, $pid, $cmt
517.
518format STDOUT2 =
519@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
520$part,
[1565]521 @>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> @>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
[1]522 $mstart, $mend, $length, $pid, $cmt
523.
[1565]524# Device Boot Start End Blocks Id System
525#/dev/hda1 1 77579 39099374+ ee EFI GPT
[1]526
[1565]527
[1]528#
529# Keep Fdisk headers
530#
[88]531# this will return bytes
[3364]532$un = get_un($device,$wpart,$verbose);
[90]533
[88]534$endmax = get_max($device);
[90]535
[88]536# This will return MB
[3355]537get_parted($device,$start,$end,\%type);
[1]538
539while (($n,$d) = each %type) {
540 # Print infos fdisk like
541 $part = ${device}.$n;
[88]542 # start and end are in cylinder in fdisk format
543 # so return in MB * 1MB / what represents 1 cyl in B
[1801]544 $mstart = sprintf("%d",$$start{$n}*$mega/$un);
[88]545 $mstart = 1 if ($mstart < 1);
[1565]546 $mstart = $endmax if ($mstart > $endmax);
[1801]547 $mend = sprintf("%d",$$end{$n}*$mega/$un - 1);
[88]548 $mend = $endmax if ($mend > $endmax);
[1565]549 $mend = 1 if ($mend < 1);
[88]550 # length is in 1K blocks
551 $length = sprintf("%d",($mend-$mstart+1)*$un/1024);
[1]552 $pid = $pid{$type{$n}};
553 $cmt = $cmt{$type{$n}};
[90]554 #print FLOG "$part - $mstart - $mend - $length\n";
[1]555
[90]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 }
[1]579 } else {
[90]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);
[1]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
[1857]599open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]600while (<FDISK>) {
[88]601 if ($_ =~ /heads/) {
602 chomp;
603 $max = $_;
604 $max =~ s/.* ([0-9]+) cylinders/$1/;
[1]605 }
606}
607close(FDISK);
608print FLOG "get_max returns $max\n";
609return($max);
610}
611
612#
[88]613# Get units from fdisk (cylinder size)
[1]614#
615sub get_un {
616
617my $device = shift;
618my $wpart = shift;
[90]619my $verbose = shift;
[1]620my $un = 0;
621my $foo;
622
[1857]623open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]624while (<FDISK>) {
[90]625 print if (($_ !~ /^\/dev\//) and (not (defined $wpart)) and ($verbose == 1));
[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
[88]637#
638# Parted gives info in MB
[1565]639# (depending on versions - 1.6.25.1 provides suffixes)
[88]640#
[1]641sub get_parted {
642
643my $device = shift;
644my $start = shift;
645my $end = shift;
646my $type = shift;
[1565]647my $void;
[1]648my $d;
649my $n;
[1565]650my $ret;
651my $mode;
652my $size;
[1801]653my $unit;
[1]654
[1565]655open (PARTED, "$parted -v |") || die "Unable to read from $parted";
656$d = <PARTED>;
657print FLOG "$d";
658close(PARTED);
[3343]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";
[1565]680
[1]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>;
[1565]686
[3343]687if ($mode == 2) {
688 $d = <PARTED>;
689 $d = <PARTED>;
690 $d = <PARTED>;
[1565]691}
[1]692print FLOG "Got from parted: \n";
[1565]693print FLOG "Minor Start End Filesystem\n";
[1]694# Get info from each partition line
695while (($n,$d) = split(/\s/, <PARTED>,2)) {
696 chomp($d);
[3343]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 ""));
[1]701 next if ($n !~ /^[1-9]/);
702 $d =~ s/^\s*//;
703 $d =~ s/\s+/ /g;
[1565]704 if ($mode == 0) {
705 ($$start{$n},$$end{$n},$$type{$n},$void) = split(/ /,$d);
[1801]706 $unit = 1;
[1565]707 } elsif ($mode == 1) {
708 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
[1801]709 $unit = $mega;
[3343]710 } elsif ($mode == 2) {
711 ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
712 $unit = $mega;
[1565]713 } else {
714 die "Undefined mode $mode";
715 }
[1]716 $$start{$n} = "" if (not defined $$start{$n});
717 $$end{$n} = "" if (not defined $$end{$n});
718 $$type{$n} = "" if (not defined $$type{$n});
[1565]719 # Handles potential suffixes in latest parted version. Return MB
[1801]720 $ret = decode_Bsuf($$start{$n},$unit);
[1565]721 $$start{$n} = $ret;
[1801]722 $ret = decode_Bsuf($$end{$n},$unit);
[1565]723 $$end{$n} = $ret;
724 print FLOG "$n $$start{$n} $$end{$n} $$type{$n}\n";
[1]725}
726close(PARTED);
727}
728
[1565]729sub decode_Bsuf {
730
731my $size = shift;
732my $unit = shift;
733my $ret = 0;
734
735#print FLOG "decode_Bsuf input: $size / $unit ";
[1837]736if ($size =~ /K[B]*$/i) {
737 $size =~ s/K[B]*$//i;
[1565]738 $size *= 1024;
[1837]739} elsif ($size =~ /M[B]*$/i) {
740 $size =~ s/M[B]*$//i;
[1565]741 $size *= 1048576;
[1837]742} elsif ($size =~ /G[B]*$/i) {
743 $size =~ s/G[B]*$//i;
[1565]744 $size *= 1073741824;
[1837]745} elsif ($size =~ /T[B]*$/i) {
746 $size =~ s/T[B]*$//i;
[1565]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
[1]756sub myexit {
757
758my $val=shift;
759
760close(FLOG);
761exit($val);
762}
763
764sub which_type {
765
766my $device = shift;
767my $type = "";
768
[1857]769open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
[1]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>) {
[3367]780 if (($_ =~ /Disk label type: msdos/) || ($_ =~ /Partition Table: msdos/)) {
[1]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.