[pve-devel] [PATCH storage 4/5] refactor diskmanagement lock_file calls

Dominik Csapak d.csapak at proxmox.com
Wed Aug 8 10:20:08 CEST 2018


so that we only have one place where we reference the lockfile
and the timeout

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/API2/Disks/Directory.pm | 5 +----
 PVE/API2/Disks/LVM.pm       | 4 +---
 PVE/API2/Disks/LVMThin.pm   | 4 +---
 PVE/API2/Disks/ZFS.pm       | 4 +---
 PVE/Diskmanage.pm           | 7 +++++++
 5 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/PVE/API2/Disks/Directory.pm b/PVE/API2/Disks/Directory.pm
index 3c5ebc5..8e6118a 100644
--- a/PVE/API2/Disks/Directory.pm
+++ b/PVE/API2/Disks/Directory.pm
@@ -214,7 +214,7 @@ __PACKAGE__->register_method ({
 	    my $mountunitname = "mnt-pve-$name.mount";
 	    my $mountunitpath = "/etc/systemd/system/$mountunitname";
 
-	    lock_file('/run/lock/pve-diskmanage.lck', undef, sub {
+	    PVE::Diskmanage::locked_disk_action(sub {
 		# create partition
 		my $cmd = [$SGDISK, '-n1', '-t1:8300', $dev];
 		print "# ", join(' ', @$cmd), "\n";
@@ -279,11 +279,8 @@ __PACKAGE__->register_method ({
 		    PVE::API2::Storage::Config->create($storage_params);
 		}
 	    });
-
-	    die $@ if $@;
 	};
 
-
 	return $rpcenv->fork_worker('dircreate', $name, $user, $worker);
     }});
 
diff --git a/PVE/API2/Disks/LVM.pm b/PVE/API2/Disks/LVM.pm
index fef5c60..19fba07 100644
--- a/PVE/API2/Disks/LVM.pm
+++ b/PVE/API2/Disks/LVM.pm
@@ -152,7 +152,7 @@ __PACKAGE__->register_method ({
 	die "device $dev is already in use\n" if PVE::Diskmanage::disk_is_used($dev);
 
 	my $worker = sub {
-	    my $res = lock_file('/run/lock/pve-diskmanage.lck', undef, sub {
+	    PVE::Diskmanage::locked_disk_action(sub {
 		PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
 
 		if ($param->{add_storage}) {
@@ -168,8 +168,6 @@ __PACKAGE__->register_method ({
 		    PVE::API2::Storage::Config->create($storage_params);
 		}
 	    });
-
-	    die $@ if $@;
 	};
 
 	return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
diff --git a/PVE/API2/Disks/LVMThin.pm b/PVE/API2/Disks/LVMThin.pm
index f776c3c..5b72c8c 100644
--- a/PVE/API2/Disks/LVMThin.pm
+++ b/PVE/API2/Disks/LVMThin.pm
@@ -112,7 +112,7 @@ __PACKAGE__->register_method ({
 	}
 
 	my $worker = sub {
-	    lock_file('/run/lock/pve-diskmanage.lck', undef, sub {
+	    PVE::Diskmanage::locked_disk_action(sub {
 		PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
 
 		# create thinpool with size 100%, let lvm handle the metadata size
@@ -131,8 +131,6 @@ __PACKAGE__->register_method ({
 		    PVE::API2::Storage::Config->create($storage_params);
 		}
 	    });
-
-	    die $@ if $@;
 	};
 
 	return $rpcenv->fork_worker('lvmthincreate', $name, $user, $worker);
diff --git a/PVE/API2/Disks/ZFS.pm b/PVE/API2/Disks/ZFS.pm
index ad6da23..1690768 100644
--- a/PVE/API2/Disks/ZFS.pm
+++ b/PVE/API2/Disks/ZFS.pm
@@ -313,7 +313,7 @@ __PACKAGE__->register_method ({
 	    if $numdisks < $mindisks->{$raidlevel};
 
 	my $worker = sub {
-	    lock_file('/run/lock/pve-diskmanage.lck', 10, sub {
+	    PVE::Diskmanage::locked_disk_action(sub {
 		# create zpool with desired raidlevel
 
 		my $cmd = [$ZPOOL, 'create', '-o', "ashift=$ashift", $name];
@@ -347,10 +347,8 @@ __PACKAGE__->register_method ({
 		    PVE::API2::Storage::Config->create($storage_params);
 		}
 	    });
-	    die $@ if $@;
 	};
 
-
 	return $rpcenv->fork_worker('zfscreate', $name, $user, $worker);
     }});
 
diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index 23e5b13..1938fa8 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -595,4 +595,11 @@ sub get_blockdev {
     return $block_dev;
 }
 
+sub locked_disk_action {
+    my ($sub) = @_;
+    my $res = PVE::Tools::lock_file('/run/lock/pve-diskmanage.lck', undef, $sub);
+    die $@ if $@;
+    return $res;
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list