From: Thomas Lamprecht Date: Fri, 1 Jun 2018 14:37:38 +0000 (+0200) Subject: add create_and_lock_config X-Git-Url: https://git.proxmox.com/?p=pve-guest-common.git;a=commitdiff_plain;h=cbbb06a5ae3a002f3ceca0850ee7600a9b4c8142 add create_and_lock_config Signed-off-by: Thomas Lamprecht --- 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