We sometimes need to execute a script on our host when starting or stopping a VM.
I was using another solution not elegant that broke after a proxmox update.
And after researching the proxmox documentation a supported solution is available.
We first need to edit our
/etc/pve/storage.cfg adding the snippets directory to the host as below :
dir: local path /var/lib/vz content backup,vztmpl,iso,snippets # Entry added manually for the ZFS local storage after the migration zfspool: local-zfs pool rpool/data sparse content images,rootdir lvmthin: HV2-FW2 thinpool HV2-FW2 vgname HV2-FW2 content images,rootdir nodes hv2
After refreshing our proxmox GUI the folder snippets will be created according to our
/etc/pve/storage.cfg according to the output below :
root@hv2:/var/lib/vz/snippets# ll /var/lib/vz/ total 26 drwxr-xr-x 2 root root 4 Aug 26 2020 dump drwxr-xr-x 2 root root 2 Dec 3 2019 images drwxr-xr-x 2 root root 3 Jul 1 13:35 snippets drwxr-xr-x 5 root root 5 Jan 9 2020 template
Copy the example script to the snippets folder with the command below :
root@hv2:~# cp /usr/share/pve-docs/examples/guest-example-hookscript.pl /var/lib/vz/snippets/network.pl
We could write a custom perl script, but unfortunately, I am not familiar with perl and the example script is detailed enough to be used.
The documentation is not clear if a bash script is supported. I will eventually test and update the article if it works.
The script is executed in 4 stages of the VM life ( pre-start, post-start, pre-stop, post-stop ) and we are interested in the post-start stage since the script to be executed in the host will configure the VM network.
We will copy the example script to the recently created folder
/var/lib/vz/snippets/network.pl and change the post-start block to execute the desired bash script as seen below :
I have added the line :
system("/bin/bash /root/network/config/hv2_net_vps.sh $vmid")
I am using the function
system() to invoke a bash script passing the VMID as an argument. You can adapt the example to your needs.
It worth mention that I could not see the pre-stop or post-stop be executed when the VM was shut down from within its OS. Although stated on the example that post-stop should be executed when the VM is stopped from the OS or crashed.
The last step is to execute the command below to activate the hookscript :
qm set 101 --hookscript local:snippets/network.pl
101 - is your VM_ID.
We now can see the script being executed when the VM starts.