]>
Commit | Line | Data |
---|---|---|
94d526d7 DM |
1 | package PVE::GuestHelpers; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Tools; | |
3acb4e74 | 7 | use PVE::Storage; |
94d526d7 DM |
8 | |
9 | # We use a separate lock to block migration while a replication job | |
10 | # is running. | |
11 | ||
7919c482 WB |
12 | our $lockdir = '/var/lock/pve-manager'; |
13 | ||
94d526d7 DM |
14 | sub guest_migration_lock { |
15 | my ($vmid, $timeout, $func, @param) = @_; | |
16 | ||
17 | my $lockid = "pve-migrate-$vmid"; | |
94d526d7 DM |
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 | ||
3acb4e74 DC |
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 | my $hookscript = eval { check_hookscript($conf->{hookscript}) }; | |
50 | if (my $err = $@) { | |
51 | if ($stop_on_error) { | |
52 | die $err; | |
53 | } else { | |
54 | warn $err; | |
55 | return; | |
56 | } | |
57 | } | |
58 | ||
59 | eval { | |
60 | PVE::Tools::run_command([$hookscript, $vmid, $phase]); | |
61 | }; | |
62 | ||
63 | if (my $err = $@) { | |
64 | my $errmsg = "hookscript error for $vmid on $phase: $err\n"; | |
65 | if ($stop_on_error) { | |
66 | die $errmsg; | |
67 | } else { | |
68 | warn $errmsg; | |
69 | } | |
70 | } | |
71 | } | |
72 | ||
94d526d7 | 73 | 1; |