]> git.proxmox.com Git - pve-docs.git/blob - examples/guest-example-hookscript.pl
fixup: s/devies/devices/
[pve-docs.git] / examples / guest-example-hookscript.pl
1 #!/usr/bin/perl
2
3 # Exmple hook script for PVE guests (hookscript config option)
4 # You can set this via pct/qm with
5 # pct set <vmid> -hookscript <volume-id>
6 # qm set <vmid> -hookscript <volume-id>
7 # where <volume-id> has to be an executable file in the snippets folder
8 # of any storage with directories e.g.:
9 # qm set 100 -hookscript local:snippets/hookscript.pl
10
11 use strict;
12 use warnings;
13
14 print "GUEST HOOK: " . join(' ', @ARGV). "\n";
15
16 # First argument is the vmid
17
18 my $vmid = shift;
19
20 # Second argument is the phase
21
22 my $phase = shift;
23
24 if ($phase eq 'pre-start') {
25
26 # First phase 'pre-start' will be executed before the guest
27 # ist started. Exiting with a code != 0 will abort the start
28
29 print "$vmid is starting, doing preparations.\n";
30
31 # print "preparations failed, aborting."
32 # exit(1);
33
34 } elsif ($phase eq 'post-start') {
35
36 # Second phase 'post-start' will be executed after the guest
37 # successfully started.
38
39 print "$vmid started successfully.\n";
40
41 } elsif ($phase eq 'pre-stop') {
42
43 # Third phase 'pre-stop' will be executed before stopping the guest
44 # via the API. Will not be executed if the guest is stopped from
45 # within e.g., with a 'poweroff'
46
47 print "$vmid will be stopped.\n";
48
49 } elsif ($phase eq 'post-stop') {
50
51 # Last phase 'post-stop' will be executed after the guest stopped.
52 # This should even be executed in case the guest crashes or stopped
53 # unexpectedly.
54
55 print "$vmid stopped. Doing cleanup.\n";
56
57 } else {
58 die "got unknown phase '$phase'\n";
59 }
60
61 exit(0);