[pve-devel] r5437 - in pve-storage/pve2: . PVE PVE/API2 PVE/API2/Storage

svn-commits at proxmox.com svn-commits at proxmox.com
Tue Jan 25 11:18:38 CET 2011


Author: dietmar
Date: 2011-01-25 11:18:38 +0100 (Tue, 25 Jan 2011)
New Revision: 5437

Modified:
   pve-storage/pve2/ChangeLog
   pve-storage/pve2/PVE/API2/Storage.pm
   pve-storage/pve2/PVE/API2/Storage/Config.pm
   pve-storage/pve2/PVE/API2/Storage/Content.pm
   pve-storage/pve2/PVE/API2/Storage/Scan.pm
   pve-storage/pve2/PVE/API2/Storage/Status.pm
   pve-storage/pve2/PVE/Storage.pm
   pve-storage/pve2/pvesm
Log:
use new cfs_read_file/cfs_write_file everywhere, remove stale lock code


Modified: pve-storage/pve2/ChangeLog
===================================================================
--- pve-storage/pve2/ChangeLog	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/ChangeLog	2011-01-25 10:18:38 UTC (rev 5437)
@@ -1,3 +1,8 @@
+2011-01-25  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/Storage.pm:  use new cfs_read_file/cfs_write_file everywhere
+	(cluster filesystem support)
+
 2010-11-08  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/Storage.pm: moved hostname read/write to INotify.pm

Modified: pve-storage/pve2/PVE/API2/Storage/Config.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Config.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/API2/Storage/Config.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -4,7 +4,7 @@
 use warnings;
 
 use PVE::SafeSyslog;
-use PVE::INotify qw(read_file write_file);;
+use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::Storage;
 use HTTP::Status qw(:constants);
 use Storable qw(dclone);
@@ -60,9 +60,9 @@
     code => sub {
 	my ($param) = @_;
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
-	my @sids =  PVE::Storage::storage_ids ($cfg);
+	my @sids =  PVE::Storage::storage_ids($cfg);
 
 	my $res = [];
 	foreach my $storeid (@sids) {
@@ -89,7 +89,7 @@
     code => sub {
 	my ($param) = @_;
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
 	return &$api_storage_config($cfg, $param->{storage});
     }});
@@ -169,7 +169,7 @@
         PVE::Storage::lock_storage_config(
 	    sub {
 
-		my $cfg = read_file('storagecfg');
+		my $cfg = cfs_read_file('storage.cfg');
 
 		if (my $scfg = PVE::Storage::storage_config ($cfg, $storeid, 1)) {
 		    die "storage ID '$storeid' already defined\n";
@@ -196,7 +196,7 @@
 	
 		PVE::Storage::activate_storage ($cfg, $storeid) if !$opts->{disable};
 
-		write_file('storagecfg', $cfg);
+		cfs_write_file('storage.cfg', $cfg);
 	    
 	    }, "create storage failed");
 
@@ -251,7 +251,7 @@
         PVE::Storage::lock_storage_config(
 	 sub {
 
-	    my $cfg = read_file('storagecfg');
+	    my $cfg = cfs_read_file('storage.cfg');
 
 	    PVE::Storage::assert_if_modified ($cfg, $digest);
 
@@ -272,7 +272,7 @@
 		}
 	    }
 
-	    write_file('storagecfg', $cfg);
+	    cfs_write_file('storage.cfg', $cfg);
 
 	    }, "update storage failed");
 
@@ -301,7 +301,7 @@
         PVE::Storage::lock_storage_config(
 	    sub {
 
-		my $cfg = read_file('storagecfg');
+		my $cfg = cfs_read_file('storage.cfg');
 
 		die "can't remove storage - storage is used as base of another storage\n"
 		    if PVE::Storage::storage_is_used ($cfg, $storeid);
@@ -310,7 +310,7 @@
 
 		delete ($cfg->{ids}->{$storeid});
 
-		write_file('storagecfg', $cfg);
+		cfs_write_file('storage.cfg', $cfg);
 
 	    }, "delete storage failed");
   

Modified: pve-storage/pve2/PVE/API2/Storage/Content.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Content.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/API2/Storage/Content.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -4,7 +4,7 @@
 use warnings;
 
 use PVE::SafeSyslog;
-use PVE::INotify qw(read_file);;
+use PVE::Cluster qw(cfs_read_file);
 use PVE::Storage;
 use PVE::Exception qw(raise_param_exc);
 use PVE::RPCEnvironment;
@@ -60,7 +60,7 @@
 
 	# fixme: verify $node
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
 	my $scfg = PVE::Storage::storage_config ($cfg, $storeid);
 
@@ -158,7 +158,7 @@
 	    $param->{format} = $fmt;
 	}
 
-	my $cfg = read_file('storagecfg');
+	my $cfg = cfs_read_file('storage.cfg');
     
 	my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, 
 					       $param->{format}, 
@@ -218,7 +218,7 @@
 
 	my $volid = &$real_volume_id($param->{storage}, $param->{volume});
 
-	my $cfg = read_file('storagecfg');
+	my $cfg = cfs_read_file('storage.cfg');
 
 	my $path = PVE::Storage::path($cfg, $volid);
 	my ($size, $format, $used) = PVE::Storage::file_size_info ($path);
@@ -256,7 +256,7 @@
 	
 	my $volid = &$real_volume_id($param->{storage}, $param->{volume});
 	
-	my $cfg = read_file('storagecfg');
+	my $cfg = cfs_read_file('storage.cfg');
 
 	PVE::Storage::vdisk_free ($cfg, $volid);
 
@@ -303,7 +303,7 @@
 	eval { PVE::Storage::parse_volume_id ($param->{source}); };
 	raise_param_exc({ source => $@}) if $@; 
 
-	my $cfg = read_file('storagecfg');
+	my $cfg = cfs_read_file('storage.cfg');
 
 	# fixme: execute in background
 	print "COPY $param->{source} from node $param->{sourceNode} to $volid\n";
@@ -318,7 +318,7 @@
 use warnings;
 
 use PVE::SafeSyslog;
-use PVE::INotify qw(read_file);;
+use PVE::Cluster qw(cfs_read_file);
 use PVE::Storage;
 use PVE::JSONSchema qw(get_standard_option);
 
@@ -358,7 +358,7 @@
     code => sub {
 	my ($param) = @_;
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
 	my $node = $param->{node};
 

Modified: pve-storage/pve2/PVE/API2/Storage/Scan.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Scan.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/API2/Storage/Scan.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -4,7 +4,6 @@
 use warnings;
 
 use PVE::SafeSyslog;
-use PVE::INotify qw(read_file);;
 use PVE::Storage;
 use HTTP::Status qw(:constants);
 use PVE::JSONSchema qw(get_standard_option);

Modified: pve-storage/pve2/PVE/API2/Storage/Status.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Status.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/API2/Storage/Status.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use PVE::INotify qw(read_file write_file);;
+use PVE::Cluster qw(cfs_read_file);
 use PVE::Storage;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
@@ -43,7 +43,7 @@
 
 	# fixme: verify $node
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
 	# fixme: connect to correct node
 
@@ -76,7 +76,7 @@
 
 	# fixme: verify $node
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file("storage.cfg");
 
 	# fixme: connect to correct node
 

Modified: pve-storage/pve2/PVE/API2/Storage.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/API2/Storage.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -5,7 +5,7 @@
 
 use PVE::SafeSyslog;
 use PVE::JSONSchema qw(get_standard_option);
-use PVE::INotify qw(read_file write_file);;
+use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::Storage;
 use HTTP::Status qw(:constants);
 use Storable qw(dclone);
@@ -100,7 +100,7 @@
 
 	my $nodes = PVE::Cluster::get_nodelist();
 
-	my $cfg = read_file ("storagecfg");
+	my $cfg = cfs_read_file ("storage.cfg");
 
 	my @sids =  PVE::Storage::storage_ids ($cfg);
 

Modified: pve-storage/pve2/PVE/Storage.pm
===================================================================
--- pve-storage/pve2/PVE/Storage.pm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/PVE/Storage.pm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -15,8 +15,8 @@
 use Getopt::Long qw(GetOptionsFromArray);
 use Socket;
 use Digest::SHA1;
-use PVE::Tools qw(run_command lock_file safe_print file_read_firstline trim);
-use PVE::INotify qw(register_file read_file write_file);
+use PVE::Tools qw(run_command file_read_firstline trim);
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::Exception qw(raise_param_exc);
 use PVE::JSONSchema;
 
@@ -25,122 +25,11 @@
 
 $ISCSIADM = undef if ! -X $ISCSIADM;
 
-my $storageconfig = "/etc/pve/storage.cfg";
+# fixme: always_call_parser => 1 ??
+cfs_register_file ('storage.cfg', 
+		   \&parse_config, 
+		   \&write_config); 
 
-my $lockfile = "/etc/pve/.lock-storage.cfg";
-
-# we need the cluster configuration for locking operations
-
-register_file ('clustercfg', "/etc/pve/cluster.cfg", 
-	       \&read_cluster_config);
-
-register_file ('storagecfg', "/etc/pve/storage.cfg", 
-	       \&parse_config, \&write_config, undef,
-	       always_call_parser => 1); 
-
-sub read_cluster_config {
-    my ($filename, $fh) = @_;
-
-    my $line;
-
-    my $cinfo;
-
-    $cinfo->{nodes} = [];
-
-    my $maxcid = 0;
-    my $level = 0;
-
-    while (defined ($line = <$fh>)) {
-	chomp $line;
-
-	next if $line =~ m/^\s*$/; # skip empty lines
-
-	if ($line =~ m/^maxcid\s+(\d+)\s*$/i) {
-	    $maxcid = $1 > $maxcid ? $1 : $maxcid;
-	    next;
-	}
-		
-	if ($line =~ m/^(master|node)\s+(\d+)\s+\{\s*$/i) {
-	    $level++;
-	    my ($t, $cid) = (lc $1, $2);
-		
-	    $maxcid = $cid > $maxcid ? $cid : $maxcid;
-
-	    my $res = { 
-		role => $t eq 'master' ? 'M' : 'N',
-		cid => $cid
-		};
-
-	    while (defined ($line = <$fh>)) {
-		chomp $line;
-		next if $line =~ m/^\s*$/; # skip empty lines
-		if ($line =~ m/^\}\s*$/) {
-		    $level--;
-		    last;
-		}
-	    
-		if ($line =~ m/^\s*(\S+)\s*:\s*(\S+)\s*$/) {
-		    my ($n, $v) = (lc $1, $2);
-
-		    # fixme: do syntax checks
-		    if ($n eq 'ip') {
-			$res->{$n} = $v;
-		    } elsif ($n eq 'name') {
-			$res->{$n} = $v;
-		    } elsif ($n eq 'hostrsapubkey') {
-			$res->{$n} = $v;
-		    } elsif ($n eq 'rootrsapubkey') {
-			$res->{$n} = $v;
-		    } else {
-			die "syntax error in configuration file\n";
-		    }
-		} else {
-		    die "syntax error in configuration file\n";
-		}
-	    }
-
-	    die "missing ip address for node '$cid'\n" if !$res->{ip};
-	    die "missing name for node '$cid'\n" if !$res->{name};
-	    die "missing host RSA key for node '$cid'\n" if !$res->{hostrsapubkey};
-	    die "missing user RSA key for node '$cid'\n" if !$res->{rootrsapubkey};
-
-	    push @{$cinfo->{nodes}}, $res;
-		    
-	    if ($res->{role} eq 'M') {
-		$cinfo->{master} = $res;
-	    } 
-	} else {
-	    die "syntax error in configuration file\n";
-	}
-    }
-
-    die "syntax error in configuration file\n" if $level;
-
-    $cinfo->{maxcid} = $maxcid;
-
-    foreach my $ni (@{$cinfo->{nodes}}) {
-	$cinfo->{"CID_$ni->{cid}"} = $ni;
-    }
-
-    my $hostname = read_file('hostname');
-
-    $cinfo->{local} = {
-	role => '-',
-	cid => 0,
-	ip => '127.0.0.1',
-	name => $hostname,
-    };
-
-    foreach my $ni (@{$cinfo->{nodes}}) {
-	if ($ni->{name} eq $hostname) {
-	    $cinfo->{local} = $ni;
-	    last;
-	}
-    }
- 
-    return $cinfo;
-}
-
 # generic utility function
 
 sub check_iscsi_support {
@@ -254,7 +143,7 @@
 
     my $parent = ( caller(1) )[3];
 
-    lock_file($lockfile, 60, $parent, $code);
+    cfs_lock_file("storage.cfg", 60, $parent, $code);
     my $err = $@;
     if ($err) {
 	$errmsg ? die "$errmsg: $err" : die $err;
@@ -516,20 +405,20 @@
 }
 
 sub parse_config {
-    my ($filename, $fh) = @_;
+    my ($filename, $raw) = @_;
 
     my $ids = {};
 
     my $sha1 = Digest::SHA1->new;
 
-    # returns defaults if !$fh;
-
     my $pri = 0;
-    while ($fh && defined (my $line = <$fh>)) {
-	$sha1->add ($line); # compute digest
 
-	chomp $line;
+    $raw = "" if !defined($raw);
+    while ($raw =~ s/^(.*)\n//) {
+	my $line = $1;
 
+ 	$sha1->add ($line); # compute digest
+
 	next if $line =~ m/^\#/;
 	next if $line =~ m/^\s*$/;
 
@@ -549,7 +438,9 @@
 		$ids->{$storeid}->{priority} = $pri++;
 	    }
 
-	    while (defined ($line = <$fh>)) {
+	    while ($raw =~ s/^(.*)\n//) {
+		$line = $1;
+
 		next if $line =~ m/^\#/;
 		last if $line =~ m/^\s*$/;
 
@@ -674,154 +565,13 @@
     return $settings;
 }
 
-sub fork_command_pipe {
-    my ($cmd) = @_;
-    
-    my $reader = IO::File->new();
-    my $writer = IO::File->new();
-
-    my $orig_pid = $$;
-
-    my $cpid;
-
-    eval { $cpid = open2 ($reader, $writer, @$cmd); };
-
-    my $err = $@;
-
-    # catch exec errors
-    if ($orig_pid != $$) {
-	logmsg ('err', "can't fork command pipe\n");
-	POSIX::_exit (1); 
-	kill ('KILL', $$); 
-    }
-
-    die $err if $err;
-
-    return { writer => $writer, reader => $reader, pid => $cpid };
-}
-
-sub finish_command_pipe { 
-    my $cmdpipe = shift;
-
-    my $writer = $cmdpipe->{writer};
-    my $reader = $cmdpipe->{reader};
-
-    $writer->close();
-    $reader->close();
-
-    my $cpid = $cmdpipe->{pid};
-
-    kill (15, $cpid) if kill (0, $cpid);
-
-    waitpid $cpid, 0;
-}
-
-sub run_with_timeout {
-    my ($timeout, $code, @param) = @_;
-
-    die "got timeout\n" if $timeout <= 0;
-
-    my $prev_alarm;
-
-    my $sigcount = 0;
-
-    my $res;
-
-    eval {
-	local $SIG{ALRM} = sub { $sigcount++; die "got timeout\n"; };
-	local $SIG{PIPE} = sub { $sigcount++; die "broken pipe\n" };
-	local $SIG{__DIE__};   # see SA bug 4631
-
-	$prev_alarm = alarm ($timeout);
-
-	$res = &$code (@param);
-
-	alarm 0; # avoid race conditions
-    };
-
-    my $err = $@;
-    
-    alarm ($prev_alarm) if defined ($prev_alarm);
-
-    die "unknown error" if $sigcount && !$err; # seems to happen sometimes
-
-    die $err if $err;
-
-    return $res;
-}
-
 sub cluster_lock_storage {
     my ($storeid, $shared, $timeout, $func, @param) = @_;
 
-    my $ccfg = read_file('clustercfg');
+    die "cluster lock not implemented";
 
-    my $masterip = $ccfg && $ccfg->{master} ? $ccfg->{master}->{ip} : undef;
-   
-    if (!$shared || !$masterip) {
-	return &$func (@param);
-    }
-
-    $timeout = 60 if !$timeout;
-
-    my $cmdpipe;
-    my $got_lock;
-
-    my $ret_value;
-
-    eval {
-
-	my $cmd;
-
-	if ($masterip eq $ccfg->{local}->{ip}) {
-	    $cmd = ['/usr/sbin/pvesm', 'lock', $storeid, $timeout];
-	} else {
-	    $cmd = ['/usr/bin/ssh', '-o', 'BatchMode=yes', $masterip,
-		    '/usr/sbin/pvesm', 'lock', $storeid, $timeout];
-	}
-
-	$cmdpipe = fork_command_pipe ($cmd);
-
-	my $helo;
-	my $reader = $cmdpipe->{reader};
-
-	eval { run_with_timeout ($timeout + 5, sub { $helo = <$reader>; }); };
-	die "can't lock storage '$storeid' - $@" if $@;
-	die "can't lock storage '$storeid' - no reply\n" if !$helo;
-	if ($helo !~ m/^lock $storeid: (.*)$/) {
-	    die "got strange reply from lock ('$helo')\n";
-	}
-	my $res = $1;
-	die "can't aquire lock - $res\n" if $res ne 'success';
-
-	$got_lock = 1;
-
-	$ret_value = &$func (@param);
-
-    };
-    my $err = $@;
-    
-    if ($cmdpipe) {
-
-	if ($got_lock) {
-	    my $writer = $cmdpipe->{writer};
-
-	    eval { 
-		run_with_timeout (60, sub {
-		    print $writer "release\n";
-		    $writer->flush();
-		}); 
-	    };
-	    warn $@ if $@;
-	}
-	finish_command_pipe ($cmdpipe);
-    }
-    
-    die $err if $err;
-
-    return $ret_value;
 }
 
-
 sub storage_config {
     my ($cfg, $storeid, $noerr) = @_;
 
@@ -882,8 +632,10 @@
 }
 
 sub write_config {
-    my ($filename, $fh, $cfg) = @_;
+    my ($filename, $cfg) = @_;
 
+    my $out = '';
+
     my $ids = $cfg->{ids};
 
     my $maxpri = 0;
@@ -924,8 +676,10 @@
 	    }
 	}
 
-	safe_print($filename, $fh, "$data\n");
+	$out .= "$data\n";
     }
+
+    return $out;
 }
 
 sub get_image_dir {

Modified: pve-storage/pve2/pvesm
===================================================================
--- pve-storage/pve2/pvesm	2011-01-25 09:16:46 UTC (rev 5436)
+++ pve-storage/pve2/pvesm	2011-01-25 10:18:38 UTC (rev 5437)
@@ -5,7 +5,9 @@
 use Fcntl ':flock';
 use File::Path;
 
-use PVE::INotify qw(read_file);
+use PVE::SafeSyslog;
+use PVE::Cluster;
+use PVE::INotify;
 use PVE::RPCEnvironment;
 use PVE::Storage;
 use PVE::API2::Storage::Config;
@@ -20,9 +22,12 @@
 
 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
+initlog ('pvesm', 'daemon');
+
 die "please run as root\n" if $> != 0;
 
 PVE::INotify::inotify_init();
+PVE::Cluster::cfs_update();
 
 my $rpcenv = PVE::RPCEnvironment->init('cli');
 
@@ -49,7 +54,7 @@
     code => sub {
 	my ($param) = @_;
 
-	my $cfg = read_file('storagecfg');
+	my $cfg = PVE::Cluster::cfs_read_file('storage.cfg');
 
 	my $path = PVE::Storage::path ($cfg, $param->{volume});
 
@@ -189,25 +194,25 @@
     }
 };
 
-my $hostname = read_file('hostname');
+my $nodename = PVE::INotify::nodename();
 
 my $cmddef = {
     add => [ "PVE::API2::Storage::Config", 'create', ['storage'] ],
     set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
     remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
     status => [ "PVE::API2::Storage::Status", 'index', [], 
-		{ node => $hostname }, $print_status ],
+		{ node => $nodename }, $print_status ],
     list => [ "PVE::API2::Storage::Content", 'index', ['storage'], 
-	      { node => $hostname }, $print_content ],
+	      { node => $nodename }, $print_content ],
     alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'], 
-	       { node => $hostname }, sub {
+	       { node => $nodename }, sub {
 		   my $volid = shift;
 		   print "sucessfuly created '$volid'\n";
 	       }],
     free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
-	      { node => $hostname } ],
+	      { node => $nodename } ],
     scan => [ "PVE::API2::Storage::Scan", 'scan', ['method', 'server'],
-	      { node => $hostname }, sub  {
+	      { node => $nodename }, sub  {
 		  my $res = shift;
 
 		  my $maxlen = 0;




More information about the pve-devel mailing list