Hookscripts: Trigger on reboot

NLay

New Member
Aug 12, 2021
8
0
1
44
Good day,

I have been struggling to find the appropriate phase name to perform in a hookscript when a VM is rebooted.

I noticed from my search that two phases named "pre-restart" and "post-restart" exist, but when I included them in Perl hookscript (it is basically the included example plus the conditionals for both phase names. However, neither phase seems to be triggerd.

Some backup info: I am using PVE 7.0-11. The VM is Ubuntu 20.04. I don't know if this matters, but I tried with both guest agent disabled (default) and enabled (I installed the guest agent on Ubuntu).

If pre-restart doesn't exist anymore. Is there a way to execute a script between VM reboots ?

Thanks,
 
Hello, in case I have proxmox 6.4 and the scripts are launched with crontab, both in the VM and in proxmox, all the VM are LXC, what are you trying to do? a backup?
 
Hello, in case I have proxmox 6.4 and the scripts are launched with crontab, both in the VM and in proxmox, all the VM are LXC, what are you trying to do? a backup?
Thank you @TECNI.sistemas. I have to run a script to reset the GPU on the host. If I don't do that, the VM wouldn't boot properly.

Could you give me an example of how the scripts are launched with crontab ? Any pointers would be appreciated.

EDIT#1: Just to clarify, I am looking for a way to be able to detect (on the host) when the VM is restarted from within the VM not to be able to restart it from the host on a regular schedule.
 
Last edited:
Hi,
This our crontab, edit with nano or vi, :
nano /etc/crontab
1631413448602.png
This crontab executes 2 script every day, at 2AM it executes the backup.sh script and at 4AM the maintenimiento.sh script.
in your case you could run a script that verifies if the VM is running, for VM you use qm for LXC you use pct,
qm status VM_ID
in my case I have a VM the 108,
qm status 108

1631414617227.png
to make the query useful, you can run a validator and with grep count the occurrences, for example:
mkdir /scripts
echo "command lines to restart GPU" > restartGPU.sh
chmod +x restartGPU.sh

[ ! `qm status 108 | grep -c "stopped"` = 0 ] && echo "The VM is Stopped, run restart GPU";/scripts/restartGPU.sh


If that works for you, you can put everything in the script and add the line in the crontab, so that it checks every time you want, it can be every minute, hour or day
 
Hi,
This our crontab, edit with nano or vi, :
nano /etc/crontab
View attachment 29373
This crontab executes 2 script every day, at 2AM it executes the backup.sh script and at 4AM the maintenimiento.sh script.
in your case you could run a script that verifies if the VM is running, for VM you use qm for LXC you use pct,
qm status VM_ID
in my case I have a VM the 108,
qm status 108

View attachment 29374
to make the query useful, you can run a validator and with grep count the occurrences, for example:
mkdir /scripts
echo "command lines to restart GPU" > restartGPU.sh
chmod +x restartGPU.sh

[ ! `qm status 108 | grep -c "stopped"` = 0 ] && echo "The VM is Stopped, run restart GPU";/scripts/restartGPU.sh


If that works for you, you can put everything in the script and add the line in the crontab, so that it checks every time you want, it can be every minute, hour or day
That sounded like a great idea. As a proof of concept, I created a shell script loop to run the qm status command every second to check the output while I rebooted the vm. Unfortunately, the status was always "running" and I couldn't find any "stopped" status.

That's weird! I cannot really explain the behavior. @TECNI.sistemas I hope you are not running out of ideas just yet :)

Thanks for your help,
 
Hello, it is easier to see it if you turn off the VM, because the reboot does it very fast. What should the command to restart the GPU look like? How do you do it?
if you look with qm or pct you can turn off, turn on, execute commands without having to do it inside the LXC or VM ... and in the case of crontab, to run a script every minute, which is the minimum, you can do it this way

*/1 * * * * root /scripts/restartGPU.sh

 
just fyi: if you reboot the vm from within, the qemu process never gets stopped, so the host does not see the vm restarted
 
@TECNI.sistemas , I ran that bash script every second and I failed to notice the status change (if it happened). Do you think it would be reliable to depend on a crontab every minute to capture the reboot ?

That is the case, exactly @dcsapak. Is there a way to detect a VM reboot on the host, if it is made from within the VM ?
 
That is the case, exactly @dcsapak. Is there a way to detect a VM reboot on the host, if it is made from within the VM ?
no not currently. we'd have to start the vm with '--no-reboot' and do the reboot manually, but this would be big behavioural change since e.g. pending changes would be applied (which they are not at the moment if you reboot from inside the vm)
 
Hello, we use crontab and to make backups of our LXC containers, we do not use any agent, with LXC accessing the file system is easy with mounting the disk "pve-vm ---- disk - 0" from the host, I did a test scenario with a VM to test the qemu commands,

1631596568285.png
1631596342530.png
Force shutdown, In my case I had to physically turn off my test PC.
And in the host

1631597072071.png

I am trying to understand what you did, and I realize that you speak of restarting the VM, in that case you could put a shell script in /etc/init.d/ this script would be executed every time the VM is restarted, if that you already did and you don't need any of the above

In another test I tried with a ping to the VM, and in a very quick moment the message "QEMU guest agent is not running" appears
could be another option

1631599730732.png
 

Attachments

  • 1631590815602.png
    1631590815602.png
    168.1 KB · Views: 1
  • 1631596959697.png
    1631596959697.png
    860.4 KB · Views: 1
  • 1631599718347.png
    1631599718347.png
    4.9 KB · Views: 1
Wow @TECNI.sistemas! Well done.

I like the ping approach. I will give that a try and see how it goes.

A quick question though, when you said to put a script inside /etc/init.d you meant inside the VM, right ?
 
Hi @NLay
Yes, the idea is to run it in the VM, I was trying the /etc/init.d thing and I didn't get it to work for me, apparently in the new Debian versions it doesn't work, I also tried @reboot in the crontab, but, it isn't supported in Debian, so I used systemd to create a service and in this way it was possible to launch a message before restarting and at startup time, I think this should solve what you need, I'm glad that in passing I learned something that already was deprecated
1631682142192.png

Result after reboot
1631682322311.png
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!