pct exec direct into lxc guest

apmuthu

Renowned Member
Feb 26, 2009
871
12
83
Chennai - India & Singapore
github.com
When a container is created, we need to populate the VMID and the NODE name into the LXC guest.
Tried the following in vain:
Perl:
system("pct exec $vmid -- echo $vmid $node > /root/VMID-NODE");
inside the post-start section of the hookscript.

There was a "-c" parameter referred to in this post for pct exec that is no longer available.
See this article.

What is the pct equivalent to lxc-attach and what are issues to lookout for when using the latter?

Examples for pct exec would be useful.

It gets deeper - after creating an LXC instance but before starting it:
Bash:
pct mount 101
# Upload file from host to LXC guest at /var/lib/lxc/101/rootfs/root
# chown 100000:100000 /var/lib/lxc/101/rootfs/root/get_vmid_node.sh
# chmod 755  /var/lib/lxc/101/rootfs/root/get_vmid_node.sh
pct push 101 /root/get_vmid_node.sh /root/get_vmid_node_1.sh
can only push files to a running CT
So we cannot push files into a mounted but not yet running container!
The push would have taken care of file ownerships.

Furthermore, we cannot set the hookscript if the container is mounted (locked) so we unmount it and then set it with:
Bash:
pct unmount 101

pct set 101 --hookscript local:snippets/lxc_hookscript.pl

pct config 101
  arch: i386
  cores: 1
  hookscript: local:snippets/lxc_hookscript.pl
  hostname: sqlamp
  memory: 512
  net0: name=eth0,bridge=vmbr0,firewall=1,gw=192.168.0.1,hwaddr=AA:BB:CC:DD:EE:FF,ip=192.168.0.101/24,type=veth
  ostype: debian
  rootfs: local:101/vm-101-disk-0.raw,size=8G
  swap: 512
  unprivileged: 1
See this post.
 
Last edited:
I believe the redirect is problematic. Please try pct exec $vmid -- bash -c "echo $vmid $node >/root/VMID-NODE". But I don't know how to escape the " within the Perl code.
 
Last edited:
I tried with and without the double quotes and it did not work.
The command works from the console on my PVE 7.2 but I don't know how to run the command in Perl. Please note that pct is not on the $PATH and you might need to use /usr/sbin/pct exec $vmid -- bash -c "echo $vmid $node >/root/VMID-NODE" in hookscripts.

EDIT: And another thing: the container might be locked and you might need to run the command a little while after the hookscript finishes. I used sleep and & in my bash-script based post-stop hookscript for destoying VMs after it shuts down.
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
 
Last edited:
  • Like
Reactions: DeamonMV