]> git.proxmox.com Git - qemu-server.git/commitdiff
add pre- start/stop hookscripts to VMs
authorDominik Csapak <d.csapak@proxmox.com>
Thu, 31 Jan 2019 13:33:39 +0000 (14:33 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 1 Feb 2019 11:58:37 +0000 (12:58 +0100)
this adds a new config option for it, and executes it on four
points in time:

'pre-start'
'post-start'
'pre-stop'
'post-stop'

on pre-start we abort if the script fails
and pre-stop will not be called if the vm crashes or if
the vm gets powered off from inside the guest

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PVE/API2/Qemu.pm
PVE/CLI/qm.pm
PVE/QemuServer.pm

index 984052c908b472436898608b5985480e5d752b7a..22f9f6a56391d3ae04a5b3f0c1e50788c5377f99 100644 (file)
@@ -1117,6 +1117,9 @@ my $update_vm_api  = sub {
            if ($param->{$opt} eq '1') {
                $param->{$opt} = PVE::QemuServer::generate_uuid();
            }
+       } elsif ($opt eq 'hookscript') {
+           eval { PVE::GuestHelpers::check_hookscript($param->{$opt}, $storecfg); };
+           raise_param_exc({ $opt => $@ }) if $@;
        }
     }
 
index f4bacd696121d94e973130ef67f3b2911906575c..cea2549ba56b037adcfd657e05f315b99adadc88 100755 (executable)
@@ -19,6 +19,7 @@ use PVE::INotify;
 use PVE::RPCEnvironment;
 use PVE::Exception qw(raise_param_exc);
 use PVE::Network;
+use PVE::GuestHelpers;
 use PVE::QemuServer;
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::OVF;
@@ -786,6 +787,7 @@ __PACKAGE__->register_method({
                # vm was shutdown from inside the guest or crashed, doing api cleanup
                PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0);
            }
+           PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop');
        });
 
        warn "Finished cleanup for $vmid\n";
index 32f52fe2b0714e94769b6a146ae9baf406a0cc05..4a903a6dd58b5f95b4b00f6385a1779a935bc26e 100644 (file)
@@ -30,6 +30,7 @@ use PVE::ProcFSTools;
 use PVE::QemuConfig;
 use PVE::QMPClient;
 use PVE::RPCEnvironment;
+use PVE::GuestHelpers;
 use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr);
 use PVE::QemuServer::Memory;
 use PVE::QemuServer::USB qw(parse_usb_device);
@@ -614,6 +615,12 @@ EODESCR
        default => "1 (autogenerated)",
        optional => 1,
     },
+    hookscript => {
+       type => 'string',
+       format => 'pve-volume-id',
+       optional => 1,
+       description => "Script that will be executed during various steps in the vms lifetime.",
+    },
 };
 
 my $confdesc_cloudinit = {
@@ -4596,6 +4603,7 @@ my $fast_plug_option = {
     'description' => 1,
     'protection' => 1,
     'vmstatestorage' => 1,
+    'hookscript' => 1,
 };
 
 # hotplug changes in [PENDING]
@@ -5114,6 +5122,8 @@ sub vm_start {
            }
        }
 
+       PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
+
        my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
 
        my $migrate_port = 0;
@@ -5311,6 +5321,7 @@ sub vm_start {
                    property => "guest-stats-polling-interval",
                    value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
 
+       PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
     });
 }
 
@@ -5483,6 +5494,7 @@ sub vm_stop {
                my $opts = PVE::JSONSchema::pve_parse_startup_order($conf->{startup});
                $timeout = $opts->{down} if $opts->{down};
            }
+           PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-stop');
        }
 
        $timeout = 60 if !defined($timeout);