Changeset 3370 in MondoRescue for branches


Ignore:
Timestamp:
Apr 18, 2015, 7:15:29 PM (9 years ago)
Author:
Bruno Cornec
Message:
  • 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi/parted2fdisk.pl

    r3367 r3370  
    1414use strict;
    1515use File::Basename;
    16 
     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;
    1724
    1825=pod
     
    2027=head1 NAME
    2128
    22 parted2fdisk is a fdisk like command using parted internally.
     29parted2fdisk is a fdisk like command using parted internally for analysing GPT labelled disks
    2330
    2431=head1 DESCRIPTION
     
    2936
    3037parted2fdisk -s partition
     38
    3139parted2fdisk -l [device]
     40
    3241parted2fdisk [-n] device
    3342
     
    4049Print the size (in blocks) of the given partition.
    4150
     51=item B<-l>
     52
     53List the partition tables for the specified device (or all if none specified) and then exit.
     54
    4255=item B<-n>
    4356
    4457Fake mode. Doesn't pass the commands just simulate.
    45 
    46 =item B<-l>
    47 
    48 List the partition tables for the specified device (or all if none specified) and then exit.
    4958
    5059=item B<no option>
     
    95104my $flog = "/var/log/parted2fdisk.log";
    96105open(FLOG, "> $flog") || die "Unable to open $flog";
    97 
    98 my $fdisk = "/sbin/fdisk";
    99 $fdisk = "/usr/sbin/fdisk" if (not -x "/sbin/fdisk");
    100 my $parted = "/sbin/parted";
    101 $parted = "/usr/sbin/parted" if (not -x "/sbin/parted");
    102106
    103107my $i;
     
    114118my $fake = 0;
    115119my $mega = 1048576;
     120my %opts;
    116121
    117122# Immediate flushing to avoids read error from mondorestore in log files
    118123$| = 1;
    119124
    120 #
    121 # Looking for fdisk
    122 #
    123 $fdisk = is_lsb($fdisk);
    124125#
    125126# We always use fdisk except with GPT types of
     
    130131my $un;
    131132my $type;
    132 my $args = "";
    133 my $device = "";
     133my $device;
    134134my $endmax = "";
     135my $appname = "parted2fdisk";
     136my ($mrver,$mrrev) = mr_version_init();
     137
     138pb_syntax_init("$appname Version $mrver-$mrrev\n");
    135139
    136140if ($#ARGV < 0) {
    137     printf FLOG "No arguments given exiting ...\n";
    138     mysyn();
    139 }
     141    pb_syntax(-1,0);
     142}
     143
     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");
    140192
    141193my %pid = ( "FAT" => "6",
     
    161213}
    162214
    163 foreach $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 
    177 if (($args ne "") && ($args ne "-l") && ($device eq "")) {
    178     mysyn();
    179 }
    180 
    181 # -s takes a partition as arg
    182 if ($args =~ /-s/) {
    183     $wpart = $device;
    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     }
    190 }
    191 
    192 if ($args =~ /-n/) {
    193     print FLOG "Fake mode. Nothing will be really done\n";
    194     $fake = 1;
    195 }
    196 
    197 print FLOG "Called with device $device and arg $args\n";
    198 
    199 if (($args =~ /-l/) && ($device eq "")) {
    200     # Pass to real fdisk directly
    201     local_fdisk($args,$device);
    202     myexit(0);
    203 }
    204 
    205215# util-linux/fdisk version
     216my $fdisk = pb_check_req("fdisk",0);
    206217open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
    207218my $version = <CMD>;
     
    223234}
    224235
    225 if (($v == 1) || (($v == 2) && ($maj < 22))) {
    226     # Check partition table type
    227     print FLOG "We use an old fdisk, activating replacement code...\n";
    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/) {
    233             fdisk_list($device,undef,\%start,\%end, 1);
    234         } elsif ($args =~ /-s/) {
    235             fdisk_list($device,$wpart,\%start,\%end, 1);
    236         } elsif (($args =~ /-/) and ($fake == 0)) {
    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$/) {
    246                     fdisk_list($device,undef,\%start,\%end, 1);
    247                     print "command (m for help) send back to fake fdisk for mondorestore\n";
    248                 } elsif ($i =~ /^n$/) {
    249                     fdisk_list($device,undef,\%start,\%end, 0);
    250                     if ($type ne "gpt") {
    251                         print FLOG "Forcing GPT type of disk label\n";
    252                         print FLOG "mklabel gpt\n";
    253                         system "$parted -s $device mklabel gpt\n" if ($fake == 0);
    254                         $type = "gpt";
     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);
     280                    } else {
     281                        $cylstart = 1;
    255282                    }
     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
     296
     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 {
    256348                    $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);
    268                     $cylstart = <STDIN>;
    269                     chomp($cylstart);
    270                     if ((not (defined $cylstart)) || ($cylstart eq "")) {
    271                         if (defined $start{$part-1}) {
    272                             # in MB => cyl
    273                             $cylstart = sprintf("%d",$end{$part-1}*$mega/$un + 1);
    274                             print FLOG "no start cyl given for creation... assuming the following $cylstart\n";
    275                         } else {
    276                             print FLOG "no start cyl given for creation... assuming the following 1\n";
    277                             $cylstart = 1;
    278                         }
    279                     }
    280                     $cylstart = 1 if ($cylstart < 1);
    281                     print FLOG "start cyl : $cylstart\n";
    282                     $un = get_un($device, "", 0);
    283                     # parted needs MB
    284                     if ($cylstart == 1) {
    285                         $start = 0.01;
    286                     } else {
    287                         $start = $cylstart* $un / $mega + 0.001;
    288                     }
    289                     # this is a size in B/KB/MB/GB
    290 
    291                     $endmax = get_max($device);
    292                     $cylend = <STDIN>;
    293                     chomp($cylend);
    294                     if ((not (defined $cylend)) || ($cylend eq "")) {
    295                         print FLOG "no end cyl given for creation... assuming full disk)\n";
    296                         $cylend = $endmax;
    297                     }
    298                     # Handles end syntaxes (+, K, M, ...)
    299                     # to give cylinders
    300                     if ($cylend =~ /^\+/) {
    301                         $cylend =~ s/^\+//;
    302                         # Handles suffixes; return bytes
    303                         $cylend = decode_Bsuf($cylend,1);
    304                         # This gives the number of cyl
    305                         $cylend /= $un;
    306                         $cylend = sprintf("%d",$cylend);
    307                         $cylend += $cylstart - 0.001;
    308                         # We now have the end cyl
    309                     }
    310                     $cylend = $endmax if ($cylend > $endmax);
    311                     print FLOG "end cyl : $cylend\n";
    312                     # parted needs MB
    313                     $end = $cylend * $un / $mega;
    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);
    316                     print "command (m for help) send back to fake fdisk for mondorestore\n";
    317                 } elsif ($i =~ /^d$/) {
    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";
    325                     system "$parted -s $device rm $part\n" if ($fake == 0);
    326                     get_parted($device,undef,\%start,\%end,undef);
    327                     print "command (m for help) send back to fake fdisk for mondorestore\n";
    328                 } elsif ($i =~ /^w$/) {
    329                     print FLOG "w => quit\n";
    330                 } elsif ($i =~ /^t$/) {
    331                     $part = <STDIN>;
    332                     if (not (defined $part)) {
    333                         print FLOG "no partition given for tagging... skipping\n";
    334                         next;
    335                     }
    336                     chomp($part);
    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                     }
    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                     }
    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";
    363                 } elsif ($i =~ /^a$/) {
    364                     $part = <STDIN>;
    365                     if (not (defined $part)) {
    366                         print FLOG "no partition given for tagging... skipping\n";
    367                         next;
    368                     }
    369                     chomp($part);
    370 
    371                     # Partition shouldn't be negative or null. Then take the first one.
    372                     $part = 1 if ($part le 0);
    373 
    374                     print FLOG "a $part => set $part boot on\n";
    375                     system "$parted -s $device set $part boot on\n" if ($fake == 0);
    376                     print "command (m for help) send back to fake fdisk for mondorestore\n";
    377                 } elsif ($i =~ /^q$/) {
    378                     print FLOG "q => quit\n";
     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                }
     359
     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);
    379364                } else {
    380                     print FLOG "Unknown command: $i\n";
    381                     print "command (m for help) send back to fake fdisk for mondorestore\n";
     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");
    382373                    next;
    383374                }
    384                    
     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;
    385389            }
     390               
    386391        }
    387         myexit(0);
    388     }
     392    }
     393    exit(0);
    389394}
    390395
     
    393398#
    394399# Print only mode
    395 local_fdisk($args,$device);
    396 myexit(0);
     400local_fdisk(\%opts,$device);
     401exit(0);
     402
     403# End of main
     404
    397405
    398406sub local_fdisk {
    399407
    400 my $args=shift;
     408my $opts=shift;
    401409my $device=shift;
    402410
    403 print FLOG "Passing everything to the real fdisk with $args $device\n";
    404 
    405 if ($args =~ /^-/) {
    406     # -l or -s
    407     open (FDISK, "$fdisk $args $device 2>/dev/null |") || die "Unable to read from $fdisk";
     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'});
     413
     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";
    408418    while (<FDISK>) {
    409419        print $_;
     
    412422} else {
    413423    # Modification mode
    414     open (FDISK, "| $fdisk $args $device 2>/dev/null") || die "Unable to modify through $fdisk";
     424    open (FDISK, "| $fdisk $device 2>/dev/null") || die "Unable to modify through $fdisk";
    415425    while (<STDIN>) {
    416426        print FDISK $_;
     
    422432}
    423433
    424 # Is your system LSB ?
    425 sub is_lsb {
    426 
    427 my $cmd = shift;
    428 my $basename = basename($cmd);
    429 
    430 if (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 }
    446 return($cmd);
    447 }
    448 
    449434# Unused for now - Kept for reference in case there is a need later on
    450435sub fdisk_list_all {
     436
    451437my $device = shift;
    452438my $wpart = shift;
     
    555541
    556542    if ($verbose == 1) {
    557         if (not (defined $wpart)) {
     543        if (not defined $wpart) {
    558544            if (length($part) > 13) {
    559545                open(STDOUT2,">&STDOUT") || die "Unable to open STDOUT2";
     
    580566            # manage the -s option of fdisk here
    581567            print "$length\n" if ($part eq $wpart);
    582             print FLOG "$part has $length KBytes\n" if ($part eq $wpart);
     568            pb_log(1,"$part has $length KBytes\n") if ($part eq $wpart);
    583569        }
    584570    }
     
    606592}
    607593close(FDISK);
    608 print FLOG "get_max returns $max\n";
     594pb_log(2,"get_max returns $max\n");
    609595return($max);
    610596}
     
    631617}
    632618close(FDISK);
    633 print FLOG "get_un returns $un\n";
     619pb_log(2,"get_un returns $un\n");
    634620return($un);
    635621}
     
    653639my $unit;
    654640
     641my $parted = pb_check_req("parted",0);
    655642open (PARTED, "$parted -v |") || die "Unable to read from $parted";
    656643$d = <PARTED>;
    657 print FLOG "$d";
     644pb_log(2,"$d");
    658645close(PARTED);
    659646chomp($d);
     
    677664    $mode=-1;
    678665}
    679 print FLOG "mode: $mode\n";
    680 
    681 open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
     666pb_log(2,"parted mode: $mode\n");
     667
     668open(PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
    682669# Skip 3 first lines
    683670$d = <PARTED>;
     
    690677    $d = <PARTED>;
    691678}
    692 print FLOG "Got from parted: \n";
    693 print FLOG "Minor    Start       End     Filesystem\n";
     679pb_log(2,"Got from parted: \n");
     680pb_log(2,"Minor    Start       End     Filesystem\n");
    694681# Get info from each partition line
    695682while (($n,$d) = split(/\s/, <PARTED>,2)) {
     
    722709    $ret = decode_Bsuf($$end{$n},$unit);
    723710    $$end{$n} = $ret;
    724     print FLOG "$n      $$start{$n}      $$end{$n}     $$type{$n}\n";
     711    pb_log(2,"$n      $$start{$n}      $$end{$n}     $$type{$n}\n");
    725712}
    726713close(PARTED);
     
    733720my $ret = 0;
    734721
    735 #print FLOG "decode_Bsuf input: $size / $unit ";
     722pb_log(2,"decode_Bsuf input: $size / $unit ");
    736723if ($size =~ /K[B]*$/i) {
    737724    $size =~ s/K[B]*$//i;
     
    750737}
    751738$ret = $size / $unit;
    752 #print FLOG " - output : $size => $ret\n";
     739pb_log(2," - output : $size => $ret\n");
    753740return($ret);
    754741}
    755 
    756 sub myexit {
    757 
    758 my $val=shift;
    759 
    760 close(FLOG);
    761 exit($val);
    762 }
    763 
    764 sub which_type {
    765 
    766 my $device = shift;
    767 my $type = "";
    768 
    769 open (FDISK, "$fdisk -l $device 2>/dev/null |") || die "Unable to read from $fdisk";
    770 while (<FDISK>) {
    771     if ($_ =~ /EFI GPT/) {
    772         $type= "gpt";
    773         print FLOG "Found a GPT partition format\n";
    774         last;
    775     }
    776 }
    777 close(FDISK);
    778 open (PARTED, "$parted -s $device print|") || die "Unable to read from $fdisk";
    779 while (<PARTED>) {
    780     if (($_ =~ /Disk label type: msdos/) || ($_ =~ /Partition Table: msdos/)) {
    781         $type= "msdos";
    782         print FLOG "Found a msdos partition format\n";
    783         last;
    784     }
    785 }
    786 close(FDISK);
    787 return ($type);
    788 }
    789 
    790 sub mysyn {
    791     print "Syntax: $0 [-l] device | [-s] partition\n";
    792     myexit(-1);
    793 }
Note: See TracChangeset for help on using the changeset viewer.