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

svn-commits at proxmox.com svn-commits at proxmox.com
Mon Aug 16 14:02:55 CEST 2010


Author: dietmar
Date: 2010-08-16 12:02:55 +0000 (Mon, 16 Aug 2010)
New Revision: 5017

Modified:
   pve-storage/pve2/ChangeLog
   pve-storage/pve2/Storage.pm
   pve-storage/pve2/pvesm
Log:
* Storage.pm: use new INotify class (last commit message was nonsense - sorry)



Modified: pve-storage/pve2/ChangeLog
===================================================================
--- pve-storage/pve2/ChangeLog	2010-08-16 11:49:42 UTC (rev 5016)
+++ pve-storage/pve2/ChangeLog	2010-08-16 12:02:55 UTC (rev 5017)
@@ -1,5 +1,7 @@
 2010-08-16  Proxmox Support Team  <support at proxmox.com>
 
+	* Storage.pm: use new INotify class
+	
 	* Storage.pm (lock_config): renamed to lock_storage_config, use
 	lock_file from PVE::Utils
 

Modified: pve-storage/pve2/Storage.pm
===================================================================
--- pve-storage/pve2/Storage.pm	2010-08-16 11:49:42 UTC (rev 5016)
+++ pve-storage/pve2/Storage.pm	2010-08-16 12:02:55 UTC (rev 5017)
@@ -15,7 +15,7 @@
 use Getopt::Long qw(GetOptionsFromArray);
 use Socket;
 use Digest::SHA1;
-use PVE::Tools qw(run_command lock_file);
+use PVE::Tools qw(run_command lock_file safe_print);
 use PVE::INotify qw(register_file read_file write_file);
 
 my $ISCSIADM = '/usr/bin/iscsiadm';
@@ -37,7 +37,7 @@
 	       \&read_cluster_config);
 
 register_file ('storagecfg', "/etc/pve/storage.cfg", 
-	       \&parse_config, undef, undef,
+	       \&parse_config, \&write_config, undef,
 	       always_call_parser => 1); 
 
 sub read_etc_hostname {
@@ -626,17 +626,6 @@
 
 }
 
-sub load_config {
-
-    my $cfg = {};
-
-    my $fh = IO::File->new ($storageconfig, 'r');
-    $cfg = parse_config ($storageconfig, $fh); # returns defaults if !$fh;
-    $fh->close() if $fh;
-
-    return $cfg;
-}
-
 sub fork_command_pipe {
     my ($cmd) = @_;
     
@@ -845,66 +834,49 @@
 }
 
 sub write_config {
-    my ($cfg) = @_;
+    my ($filename, $fh, $cfg) = @_;
 
-    my $tmpfn = "$storageconfig.tmp$$";
-    my $fh = IO::File->new ($tmpfn, 'w') ||
-	die "unable to write file '$tmpfn' - $!";
-
     my $ids = $cfg->{ids};
 
-    eval {
-	my $maxpri = 0;
-	foreach my $storeid (keys %$ids) {
-	    my $pri = $ids->{$storeid}->{priority}; 
-	    $maxpri = $pri if $pri && $pri > $maxpri;
-	}
-	foreach my $storeid (keys %$ids) {
-	    if (!defined ($ids->{$storeid}->{priority})) {
-		$ids->{$storeid}->{priority} = ++$maxpri;
-	    } 
-	}
+    my $maxpri = 0;
+    foreach my $storeid (keys %$ids) {
+	my $pri = $ids->{$storeid}->{priority}; 
+	$maxpri = $pri if $pri && $pri > $maxpri;
+    }
+    foreach my $storeid (keys %$ids) {
+	if (!defined ($ids->{$storeid}->{priority})) {
+	    $ids->{$storeid}->{priority} = ++$maxpri;
+	} 
+    }
 
-	foreach my $storeid (sort {$ids->{$a}->{priority} <=> $ids->{$b}->{priority}} keys %$ids) {
-	    my $scfg = $ids->{$storeid};
-	    my $type = $scfg->{type};
-	    my $def = $default_config->{$type};
+    foreach my $storeid (sort {$ids->{$a}->{priority} <=> $ids->{$b}->{priority}} keys %$ids) {
+	my $scfg = $ids->{$storeid};
+	my $type = $scfg->{type};
+	my $def = $default_config->{$type};
 
-	    die "unknown storage type '$type'\n" if !$def;
+	die "unknown storage type '$type'\n" if !$def;
 
-	    my $data = "$type: $storeid\n";
+	my $data = "$type: $storeid\n";
 
-	    $data .= "\tdisable\n" if $scfg->{disable};
+	$data .= "\tdisable\n" if $scfg->{disable};
 
-	    my $done_hash = { disable => 1};
-	    foreach my $k (@{$required_config->{$type}}) {
-		$done_hash->{$k} = 1;
-		my $v =  $ids->{$storeid}->{$k};
-		die "storage '$storeid' - missing value for required option '$k'\n"
-		    if !defined ($v);
+	my $done_hash = { disable => 1};
+	foreach my $k (@{$required_config->{$type}}) {
+	    $done_hash->{$k} = 1;
+	    my $v =  $ids->{$storeid}->{$k};
+	    die "storage '$storeid' - missing value for required option '$k'\n"
+		if !defined ($v);
+	    $data .= sprint_config_line ($k, $v);
+	}
+
+	foreach my $k (keys %$def) {
+	    next if defined ($done_hash->{$k});
+	    if (defined (my $v = $ids->{$storeid}->{$k})) {
 		$data .= sprint_config_line ($k, $v);
 	    }
-
-	    foreach my $k (keys %$def) {
-		next if defined ($done_hash->{$k});
-		if (defined (my $v = $ids->{$storeid}->{$k})) {
-		    $data .= sprint_config_line ($k, $v);
-		}
-	    }
-
-	    print $fh "$data\n";
 	}
-    };
 
-    my $err = $@;
-
-    close ($fh);
-
-    if ($err) {
-	unlink $tmpfn;
-	die $err;
-    } else {
-	rename $tmpfn, $storageconfig;
+	safe_print($filename, $fh, $data);
     }
 }
 
@@ -2396,7 +2368,7 @@
     lock_storage_config (
 	sub {
 
-	    my $cfg = load_config();
+	    my $cfg = read_file('storagecfg');
 
 	    if (my $scfg = storage_config ($cfg, $storeid, 1)) {
 		die "storage ID '$storeid' already defined\n";
@@ -2423,7 +2395,7 @@
 	
 	    activate_storage ($cfg, $storeid) if !$opts->{disable};
 
-	    write_config($cfg);
+	    write_file('storagecfg', $cfg);
 	    
 	}, "create storage failed");
 }
@@ -2434,7 +2406,7 @@
     lock_storage_config (
 	sub {
 
-	    my $cfg = load_config();
+	    my $cfg = read_file('storagecfg');
 
 	    __assert_if_modified ($cfg, $digest);
 
@@ -2445,8 +2417,9 @@
 	    foreach my $k (%$opts) {
 		$scfg->{$k} = $opts->{$k};
 	    }
-	
-	    write_config ($cfg);
+
+	    write_file('storagecfg', $cfg);
+
 	}, "update storage failed");
 }
 
@@ -2456,7 +2429,7 @@
     lock_storage_config (
 	sub {
 
-	    my $cfg = load_config();
+	    my $cfg = read_file('storagecfg');
 
 	    __assert_if_modified ($cfg, $digest);
 
@@ -2467,7 +2440,8 @@
 
 	    delete ($cfg->{ids}->{$storeid});
 
-	    write_config ($cfg);
+	    write_file('storagecfg', $cfg);
+
 	}, "delete storage failed");
 }
 
@@ -2477,7 +2451,7 @@
     lock_storage_config (
 	sub {
 
-	    my $cfg = load_config();
+	    my $cfg = read_file('storagecfg');
 
 	    __assert_if_modified ($cfg, $digest);
 
@@ -2487,7 +2461,8 @@
 
 	    activate_storage ($cfg, $storeid);	
 
-	    write_config ($cfg);
+	    write_file('storagecfg', $cfg);
+
 	}, "enable storage failed");
 }
 
@@ -2497,7 +2472,7 @@
     lock_storage_config (
 	sub {
 
-	    my $cfg = load_config();
+	    my $cfg = read_file('storagecfg');
 
 	    __assert_if_modified ($cfg, $digest);
 
@@ -2507,7 +2482,8 @@
 
 	    deactivate_storage ($cfg, $storeid);	
 
-	    write_config ($cfg);
+	    write_file('storagecfg', $cfg);
+
 	}, "disable storage failed");
 }
 

Modified: pve-storage/pve2/pvesm
===================================================================
--- pve-storage/pve2/pvesm	2010-08-16 11:49:42 UTC (rev 5016)
+++ pve-storage/pve2/pvesm	2010-08-16 12:02:55 UTC (rev 5017)
@@ -2,10 +2,17 @@
 
 use strict;
 use Getopt::Long;
+use PVE::INotify qw(read_file);
 use PVE::Storage;
 use Fcntl ':flock';
 use File::Path;
 
+$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+
+die "please run as root\n" if $> != 0;
+
+PVE::INotify::inotify_init();
+
 sub print_usage {
     my $msg = shift;
 
@@ -186,7 +193,7 @@
 	die "unable to parse size '$sizestr'\n";
     }
 
-    my $cfg = PVE::Storage::load_config();
+    my $cfg = read_file('storagecfg');
     
     my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $vmid, 
 					   $opts->{format}, 
@@ -202,7 +209,7 @@
 
     my $volid = shift;
 
-    my $cfg = PVE::Storage::load_config();
+    my $cfg = read_file('storagecfg');
     
     PVE::Storage::vdisk_free ($cfg, $volid);
 
@@ -218,7 +225,7 @@
 	die "wrong number of arguments\n";
     }
  
-    my $cfg = PVE::Storage::load_config();
+    my $cfg = read_file('storagecfg');
 
     my @sids =  PVE::Storage::storage_ids ($cfg);
 
@@ -304,7 +311,7 @@
 
     my $volid = shift;
 
-    my $cfg = PVE::Storage::load_config();
+    my $cfg = read_file('storagecfg');
 
     my $path = PVE::Storage::path ($cfg, $volid);
 



More information about the pve-devel mailing list