Modifying xml definition of a running KVM domain via script

LinuxGuru-Wannabe asked:

How can I modify the xml definition of a VM over ssh?
I know that I can do this interactively by logging into the host and using:

virsh edit <domain name>

But I want to automate this modification of xml using a script that runs on a remote host and then executes commands over ssh on the KVM box.

From what I have observed is that virsh edit is opening the default editor (vi in my case) for me to edit the file. I want to somehow overwrite this file (which is interactively opened by virsh edit command) with vm definition contained in another text file.

I have found threads on how text can be piped to vi using:

cat <file name> | vi -

But that wont work in my case since,

  1. virsh is opening up default editor with arguments that I have no control over. (or at least I don’t know how to modify those arguments)
  2. The default editor could be different on different KVM boxes.

Thanks in advance for helping out.

Edit: Just wanted to make clear that I do not have an option to power off the VM and redefine it. I want to modify a running domain.

My answer:

Use virsh dumpxml to get the XML, and then virsh define to replace the domain definition with your modifications.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.