Run commands on a guest

Matteo Calorio

Renowned Member
Jun 30, 2017
38
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?
 
Sure, thanks!

Bash:
proxmox-ve: 8.0.2 (running kernel: 6.2.16-18-pve)
pve-manager: 8.0.4 (running version: 8.0.4/d258a813cfa6b390)
proxmox-kernel-helper: 8.0.3
pve-kernel-5.19: 7.2-15
proxmox-kernel-6.2.16-18-pve: 6.2.16-18
proxmox-kernel-6.2: 6.2.16-18
pve-kernel-5.19.17-2-pve: 5.19.17-2
ceph-fuse: 16.2.11+ds-2
corosync: 3.1.7-pve3
criu: 3.17.1-2
glusterfs-client: 10.3-5
ifupdown2: 3.2.0-1+pmx4
ksm-control-daemon: 1.4-1
libjs-extjs: 7.0.0-4
libknet1: 1.26-pve1
libproxmox-acme-perl: 1.4.6
libproxmox-backup-qemu0: 1.4.0
libproxmox-rs-perl: 0.3.0
libpve-access-control: 8.0.4
libpve-apiclient-perl: 3.3.1
libpve-common-perl: 8.0.7
libpve-guest-common-perl: 5.0.3
libpve-http-server-perl: 5.0.4
libpve-rs-perl: 0.8.4
libpve-storage-perl: 8.0.2
libspice-server1: 0.15.1-1
lvm2: 2.03.16-2
lxc-pve: 5.0.2-4
lxcfs: 5.0.3-pve3
novnc-pve: 1.4.0-2
proxmox-backup-client: 3.0.3-1
proxmox-backup-file-restore: 3.0.3-1
proxmox-kernel-helper: 8.0.3
proxmox-mail-forward: 0.2.0
proxmox-mini-journalreader: 1.4.0
proxmox-offline-mirror-helper: 0.6.2
proxmox-widget-toolkit: 4.0.9
pve-cluster: 8.0.2
pve-container: 5.0.4
pve-docs: 8.0.5
pve-edk2-firmware: 3.20230228-4
pve-firewall: 5.0.3
pve-firmware: 3.8-3
pve-ha-manager: 4.0.2
pve-i18n: 3.0.7
pve-qemu-kvm: 8.0.2-6
pve-xtermjs: 4.16.0-3
qemu-server: 8.0.7
smartmontools: 7.3-pve1
spiceterm: 3.3.0
swtpm: 0.8.0+pve1
vncterm: 1.8.0
zfsutils-linux: 2.1.13-pve1
 
Just to make sure, that the guest-agent is working as expected:
Run on the node with the vm
socat - /var/run/qemu-server/906.qga
then paste the following: {"execute": "guest-exec", "arguments": {"path": "/bin/bash", "arg": ["-c", "apt update"], "capture-output": true}}
this should output something like {"return": {"pid": 3558}}. This means that the qemu-guest-agent might work as expected. You can go further and paste (with pid from last output) {"execute": "guest-exec-status", "arguments": {"pid": 3558}}, which will return base64 encoded "err-data" and "out-data" of the executed command.

Otherwise: have you considered updating the PVE to a more current version?