pf-tools commit: r899 [parmelan-guest] - /branches/next-gen/lib/PFTools/Disk.pm

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Wed Sep 8 12:53:33 UTC 2010


Author: parmelan-guest
Date: Wed Sep  8 12:53:27 2010
New Revision: 899

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=899
Log:
Disk.pm: style

Modified:
    branches/next-gen/lib/PFTools/Disk.pm

Modified: branches/next-gen/lib/PFTools/Disk.pm
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/lib/PFTools/Disk.pm?rev=899&op=diff
==============================================================================
--- branches/next-gen/lib/PFTools/Disk.pm (original)
+++ branches/next-gen/lib/PFTools/Disk.pm Wed Sep  8 12:53:27 2010
@@ -79,6 +79,7 @@
 my $PROC_DRBD = '/proc/drbd';
 
 ### Pattern(s) for misc checks
+# FIXME qr{} + hash
 my $DISK_DEV_PATTERN = '\/dev\/(h|s)d[a-z]([\d]+)?';
 my $RAID_DEV_PATTERN = '\/dev\/md([\d]+)';
 my $RAID_DEV         = 'md0';
@@ -91,102 +92,114 @@
 my $DRBD_DEV         = 'drbd0';
 
 ### Misc files
-my $DUMP_PART_FILE = '/tmp/device_part.dmp';
+my $DUMP_PART_FILE = '/tmp/device_part.dmp';    # FIXME File::Temp ?
 my $TPL_SPARC_PART = '';
 
 #
 # Misc functions
 #
 
-sub Build_structure_from_fstab ($) {
+sub Build_structure_from_fstab {
     my ($fstab_file) = @_;
-    my $struct = {};
-
+
+    return unless $fstab_file;
+
+    # FIXME IO::File or File::Slurp or Perl6::Slurp
     if ( !open( FSTAB, $fstab_file ) ) {
-        Warn( $CODE->{'OPEN'}, "Unable to open " . $fstab_file );
-        return undef;
+        Warn( $CODE->{'OPEN'}, "Unable to open $fstab_file: $OS_ERROR" );
+        return;
     }
     my @current_fstab = <FSTAB>;
     close(FSTAB);
 
     print join @current_fstab;
+
+    my $struct = {};
     foreach my $line (@current_fstab) {
 
         # Skip comments
-        next if ( $line =~ /^#/ );
+        next if $line =~ m/^#/;
 
         # Removing trailing spaces
         $line =~ s/^\s*//;
         $line =~ s/\s*$//;
 
         # Skipping empty lines
-        next if ( $line =~ /^$/ );
+        next if $line =~ m/^$/;
         my ( $src_mnt, $mnt_pt, $type, $opt_mnt, $dump, $pass )
-            = split( /\s+/, $line );
-        push( @{ $struct->{'__mnt_order'} }, $mnt_pt );
+            = split /\s+/, $line;
+        push @{ $struct->{'__mnt_order'} }, $mnt_pt;
         $struct->{$mnt_pt} = {
             'source'  => $src_mnt,
             'dest'    => $mnt_pt,
             'fstype'  => $type,
-            'options' => join( ',', sort split( ',', $opt_mnt ) ),
+            'options' => join( ',', sort split ',', $opt_mnt ),
             'dump'    => $dump,
-            'pass'    => $pass
+            'pass'    => $pass,
         };
     }
+
     return $struct;
 }
 
-sub Build_fstab_from_structure ($) {
+sub Build_fstab_from_structure {
     my ($struct) = @_;
-    my $fstab = [];
-
-    push( @{$fstab}, "###################################################" );
-    push( @{$fstab}, "# Fstab generated by Build_fstab_from_structure" );
-    push( @{$fstab}, "#\n" );
+
+    return unless $struct;
+
+    my @fstab = ();
+
+    push @fstab, '###################################################';
+    push @fstab, '# Fstab generated by Build_fstab_from_structure';
+    push @fstab, "#\n";
     foreach my $entry ( @{ $struct->{'__mnt_order'} } ) {
         my @line;
         foreach my $field (@FSTAB_FIELDS_ORDER) {
-            push( @line, $struct->{$entry}->{$field} );
-        }
-        push( @{$fstab}, join( "\t", @line ) );
-    }
-    push( @{$fstab}, "" );
-    return $fstab;
-}
-
-sub Exec_cmd ($;$) {
+            push @line, $struct->{$entry}->{$field};
+        }
+        push @fstab, join( "\t", @line );
+    }
+    push @fstab, '';
+
+    return \@fstab;
+}
+
+# FIXME: duplicate code?
+sub Exec_cmd {
     my ( $cmd, $msg ) = @_;
+
+    return unless $cmd;
+    $msg = "Problem when executing command $cmd:\n"
+        unless $msg;
 
     if ($DEBUG) {
         print 'Exec :' . $cmd . "\n";
-    }
-    else {
-        system($cmd );
-        if ($CHILD_ERROR) {
-            if ( !defined($msg) ) {
-                $msg
-                    = "Problem when executing command " 
-                    . $cmd
-                    . " with the following error(s)\n";
-            }
-            warn $msg if ($VERBOSE);
-            if ( $CHILD_ERROR == -1 ) {
-                warn "failed to execute: $OS_ERROR\n" if ($VERBOSE);
-            }
-            elsif ( $CHILD_ERROR & 127 ) {
-                printf STDERR "child died with signal %d, %s coredump\n",
-                    ( $CHILD_ERROR & 127 ),
-                    ( $CHILD_ERROR & 128 ) ? 'with' : 'without'
-                    if ($VERBOSE);
-            }
-            else {
-                printf STDERR "child exited with value %d\n",
-                    $CHILD_ERROR >> 8
-                    if ($VERBOSE);
-            }
-            return 0;
-        }
-    }
+        return 1;
+    }
+
+    system($cmd);
+    if ($CHILD_ERROR) {
+        warn $msg if $VERBOSE;
+        if ( $CHILD_ERROR == -1 ) {
+            warn "failed to execute: $OS_ERROR\n"
+                if $VERBOSE;    # FIXME carp or Warn
+        }
+        elsif ( $CHILD_ERROR & 127 ) {
+            warn "child died with signal %d, %s coredump\n"
+                ,               # FIXME carp or Warn
+                ( $CHILD_ERROR & 127 ),
+                ( $CHILD_ERROR & 128 ) ? 'with' : 'without'
+                if $VERBOSE;
+        }
+        else {
+            warn "child exited with value %d\n",    # FIXME carp or Warn
+                $CHILD_ERROR >> 8
+                if $VERBOSE;
+        }
+
+        return;
+    }
+
     return 1;
 }
 
@@ -194,85 +207,83 @@
 # System check and analysis ...
 #
 
-sub GetDiskDevice () {
-
-    # Call parameter(s)
-
-    # Local(s) var(s)
+sub GetDiskDevice {
+
     my $part;
-    my $result = {};
-
+
+    # FIXME IO::File
     if ( !open( $part, $PROC_PART ) ) {
         warn "GetDiskDevice -- Unable to parse "
             . $PROC_PART
             . " for analysing disk structures\n"
-            if ($VERBOSE);
-        return undef;
-    }
-    else {
-
-        # Parsing /proc/partitions file
-        while (<$part>) {
-            next if (/^$/);
-            if (/^\s*([\d]+)\s+([\d]+)\s+([\d]+)\s+([^\s]+)$/) {
-                my ( $major, $minor, $block_size, $name )
-                    = ( $1, $2, $3, $4 );
-                if ( $name =~ /^$DISK_DEV_PATTERN$/ ) {
-                    push( @{ $result->{'disk'} }, $name );
-                    if ( !defined $result->{$name} ) { $result->{$name} = 0; }
-                }
-                if ( $name =~ /^$DISK_DEV_PATTERN$/ ) {
-                    $result->{$name} += 1;
-                }
-                if ( $name =~ /^$RAID_DEV_PATTERN$/ ) {
-                    push( @{ $result->{'raid'} }, $name );
-                }
-                if ( $name =~ /^$DRBD_DEV_PATTERN$/ ) {
-                    push( @{ $result->{'drbd'} }, $name );
-                }
-            }
-        }
-        close($part);
-    }
+            if $VERBOSE;
+        return;
+    }
+
+    # Parsing /proc/partitions file
+    my $result = {};
+    while (<$part>) {
+        next if /^$/;
+        next unless /^\s*([\d]+)\s+([\d]+)\s+([\d]+)\s+([^\s]+)$/;
+
+        my ( $major, $minor, $block_size, $name ) = ( $1, $2, $3, $4 );
+        if ( $name =~ /^$DISK_DEV_PATTERN$/ ) {
+            push @{ $result->{'disk'} }, $name;
+            $result->{$name} = 0
+                unless defined $result->{$name};
+        }
+        if ( $name =~ /^$DISK_DEV_PATTERN$/ ) {
+            $result->{$name} += 1;
+        }
+        if ( $name =~ /^$RAID_DEV_PATTERN$/ ) {
+            push @{ $result->{'raid'} }, $name;
+        }
+        if ( $name =~ /^$DRBD_DEV_PATTERN$/ ) {
+            push @{ $result->{'drbd'} }, $name;
+        }
+    }
+    close($part);
+
     return $result;
 }
 
-sub GetDiskGeometry ($;$) {
-
-    # Call parameter(s)
+sub GetDiskGeometry {
     my ( $device, $arch ) = @_;
 
+    return unless $device;
+    $arch = 'i386' unless $arch;    # default
+
+    if ( $device !~ /^$DISK_DEV_PATTERN$/ ) {
+        warn
+            "GetDiskGeometry -- device name $device doesn't match $DISK_DEV_PATTERN\n" # FIXME carp or Warn
+            if $VERBOSE;
+        return;
+    }
+
     # Local(s) var(s)
-    my $geo = {};
+    my $geo = { name => $device };
     my ( $pad, $cyls, $heads, $sectors );
 
-    if ( $device !~ /^$DISK_DEV_PATTERN$/ ) {
-        warn "GetDiskGeometry -- Wrong device name " 
-            . $device
-            . " : unable to get geometry\n"
-            if ($VERBOSE);
-        return undef;
-    }
-    $geo->{'name'} = $device;
-    if ( !defined($arch) || $arch eq 'i386' ) {
+    if ( $arch eq 'i386' ) {
 
         # Retrieving geometry by sfdisk command
         my $cmd = $SFDISK . ' -f -g ' . $device;
         ( $pad, $cyls, $pad, $heads, $pad, $sectors )
-            = split( /\s+/, `$cmd` );
+            = split( /\s+/, `$cmd` );    # FIXME no `` ever!
         if ( $cyls == 0 || $heads == 0 || $sectors == 0 ) {
             warn
-                "GetDiskGeometry -- Invalid values retriveved by sfdisk for device "
-                . $device . "\n"
-                if ($VERBOSE);
-            return undef;
+                "GetDiskGeometry -- Invalid values retriveved by sfdisk for device $device\n"
+                if $VERBOSE;
+            return;
         }
         $geo->{'cyls'}    = $cyls;
         $geo->{'heads'}   = $heads;
         $geo->{'sectors'} = $sectors;
+
         return $geo;
     }
-    elsif ( $arch eq 'sparc' ) {
+
+    if ( $arch eq 'sparc' ) {
         my $cmd = $FDISK . ' -l /dev/' . $device;
 
         # Disk /dev/sda: 160.0 GB, 160041885696 bytes
@@ -295,67 +306,50 @@
             }
         }
         close(FDL);
+
         return $geo;
     }
-    else {
-        warn "GetDiskGeometry -- Wrong architecture specified " 
-            . $arch
-            . " : unable to get geometry\n"
-            if ($VERBOSE);
-        return undef;
-    }
-}
-
-sub GetAllGeometry ($;$) {
-
-    # Call parameter(s)
+
+    warn
+        "GetDiskGeometry -- Unsupported architecture $arch: unable to get geometry\n"
+        if $VERBOSE;
+    return;
+}
+
+sub GetAllGeometry {
     my ( $dev_list, $arch ) = @_;
 
-    # Local(s) var(s)
     my $geo = {};
-
     foreach my $disk ( @{ $dev_list->{'disk'} } ) {
         $geo->{$disk} = GetDiskGeometry( $disk, $arch );
         if ( !defined( $geo->{$disk} ) ) {
             warn
-                "GetAllGeometry -- Cannot retrieve geometry for all disks : see message bellow\n"
-                if ($VERBOSE);
-            return undef;
+                "GetAllGeometry -- Cannot retrieve geometry for all disks: see message below\n"
+                if $VERBOSE;
+            return;
         }
     }
     return $geo;
 }
 
-sub CheckDiskGeometry ($$;$) {
-
-    # Call parameter(s)
+sub CheckDiskGeometry {
     my ( $device, $ref_wanted, $arch ) = @_;
 
     # Local(s) var(s)
     my $ref_geo_device = {};
     my ( $check_name, $wanted_name );
 
-    if ( ref($device) ne 'HASH' ) {
-
-        # $device is not an HASHREF on disk geometry
-        if ( $device !~ /^$DISK_DEV_PATTERN$/ ) {
-            warn "CheckDiskGeometry -- Wrong device name " 
-                . $device
-                . " : unable to check geometry\n"
-                if ($VERBOSE);
-            return 0;
-        }
-        $ref_geo_device = GetDiskGeometry( $device, $arch );
-        if ( !defined($ref_geo_device) ) {
-            warn "CheckDiskGeometry -- Unable to retrive geometry for device "
-                . $device . "\n"
-                if ($VERBOSE);
-            return 0;
-        }
-    }
-    else {
-        $ref_geo_device = $device;
-    }
+    $ref_geo_device
+        = ref($device) eq 'HASH'
+        ? $device
+        : GetDiskGeometry( $device, $arch );
+    if ( !defined($ref_geo_device) ) {
+        warn "CheckDiskGeometry -- Unable to retrive geometry for device "
+            . $device . "\n"
+            if $VERBOSE;
+        return;
+    }
+
     $check_name  = $ref_geo_device->{'name'};
     $wanted_name = $ref_wanted->{'name'};
     foreach my $char ( keys %{$ref_wanted} ) {
@@ -365,61 +359,55 @@
                 . " and reference device "
                 . $wanted_name
                 . " have not the same geometry\n"
-                if ($VERBOSE);
-            return 0;
-        }
-    }
+                if $VERBOSE;
+            return;
+        }
+    }
+
     return 1;
 }
 
-sub CheckAllGeometry ($;$) {
-
-    # Call parameter(s)
+sub CheckAllGeometry {
     my ( $ref_wanted, $arch ) = @_;
 
-    # Local(s) var(s)
-    my ( $dev_list, $all_geo_dev );
-
-    $dev_list = GetDiskDevice();
+    my $dev_list = GetDiskDevice();
     if ( !defined($dev_list) ) {
         warn "CheckAllGeometry -- Unable to get devices list on host\n"
-            if ($VERBOSE);
-        return 0;
-    }
-    $all_geo_dev = GetAllGeometry( $dev_list, $arch );
+            if $VERBOSE;
+        return;
+    }
+
+    my $all_geo_dev = GetAllGeometry( $dev_list, $arch );
     if ( !defined($all_geo_dev) ) {
         warn
             "CheckAllGeometry -- Unable to retrieve one ore more geometry : see error bellow\n"
-            if ($VERBOSE);
-        return 0;
+            if $VERBOSE;
+        return;
     }
 
     foreach my $disk ( keys %{$all_geo_dev} ) {
         if ( !CheckDiskGeometry( $disk, $ref_wanted, $arch ) ) {
             warn
                 "CheckAllGeometry -- One ore more disk(s) has not the same geometry see error bellow\n"
-                if ($VERBOSE);
-            return 0;
-        }
-    }
+                if $VERBOSE;
+            return;
+        }
+    }
+
     return 1;
 }
 
-sub CheckRaidArray ($) {
-
-    # Call parameter(s)
+sub CheckRaidArray {
     my ($raid_dev) = @_;
 
-    # Local(s) var(s)
     my $part;
-    my $stat = {};
-
     if ( !open( $part, $MDADM . ' -D ' . $raid_dev ) ) {
         warn "Unable to analyse raid status for RAID array "
             . $raid_dev . "\n";
-        return undef;
-    }
-    $stat->{'failed'} = 0;
+        return;
+    }
+
+    my $stat = { failed => 0 };
     while (<$part>) {
         if ( /^\s*Failed Devices : ([\d]+)$/ && $1 > 0 ) {
             $stat->{'failed'} = $1;
@@ -444,32 +432,30 @@
         }
     }
     close($part);
+
     return $stat;
 }
 
-sub CheckArrayRecovery ($) {
-
-    # Call parameter(s)
+sub CheckArrayRecovery {
     my ($raid_dev) = @_;
 
-    # Local(s) var(s)
-    my ( $proc, $build, $active, $check, $fail, $last_size );
-
-    $active = $fail = $last_size = $check = 0;
-    $build = 1;
+    my ( $active, $check, $fail, $last_size ) = ( 0, 0, 0, 0 );
+    my $build = 1;
+    my $proc;
     while ( !$active ) {
         if ( !open( $proc, $PROC_RAID ) ) {
             warn "Unable to open proc file "
                 . $PROC_RAID
                 . " for checking Raid ARRAY status\n";
-            return 0;
+            return;
         }
 
 # [>....................]  recovery =  0.1% (90880/56998528) finish=93.9min speed=10097K/sec
         while (<$proc>) {
             if (/^$raid_dev : .*$/) { $check = 1; }
             if (/^\s*(\[[^\]]+\])\s*recovery =\s*([\d]+.[\d+]%) \(([\d]+)\/[\d]+\).+$/
-                && $check )
+                && $check
+                )
             {
                 if ( !$last_size ) {
                     $last_size = $3;
@@ -477,7 +463,7 @@
                 elsif ( $last_size == $3 ) {
                     if ( $fail == 3 ) {
                         warn "Failure during array RAID operation\n";
-                        return 0;
+                        return;
                     }
                     else {
                         $fail += 1;
@@ -498,28 +484,28 @@
             }
         }
     }
+
     return 1;
 }
 
-sub CheckDrbdSyncer ($) {
-
-    # Call parameter(s)
+sub CheckDrbdSyncer {
     my ($drbd_dev) = @_;
 
-    # Local(s) var(s)
-    my ( $proc, $num_drbd, $build, $active, $check, $fail, $last_size );
-
-    $drbd_dev =~ /^$DRBD_DEV_PATTERN([\d])$/;
-
-    $num_drbd = $1;
-    $active   = $fail = $last_size = $check = 0;
-    $build    = 1;
+    unless ( $drbd_dev =~ /^$DRBD_DEV_PATTERN([\d])$/ ) {
+        warn "Unsupported drbd device name $drbd_dev\n";
+        return;
+    }
+    my $num_drbd = $1;
+
+    my ( $proc, $build, $active, $check, $fail, $last_size );
+    $active = $fail = $last_size = $check = 0;
+    $build = 1;
     while ( !$active ) {
         if ( !open( $proc, $PROC_DRBD ) ) {
             warn "Unable to open file "
                 . $PROC_DRBD
                 . " for checking DRBD syncer status\n";
-            return 0;
+            return;
         }
 
         # 0: cs:SyncSource st:Primary/Secondary ld:Consistent
@@ -538,15 +524,16 @@
             }
             elsif (
                 /^\s*(\[[^\]]+\]) sync'ed: ([\d]+\.[\d]+\%) \(([\d]+)\/[\d]+\)+$/
-                && $check )
+                && $check
+                )
             {
                 if ( !$last_size ) {
                     $last_size = $3;
                 }
                 elsif ( $last_size == $3 ) {
                     if ( $fail == 3 ) {
-                        print STDERR "Failure during array RAID operation\n";
-                        return 0;
+                        warn "Failure during array RAID operation\n";
+                        return;
                     }
                     else {
                         $fail += 1;
@@ -561,6 +548,7 @@
             }
         }
     }
+
     return 1;
 }
 
@@ -568,19 +556,22 @@
 # Managing scsi disk(s) (add, remove)
 #
 
-sub ManageScsiDevice ($$) {
-
-    # Call parameter(s)
+sub ManageScsiDevice {
     my ( $ref_device, $action ) = @_;
 
-    # Local(s) var(s)
+    unless ( $ref_device and $action ) {
+
+        #FIXME confess "BUG: invalid arguments";
+        return;
+    }
+
     my $cmd;
 
     if ( ref($ref_device) ne 'ARRAY' ) {
         warn
             "ManageScsiDevice -- Wrong device definition for managing SCSI channel(s)\n"
-            if ($VERBOSE);
-        return 0;
+            if $VERBOSE;
+        return;
     }
     if ( $action eq 'add' ) {
         $cmd
@@ -598,50 +589,54 @@
     }
     else {
         warn "ManageScsiDevice -- Wrong action parameter " . $action . "\n"
-            if ($VERBOSE);
-        return 0;
-    }
-
-    return Exec_cmd( $cmd,
-              "Problem when managing SCSI device with command " 
+            if $VERBOSE;
+        return;
+    }
+
+    return Exec_cmd(
+        $cmd,
+        "Problem when managing SCSI device with command "
             . $cmd
-            . " and with the following error(s)\n" );
+            . " and with the following error(s)\n"
+    );
 }
 
 #
 # Managing partitions (dump, restore, ...)
 #
 
-sub AddRaidPartition ($;$) {
-
-    # Call parameter(s)
+sub AddRaidPartition {
     my ( $device, $arch ) = @_;
 
-    # Local(s) var(s)
+    return unless $device;
+    $arch = 'i386' unless $arch;
+
     my $cmd;
 
-    if ( !defined($arch) || $arch eq 'i386' ) {
+    if ( $arch eq 'i386' ) {
         $cmd
-            = $SFDISK . ' -f ' 
+            = $SFDISK . ' -f '
             . $device
             . ' << EOF '
             . $FOLLOW . ','
             . $SIZE . ','
             . $RAID_PART_TYPE . ' EOF';
-        return Exec_cmd( $cmd,
-                  "Unable to add raid partition on device " 
+        return Exec_cmd(
+            $cmd,
+            "Unable to add raid partition on device "
                 . $device
                 . " with command "
                 . $cmd
-                . " and with the following error(s)\n" );
+                . " and with the following error(s)\n"
+        );
     }
     elsif ( $arch eq 'sparc' ) {
         if ( !open( CMD, "| " . $FDISK . " " . $device ) ) {
-            warn "Unable to add raid partition on device " 
+            warn "Unable to add raid partition on device "
                 . $device
                 . " with fdisk command\n"
-                if ($VERBOSE);
-            return 0;
+                if $VERBOSE;
+            return;
         }
         print CMD "n\n4\n\n\nt\n4\n$RAID_PART_TYPE\nw\n";
         close(CMD);
@@ -650,70 +645,73 @@
     }
     else {
         warn
-            "Invalid architecture for platform : unable to add raid partition on device "
-            . $device . "\n"
-            if ($VERBOSE);
-        return 0;
-    }
-
-    return Exec_cmd( $cmd,
-              "Unable to add raid partition on device " 
+            "Unsupported architecture $arch: unable to add raid partition on device $device\n"
+            if $VERBOSE;
+        return;
+    }
+
+    # FIXME: notreached?
+    return Exec_cmd(
+        $cmd,
+        "Unable to add raid partition on device "
             . $device
             . " with command "
             . $cmd
-            . " and with the following error(s)\n" );
+            . " and with the following error(s)\n"
+    );
 }
 
 # This function must be used with sparc architecture
-sub EraseAllpartitions ($;$) {
-
-    # Call parameter(s)
+sub EraseAllpartitions {
     my ( $device, $arch ) = @_;
 
-    # Local(s) var(s)
-    my $cmd;
-    my @actions = ();
+    return unless $device;
 
     my $disk_list = GetDiskDevice();
     if ( !defined($disk_list) ) {
-        warn "Unable to retrieve partitions for device " . $device . "\n"
-            if ($VERBOSE);
-        return 0;
-    }
+        warn "Unable to retrieve partitions for device $device\n"
+            if $VERBOSE;
+        return;
+    }
+
+    my @actions = ();
     foreach my $part ( @{ $disk_list->{'disk'} } ) {
-        next if ( $part !~ /^\Q$device\E[\d]+$/ );
+        next if $part !~ /^\Q$device\E[\d]+$/;
         $part =~ /^\Q$device\E([\d]+)$/;
-        push( @actions, "d\n$1\n" );
-    }
-    push( @actions, "w\n" );
+        push @actions, "d\n$1\n";
+    }
+    push @actions, "w\n";
+
     if ( !open( ERASE, "|" . $FDISK . " " . $device ) ) {
-        warn "Unable to erase partition table for device " . $device . "\n"
-            if ($VERBOSE);
-        return 0;
+        warn "Unable to erase partition table for device $device\n"
+            if $VERBOSE;
+        return;
     }
     foreach my $action (@actions) {
         print ERASE $action;
     }
     close(ERASE);
+
     return 1;
 }
 
-sub DumpAllPartitions ($;$) {
-
-    # Call parameter(s)
+sub DumpAllPartitions {
     my ( $device, $arch ) = @_;
 
-    # Local(s) var(s)
+    return unless $device;
+    $arch = 'i386' unless $arch;
+
     my $cmd;
-
-    if ( !defined($arch) || $arch eq 'i386' ) {
+    if ( $arch eq 'i386' ) {
         $cmd = $SFDISK . ' -d ' . $device . ' > ' . $DUMP_PART_FILE;
-        return Exec_cmd( $cmd,
-                  "Unable to dump partiotion table from device " 
+        return Exec_cmd(
+            $cmd,
+            "Unable to dump partiotion table from device "
                 . $device
                 . " with command "
                 . $cmd
-                . " and with the following error(s)\n" );
+                . " and with the following error(s)\n"
+        );
     }
     elsif ( $arch eq 'sparc' ) {
         my @actions = ();
@@ -730,15 +728,14 @@
 # /dev/sda5            7150        7392     1951866   82  Linux swap / Solaris
 # /dev/sda6            7393       19457    96912081   83  Linux
         if ( !open( DUMP, $cmd . "|" ) ) {
-            warn "Unable to dump partitions table for device " 
-                . $device . "\n"
-                if ($VERBOSE);
-            return 0;
-        }
-        push( @actions, "n\n3\n\nt\n3\n5\n" );
+            warn "Unable to dump partitions table for device $device\n"
+                if $VERBOSE;
+            return;
+        }
+        push @actions, "n\n3\n\nt\n3\n5\n";
         while (<DUMP>) {
             my ( $part, $bootable, $first, $last, $type, $type_name );
-            next if (/\Q$device\E\3.*Whole disk.*$/);
+            next if /\Q$device\E\3.*Whole disk.*$/;
 
             # Fetching partition description line(s)
             if (/^$device([\d]+)\s*(\*)?\s*([\d]+)\s*([\d+])\s*[\d]+([^\s]+)\s*(.)*$/
@@ -749,31 +746,28 @@
             }
 
 # Create the actions to do with fdisk command for creating partition which is parsed on this line
-            push( @actions, "n\n$part\n$first\n$last\nt\n$part\n$type\n" );
+            push @actions, "n\n$part\n$first\n$last\nt\n$part\n$type\n";
         }
         close(DUMP);
 
         # Command for writing changes to disk and exit
-        push( @actions, "w\n" );
+        push @actions, "w\n";
 
         # Initialize dumpfile with sparc template ;
         if ( !open( TPL, $TPL_SPARC_PART ) ) {
-            warn "Unable to initialize dump file for sparc device " 
-                . $device . "\n"
-                if ($VERBOSE);
-            return 0;
+            warn "Unable to initialize dump file for sparc device $device\n"
+                if $VERBOSE;
+            return;
         }
         @actions = <TPL>;
         close(TPL);
 
         # Adding partitions retrieved by command $cmd
         if ( !open( DUMP, ">" . $DUMP_PART_FILE ) ) {
-            warn "Unable to write dump partition for device " 
-                . $device
-                . " on file "
-                . $DUMP_PART_FILE . "\n"
-                if ($VERBOSE);
-            return 0;
+            warn
+                "Unable to write dump partition for device $device on file $DUMP_PART_FILE\n"
+                if $VERBOSE;
+            return;
         }
         foreach my $action (@actions) {
             print DUMP $action;
@@ -782,178 +776,128 @@
     }
     else {
         warn
-            "Invalid architecture for platform : unable to dump partition table for device "
-            . $device . "\n"
-            if ($VERBOSE);
-        return 0;
-    }
+            "Unsupported architecture $arch: unable to dump partition table for device $device\n"
+            if $VERBOSE;
+        return;
+    }
+
     return 1;
 }
 
-sub RestoreAllPartitions ($;$$) {
-
-    # Call parameter(s)
+sub RestoreAllPartitions {
     my ( $device, $dumpfile, $arch ) = @_;
+
+    return unless $device;
+    $dumpfile = $DUMP_PART_FILE unless $dumpfile;
+    $arch     = 'i386'          unless $arch;
 
     # Local(s) var(s)
     my $cmd;
 
-    if ( !defined($dumpfile) ) {
-        $dumpfile = $DUMP_PART_FILE;
-    }
     if ( !-e $dumpfile ) {
-        warn "Dump file for partition table " . $dumpfile . " doesn't exist\n"
-            if ($VERBOSE);
-        return 0;
+        warn "Dump file for partition table $dumpfile doesn't exist\n"
+            if $VERBOSE;
+        return;
     }
     elsif ( -z $dumpfile ) {
-        warn "Dump file for partition table "
-            . $dumpfile
-            . " is an empty file\n"
-            if ($VERBOSE);
-        return 0;
-    }
-
-    if ( !defined($arch) || $arch eq 'i386' ) {
+        warn "Dump file for partition table $dumpfile is empty\n"
+            if $VERBOSE;
+        return;
+    }
+
+    if ( $arch eq 'i386' ) {
         $cmd = $SFDISK . ' ' . $device . ' < ' . $dumpfile;
     }
     elsif ( $arch eq 'sparc' ) {
         if ( !Erase_disk_partition($device) ) {
             warn
-                "Unable to erase partition table before restoring from dump file "
-                . $dumpfile . "\n"
-                if ($VERBOSE);
-            return 0;
+                "Unable to erase partition table before restoring from dump file $dumpfile\n"
+                if $VERBOSE;
+            return;
         }
         $cmd = $FDISK . ' ' . $device . ' < ' . $dumpfile;
     }
     else {
         warn
-            "Invalid architecture for platform : unable to restore partition table for device "
-            . $device . "\n"
-            if ($VERBOSE);
-        return 0;
-    }
-    return Exec_cmd( $cmd,
-              "Unable to restore partition table for " 
+            "Unsupported architecture $arch: unable to restore partition table for device $device\n"
+            if $VERBOSE;
+        return;
+    }
+    return Exec_cmd(
+        $cmd,
+        "Unable to restore partition table for "
             . $device
             . " with command "
             . $cmd
-            . " and with the following error(s)\n" );
+            . " and with the following error(s)\n"
+    );
 }
 
 #
 # Managing RAID array(s) (create, add a disk ...)
 #
 
-sub MakeRaidArray ($$$) {
+sub MakeRaidArray {
     my ( $raid_dev, $raid_level, $dev_list ) = @_;
 
-    my ( $cmd, $stat );
-    $cmd
+    return unless $raid_dev and $raid_level;
+
+    my $cmd
         = $MDADM . ' -C '
         . $raid_dev . ' -l '
         . $raid_level . ' -n '
         . scalar( @{$dev_list} ) . ' '
-        . join( " ", @{$dev_list} );
-    if ($DEBUG) {
-        print 'Exec : ' . $cmd . "\n";
-    }
-    else {
-        if (!Exec_cmd(
-                $cmd,
-                "Unable to create RAID array level "
-                    . $raid_level
-                    . " with command "
-                    . $cmd
-                    . " and with the following error(s)\n"
-            )
-            )
-        {
-            return 0;
-        }
-    }
+        . join( ' ', @{$dev_list} );
+
+    return
+        unless Exec_cmd(
+        $cmd,
+        "Unable to create RAID array level $raid_level with command $cmd and with the following error(s)\n"
+        );
+
     return CheckArrayRecovery($raid_dev);
 }
 
-sub AddDeviceOnArray ($$) {
-
-    # Variables en parametres d'appel
+sub AddDeviceOnArray {
     my ( $raid_dev, $device ) = @_;
 
-    # Variables locales a la fonction
+    return unless $raid_dev and $device;
+
+    my $cmd = "$MDADM $raid_dev -a $device";
+    return unless Exec_cmd(
+        $cmd,
+        "Problem when adding device $device on ARRAY $raid_dev with command $cmd and with the following error(s)\n"
+    );
+
+    return CheckArrayRecovery($raid_dev);
+}
+
+sub DelDeviceOnArray {
+    my ( $raid_dev, $device ) = @_;
+
+    return unless $raid_dev and $device;
+
+    my $cmd = "$MDADM $raid_dev -r $device";
+    return Exec_cmd(
+        $cmd,
+        "Problem when deleting device $device on ARRAY $raid_dev with command $cmd and with the following error(s)\n"
+    );
+}
+
+#
+# Managing DRBD cluster(s)
+#
+
+#
+# Managing filesystems ( formatting, checking ...)
+#
+
+sub ManageFilesystem {
+    my ( $device, $fs_type, $action ) = @_;
+
+    return unless $device and $fs_type and $action;
+
     my $cmd;
-
-    $cmd = $MDADM . ' ' . $raid_dev . ' -a ' . $device;
-    if ($DEBUG) {
-        print 'Exec : ' . $cmd . "\n";
-    }
-    else {
-        if (!Exec_cmd(
-                $cmd,
-                "Problem when adding device " 
-                    . $device
-                    . " on ARRAY "
-                    . $raid_dev
-                    . " with command "
-                    . $cmd
-                    . " and with the following error(s)\n"
-            )
-            )
-        {
-            return 0;
-        }
-    }
-    return CheckArrayRecovery($raid_dev);
-}
-
-sub DelDeviceOnArray ($$) {
-
-    # Variables en parametres d'appel
-    my ( $raid_dev, $device ) = @_;
-
-    # Variables locales a la fonction
-    my $cmd;
-
-    $cmd = $MDADM . ' ' . $raid_dev . ' -r ' . $device;
-    if ($DEBUG) {
-        print 'Exec : ' . $cmd . "\n";
-    }
-    else {
-        if (!Exec_cmd(
-                $cmd,
-                "Problem when deleting device " 
-                    . $device
-                    . " on ARRAY "
-                    . $raid_dev
-                    . " with command "
-                    . $cmd
-                    . " and with the following error(s)\n"
-            )
-            )
-        {
-            return 0;
-        }
-    }
-    return 1;
-}
-
-#
-# Managing DRBD cluster(s)
-#
-
-#
-# Managing filesystems ( formatting, checking ...)
-#
-
-sub ManageFilesystem ($$) {
-
-    # Variables en parametres d'appel
-    my ( $device, $fs_type, $action ) = @_;
-
-    # Variables locales a la fonction
-    my $cmd;
-
     if ( $action eq 'make' ) {
         $cmd = $MKFS . $fs_type . ' ' . $device;
     }
@@ -963,17 +907,11 @@
     else {
         warn "ManageFilesystem -- Unknown action " . $action
             . " : allowed actions are make and check\n";
-        return 0;
-    }
-    if ($DEBUG) {
-        print 'Exec : ' . $cmd . "\n";
-    }
-    else {
-        return Exec_cmd( $cmd,
-                  "Unable to manage filesystem with command " 
-                . $cmd
-                . " and with the following error(s)\n" );
-    }
+        return;
+    }
+    return Exec_cmd( $cmd,
+        "Unable to manage filesystem with command $cmd and with the following error(s)\n"
+    );
 }
 
 1;




More information about the pf-tools-commits mailing list