pf-tools commit: r543 [ccaillet-guest] - /trunk/lib/PFTools/Disk.pm

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri Sep 14 11:31:08 UTC 2007


Author: ccaillet-guest
Date: Fri Sep 14 11:31:08 2007
New Revision: 543

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=543
Log:
Factorizing code

Modified:
    trunk/lib/PFTools/Disk.pm

Modified: trunk/lib/PFTools/Disk.pm
URL: http://svn.debian.org/wsvn/pf-tools/trunk/lib/PFTools/Disk.pm?rev=543&op=diff
==============================================================================
--- trunk/lib/PFTools/Disk.pm (original)
+++ trunk/lib/PFTools/Disk.pm Fri Sep 14 11:31:08 2007
@@ -83,6 +83,34 @@
 my $DUMP_PART_FILE	= '/tmp/device_part.dmp' ;
 my $TPL_SPARC_PART	= '' ;
 
+#
+# Misc functions
+#
+
+sub Exec_cmd ($;$) {
+	my ( $cmd, $msg ) = @_ ;
+
+	if ( $DEBUG ) {
+		print 'Exec :'.$cmd."\n" ;
+	} else {
+		system ( $cmd ) ;
+		if ( $? ) {
+			if ( ! defined ( $msg ) ) {
+				$msg = "Problem when executing command ".$cmd." with the following error(s)\n" ;
+			}
+			warn $msg if ( $VERBOSE ) ;
+			if ( $? == -1 ) {
+				warn "failed to execute: $!\n" if ( $VERBOSE ) ;
+			} elsif ( $? & 127 ) {
+				printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
+			} else {
+				printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
+			}
+			return 0 ;
+		}
+	}
+	return 1 ;
+}
 
 #
 # System check and analysis ...
@@ -390,23 +418,7 @@
 		return 0 ;
 	}
 	
-	if ( $DEBUG ) {
-		print 'Exec :'.$cmd."\n" ;
-	} else {
-		system ( $cmd ) ;
-		if ( $? ) {
-			warn "Problem when managing SCSI device with command ".$cmd." and with the following error(s)\n" if ( $VERBOSE ) ;
-			if ( $? == -1 ) {
-				warn "failed to execute: $!\n" if ( $VERBOSE ) ;
-			} elsif ( $? & 127 ) {
-				printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
-			} else {
-				printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
-			}
-			return 0 ;
-		}
-	}
-	return 1 ;
+	return Exec_cmd ( $cmd, "Problem when managing SCSI device with command ".$cmd." and with the following error(s)\n" ) ;
 }
 
 #
@@ -420,31 +432,15 @@
 	my $cmd ;
 
 	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
-		if ( $DEBUG ) {
-			$cmd = $SFDISK.' -f '.$device.' << EOF '.$FOLLOW.','.$SIZE.','.$RAID_PART_TYPE.' EOF' ;
-			print 'Exec : '.$cmd."\n" ;
-		} else {
-			system ( $cmd ) ;
-			if ( $? ) {
-				warn "Unable to add raid partition on device ".$device." with command ".$cmd." and with the following error(s)\n" if ( $VERBOSE ) ;
-				if ( $? == -1 ) {
-					warn "failed to execute: $!\n" if ( $VERBOSE ) ;
-				} elsif ( $? & 127 ) {
-					printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
-				} else {
-					printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
-				}
-				return 0 ;
-			}
-		}
+		$cmd = $SFDISK.' -f '.$device.' << EOF '.$FOLLOW.','.$SIZE.','.$RAID_PART_TYPE.' EOF' ;
 	} elsif ( $arch eq 'sparc' ) {
-	
+		$cmd = $FDISK." ".$device.' < `echo "n\n4t\n4\n$RAID_PART_TYPE\nw\n"`' ;
 	} else {
 		warn "Invalid architecture for platform : unable to add raid partition on device ".$device."\n" if ( $VERBOSE ) ;
 		return 0 ;
 	}
 	
-	return 1 ;
+	return Exec_cmd ( $cmd, "Unable to add raid partition on device ".$device." with command ".$cmd." and with the following error(s)\n" ) ;
 }
 
 sub Dump_disk_partitions ($;$) {
@@ -455,22 +451,7 @@
 	
 	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
 		$cmd = $SFDISK.' -d '.$device.' > '.$DUMP_PART_FILE ;
-		if ( $DEBUG ) {
-			print 'Exec : '.$cmd."\n" ;
-		} else {
-			system ( $cmd ) ;
-			if ( $? ) {
-				warn "Unable to dump partiotion table from device ".$device." with command ".$cmd." and with the following error(s)\n"if ( $VERBOSE ) ;
-				if ( $? == -1 ) {
-					warn "failed to execute: $!\n" if ( $VERBOSE ) ;
-				} elsif ( $? & 127 ) {
-					printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
-				} else {
-					printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
-				}
-				return 0 ;
-			}
-		}
+		return Exec_cmd ( $cmd, "Unable to dump partiotion table from device ".$device." with command ".$cmd." and with the following error(s)\n" ) ;
 	} elsif ( $arch eq 'sparc' ) {
 		my @actions ;
 		# Dumping partition via command $cmd
@@ -498,6 +479,8 @@
 			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" ) ;
 		# Initialize dumpfile with sparc template ;
 		if ( ! open ( TPL, $TPL_SPARC_PART ) ) {
 			warn "Unable to initialize dump file for sparc device ".$device."\n" if ( $VERBOSE ) ;
@@ -529,30 +512,14 @@
 	
 	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
 		$cmd = $SFDISK.' '.$device.' < '.$DUMP_PART_FILE ;
-		if ( $DEBUG ) {
-			print 'Exec : '.$cmd."\n" ;
-		} else {
-			system ( $cmd ) ;
-			if ( $? ) {
-				warn "Unable to restore partition table for ".$device." with command ".$cmd." and with the following error(s)\n" if ( $VERBOSE ) ;
-				if ( $? == -1 ) {
-					warn "failed to execute: $!\n" if ( $VERBOSE ) ;
-				} elsif ( $? & 127 ) {
-					printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
-				} else {
-					printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
-				}
-				return 0 ;
-			}
-		}
 	} elsif ( $arch eq 'sparc' ) {
-	
+		$cmd = $FDISK.' '.$device.' < '.$DUMP_PART_FILE ;
+		
 	} else {
 		warn "Invalid architecture for platform : unable to restore partition table for device ".$device."\n" if ( $VERBOSE ) ;
 		return 0 ;
 	}
-	
-	return 1 ;
+	return Exec_cmd ( $cmd, "Unable to restore partition table for ".$device." with command ".$cmd." and with the following error(s)\n" ) ;
 }
 
 #




More information about the Pf-tools-commits mailing list