Run commands on a guest

Matteo Calorio

Renowned Member
Jun 30, 2017
37
0
71
53
Hi everyone, what's the best way to run commands from an host on a guest (Linux) that can also be on a different host?

Something like:
  • qm guest exec 100 --node node01 -- apt update -y, or:
  • pvesh set /nodes/node01/qemu/100/agent/exec -command "apt update"?
M.
 
Hi,

qm can control only vms on the node, it is running on.
I would use pvesh and also use it to look up on which node the vm is running on. Something like this (requires jq installed):
Bash:
TARGET=100
NODE=$(pvesh get /cluster/resources --output-format json | \
        jq -r --argjson TARGETVM $TARGET '.[] | select(.vmid==$TARGETVM) | .node')
pvesh create /nodes/${NODE}/qemu/${TARGET}/agent/exec -command "apt" -command "update"

Aside of that: There are better ways for infrastructure tasks like using Ansible, Foreman or for simplicity Cockpit.
 
Last edited:
Bash:
pvesh create /nodes/${NODE}/qemu/${TARGET}/agent/exec -command "apt" -command "update"

Thanks fba, yes, I'm currently using pvesh in scripts like this:

Bash:
for vmid in 808 811 813 844; do
  nodename=$(pvesh get /cluster/resources --type vm --output-format json | jq -r " .[] | select(.vmid == $vmid) | .node")
  vmname=$(pvesh get /cluster/resources --type vm --output-format json | jq -r " .[] | select(.vmid == $vmid) | .name")
  vmstatus=$(pvesh get /nodes/$nodename/qemu/$vmid/status/current --output-format text --noborder | grep status | tail -1 | awk '{print $2}')
  echo "Virtual Machine $vmid ($vmname) on node $nodename: $vmstatus"
  if [ $vmstatus == "running" ]
  then
    pvesh create /nodes/$nodename/qemu/$vmid/status/shutdown
  fi
done

I tried running the command you suggested—I've already done it—but I still get this error:

Bash:
Wide character in die at /usr/share/perl5/PVE/RESTHandler.pm line 918.
proxy handler failed: Agent error: Guest agent command failed, error was 'Failed to execute child process “ARRAY(0x55f3d5e9d850)” (No such file or directory)'

I will also look into Ansible, which I already use for other things.

Bye and have a good day!
 
Bash:
Wide character in die at /usr/share/perl5/PVE/RESTHandler.pm line 918.
proxy handler failed: Agent error: Guest agent command failed, error was 'Failed to execute child process “ARRAY(0x55f3d5e9d850)” (No such file or directory)'
Which command triggers this error? The ARRAY-Thingy looks like an array reference in perl, but obviously a string is expected there.
 
Unusual error for this command. Looking into the mentioned file on my PVE 8.4.8 (libpve-common-perl 8.3.3), there is nothing on this line and actually even the function it belongs to seems to have nothing to do with the guest-agent.
Would you like to share output ofpveversion -v?