lxc.hook.pre-start script for mounting drives on host

Patrik Stenfeldt

New Member
Jan 6, 2019
17
0
1
50
I have setup a container with smbd to host files on my home network.

And i have another container that need access to those files.
The way i want to do that is to cifs mount the share on the host and then use it as a mountpoint in the new container.

Everything works perfectly when i start everything manually.

Now i want everything to be able to start automaticly.
Since the container is started after the host i cant add it in the hosts fstab, so i came up with the idea that when container2 starts i just add a lxc.hook.pre-start to execute a script that mounts the share.

The script works when i run it from console on host and if i start the container with the hook, according to logs, the script is executed. But the mount is never mounted.

Are there restrictions to what an lxc hook is allowed to do?
 
I have a LXC container which needs underlying ZFS mounts. These are on an encrypted pool, so I've been struggling to find a way to mount them at the right point.

In short, one of the things I tried was a hook script to unlock the ZFS pool but the hookscript will not get executed, because PVE will attempt (and fail) to mount the ZFS vols before it runs the hook script.

@proxmox - would be nice if we could have a phase pre-mount in the hookscript to enable this sort of thing.
 
I have setup a container with smbd to host files on my home network.

And i have another container that need access to those files.
The way i want to do that is to cifs mount the share on the host and then use it as a mountpoint in the new container.

Everything works perfectly when i start everything manually.

Now i want everything to be able to start automaticly.
Since the container is started after the host i cant add it in the hosts fstab, so i came up with the idea that when container2 starts i just add a lxc.hook.pre-start to execute a script that mounts the share.

The script works when i run it from console on host and if i start the container with the hook, according to logs, the script is executed. But the mount is never mounted.

Are there restrictions to what an lxc hook is allowed to do?
I have the same issue, no info on the internet.
`lxc.hook.pre-start: sh -c "mount -v -t nfs x.x.x.x:/volume2/X /mnt/X"`
If I execute the command in the host alone, it works. But not when using lxc.hook.pre-start

Debug gives a bit of info:
```
DEBUG utils - ../src/lxc/utils.c:run_buffer:560 - Script exec /root/test.sh 105 lxc pre-start produced output: mount.nfs: mount(2): Protocol not supported
```

So the question is, how do I run a custom script to mount a share folder to the host before the container starts ?. I don't want to use the /etc/fstab option
 
Proxmox containers also support hookscripts. You don't have to write it in Perl (like the example); a Bash-script will also work.
Thanks, it worked, but it isnt centralized. If I can write the command directly in the config file then it is nice, since I can back it up and bring everywhere.
Here the script for if other ppl need it:

(your script must be in this path)
nano /var/lib/vz/snippets/100.sh
Bash:
#!/bin/bash
# Proxmox hook script for mounting NFS share

# Arguments: <vmid> <phase>
vmid=$1
phase=$2

case $phase in
  pre-start)
    echo "Mounting NFS share before starting container $vmid"
    mount -t nfs 123.456.789.10:/your/path /mnt/yourpath
    if [ $? -ne 0 ]; then
      echo "Failed to mount NFS share"
      exit 1
    fi
    ;;
  post-stop)
    echo "Unmounting NFS share after stopping container $vmid"
    umount /mnt/yourpath
    if [ $? -ne 0 ]; then
      echo "Failed to unmount NFS share"
      exit 1
    fi
    ;;
  *)
    echo "Invalid phase: $phase"
    exit 1
    ;;
esac

exit 0

chmod +x /var/lib/vz/snippets/100.sh

pct set 100 -hookscript local:snippets/100.sh
 

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!