Umount drive in hookscript post-stop

freeDom-

New Member
Apr 2, 2025
7
1
3
Hi,

I have a simple hookscript to make sure that required mount points for containers are mounted in the host at startup. Mounting is working as expected, but umounting in the post-stop phase does not work. The post-stop trigger is executed (I tested by creating a tmp file in the post-stop phase), but it seems that the resources are not freed yet by the guest (see https://forum.proxmox.com/threads/hookscript-wird-nicht-genutzt.151222/ ).

What can I do to umount the drives properly after ctx shutdown if the post-stop phase hook cannot be used? Would this not be the correct place to do this?

Code:
#!/usr/bin/perl

# Exmple hook script for PVE guests (hookscript config option)
# You can set this via pct/qm with
# pct set <vmid> -hookscript <volume-id>
# qm set <vmid> -hookscript <volume-id>
# where <volume-id> has to be an executable file in the snippets folder
# of any storage with directories e.g.:
# qm set 100 -hookscript local:snippets/hookscript.pl

use strict;
use warnings;

print "GUEST HOOK: " . join(' ', @ARGV). "\n";

# First argument is the vmid

my $vmid = shift;

# Second argument is the phase

my $phase = shift;

if ($phase eq 'pre-start') {

    # First phase 'pre-start' will be executed before the guest
    # is started. Exiting with a code != 0 will abort the start

    print "$vmid is starting, doing preparations.\n";
    system("mount /mnt/lxc/$vmid/*");

    # print "preparations failed, aborting."
    # exit(1);

} elsif ($phase eq 'post-start') {

    # Second phase 'post-start' will be executed after the guest
    # successfully started.

    print "$vmid started successfully.\n";

} elsif ($phase eq 'pre-stop') {

    # Third phase 'pre-stop' will be executed before stopping the guest
    # via the API. Will not be executed if the guest is stopped from
    # within e.g., with a 'poweroff'

    print "$vmid will be stopped.\n";

} elsif ($phase eq 'post-stop') {

    # Last phase 'post-stop' will be executed after the guest stopped.
    # This should even be executed in case the guest crashes or stopped
    # unexpectedly.

    print "$vmid stopped. Doing cleanup.\n";
    # Currently not working, because passthrough devices are only released after post-stop
    #system("umount /mnt/lxc/$vmid/*");

} else {
    die "got unknown phase '$phase'\n";
}

exit(0);

Here is my /etc/fstab (removed ip addresses and mount points)
Code:
...
# lxc file shares
//ipaddr/mountpoint1 /mnt/lxc/178/mountpoint1 cifs nofail,credentials=/root/.smbcredentials_1,uid=100000,gid=100000 0 0
//ipaddr/mountpoint2 /mnt/lxc/178/mountpoint2 cifs nofail,credentials=/root/.smbcredentials_1,uid=100000,gid=100000 0 0
//ipaddr/mountpoint3 /mnt/lxc/178/mountpoint3 cifs nofail,credentials=/root/.smbcredentials_1,uid=100000,gid=100000 0 0
//ipaddr/mountpoint4 /mnt/lxc/179/mountpoint4 cifs nofail,credentials=/root/.smbcredentials_1,uid=100000,gid=100000 0 0
//ipaddr/mountpoint5 /mnt/lxc/176/mountpoint5 cifs nofail,credentials=/root/.smbcredentials_2,uid=100000,gid=100000 0 0

Here is my log output from journalctl -r
Code:
Apr 02 13:51:37 hypercat systemd[1]: pve-container@179.service: Deactivated successfully.
Apr 02 13:51:37 hypercat pct[303863]: <root@pam> end task UPID:hypercat:0004A2F9:00453686:67ED24C1:vzshutdown:179:root@>
Apr 02 13:51:37 hypercat kernel: EXT4-fs (dm-21): unmounting filesystem 0ed234dc-aff3-4891-90f3-f8feafef1d33.
Apr 02 13:51:36 hypercat pvestatd[1248]: unable to get PID for CT 179 (not running?)
Apr 02 13:51:36 hypercat kernel: audit: type=1400 audit(1743594696.243:77): apparmor="STATUS" operation="profile_remove>
Apr 02 13:51:36 hypercat audit[304043]: AVC apparmor="STATUS" operation="profile_remove" profile="/usr/bin/lxc-start" n>
Apr 02 13:51:29 hypercat pct[303865]: shutdown CT 179: UPID:hypercat:0004A2F9:00453686:67ED24C1:vzshutdown:179:root@pam:
Apr 02 13:51:29 hypercat pct[303863]: <root@pam> starting task UPID:hypercat:0004A2F9:00453686:67ED24C1:vzshutdown:179:>
Apr 02 13:51:16 hypercat pct[303367]: <root@pam> end task UPID:hypercat:0004A108:004530A7:67ED24B2:vzstart:179:root@pam>
Apr 02 13:51:15 hypercat kernel: audit: type=1400 audit(1743594675.617:76): apparmor="STATUS" operation="profile_load" >
Apr 02 13:51:15 hypercat audit[303398]: AVC apparmor="STATUS" operation="profile_load" profile="/usr/bin/lxc-start" nam>
Apr 02 13:51:15 hypercat kernel: EXT4-fs (dm-21): mounted filesystem 0ed234dc-aff3-4891-90f3-f8feafef1d33 r/w with orde>
Apr 02 13:51:14 hypercat systemd[1]: Started pve-container@179.service - PVE LXC Container: 179.
Apr 02 13:51:14 hypercat kernel: CIFS: Attempting to mount //ipaddr/mountpoint4
Apr 02 13:51:14 hypercat kernel: CIFS: enabling forcegid mount option implicitly because gid= option is specified
Apr 02 13:51:14 hypercat kernel: CIFS: enabling forceuid mount option implicitly because uid= option is specified
Apr 02 13:51:14 hypercat pct[303368]: starting CT 179: UPID:hypercat:0004A108:004530A7:67ED24B2:vzstart:179:root@pam:
Apr 02 13:51:14 hypercat pct[303367]: <root@pam> starting task UPID:hypercat:0004A108:004530A7:67ED24B2:vzstart:179:root@pam:
 
Last edited:
Check the comment above.
# Currently not working, because passthrough devices are only released after post-stop
This line is commented intentionally, because it is not working. The device is not yet released in the post-stop phase and thus it cannot be unmounted.
 
If you're passing a pci disk device through, shouldn't the guest take care of the mounting/unmounting?
 
I use a Bash script instead of Perl and run commands in the post-stop in parallel with &. In my case the qm automatically waits a little when the VM is still locked and executes fine.
Bash:
#!/bin/bash
if [ "$2" == "pre-start" ]
then
    echo "VM $1 will self-destruct after shutdown!"
elif [ "$2" == "post-stop" ]
then
    nohup /usr/sbin/qm destroy "$1" &>/dev/null &
fi
 
I use a Bash script instead of Perl and run commands in the post-stop in parallel with &. In my case the qm automatically waits a little when the VM is still locked and executes fine.
Bash:
#!/bin/bash
if [ "$2" == "pre-start" ]
then
    echo "VM $1 will self-destruct after shutdown!"
elif [ "$2" == "post-stop" ]
then
    nohup /usr/sbin/qm destroy "$1" &>/dev/null &
fi

Calling a different script (and using nohup) might be the way. Thanks, I try will try if it works for me when I find the time!