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

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Wed Sep 26 14:32:43 UTC 2007


Author: ccaillet-guest
Date: Wed Sep 26 14:32:43 2007
New Revision: 544

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=544
Log:
CLEAN
  * /print STDERR/warn/ : better like this
  * renaming some functions*

FEATURE
  * adding EraseAllPartitions

FIX
  * fixing misc typo and command handlers


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=544&op=diff
==============================================================================
--- trunk/lib/PFTools/Disk.pm (original)
+++ trunk/lib/PFTools/Disk.pm Wed Sep 26 14:32:43 2007
@@ -68,15 +68,15 @@
 my $PROC_DRBD		= '/proc/drbd' ;
 
 ### Pattern(s) for misc checks
-my $DISK_DEV_PATTERN	= '(h|s)d[a-z]([\\d]+)?' ;
-my $RAID_DEV_PATTERN	= 'md' ;
+my $DISK_DEV_PATTERN	= '\/dev\/(h|s)d[a-z]([\d]+)?' ;
+my $RAID_DEV_PATTERN	= '\/dev\/md([\d]+)' ;
 my $RAID_DEV		= 'md0' ;
 my $RAID_DEV_STATUS	= '(active sync|removed|faulty)' ;
-my $RAID_DEV_PART	= $DISK_DEV_PATTERN.'\4' ;
+my $RAID_DEV_PART	= '\/dev\/(h|s)d[a-z]4' ;
 my $RAID_PART_NUM	= '4' ;
 my $RAID_PART_TYPE	= 'fd' ;
 my $RAID_FS		= 'ext3' ;
-my $DRBD_DEV_PATTERN	= 'drbd' ;
+my $DRBD_DEV_PATTERN	= '\/dev\/drbd' ;
 my $DRBD_DEV		= 'drbd0' ;
 
 ### Misc files
@@ -136,9 +136,9 @@
 					push ( @{$result->{'disk'}}, $name ) ;
 					if ( ! defined $result->{$name} ) { $result->{$name} = 0 ; }
 				}
-				if ( $name =~ /^$DISK_DEV_PATTERN[\d]+$/ ) { $result->{$name} +=1 }
-				if ( $name =~ /^$RAID_DEV_PATTERN[\d]+$/ ) { push ( @{$result->{'raid'}}, $name ) ; }
-				if ( $name =~ /^$DRBD_DEV_PATTERN[\d]+$/ ) { push ( @{$result->{'drbd'}}, $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 ) ;
@@ -160,7 +160,7 @@
 	$geo->{'name'} = $device ;
 	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
 		# Retrieving geometry by sfdisk command
-		my $cmd = $SFDISK.' -f -g /dev/'.$device ;
+		my $cmd = $SFDISK.' -f -g '.$device ;
 		( $pad, $cyls, $pad, $heads, $pad, $sectors ) = split ( /\s+/, `$cmd` ) ;
 		if ( $cyls == 0 || $heads == 0 || $sectors == 0 ) {
 			warn "GetDiskGeometry -- Invalid values retriveved by sfdisk for device ".$device."\n" if ( $VERBOSE ) ;
@@ -278,7 +278,7 @@
 	my $stat = {} ;
 	
 	if ( ! open ( $part, $MDADM.' -D '.$raid_dev ) ) {
-		print STDERR 'Unable to analyse raid status for RAID array '.$raid_dev."\n" ;
+		warn "Unable to analyse raid status for RAID array ".$raid_dev."\n" ;
 		return undef ;
 	}
 	$stat->{'failed'} = 0 ;
@@ -286,7 +286,7 @@
 		if ( /^\s*Failed Devices : ([\d]+)$/ && $1 > 0 ) {
 			$stat->{'failed'} = $1 ;
 		} elsif ( $stat->{'failed'} ) {
-			if ( /^\s*([\d]+)\s([\d]+)\s*([\d]+)\s*([\d]+)\s*([$RAID_DEV_STATUS])\s*($RAID_DEV_PART)$/ ) {
+			if ( /^\s*([\d]+)\s([\d]+)\s*([\d]+)\s*([\d]+)\s*([$RAID_DEV_STATUS])\s*(\Q$RAID_DEV_PART\E)$/ ) {
 				my ( $number, $major, $minor, $raid_num, $status, $device ) = ( $1, $2, $3, $4, $5, $6 ) ;
 				if ( $status !~ /^fault|failed$/ ) {
 					next ;
@@ -313,7 +313,8 @@
 	$build = 1 ;
 	while ( ! $active ) {
 		if ( ! open ( $proc, $PROC_RAID ) ) {
-			print STDERR "" ;
+			warn "Unable to open proc file ".$PROC_RAID." for checking Raid ARRAY status\n" ;
+			return 0 ;
 		}
 		# [>....................]  recovery =  0.1% (90880/56998528) finish=93.9min speed=10097K/sec
 		while ( <$proc> ) {
@@ -323,7 +324,7 @@
 					$last_size = $3 ;
 				} elsif ( $last_size == $3 ) {
 					if ( $fail == 3 ) {
-						print STDERR "Failure during array RAID operation\n" ;
+						warn "Failure during array RAID operation\n" ;
 						return 0 ;
 					} else {
 						$fail += 1 ;
@@ -351,14 +352,14 @@
 	# Local(s) var(s)
 	my ( $proc, $num_drbd, $build, $active, $check, $fail, $last_size ) ;
 	
-	$drbd_dev =~ /^drbd([\d])$/ ;
+	$drbd_dev =~ /^$DRBD_DEV_PATTERN([\d])$/ ;
 	
 	$num_drbd = $1 ;
 	$active = $fail = $last_size = $check = 0 ;
 	$build = 1 ;
 	while ( ! $active ) {
 		if ( ! open ( $proc, $PROC_DRBD ) ) {
-			print STDERR "" ;
+			warn "Unable to open file ".$PROC_DRBD." for checking DRBD syncer status\n" ;
 			return 0 ;
 		}
 		# 0: cs:SyncSource st:Primary/Secondary ld:Consistent
@@ -425,7 +426,7 @@
 # Managing partitions (dump, restore, ...)
 #
 
-sub Add_raid_disk_partition ($;$) {
+sub AddRaidPartition ($;$) {
 	# Call parameter(s)
 	my ( $device, $arch ) = @_ ;
 	# Local(s) var(s)
@@ -433,8 +434,15 @@
 
 	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
 		$cmd = $SFDISK.' -f '.$device.' << EOF '.$FOLLOW.','.$SIZE.','.$RAID_PART_TYPE.' EOF' ;
+		return Exec_cmd ( $cmd, "Unable to add raid partition on device ".$device." with command ".$cmd." and with the following error(s)\n" ) ;
 	} elsif ( $arch eq 'sparc' ) {
-		$cmd = $FDISK." ".$device.' < `echo "n\n4t\n4\n$RAID_PART_TYPE\nw\n"`' ;
+		if ( ! open ( CMD, "| ".$FDISK." ".$device ) ) {
+			warn "Unable to add raid partition on device ".$device." with fdisk command\n" if ( $VERBOSE ) ;
+			return 0 ;
+		}
+		print CMD "n\n4\n\n\nt\n4\n$RAID_PART_TYPE\nw\n" ;
+		close ( CMD ) ;
+# 		$cmd = "echo \"n\\n4t\\n4\\n$RAID_PART_TYPE\\nw\\n\" | ".$FDISK." ".$device. ;
 	} else {
 		warn "Invalid architecture for platform : unable to add raid partition on device ".$device."\n" if ( $VERBOSE ) ;
 		return 0 ;
@@ -443,7 +451,37 @@
 	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 ($;$) {
+# This function must be used with sparc architecture
+sub EraseAllpartitions ($;$) {
+	# Call parameter(s)
+	my ( $device, $arch ) = @_ ;
+	# Local(s) var(s)
+	my $cmd ;
+	my @actions = () ;
+	
+	my $disk_list = GetDiskDevice () ;
+	if ( ! defined ( $disk_list ) ) {
+		warn "Unable to retrieve partitions for device ".$device."\n" if ( $VERBOSE ) ;
+		return 0 ;
+	}
+	foreach my $part ( @{$disk_list->{'disk'}} ) {
+		next if ( $part !~ /^\Q$device\E[\d]+$/ ) ;
+		$part =~ /^\Q$device\E([\d]+)$/ ;
+		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 ;
+	}
+	foreach my $action ( @actions ) {
+		print ERASE $action ;
+	}
+	close ( ERASE ) ;
+	return 1 ;
+}
+
+sub DumpAllPartitions ($;$) {
 	# Call parameter(s)
 	my ( $device, $arch ) = @_ ;
 	# Local(s) var(s)
@@ -453,7 +491,7 @@
 		$cmd = $SFDISK.' -d '.$device.' > '.$DUMP_PART_FILE ;
 		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 ;
+		my @actions = () ;
 		# Dumping partition via command $cmd
 		$cmd = $FDISK.' -l '.$device ;
 		# 	Device Boot      Start         End      Blocks   Id  System
@@ -468,9 +506,10 @@
 			warn "Unable to dump partitions table for device ".$device."\n" if ( $VERBOSE ) ;
 			return 0 ;
 		}
+		push ( @actions, "n\n3\n\nt\n3\n5\n" ) ;
 		while ( <DUMP> ) {
 			my ( $part, $bootable, $first, $last, $type, $type_name ) ;
-			next if ( /$device\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*(.)*$/ ) {
 				( $part, $bootable, $first, $last, $type, $type_name ) = ( $1, $2, $3, $4, $5, $6 ) ;
@@ -504,17 +543,31 @@
 	return 1 ;
 }
 
-sub Restore_disk_partitions ($;$) {
-	# Call parameter(s)
-	my ( $device, $arch ) = @_ ;
+sub RestoreAllPartitions ($;$$) {
+	# Call parameter(s)
+	my ( $device, $dumpfile, $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 ;
+	} 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' ) {
-		$cmd = $SFDISK.' '.$device.' < '.$DUMP_PART_FILE ;
+		$cmd = $SFDISK.' '.$device.' < '.$dumpfile ;
 	} elsif ( $arch eq 'sparc' ) {
-		$cmd = $FDISK.' '.$device.' < '.$DUMP_PART_FILE ;
-		
+		if ( ! Erase_disk_partition ( $device ) ) {
+			warn "Unable to erase partition table before restoring from dump file ".$dumpfile."\n" if ( $VERBOSE ) ;
+			return 0 ;
+		}
+		$cmd = $FDISK.' '.$device.' < '.$dumpfile ;
 	} else {
 		warn "Invalid architecture for platform : unable to restore partition table for device ".$device."\n" if ( $VERBOSE ) ;
 		return 0 ;




More information about the Pf-tools-commits mailing list