Changeset 3343 in MondoRescue for branches/3.2/mindi/parted2fdisk.pl


Ignore:
Timestamp:
Feb 13, 2015, 8:27:10 PM (9 years ago)
Author:
Bruno Cornec
Message:
  • parted2fdisk now works also on x86 machines and just check fdisk version to replace it when too old to support GPT. Is now a full

replacement of fdisk usage in mondo

File:
1 edited

Legend:

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

    r3330 r3343  
    44#
    55# parted2fdisk: fdisk like interface for parted
    6 # [developped for mindi/mondo http://www.mondorescue.org]
     6# [developed for mindi/mondo http://www.mondorescue.org]
    77#
    88# Aims to be architecture independant (i386/ia64)
    99# Tested on ia64 with RHAS 2.1 - Mandrake 9.0 - RHEL 3.0 - SLES 10
    1010#
    11 # Copyright B. Cornec 2000-2013
     11# Copyright B. Cornec 2000-2015
    1212# Provided under the GPL v2
    1313
    1414use strict;
     15use File::Basename;
    1516
    1617
     
    1920=head1 NAME
    2021
    21 parted2fdisk is a fdisk lie command using parted internally.
     22parted2fdisk is a fdisk like command using parted internally.
    2223
    2324=head1 DESCRIPTION
    2425
    25 parted2fdisk behaves like a fdisk command, but dialog internally with parted in order to manipulate partiion tables, which allow it to support GPT partition format as well as MBR, contrary to fdisk. It aimed at providing compatible external interface wti fdisk. Developed initialy for ia64 Linux, but is also useful now on x86 systems using GPT partition format (for large HDDs).
     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).
    2627
    2728=head1 SYNOPSIS
     
    9697
    9798my $fdisk = "/sbin/fdisk";
     99$fdisk = "/usr/sbin/fdisk" if (not -x "/sbin/fdisk");
    98100my $parted = "/sbin/parted";
     101$parted = "/usr/sbin/parted" if (not -x "/sbin/parted");
    99102
    100103my $i;
     
    109112my %end;
    110113my %type;
    111 my $arch;
    112114my $fake = 0;
    113115my $mega = 1048576;
     
    116118$| = 1;
    117119
    118 # Determine on which arch we're running
    119 if (defined ($ENV{ARCH})) {
    120     $arch = $ENV{ARCH};
    121 } else {
    122     $arch = `uname -m`;
    123     chomp($arch);
    124 }
    125 
    126120#
    127121# Looking for fdisk
     
    129123$fdisk = is_lsb($fdisk);
    130124#
    131 # We always use fdisk except on ia64 with GPT types of
     125# We always use fdisk except with GPT types of
    132126# partition tables where we need parted
    133127# All should return fdisk like format so that callers
     
    156150        "linux-swap" => "82",
    157151        "lvm" => "8e",
     152        "raid" => "fd",
    158153        "" => "",
    159154    );
     
    197192print FLOG "Called with device $device and arg $args\n";
    198193
    199 if ($arch =~ /^ia64/) {
     194# util-linux/fdisk version
     195open(CMD,"$fdisk -v |") || die "Unable to execute $fdisk";
     196my $version = <CMD>;
     197close(CMD);
     198chomp($version);
     199$version =~ s/[^0-9\.]*([0-9\.]+)[\)]$/$1/;
     200my ($v,$maj,$min) = split(/\./,$version);
     201
     202if (($v == 1) || (($v == 2) && ($maj < 22))) {
    200203    # Check partition table type
    201     print FLOG "We're on ia64 ...\n";
     204    print FLOG "We use an old fdisk, activating replacement code...\n";
    202205    $parted = is_lsb($parted);
    203206    $type = which_type($device);
     
    435438        "linux-swap" => "Linux swap",
    436439        "lvm" => "Linux LVM",
     440        "raid" => "RAID Linux auto",
    437441        "fat16" => "fat16",
    438442        "fat32" => "fat32",
     
    601605print FLOG "$d";
    602606close(PARTED);
     607chomp($d);
     608# parted version
     609$d =~ s/[^0-9\.]*([0-9\.]+)$/$1/;
     610my ($v,$maj,$min) = split(/\./,$d);
     611# depending on parted version, information given change:
     612if ($v == 2) {
     613    # RHEL 6 parted 2.1
     614    $mode=2;
     615} elsif ($v == 1) {
     616    if (($maj <= 5) || (($maj == 6) && (defined $min) && ($min < 25))) {
     617        # RHEL 3 parted 1.6.3
     618        # RHEL 4 parted 1.6.19
     619        $mode=0;
     620    } else {
     621        # SLES 10 parted >= 1.6.25
     622        $mode=1;
     623    }
     624} else {
     625    $mode=-1;
     626}
     627print FLOG "mode: $mode\n";
    603628
    604629open (PARTED, "$parted -s $device print |") || die "Unable to read from $parted";
     
    608633$d = <PARTED>;
    609634
    610 # depending on parted version, information given change:
    611 if ($d =~ /\bSize\b/) {
    612     # SLES 10 parted >= 1.6.25
    613     $mode=1;
    614 } else {
    615     # RHEL 3 parted 1.6.3
    616     # RHEL 4 parted 1.6.19
    617     $mode=0;
    618 }
    619 print FLOG "mode: $mode\n";
     635if ($mode == 2) {
     636    $d = <PARTED>;
     637    $d = <PARTED>;
     638    $d = <PARTED>;
     639}
    620640print FLOG "Got from parted: \n";
    621641print FLOG "Minor    Start       End     Filesystem\n";
     
    623643while (($n,$d) = split(/\s/, <PARTED>,2)) {
    624644    chomp($d);
     645    # v2 of parted ends with empty line
     646    next if (($mode == 2) && ($n eq "") && ($d eq ""));
     647    # v2 of parted starts with space potentially
     648    ($n,$d) = split(/\s/, $d,2) if (($mode == 2) && ($n eq ""));
    625649    next if ($n !~ /^[1-9]/);
    626650    $d =~ s/^\s*//;
     
    630654        $unit = 1;
    631655    } elsif ($mode == 1) {
     656        ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
     657        $unit = $mega;
     658    } elsif ($mode == 2) {
    632659        ($$start{$n},$$end{$n},$size,$$type{$n},$void) = split(/ /,$d);
    633660        $unit = $mega;
     
    675702}
    676703
    677 
    678 # Based on Version 2.4  27-Sep-1996  Charles Bailey  bailey@genetics.upenn.edu
    679 # in Basename.pm
    680 
    681 sub basename {
    682 
    683 my($fullname) = shift;
    684 
    685 my($dirpath,$basename);
    686 
    687 ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);
    688 
    689 return($basename);
    690 }
    691 
    692704sub myexit {
    693705
Note: See TracChangeset for help on using the changeset viewer.