virt-manager launcher action to directly start and open a specific VM

I use virt-manager for my Qemu/KVM virtual machines. Booting a VM currently means launching virt-manager, selecting a VM to open it in the VM viewer and then finally turning the VM on.

I would like to add a launcher context menu option for it that directly opens the VM viewer window and launches the VM.

Copying and editing the virt-manager.desktop file to add the context menu action is no problem, but I don't know a suitable command to make virt-manager behave as I desire. How can I achieve this?


Solution 1:

Use the virt-manager to find the "Libvirt URI" (select the VM, Edit -> Connection Details), then the name of the VM (select the VM, Edit -> Virtual Machine Details - if you have the machine open, you will have to make sure it is not maximized, then View -> Details, and go back to the Console when you're done.)

Then use (don't type the leading $)

$ virt-manager --connect <<URI>> --show-domain-console <<NAME>>

Where URI and name are where you got them from above. For example, mine looks like:

$ virt-manager --connect qemu:///system --show-domain-console win7

Run that from the command line. If that works, you should be able to put it in a simple script

$ cat > ~/bin/run-vm
#!/bin/bash
virt-manager --connect qemu:///system --show-domain-console win7
^D

(Where ^D is control + D).

$ chmod u+x ~/bin/run-vm

And, then you should be able to add this command to your menu - see for example Answer on Unix and Linux Stackoverflow site.

You can also add the command directly to the menu with the parameters, but often it is easier for debugging problems to get the command running in the command line as a script.

Solution 2:

I wrote a small script "~/scripts/StartVirtDomain.sh"

#!/bin/sh
# call this script with domainname as parameter 
# to start domain and open viewer

/usr/bin/virsh start $1         # domain must be known to virsh
/usr/bin/virt-viewer -w $1      # -w to wait until domain is running. 

virt-viewer should be installed with virt-manager; if not, sudo apt-get install virt-viewer.

You may call it with the name of the domain as parameter. It will start the domain (if not already started), then start virt-viewer to connect with this domain.

A sample desktop file (W7Pro.desktop)

[Desktop Entry]
Version=1.0
Type=Application
Name=W7Pro
Comment=Start Windows VM 
Exec=~/scripts/StartVirtDomain.sh Win7Pro
Icon=
Path=~/scripts
Terminal=false
StartupNotify=false

did the job (domain name is Win7Pro). There is no error handling in the script; you may add some if necessary. Especially, if the domain is not known to virsh or fails to start, virt-viewer will probably wait forever.

Edit:

If you really need the console window of virt-manager, just call it like

virt-manager --connect=<URI> --show-domain-console <domain>

e.g. in my script this would be

/usr/bin/virt-manager --connect=qemu:///system --show-domain-console $1

The connection URI may be found in the virt-manager main window by selecting the respective connection (e.g. QEMU/KVM) and then using "Details" in the context menu.

You may even delete the line calling virsh, as the domain may be started from the virt-manager console.