Snippets hookscript can not change CT net config

vaka

New Member
Dec 21, 2022
3
0
1
Hi all,

I have a two node cluster PVE 7.3-4 on different data centers with different public networks.

LXC container running on node1 and replicated to node2.

I need to change container's network settings (MAC addres, ip-addres and gw) when it migrated on node2 and change it back when it migrated back on node1.
So I created hook-scripts at snippets on both nodes that change container's network settings on pre-start phase.

Script:
Bash:
#!/bin/bash

ID=$1
PHASE=$2

echo "CT HOOK: $ID, PHASE: $PHASE"

case $PHASE in
  pre-start)
    if [[ "x$ID" == "x105" ]]; then
      echo pct set $ID -net0 name=eth0,bridge=vmbr0,firewall=0,gw=1.2.3.1,hwaddr=00:00:00:01:02:03,ip=1.2.3.115/24
      sudo /usr/sbin/pct set $ID -net0 name=eth0,bridge=vmbr0,firewall=0,gw=1.2.3.1,hwaddr=00:00:00:01:02:03,ip=1.2.3.115/24
    fi
    ;;
  *)
    exit 0
    ;;
esac

User "www-data" can sudo /usr/sbin/pct set *
Script works well when I run it by hand from user "www-data" but throw an error when it called by proxmox.

Output:
Code:
GUEST HOOK: 105, PHASE: pre-start
pct set 105 -net0 name=eth0,bridge=vmbr1,firewall=0,gw=1.2.3.1,hwaddr=00:00:00:01:02:03,ip=1.2.3.115/24
trying to acquire lock...
can't lock file '/run/lock/lxc/pve-config-105.lock' - got timeout
TASK ERROR: hookscript error for 105 on pre-start: command '/var/lib/vz/snippets/pct105-net0 105 pre-start' failed: exit code 4

Have somebody any idea how to automate changing network settings on container start?
 
Last edited:
the pre-start hook is called in a locked context, so it cannot do actions that in turn require obtaining the lock..
 
post-start hook also called in a locked context :oops:

So there is no way to automate lxc net config changing.
 
Last edited:
Finally I done it in a hack way. But it works.
I used "post-start" phase and simply change /etc/network/interfaces inside container.

Bash:
#!/bin/bash

ID=$1
PHASE=$2
IMAGE="/rpool/subvol-${ID}-disk-0"

echo "GUEST HOOK: $ID, PHASE: $PHASE"

case $PHASE in
  post-start)
    if [[ "x$ID" == "x105" ]]; then
    cat <<EOF > $IMAGE/etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address 1.2.3.105/24
        gateway 1.2.3.1
        hwaddress ether 00:01:02:03:04:05
EOF
    fi
    ;;
  *)
    exit 0
    ;;
esac
 
Last edited:
the pre-start hook is called in a locked context, so it cannot do actions that in turn require obtaining the lock..
Do we have any "alternative" to this behaviour? a simple "remove a passthrough drive if not connected" can not be done using hookscripts at start time :(
 
Do we have any "alternative" to this behaviour? a simple "remove a passthrough drive if not connected" can not be done using hookscripts at start time :(
Ok, found a way. I know needs to be done carefully but if you put a
nohup ./script.sh > /dev/null 2>&1 &

code inside script.sh does not have locked context and just gets executed.

Thank you,
 

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!