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

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Thu Sep 6 13:07:27 UTC 2007


Author: ccaillet-guest
Date: Thu Sep  6 13:07:27 2007
New Revision: 541

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=541
Log:
FEATURE: introducing sparc architecture for disk settings

ADDS:
  * adding skeleton for dumping and restoring partition including i386 and sparc architecture

FIX:
  * fixing some functions for adding sparc architecture support


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=541&op=diff
==============================================================================
--- trunk/lib/PFTools/Disk.pm (original)
+++ trunk/lib/PFTools/Disk.pm Thu Sep  6 13:07:27 2007
@@ -48,6 +48,8 @@
 ### Env vars
 our $DEBUG	= 0 ;
 my $VERBOSE	= 0 ;
+my $FOLLOW	= '' ;
+my $SIZE	= '' ;
 
 if ( $DEBUG ) { $VERBOSE = 1 ; }
 
@@ -77,6 +79,8 @@
 my $DRBD_DEV_PATTERN	= 'drbd' ;
 my $DRBD_DEV		= 'drbd0' ;
 
+### Misc files
+my $DUMP_PART_FILE	= '/tmp/device_part.dmp' ;
 
 
 #
@@ -113,9 +117,9 @@
 	return $result ;
 }
 
-sub GetDiskGeometry ($) {
-	# Call parameter(s)
-	my ( $device ) = @_ ;
+sub GetDiskGeometry ($;$) {
+	# Call parameter(s)
+	my ( $device, $arch ) = @_ ;
 	# Local(s) var(s)
 	my $geo = {} ;
 	my ( $pad, $cyls, $heads, $sectors ) ;
@@ -124,28 +128,53 @@
 		warn "GetDiskGeometry -- Wrong device name ".$device." : unable to get geometry\n" if ( $VERBOSE ) ;
 		return undef ;
 	}
-	# Retrieving geometry by sfdisk command
-	my $cmd = $SFDISK.' -f -g /dev/'.$device ;
 	$geo->{'name'} = $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 ) ;
+	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
+		# Retrieving geometry by sfdisk command
+		my $cmd = $SFDISK.' -f -g /dev/'.$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 ) ;
+			return undef ;
+		}
+		$geo->{'cyls'} = $cyls ;
+		$geo->{'heads'} = $heads ;
+		$geo->{'sectors'} = $sectors ;
+		return $geo ;
+	} elsif ( $arch eq 'sparc' ) {
+		my $cmd = $FDISK. ' -l /dev/'.$device ;
+		# Disk /dev/sda: 160.0 GB, 160041885696 bytes
+		# 255 heads, 63 sectors/track, 19457 cylinders
+		# Units = cylinders of 16065 * 512 = 8225280 bytes
+		# Disk identifier: 0xf98d6e74
+
+		if ( ! open ( FDL, $cmd ) ) {
+			warn "GetDiskGeometry -- Unable to get geometry with command ".$cmd."\n" if ( $VERBOSE ) ;
+			return undef ;
+		}
+		while ( <FDL> ) {
+			if ( /^([\d]+) heads, ([\d]+) sectors\/track, ([\d]+) cylinders$/ ) {
+				$geo->{'cyls'}		= $3 ;
+				$geo->{'heads'}		= $1 ;
+				$geo->{'sectors'}	= $2 ;
+			}
+		}
+		close ( FDL ) ;
+		return $geo ;
+	} else {
+		warn "GetDiskGeometry -- Wrong architecture specified ".$arch." : unable to get geometry\n" if ( $VERBOSE ) ;
 		return undef ;
 	}
-	$geo->{'cyls'} = $cyls ;
-	$geo->{'heads'} = $heads ;
-	$geo->{'sectors'} = $sectors ;
-	return $geo ;
-}
-	
-sub GetAllGeometry ($) {
-	# Call parameter(s)
-	my ( $dev_list ) = @_ ;
+}
+	
+sub GetAllGeometry ($;$) {
+	# Call parameter(s)
+	my ( $dev_list, $arch ) = @_ ;
 	# Local(s) var(s)
 	my $geo = {} ;
 
 	foreach my $disk ( @{$dev_list->{'disk'}} ) {
-		$geo->{$disk} = GetDiskGeometry ( $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 ;
@@ -154,9 +183,9 @@
 	return $geo ;
 }
 
-sub CheckDiskGeometry ($) {
-	# Call parameter(s)
-	my ( $device, $ref_wanted ) = @_ ;
+sub CheckDiskGeometry ($$;$) {
+	# Call parameter(s)
+	my ( $device, $ref_wanted, $arch ) = @_ ;
 	# Local(s) var(s)
 	my $ref_geo_device = {} ;
 	my ( $check_name, $wanted_name ) ;
@@ -167,7 +196,7 @@
 			warn "CheckDiskGeometry -- Wrong device name ".$device." : unable to check geometry\n" if ( $VERBOSE ) ;
 			return 0 ;
 		}
-		$ref_geo_device = GetDiskGeometry ( $device ) ;
+		$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 ;
@@ -186,9 +215,9 @@
 	return 1 ;
 }
 
-sub CheckAllGeometry ($) {
-	# Call parameter(s)
-	my ( $ref_wanted ) = @_ ;
+sub CheckAllGeometry ($;$) {
+	# Call parameter(s)
+	my ( $ref_wanted, $arch ) = @_ ;
 	# Local(s) var(s)
 	my ( $dev_list, $all_geo_dev ) ;
 	
@@ -197,14 +226,14 @@
 		warn "CheckAllGeometry -- Unable to get devices list on host\n" if ( $VERBOSE ) ;
 		return 0 ;
 	}
-	$all_geo_dev = GetAllGeometry ( $dev_list ) ;
+	$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 ;
 	}
 	
 	foreach my $disk ( keys %{$all_geo_dev} ) {
-		if ( ! CheckDiskGeometry ( $ref_wanted ) ) {
+		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 ;
 		}
@@ -373,6 +402,110 @@
 }
 
 #
+# Managing partitions (dump, restore, ...)
+#
+
+sub Add_raid_disk_partition ($) {
+	# Call parameter(s)
+	my ( $device ) = @_ ;
+	# Local(s) var(s)
+	my $cmd ;
+
+	if ( $DEBUG ) {
+		$cmd = $SFDISK.' -f '.$device.' << EOF '.$FOLLOW.','.$SIZE.','.$RAID_PART_TYPE.' EOF' ;
+		print 'Exec : '.$cmd."\n" ;
+	} else {
+		if ( ! open ( $cmd, '|'.$SFDISK.' -f '.$device.' -N'.$RAID_PART_NUM ) ) {
+			warn "Unable to add raid partition on root disk with ".$SFDISK."\n" if ( $VERBOSE ) ;
+			return 0 ;
+		}
+		print $cmd $FOLLOW.','.$SIZE.','.$RAID_PART_TYPE."\n" ;
+		close ( $cmd ) ;
+	}
+	return 1 ;
+}
+
+sub Dump_disk_partitions ($;$) {
+	# Call parameter(s)
+	my ( $device, $arch ) = @_ ;
+	# Local(s) var(s)
+	my $cmd ;
+	
+	if ( ! defined ( $arch ) || $arch eq 'i386' ) {
+		$cmd = $SFDISK.' -d '.$device.' > '.$DUMP_PART_FILE ;
+		if ( $DEBUG ) {
+			print 'Exec : '.$cmd."\n" ;
+		} else {
+			`$cmd` ;
+			if ( $? ) {
+				if ( $? == -1 ) {
+					warn "failed to execute: $!\n" if ( $VERBOSE ) ;
+					return 0 ;
+				} elsif ( $? & 127 ) {
+					printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
+					return 0 ;
+				} else {
+					printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
+					return 0 ;
+				}
+			}
+		}
+	} elsif ( $arch eq 'sparc' ) {
+		# 	Device Boot      Start         End      Blocks   Id  System
+		# /dev/sda1               1         893     7168000   1c  Hidden W95 FAT32 (LBA)
+		# Partition 1 does not end on cylinder boundary.
+		# /dev/sda2   *         894        4717    30716280    7  HPFS/NTFS
+		# /dev/sda3            4718        7149    19535040   83  Linux
+		# /dev/sda4            7150       19457    98864010    5  Extended
+		# /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 ;
+		}
+		while ( <DUMP> ) {
+			# Fetching partition description line(s)
+			if ( /^\/dev\/$device([\d]+)\s*(\*)?\s*([\d]+)\s*([\d+])\s*[\d]+([^\s]+)\s*(.)*$/ ) {
+				my ( $part, $bootable, $first, $last, $type, $type_name ) = ( $1, $2, $3, $4, $5, $6 ) ;
+			}
+			# Create the file with fdisk command to create partition which is parsed on this line
+		}
+		close ( DUMP ) ;
+	} else {
+		warn "Invalid architecture for platform : unable to dump partition table for device ".$device."\n" if ( $VERBOSE ) ;
+		return 0 ;
+	}
+	return 1 ;
+}
+
+sub Restore_disk_partitions ($) {
+	# Call parameter(s)
+	my ( $device ) = @_ ;
+	# Local(s) var(s)
+	my $cmd ;
+	
+	$cmd = $SFDISK.' /dev/'.$device.' < '.$DUMP_PART_FILE ;
+	if ( $DEBUG ) {
+		print 'Exec : '.$cmd."\n" ;
+	} else {
+		`$cmd` ;
+		if ( $? ) {
+			if ( $? == -1 ) {
+				warn "failed to execute: $!\n" if ( $VERBOSE ) ;
+				exit 1 ;
+			} elsif ( $? & 127 ) {
+				printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' if ( $VERBOSE ) ;
+				exit 1 ;
+			} else {
+				printf STDERR "child exited with value %d\n", $? >> 8 if ( $VERBOSE ) ;
+				exit 1 ;
+			}
+		}
+	}
+	return 1 ;
+}
+
+#
 # Managing RAID array(s) (create, add a disk ...)
 #
 




More information about the Pf-tools-commits mailing list