[pve-devel] [PATCH guest-common v3 1/2] add exec_hookscript to GuestHelpers

Dominik Csapak d.csapak at proxmox.com
Tue Jan 29 16:28:41 CET 2019


this can be called from qemu-server/pve-container to execute a hookscript

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/GuestHelpers.pm | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/PVE/GuestHelpers.pm b/PVE/GuestHelpers.pm
index c326812..8327dcb 100644
--- a/PVE/GuestHelpers.pm
+++ b/PVE/GuestHelpers.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use PVE::Tools;
+use PVE::Storage;
 
 # We use a separate lock to block migration while a replication job
 # is running.
@@ -23,4 +24,35 @@ sub guest_migration_lock {
     return $res;
 }
 
+sub exec_hookscript {
+    my ($conf, $vmid, $phase, $stop_on_error) = @_;
+
+    return if !$conf->{hookscript};
+    my $storecfg = PVE::Storage::config();
+    my $hookscript = PVE::Storage::path($storecfg, $conf->{hookscript});
+    if (!$hookscript || ! -x $hookscript) {
+	my $errmsg = "hookscript $hookscript not found or not executable\n";
+
+	if ($stop_on_error) {
+	    die $errmsg;
+	} else {
+	    warn $errmsg;
+	}
+	return;
+    }
+
+    eval {
+	PVE::Tools::run_command([$hookscript, $vmid, $phase]);
+    };
+
+    if (my $err = $@) {
+	my $errmsg = "hookscript error for $vmid on $phase: $err\n";
+	if ($stop_on_error) {
+	    die $errmsg;
+	} else {
+	    warn $errmsg;
+	}
+    }
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list