Hookscript with 'post-stop' when the VM was shutdown from the VM itself

drletai

New Member
Jul 13, 2020
4
0
1
41
Hello,

i was wondering if someone knows the answer...
Is it possible to run a hookscript after stopping the VM, when it was shutdown from itself?
From Proxmox GUI, with the Stop button, it is not a problem, the post-stop event wil be fired, and the script runs.
But with the shutdown button from a Windows VM, qmeventd says that the VM is still running, and no post-stop will be fired.

Has anybody an idea, or patch eventually?

Thanks
 
Ok, that is clear, I was not concrete enough...

Well, I can stop a VM from the Proxmox GUI with a button. After that will be an event fired: post-stop. With that I can run my script.
But when i start a Windows VM, and shut it down from the VM with the start menu, Power off button, then I do not get a post-stop event. In the qmeventd stays the vm running, and my script will not executed.
The question is, how can I execute a script, when I use the start menu from Windows 10 to shut the VM down.

proxmox-ve: 6.2-1 (running kernel: 5.4.44-2-pve)
pve-manager: 6.2-9 (running version: 6.2-9/4d363c5b)
pve-kernel-5.4: 6.2-4
pve-kernel-helper: 6.2-4
pve-kernel-5.4.44-2-pve: 5.4.44-2
ceph-fuse: 12.2.11+dfsg1-2.1+b1
corosync: 3.0.4-pve1
criu: 3.11-3
glusterfs-client: 5.5-3
ifupdown: 0.8.35+pve1
ksm-control-daemon: 1.3-1
libjs-extjs: 6.0.1-10
libknet1: 1.16-pve1
libproxmox-acme-perl: 1.0.4
libpve-access-control: 6.1-2
libpve-apiclient-perl: 3.0-3
libpve-common-perl: 6.1-5
libpve-guest-common-perl: 3.0-11
libpve-http-server-perl: 3.0-6
libpve-storage-perl: 6.2-3
libqb0: 1.0.5-1
libspice-server1: 0.14.2-4~pve6+1
lvm2: 2.03.02-pve4
lxc-pve: 4.0.2-1
lxcfs: 4.0.3-pve3
novnc-pve: 1.1.0-1
proxmox-mini-journalreader: 1.1-1
proxmox-widget-toolkit: 2.2-9
pve-cluster: 6.1-8
pve-container: 3.1-10
pve-docs: 6.2-4
pve-edk2-firmware: 2.20200531-1
pve-firewall: 4.1-2
pve-firmware: 3.1-1
pve-ha-manager: 3.0-9
pve-i18n: 2.1-3
pve-qemu-kvm: 5.0.0-9
pve-xtermjs: 4.3.0-1
qemu-server: 6.2-8
smartmontools: 7.1-pve2
spiceterm: 3.1-1
vncterm: 1.6-1
zfsutils-linux: 0.8.4-pve1
 
I have this issue too.

I have two VMs a) Catalina and b) Windows 10
Both point to a hook script with only a post-stop hook defined

1) When I shutdown Catalina from inside that VM or stop the VM from the proxmox GUI the post-stop hook fires
2) When I shutdown the Windows 10 machine from within the proxmox web GUI, the post-stop hook fires
3) When I shutdown the Windows 10 machine from within Windows 10 the machine seems to shut down as indicated in the web interface, the post-stop hook does not fire


proxmox-ve: 6.2-1 (running kernel: 5.4.44-1-pve)
pve-manager: 6.2-6 (running version: 6.2-6/ee1d7754)
pve-kernel-5.4: 6.2-3
pve-kernel-helper: 6.2-3
pve-kernel-5.4.44-1-pve: 5.4.44-1
ceph-fuse: 12.2.11+dfsg1-2.1+b1
corosync: 3.0.3-pve1
criu: 3.11-3
glusterfs-client: 5.5-3
ifupdown: 0.8.35+pve1
ksm-control-daemon: 1.3-1
libjs-extjs: 6.0.1-10
libknet1: 1.15-pve1
libproxmox-acme-perl: 1.0.4
libpve-access-control: 6.1-1
libpve-apiclient-perl: 3.0-3
libpve-common-perl: 6.1-3
libpve-guest-common-perl: 3.0-10
libpve-http-server-perl: 3.0-5
libpve-storage-perl: 6.1-8
libqb0: 1.0.5-1
libspice-server1: 0.14.2-4~pve6+1
lvm2: 2.03.02-pve4
lxc-pve: 4.0.2-1
lxcfs: 4.0.3-pve2
novnc-pve: 1.1.0-1
proxmox-mini-journalreader: 1.1-1
proxmox-widget-toolkit: 2.2-7
pve-cluster: 6.1-8
pve-container: 3.1-8
pve-docs: 6.2-4
pve-edk2-firmware: 2.20190614-1
pve-firewall: 4.1-2
pve-firmware: 3.1-1
pve-ha-manager: 3.0-9
pve-i18n: 2.1-3
pve-qemu-kvm: 5.0.0-4
pve-xtermjs: 4.3.0-1
qemu-server: 6.2-3
smartmontools: 7.1-pve2
spiceterm: 3.1-1
vncterm: 1.6-1
zfsutils-linux: 0.8.4-pve1[/CODE]
 
Can anyone confirm that they can get a post-stop hook to fire for Windows10 on shtudown from within the VM?
Any ideas on how to fix this?
Is it an error I should report?

Thx
 
The output for journalctl -u qmeventd.service -f

-- Logs begin at Fri 2020-08-14 12:09:37 BST. -- Aug 14 12:09:39 jasper1 systemd[1]: Starting PVE Qemu Event Daemon... Aug 14 12:09:39 jasper1 systemd[1]: Started PVE Qemu Event Daemon. Aug 14 12:57:19 jasper1 qmeventd[7035]: Starting cleanup for 122 Aug 14 12:57:19 jasper1 qmeventd[7035]: vm still running

So it seems like Windows 10 is still running, even though I told it to shutdown within the OS
Does anyone have any ideas on how to troubleshoot this?
 
Is the KVM process of the VM still running after it appears to be shutdown?

You can run the following command in an SSH session to keep an eye on it while you shut down the VM
Code:
watch -n 0.5 'ps auxwf | grep "id <VMID>"'

It will update every half second.
 
Is the KVM process of the VM still running after it appears to be shutdown?

You can run the following command in an SSH session to keep an eye on it while you shut down the VM
Code:
watch -n 0.5 'ps auxwf | grep "id <VMID>"'

It will update every half second.

No, the KVM process disappears from the ps listing when shutting the windows VM down from within Windows

I've attached the output from syslog on shutdown below.

Line 17 and 18
Aug 18 13:33:29 jasper1 qmeventd[4260]: Starting cleanup for 122 Aug 18 13:33:29 jasper1 qmeventd[4260]: vm still running

¯\_(ツ)_/¯ Something is preventing the hook from firing on post-stop even when the KVM process is definitely stopped
 

Attachments

  • syslog.txt
    6.5 KB · Views: 2
Last edited:
The code that would trigger the hook is running, but it is checking first if the VM is actually off. This is where you get the vm still running error.

As far as I can tell so far it checks if the /var/run/qemu-server/<vmid>.pid still exists and a process with the PID in the file is running. For some reason this check concludes that the VM is still running.[/icode]
 
Last edited:
Interesting.

The syslog entries are different for shutting the VM down from within the OS and from the proxmox GUI

Catalina shutting down from Inside the OS

Aug 19 14:18:38 jasper1 qmeventd[3920]: Starting cleanup for 170 Aug 19 14:18:38 jasper1 qmeventd[3920]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:18:38 jasper1 qmeventd[3920]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Shutting Down Proxmox bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:18:38 jasper1 qmeventd[3920]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:18:38 jasper1 qmeventd[3920]: Finished cleanup for 170

stopping catalina from the GUI

Aug 19 14:32:10 jasper1 qmeventd[4023]: Starting cleanup for 170 Aug 19 14:32:10 jasper1 qmeventd[4023]: trying to acquire lock… Aug 19 14:32:10 jasper1 qmeventd[4023]: OK Aug 19 14:32:10 jasper1 qmeventd[4023]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:32:10 jasper1 qmeventd[4023]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Shutting Down Proxmox bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:32:10 jasper1 qmeventd[4023]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft

Windows 10 shutting down from Proxmox GUI

Aug 19 14:14:36 jasper1 qmeventd[3972]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:14:36 jasper1 qmeventd[3972]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Shutting Down Proxmox bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:14:36 jasper1 qmeventd[3972]: bin boot dev etc home lib lib32 lib64 libx32 media mnt opt pool1 proc root rpool run sbin srv sys tmp usr var win-soft Aug 19 14:14:36 jasper1 qmeventd[3972]: Finished cleanup for 122


Windows 10 shutting down from inside the OS

Aug 19 14:35:35 jasper1 qmeventd[3902]: Starting cleanup for 122 Aug 19 14:35:35 jasper1 qmeventd[3902]: vm still running


So something is definitely different with the three cases that fire off a post-stop hook and the final case that doesn't


I also ran:

while kill -s 0 $pid ; do : ; sleep 0.25 ; done && date

While closing down the VM's looking for when the kvm process actually finishes.
Interestingly, it's 3-4 seconds after the 'Finished Cleaning up' message is sent to syslog in all cases
 
Last edited:
I also encountered vm still running recently. I found that it was caused by the pass-through of usb in the configuration file. I removed usb0 and usb1 to trigger the post-stop event normally.

Code:
agent: 1
balloon: 0
bios: ovmf
bootdisk: scsi0
cores: 2
cpu: host
efidisk0: local-lvm:vm-103-disk-1,size=4M
hookscript: local:snippets/hackintosh.sh
hostpci0: 07:00,pcie=1,romfile=patchedGTX1050.rom,x-vga=1
machine: q35
memory: 16384
name: gwin10
net0: virtio=5A:75:69:72:05:19,bridge=vmbr0,firewall=1
numa: 1
ostype: win10
scsi0: local-lvm:vm-103-disk-0,size=100G
scsihw: virtio-scsi-pci
smbios1: uuid=9dfa4298-89b6-4805-9510-54a04e3d8d6c
sockets: 2
usb0: host=1-3,usb3=1
usb1: host=1-4,usb3=1
vmgenid: 11a753d0-e5ee-4ced-aae6-cd0b73ee7bf2
 
I also encountered vm still running recently. I found that it was caused by the pass-through of usb in the configuration file. I removed usb0 and usb1 to trigger the post-stop event normally.

Code:
agent: 1
balloon: 0
bios: ovmf
bootdisk: scsi0
cores: 2
cpu: host
efidisk0: local-lvm:vm-103-disk-1,size=4M
hookscript: local:snippets/hackintosh.sh
hostpci0: 07:00,pcie=1,romfile=patchedGTX1050.rom,x-vga=1
machine: q35
memory: 16384
name: gwin10
net0: virtio=5A:75:69:72:05:19,bridge=vmbr0,firewall=1
numa: 1
ostype: win10
scsi0: local-lvm:vm-103-disk-0,size=100G
scsihw: virtio-scsi-pci
smbios1: uuid=9dfa4298-89b6-4805-9510-54a04e3d8d6c
sockets: 2
usb0: host=1-3,usb3=1
usb1: host=1-4,usb3=1
vmgenid: 11a753d0-e5ee-4ced-aae6-cd0b73ee7bf2


Unfortunately this solution doesn't work for me as I need the usb devices connected to the VM.
 
Depending on what you need to execute on VM shutdown, a systemd timer that polls the status of the VM might be a viable option.

I have a use case for this where a Proxmox host's main function is to run a single VM used as a desktop machine with GPU passthrough. The user of this desktop VM doesn't have to - and really don't want to - know about Proxmox as long as the USB ports are all assigned to the VM, except unfortunately shutting down the VM (obviously in this case from within the guest OS) will not power the host down even when using hookscripts.

The following systemd timer and Python script using the Proxmox API through the proxmoxer library does the trick and powers down the host a few minutes after the desktop VM is shutdown. It also tries to make sure the host is only powered down if the desktop VM was actually started and then shutdown to avoid the host keep going down in a vicious circle eg. if the VM failed to start after boot for some reason and the admin needs to work on the host itself.
  1. Create file /etc/systemd/system/desktop-shutdown.timer
    Code:
    [Unit]Description=Checking if desktop VM is down
    
    [Timer]
    OnBootSec=5min
    OnUnitActiveSec=5min
    Unit=desktop-shutdown.service
    
    [Install]
    WantedBy=timers.target
  2. Create file /etc/systemd/system/desktop-shutdown.service
    Code:
    [Unit]Description=Checking if desktop VM is down
    
    [Service]
    Type=oneshot
    ExecStart=python /var/lib/vz/snippets/desktop-shutdown.py
    
    [Install]
    WantedBy=multi-user.target
  3. Create file /var/lib/vz/snippets/desktop-shutdown.py and replace the hostIP, password and VMID placeholders
    Python:
    import os
    from proxmoxer import ProxmoxAPI
    
    os.system('logger "Starting desktop-shutdown.py"')
    proxmox = ProxmoxAPI("hostIP", user="root@pam", password="password", verify_ssl=False)
    is_vm_running = True if proxmox("nodes/prox/qemu/VMID/status/current").get()['status'] == 'running' else False
    tasks = proxmox("nodes/prox/tasks").get()
    host_last_start = max([ t['starttime'] for t in tasks if t['id'] == '' and t['type'] == 'startall' and t['status'] == 'OK' ])
    vm_last_start = max([ t['starttime'] for t in tasks if t['id'] == 'VMID' and t['type'] == 'qmstart' and t['status'] == 'OK' ])
    has_vm_started_after_boot = vm_last_start > host_last_start
    
    if not has_vm_started_after_boot:
            os.system('logger "Desktop VM has not been started, no action."')
    elif is_vm_running:
            os.system('logger "Desktop VM is still running, no action."')
    else:
            os.system('logger "Desktop VM was shutdown triggering poweroff for Proxmox host now!"')
            os.system('shutdown -h now')
  4. pip install proxmoxer
  5. systemctl enable desktop-shutdown.timer
Let me know if you found this useful.
 
I have a PVE7 and PVE8, seems like this is fixed in PVE8. I was able to shutdown /s /t 0 in Windows 10 and it lead to the hookscript running.
 

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!