]> git.proxmox.com Git - pve-guest-common.git/blob - PVE/GuestHelpers.pm
follouwp: saner exception handling
[pve-guest-common.git] / PVE / GuestHelpers.pm
1 package PVE::GuestHelpers;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools;
7 use PVE::Storage;
8
9 # We use a separate lock to block migration while a replication job
10 # is running.
11
12 our $lockdir = '/var/lock/pve-manager';
13
14 sub guest_migration_lock {
15 my ($vmid, $timeout, $func, @param) = @_;
16
17 my $lockid = "pve-migrate-$vmid";
18
19 mkdir $lockdir;
20
21 my $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
22 die $@ if $@;
23
24 return $res;
25 }
26
27 sub check_hookscript {
28 my ($volid, $storecfg) = @_;
29
30 $storecfg = PVE::Storage::config() if !defined($storecfg);
31 my ($path, undef, $type) = PVE::Storage::path($storecfg, $volid);
32
33 die "'$volid' is not in the snippets directory\n"
34 if $type ne 'snippets';
35
36 die "script '$volid' does not exists\n"
37 if ! -f $path;
38
39 die "script '$volid' is not executable\n"
40 if ! -x $path;
41
42 return $path;
43 }
44
45 sub exec_hookscript {
46 my ($conf, $vmid, $phase, $stop_on_error) = @_;
47
48 return if !$conf->{hookscript};
49
50 eval {
51 my $hookscript = check_hookscript($conf->{hookscript});
52 die $@ if $@;
53
54 PVE::Tools::run_command([$hookscript, $vmid, $phase]);
55 };
56 if (my $err = $@) {
57 my $errmsg = "hookscript error for $vmid on $phase: $err\n";
58 die $errmsg if ($stop_on_error);
59 warn $errmsg;
60 }
61 }
62
63 1;