[pve-devel] r5011 - pve-storage/pve2

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Aug 16 13:30:37 CEST 2010


Author: dietmar
Date: 2010-08-16 11:30:37 +0000 (Mon, 16 Aug 2010)
New Revision: 5011

Modified:
   pve-storage/pve2/ChangeLog
   pve-storage/pve2/Storage.pm
Log:
	* Storage.pm (lock_config): renamed to lock_storage_config, use
	lock_file from PVE::Utils



Modified: pve-storage/pve2/ChangeLog
===================================================================
--- pve-storage/pve2/ChangeLog	2010-08-16 11:16:27 UTC (rev 5010)
+++ pve-storage/pve2/ChangeLog	2010-08-16 11:30:37 UTC (rev 5011)
@@ -1,3 +1,10 @@
+2010-08-16  Proxmox Support Team  <support at proxmox.com>
+
+	* Storage.pm (lock_config): renamed to lock_storage_config, use
+	lock_file from PVE::Utils
+
+	* control.in (Depends): add libpve-common-perl
+
 2010-07-16  Proxmox Support Team  <support at proxmox.com>
 
 	* Storage.pm (parse_options): added ability to verify a

Modified: pve-storage/pve2/Storage.pm
===================================================================
--- pve-storage/pve2/Storage.pm	2010-08-16 11:16:27 UTC (rev 5010)
+++ pve-storage/pve2/Storage.pm	2010-08-16 11:30:37 UTC (rev 5011)
@@ -10,13 +10,12 @@
 use File::Basename;
 use File::Path;
 use IPC::Open2;
-use IPC::Open3;
 use Filesys::Df;
 use Cwd 'abs_path';
 use Getopt::Long qw(GetOptionsFromArray);
 use Socket;
 use Digest::SHA1;
-use PVE::Tools qw(run_command);
+use PVE::Tools qw(run_command lock_file);
 
 my $ISCSIADM = '/usr/bin/iscsiadm';
 my $UDEVADM = '/sbin/udevadm';
@@ -266,49 +265,16 @@
 
 #  PVE::Storage utility functions
 
-sub lock_config {
-    my ($code, @param) = @_;
+sub lock_storage_config {
+    my ($code, $errmsg) = @_;
 
-    my $timeout = 60;
+    my $parent = ( caller(1) )[3];
 
-    my $fh;
-
-    my $res;
-
-    eval {
-
-	local $SIG{ALRM} = sub { die "got timeout\n"; };
-
-	alarm ($timeout);
-
-	$fh = new IO::File (">>$lockfile") ||
-	    die "can't open lock for '$lockfile' - $!\n";
-
-	if (!flock ($fh, LOCK_EX|LOCK_NB)) {
-	    print STDERR "trying to aquire lock...";
-	    if (!flock ($fh, LOCK_EX)) {
-		print STDERR " failed\n";
-		die "can't aquire lock for '$lockfile' - $!\n";
-	    }
-	    print STDERR " OK\n";
-	}
-	alarm (0);
-
-	$res = &$code (@param);
-    };
-
+    lock_file($lockfile, 60, $parent, $code);
     my $err = $@;
-
-    alarm (0);
-
-    close ($fh) if $fh;
-
     if ($err) {
-	$@ = $err;
-	return undef;
+	$errmsg ? die "$errmsg: $err" : die $err;
     }
-
-    return $res;
 }
 
 my $confvars = {
@@ -2424,126 +2390,122 @@
 
     my $opts = parse_options ($storeid, $type, $param, 1);
 
-    lock_config (sub {
+    lock_storage_config (
+	sub {
 
-	my $cfg = load_config();
+	    my $cfg = load_config();
 
-	if (my $scfg = storage_config ($cfg, $storeid, 1)) {
-	    die "storage ID '$storeid' already defined\n";
-	}
+	    if (my $scfg = storage_config ($cfg, $storeid, 1)) {
+		die "storage ID '$storeid' already defined\n";
+	    }
 
-	$cfg->{ids}->{$storeid} = $opts;
+	    $cfg->{ids}->{$storeid} = $opts;
 
-	if ($type eq 'lvm' && $opts->{base}) {
+	    if ($type eq 'lvm' && $opts->{base}) {
 
-	    my ($baseid, $volname) = parse_volume_id ($opts->{base});
+		my ($baseid, $volname) = parse_volume_id ($opts->{base});
 
-	    my $basecfg = storage_config ($cfg, $baseid, 1);
-	    die "base storage ID '$baseid' does not exist\n" if !$basecfg;
+		my $basecfg = storage_config ($cfg, $baseid, 1);
+		die "base storage ID '$baseid' does not exist\n" if !$basecfg;
        
-	    # we only support iscsi for now
-	    if (!($basecfg->{type} eq 'iscsi')) {
-		die "unsupported base type '$basecfg->{type}'";
-	    }
+		# we only support iscsi for now
+		if (!($basecfg->{type} eq 'iscsi')) {
+		    die "unsupported base type '$basecfg->{type}'";
+		}
 
-	    my $path = path ($cfg, $opts->{base});
+		my $path = path ($cfg, $opts->{base});
 
-	    lvm_create_volume_group ($path, $opts->{vgname}, $opts->{shared});
-	}
+		lvm_create_volume_group ($path, $opts->{vgname}, $opts->{shared});
+	    }
 	
-	activate_storage ($cfg, $storeid) if !$opts->{disable};
+	    activate_storage ($cfg, $storeid) if !$opts->{disable};
 
-	write_config($cfg);
-    });
-
-    die $@ if $@;
+	    write_config($cfg);
+	    
+	}, "create storage failed");
 }
 
 sub storage_set {
     my ($storeid, $param, $digest) = @_;
 
-    lock_config (sub {
+    lock_storage_config (
+	sub {
 
-	my $cfg = load_config();
+	    my $cfg = load_config();
 
-	__assert_if_modified ($cfg, $digest);
+	    __assert_if_modified ($cfg, $digest);
 
-	my $scfg = storage_config ($cfg, $storeid);
+	    my $scfg = storage_config ($cfg, $storeid);
 
-	my $opts = parse_options ($storeid, $scfg->{type}, $param);
+	    my $opts = parse_options ($storeid, $scfg->{type}, $param);
 
-	foreach my $k (%$opts) {
-	    $scfg->{$k} = $opts->{$k};
-	}
+	    foreach my $k (%$opts) {
+		$scfg->{$k} = $opts->{$k};
+	    }
 	
-	write_config ($cfg);
-    });
-
-    die $@ if $@;
+	    write_config ($cfg);
+	}, "update storage failed");
 }
 
 sub storage_remove {
     my ($storeid, $digest) = @_;
 
-    lock_config (sub {
+    lock_storage_config (
+	sub {
 
-	my $cfg = load_config();
+	    my $cfg = load_config();
 
-	__assert_if_modified ($cfg, $digest);
+	    __assert_if_modified ($cfg, $digest);
 
-	die "can't remove storage - storage is used as base of another storage\n"
-	    if storage_is_used ($cfg, $storeid);
+	    die "can't remove storage - storage is used as base of another storage\n"
+		if storage_is_used ($cfg, $storeid);
 
-	deactivate_storage ($cfg, $storeid);
+	    deactivate_storage ($cfg, $storeid);
 
-	delete ($cfg->{ids}->{$storeid});
+	    delete ($cfg->{ids}->{$storeid});
 
-	write_config ($cfg);
-    });
- 
-    die $@ if $@;
+	    write_config ($cfg);
+	}, "delete storage failed");
 }
 
 sub storage_enable {
     my ($storeid, $digest) = @_;
 
-    lock_config (sub {
+    lock_storage_config (
+	sub {
 
-	my $cfg = load_config();
+	    my $cfg = load_config();
 
-	__assert_if_modified ($cfg, $digest);
+	    __assert_if_modified ($cfg, $digest);
 
-	my $scfg = storage_config ($cfg, $storeid);
+	    my $scfg = storage_config ($cfg, $storeid);
 
-	delete $scfg->{disable};
+	    delete $scfg->{disable};
 
-	activate_storage ($cfg, $storeid);	
+	    activate_storage ($cfg, $storeid);	
 
-	write_config ($cfg);
-    });
-
-    die $@ if $@;
+	    write_config ($cfg);
+	}, "enable storage failed");
 }
 
 sub storage_disable {
     my ($storeid, $digest) = @_;
 
-    lock_config (sub {
+    lock_storage_config (
+	sub {
 
-	my $cfg = load_config();
+	    my $cfg = load_config();
 
-	__assert_if_modified ($cfg, $digest);
+	    __assert_if_modified ($cfg, $digest);
 
-	my $scfg = storage_config ($cfg, $storeid);
+	    my $scfg = storage_config ($cfg, $storeid);
+	    
+	    $scfg->{disable} = 1;
 
-	$scfg->{disable} = 1;
+	    deactivate_storage ($cfg, $storeid);	
 
-	deactivate_storage ($cfg, $storeid);	
-
-	write_config ($cfg);
-    });
-
-    die $@ if $@;
+	    write_config ($cfg);
+	}, "disable storage failed");
 }
 
 sub foreach_volid {



More information about the pve-devel mailing list