From mboxrd@z Thu Jan 1 00:00:00 1970 From: rohara@sourceware.org Date: 14 Dec 2006 22:41:57 -0000 Subject: [Cluster-devel] cluster/fence/agents/scsi fence_scsi.pl fence_ ... Message-ID: <20061214224157.23132.qmail@sourceware.org> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit CVSROOT: /cvs/cluster Module name: cluster Branch: RHEL50 Changes by: rohara at sourceware.org 2006-12-14 22:41:56 Modified files: fence/agents/scsi: fence_scsi.pl fence_scsi_test.pl scsi_reserve Log message: Update all changes/fixes that went into RHEL5 branch. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/scsi/fence_scsi.pl.diff?cvsroot=cluster&only_with_tag=RHEL50&r1=1.5.4.1&r2=1.5.4.2 http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/scsi/fence_scsi_test.pl.diff?cvsroot=cluster&only_with_tag=RHEL50&r1=1.1&r2=1.1.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/scsi/scsi_reserve.diff?cvsroot=cluster&only_with_tag=RHEL50&r1=1.1.4.1&r2=1.1.4.2 --- cluster/fence/agents/scsi/fence_scsi.pl 2006/12/01 20:24:18 1.5.4.1 +++ cluster/fence/agents/scsi/fence_scsi.pl 2006/12/14 22:41:56 1.5.4.2 @@ -12,8 +12,8 @@ # WARNING!! Do not add code bewteen "#BEGIN_VERSION_GENERATION" and # "#END_VERSION_GENERATION" It is generated by the Makefile -#BEGIN_VERSION_GENERATION +#BEGIN_VERSION_GENERATION $FENCE_RELEASE_NAME=""; $REDHAT_COPYRIGHT=""; $BUILD_DATE=""; @@ -131,7 +131,7 @@ next unless $opt; - ($name, $val) = split /\s*=\s*/, $opt; + ($name, $val) = split(/\s*=\s*/, $opt); if ($name eq "") { @@ -156,33 +156,78 @@ } } -sub get_scsi_devices +sub get_key_list { + ($dev) = @_; + my ($in, $out, $err); - my $cmd = "lvs --noheadings --separator : -o vg_attr,devices"; + my $cmd = "sg_persist -d $dev -i -k"; my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; waitpid($pid, 0); - die "Unable to execute lvs.\n" if ($?>>8); + die "Unable to execute sg_persist.\n" if ($?>>8); while (<$out>) { chomp; - print "OUT: $_\n" if $opt_v; - - my ($vg_attrs, $device) = split /:/, $_, 3; - - if ($vg_attrs =~ /.*c$/) + if ($_ =~ /^\s*0x/) { - $device =~ s/\(.*\)//; - push @volumes, $device; + s/^\s+0x//; + s/\s+$//; + + my $key = sprintf("%8.8x", hex($_)); + $key_list{$key} = 1; } } close($in); close($out); close($err); + + return %key_list; +} + +#sub get_scsi_devices +#{ +# my ($in, $out, $err); +# my $cmd = "lvs --noheadings --separator : -o vg_attr,devices"; +# my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; +# +# waitpid($pid, 0); +# +# die "Unable to execute lvs.\n" if ($?>>8); +# +# while (<$out>) +# { +# chomp; +# print "OUT: $_\n" if $opt_v; +# +# my ($vg_attrs, $device) = split(/:/, $_); +# +# if ($vg_attrs =~ /.*c$/) +# { +# $device =~ s/\(.*\)//; +# push(@volumes, $device); +# } +# } +# +# close($in); +# close($out); +# close($err); +#} + +sub get_scsi_devices +{ + open(FILE, "/var/run/scsi_reserve") or die "$!\n"; + + while () + { + chomp; + push(@volumes, $_); + } + + close FILE; } sub check_sg_persist @@ -206,6 +251,29 @@ close($err); } +sub do_register +{ + ($dev, $key) = @_; + + my ($in, $out, $err); + my $cmd = "sg_persist -d $dev -o -G -S $key"; + my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; + + waitpid($pid, 0); + + die "Unable to execute sg_persist.\n" if ($?>>8); + + while (<$out>) + { + chomp; + print "OUT: $_\n" if $opt_v; + } + + close($in); + close($out); + close($err); +} + sub fence_node { my $host_name = get_host_name(); @@ -213,12 +281,18 @@ my $host_key = get_key($host_name); my $node_key = get_key($node_name); - - my $cmd; + my ($in, $out, $err); foreach $dev (@volumes) { + my %key_list = get_key_list($dev); + + if (!$key_list{$host_key}) + { + do_register($dev, $host_key); + } + if ($host_key eq $node_key) { $cmd = "sg_persist -d $dev -o -G -K $host_key -S 0"; --- cluster/fence/agents/scsi/fence_scsi_test.pl 2006/11/03 17:31:54 1.1 +++ cluster/fence/agents/scsi/fence_scsi_test.pl 2006/12/14 22:41:56 1.1.4.1 @@ -19,6 +19,7 @@ sub get_key { + my $name = @_; my $addr = gethostbyname($name) or die "$!\n"; return unpack("H*", $addr); @@ -32,7 +33,7 @@ print "DEBUG: $func ($dev, $key)\n" if ($opt_d); my ($in, $out, $err); - my $cmd = "sg_persist $dev -o -G -S $key"; + my $cmd = "sg_persist -d $dev -o -G -S $key"; my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; @@ -42,7 +43,7 @@ $results{$dev}[0] = $rval; - print "DEBUG: [$rval] $cmd\n" if $opt_d; + print "DEBUG: [$rval] $cmd\n" if ($opt_d); close($in); close($out); @@ -56,10 +57,10 @@ my $func = (caller(0))[3]; my ($dev, $key) = @_; - print "DEBUG: $func ($dev, $key)\n" if $opt_d; + print "DEBUG: $func ($dev, $key)\n" if ($opt_d); my ($in, $out, $err); - my $cmd = "sg_persist $dev -o -G -K $key -S 0"; + my $cmd = "sg_persist -d $dev -o -G -K $key -S 0"; my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; @@ -69,7 +70,7 @@ $results{$dev}[1] = $rval; - print "DEBUG: [$rval] $cmd\n" if $opt_d; + print "DEBUG: [$rval] $cmd\n" if ($opt_d); close($in); close($out); @@ -98,6 +99,7 @@ { my ($in, $out, $err); my $cmd = "lvs --noheadings --separator : -o vg_attr,devices"; + my $pid = open3($in, $out, $err, $cmd) or die "$!\n"; waitpid($pid, 0); @@ -108,7 +110,7 @@ { chomp; - my ($vg_attr, $dev) = split /:/, $_, 3; + my ($vg_attr, $dev) = split(/:/, $_); if ($vg_attr =~ /.*c$/) { @@ -125,8 +127,7 @@ sub test_devices { my $name = hostname() or die "$!\n"; - my $addr = gethostbyname($name) or die "$!\n"; - my $key = unpack("H*", $addr); + my $key = get_key($name); foreach $dev (@devices) { @@ -195,7 +196,7 @@ print " -h Help. Prints out this usage information.\n\n"; } -### main ### +### MAIN ####################################################### if (getopts("cdhsv") == 0) { @@ -232,3 +233,5 @@ print_results; +exit 0; + --- cluster/fence/agents/scsi/scsi_reserve 2006/12/01 20:31:30 1.1.4.1 +++ cluster/fence/agents/scsi/scsi_reserve 2006/12/14 22:41:56 1.1.4.2 @@ -8,24 +8,24 @@ # check for sg_persist command provided by sg3_utils package # if ! sg_persist -V &> /dev/null ; then - echo "error: sg_persist not found" - exit 2 + echo "error: sg_persist not found" + exit 2 fi # check for gethostip command provided by syslinux package # if ! gethostip -h &> /dev/null ; then - echo "error: gethostip not found" - exit 3 + echo "error: gethostip not found" + exit 3 fi # get scsi devices that are part of clustered volumes # scsi_devices=$( lvs -o vg_attr,devices --noheadings \ - | awk --posix ' $1 ~ /[-a-z]{5}c/ { print $2 } ' \ - | sed -e 's/([0-9]*)//' | sort | uniq ) + | awk --posix ' $1 ~ /[-a-z]{5}c/ { print $2 } ' \ + | sed -e 's/([0-9]*)//' | sort | uniq ) -# if not scsi devices were found we can exit now +# if no scsi devices were found we can exit now # [ -z "$scsi_devices" ] && exit 0 @@ -46,12 +46,15 @@ rval=0 -touch /var/lock/subsys/${0##*/} +touch /var/lock/subsys/scsi_reserve + +cat /dev/null > /var/run/scsi_reserve # register each device using our key # for dev in $scsi_devices do + echo -n "Registering device: $dev" for error in 1 @@ -61,15 +64,23 @@ done if [ $error -eq 0 ]; then + echo $dev >> /var/run/scsi_reserve success else - failure - rval=1 + # perhaps we are already resgistered + # + if sg_persist -d $dev -i -k 2>/dev/null | grep -qiE "${key#0}" ; then + echo $dev >> /var/run/scsi_reserve + success + else + failure + rval=1 + fi fi echo - # attempt to create a reservation + # create a reservation # sg_persist -d $dev -o -R -K $key -T 5 &>/dev/null @@ -86,13 +97,25 @@ do echo -n "Unregistering device: $dev" + # get list of keys registered for this device + # + reg_keys=$( sg_persist -d $dev -i -k | grep '^[[:space:]]*0x' ) + # check if this node/key is the node/key holding the reservation # if sg_persist -d $dev -i -r 2>/dev/null | grep -qiE "${key#0}" ; then - error=1 + if echo "$reg_keys" | grep -qivE "${key#0}" ; then + error=1 + else + for error in 1 + do + sg_persist -d $dev -o -G -K $key -S 0 &>/dev/null || break + error=0 + done + fi else for error in 1 - do + do sg_persist -d $dev -o -G -K $key -S 0 &>/dev/null || break error=0 done @@ -108,7 +131,7 @@ echo done -rm -f /var/lock/subsys/${0##*/} +rm -f /var/lock/subsys/scsi_reserve ;; @@ -126,7 +149,7 @@ done if [ -z "$devices" ]; then - echo "No devices registered." + echo "No devices resgistered." else echo "Found ${#devices[@]} registered device(s):"