[stgt] [PATCH 2/2] tgt-admin: check if device is mounted on /

Doron Shoham dorons at Voltaire.COM
Thu Sep 4 15:39:24 CEST 2008


deny allocation of a device which
which mounted on the same device as rootfs.
also deny the allocation of swap devices.
add --allow-mounted flag for overriding this.


Signed-off-by: Doron Shoham <dorons at voltaire.com>
---
 scripts/tgt-admin |   91 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 85 insertions(+), 6 deletions(-)

diff --git a/scripts/tgt-admin b/scripts/tgt-admin
index 22c4725..6418d56 100755
--- a/scripts/tgt-admin
+++ b/scripts/tgt-admin
@@ -32,6 +32,8 @@ This tool configures tgt targets.
 				(see "--ready help" for more info)
       --update <value>		update configuration for all or selected targets
 				(see "--update help" for more info)
+      --allow-mounted		allow allocation of the device which rootfs is
+      				mounted on
   -s, --show			show all the targets
   -c, --conf <conf file>	specify an alternative configuration file
       --ignore-errors		continue even if tgtadm exits with non-zero code
@@ -60,6 +62,7 @@ my $pretend = 0;
 my $dump = 0;
 my $verbose = 0;
 my $help = 0;
+my $allow_mnt = 0;
 my $result = GetOptions (
 	"e|execute"     => \$execute,
 	"delete=s"      => \$delete,
@@ -73,6 +76,7 @@ my $result = GetOptions (
 	"p|pretend"     => \$pretend,
 	"dump"          => \$dump,
 	"v|verbose"     => \$verbose,
+	"allow-mounted"  => \$allow_mnt,
 	"h|help"        => \$help,
 );
 
@@ -92,6 +96,7 @@ my %tgtadm_output_tid;
 my %tgtadm_output_name;
 my @largest_tid;
 my $next_tid;
+my @rootfs_dev;
 
 # Look up which targets are configured
 sub process_targets {
@@ -273,13 +278,20 @@ sub process_options {
 
 		foreach my $backing_store (@value_arr) {
 			# Check if device exists
-			if ( -e $backing_store) {
+			if ( ! -e $backing_store) {
+				print "skipping device $backing_store\n";
+				print "$backing_store does not exists - please check the configuration file\n";
+				next;
+			}
+
+			(my $can_alloc, my $dev) = check_device($backing_store);
+			if ($can_alloc) {
 				execute("tgtadm --lld $driver --op new --mode logicalunit --tid $next_tid --lun $i -b $backing_store");
 				$i += 1;
 			}
 			else {
-				print("skipping device $backing_store\n");
-				print("$backing_store does not exist - please check the configuration file\n");
+				print "skipping device $backing_store\n";
+				print "rootfs is mounted on this device: $dev\n";
 			}
 		}
 	}
@@ -298,7 +310,14 @@ sub process_options {
 		my $i = 1;
 		foreach my $direct_store (@value_arr) {
 			# Check if device exists
-			if ( -e $direct_store) {
+			if ( ! -e $direct_store) {
+				print "skipping device $direct_store\n" ;
+				print "$direct_store does not exists - please check the configuration file\n";
+				next;
+			}
+
+			(my $can_alloc, my $dev) = check_device($direct_store);
+			if ($can_alloc) {
 				$inq=`sg_inq $direct_store`;
 				if ($inq=~/Vendor identification:\s*(\w+)\s*\n*Product identification:\s*([\w\s\/\-]+)\n\s*\n*Product revision level:\s*(\w*)\s*\n*Unit serial number:\s*(\w+)/)
 				{
@@ -317,9 +336,10 @@ sub process_options {
 				$i += 1;
 			}
 			else {
-				print("skipping device $direct_store\n");
-				print("$direct_store does not exist - please check the configuration file\n");
+				print "skipping device $direct_store\n";
+				print "rootfs is mounted on this device: $dev \n";
 			}
+				
 		}
 	}
 
@@ -829,6 +849,65 @@ sub execute {
 	}
 }
 
+#Check if a device can be allocated
+sub check_device {
+	my $tmp_dev = $_[0];
+	
+	# Check if allow_mnt flag is set
+	if (!$allow_mnt) {
+		# Check for rootfs devices
+		&find_rootfs_device();
+		$tmp_dev =~ s/\d//g;
+		# Check if device is on the same disk as rootfs
+		if (grep {$_ eq $tmp_dev} @rootfs_dev) {
+			return (0,$tmp_dev);
+		}
+	}
+	return 1;
+}
+
+# finds all the devices that rootfs is mounted on
+sub find_rootfs_device {	
+	my @files=("/etc/mtab","/proc/mounts");
+	my @lines;
+	# read files
+	foreach my $file (@files){
+		if (open(FH,"$file")) {
+			@lines=(@lines,<FH>);
+			close (FH);
+		}
+	}
+
+	# parse files and finds all the device which mounted on /
+	foreach my $line (@lines){
+		chomp $line; 
+		if (($line=~/^\/dev\//) && ($line=~/ \/ /)){
+			my @ln=split(' ',$line);
+			$ln[0]=~s/\d//g;
+			push(@rootfs_dev,$ln[0]);
+		}
+	}
+
+	# read swap file
+	my $swap_file="/proc/swap";
+	if (open(FH,"$swap_file")) {
+		@lines=<FH>;
+		close (FH);
+	}
+	# parse swap file and finds all the swap devices
+	foreach my $line (@lines){
+		chomp $line; 
+		if ($line=~/^\/dev\//) {
+			my @ln=split(' ',$line);
+			$ln[0]=~s/\d//g;
+			push(@rootfs_dev,$ln[0]);
+		}
+	}
+	# remove duplicate entries from @rootfs_dev
+	my %seen = ();
+	@rootfs_dev = grep { ! $seen{ $_ }++ } @rootfs_dev;
+}
+
 if ($execute == 1) {
 	&process_targets;
 	&parse_configs;
-- 
1.5.3.8


Hi,
This is patch checks only /etc/mtab,/proc/mounts and /proc/swap.
later we can add more tests.

Thanks,
Doron
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list