From cbbb06a5ae3a002f3ceca0850ee7600a9b4c8142 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Fri, 1 Jun 2018 16:37:38 +0200 Subject: [PATCH] add create_and_lock_config Signed-off-by: Thomas Lamprecht --- PVE/AbstractConfig.pm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/PVE/AbstractConfig.pm b/PVE/AbstractConfig.pm index 3473988..11180df 100644 --- a/PVE/AbstractConfig.pm +++ b/PVE/AbstractConfig.pm @@ -82,6 +82,19 @@ sub lock_config_full { return $res; } +sub create_and_lock_config { + my ($class, $vmid, $allow_existing) = @_; + + $class->lock_config_full($vmid, 5, sub { + PVE::Cluster::check_vmid_unused($vmid, $allow_existing); + + my $conf = eval { $class->load_config($vmid) } || {}; + $class->check_lock($conf); + $conf->{lock} = 'create'; + $class->write_config($vmid, $conf); + }); +} + # Lock config file using flock, run $code with @param, unlock config file. # $timeout is the maximum time to aquire the flock # $shared eq 1 creates a non-exclusive ("read") flock -- 2.39.2