Cron jobs not working with virsh commands

Recently, while testing backup scripts, I happened to notice that 'virsh snaphot-create-as . . .' has been failing with 'error: failed to get domain '$DOMAIN'' when run through crontab. So I've been unwittingly backing up the live, running, QCOW2 images for quite some time because scripts which run fine when testing from CLI, fail to work as expected when run as a cron job. After much searching, I stumbled upon a related issue with the (obvious?) simple solution almost hidden in the answers. You have to explicitly connect to the local server with, e.g.: virsh -c qemu:///system snapshot-create-as . . . Humble apologies if I missed something I should have been aware of, but I just thought it worthwhile mentioning here for anyone else searching for an answer in the archives. Best regards, Jeff

On Fri, May 26, 2023 at 12:30:18PM +0200, Jeff Brown wrote:
Recently, while testing backup scripts, I happened to notice that 'virsh snaphot-create-as . . .' has been failing with 'error: failed to get domain '$DOMAIN'' when run through crontab. So I've been unwittingly backing up the live, running, QCOW2 images for quite some time because scripts which run fine when testing from CLI, fail to work as expected when run as a cron job.
After much searching, I stumbled upon a related issue with the (obvious?) simple solution almost hidden in the answers. You have to explicitly connect to the local server with, e.g.:
virsh -c qemu:///system snapshot-create-as . . .
Humble apologies if I missed something I should have been aware of, but I just thought it worthwhile mentioning here for anyone else searching for an answer in the archives.
If you don't specify a URI, then libvirt attempts to guess one based on what's installed, what's running and what user you're running the command as. Assuming libvirt qemu driver is installed & running, the latter point will be where you hit the problem Running as 'root' libvirt defaults to qemu:///system. Running as any non-root account, it defaults to qemu:///session Some more info here: https://libvirt.org/uri.html#default-uri-choice https://libvirt.org/drvqemu.html#connections-to-qemu-driver With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (2)
-
Daniel P. Berrangé
-
Jeff Brown