-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone VM #148
Comments
Hi @x09 thank you for putting in this suggestion, much appreciated! I can see how a clone function could be useful. I expect that it would have to update MAC addresses and create new images, etc. Perhaps have to see how existing libvirt manages this, for example, selection of storage location, etc. If you need this kind of functionality right now, I wonder if you could dump the XML from the virtual machine, modify it and define a new machine? Definitely a few more steps, but might be something to consider if it's useful for you. Another option might be to use a Thanks again for the suggestion, we can continue to discuss any thoughts on this issue. |
Since it is not currently possible to properly clone a VM directly from ansible (or even from a single libvirt command), I have implemented a simple bash command-line wrapper to perform this operation and work around the quirks. You can find it here (L361-492). The quirks are:
You can try it like this: $ wget https://raw.githubusercontent.com/nodiscc/xsrv/master/xsrv
$ chmod a+x xsrv
$ ./xsrv init-vm --help
USAGE: ./xsrv init-vm --name VM_NAME [--template debian11-base] --ip VM_IP --netmask VM_NETMASK --gateway VM_GATEWAY [--ssh-port VM_SSH_PORT] [--sudo-user deploy] [--sudo-password VM_SUDO_PASSWORD] --ssh-pubkey 'ssh-rsa AAAAB...' [--root-password VM_ROOT_PASSWORD] [--disk-path /path/to/my.CHANGEME.org.qcow2] [--memory 1024] [--vcpus NUM_CPU]
EXAMPLE: ./xsrv init-vm --template debian11-base --name my.CHANGEME.org --ip 10.0.0.223 --netmask 24 --gateway 10.0.0.1 --sudo-user deploy --sudo-password CHANGEME --ssh-pubkey 'ssh-rsa AAAAB...' --root-password CHANGEME --memory 3G --vcpus 4 [--dump]
Initialize a libvirt VM from a template, configure resources/users/SSH access, and start the VM.
Requirements: openssh-client sshpass libvirt virtinst libvirt-daemon-system libguestfs-tools pwgen netcat-openbsd util-linux
--template name of the template to create the new VM from (default debian11-base)
--name REQUIRED name of the VM to create
--ip REQUIRED IP address of the VM
--gateway REQUIRED default network gateway of the VM
--netmask network mask of the VM (CIDR notation, default 24)
--root-password root account password (default generate and display a random password)
--sudo-user administrative (sudoer) user account (default deploy)
--sudo-password password for the administrative (sudoer) user account (default generate and display a random password)
--ssh-pubkey REQUIRED public key to authorize on the administrative (sudoer) account
--disk-path path to the qcow2 disk image to create (default: /var/lib/libvirt/images/VM_NAME.qcow2)
--memory VM memory with M or G suffix (default 1G)
--vcpus number of vCPUs (default: same value as the template)
--dump display the VM XML definition after creation, for use with the nodiscc.xsrv.libvirt role I would also be interested in having this functionality directly available from the libvirt collection/module. I imagine this would take the form of a |
I also have a mechanism for cloning VMs in my own fork of this repo (I've never found the time to write testcases for it, hence why no PR yet) - https://github.com/dseeley/community.libvirt/tree/develop:
I use it in my cluster management software (https://github.com/dseeley/clusterverse), the relevant playbook of which is: |
Hello @x09, Hello All, To do that I've developed an Ansible Role that enables you to specify the VMs you need and the template to use as a YAML dictionary. Then the role uses this dictionary to perform the following tasks:
You find this role at:
Feel free to give it a try. I use it for some time now and would be happy if other folks find it useful, too. Any feedback is appreciated. Best regards, |
Thanks for the suggestion @Tronde and apologies for the delayed response. I think it would be good to include some example playbooks on how to define a machine, one that uses XML template and one that uses I say example playbooks because I'm not sure how possible it would be to include a universal playbook at this stage because it needs to be both flexible and robust, which I don't think is a particularly trivial undertaking (that's is why in my own role I've wrapped Please feel free to create a merge request to add a generic playbook based off your work and I'll review and test it! I think it would be good to use as cut down an XML as possible and let Please let me know how you go and reach out if you need a hand. Cheers! |
No worries @csmart and thanks a lot for your feedback. The role may improve over time as I use it in my lab environments. I'll show it again when it has advanced. Cheers! |
Hey @Tronde, what I meant to say was that I think your role as it is would be great to add as an example playbook on how to use the collection to define a VM using an XML template. I didn't mean to suggest you need to go and make it more awesome before we can merge it in, so if you're happy to create a MR to get it in as an example for now, feel free. We can always work to improve it over time, but I'll leave it up to you. Cheers! |
Well, I guess I misunderstood you then. Let me give the role one more tweak and I'll send a merge regquest. :-) |
Hey, I also need this feature. |
Hi @jurelou, You are welcome to fork it and/or send a merge request to community.libvirt as well. :-) |
SUMMARY
Clone VM functionality
ISSUE TYPE
I have few VM as templates (Server OS, Desktop OS, etc).
if necessary, I clone the original (source) VM and work in its replica (destination).
If I need to create a test bench from 1 server and 5 desktop machines, 2 source (as template) VMs are enough for me.
I just clone from them.
I want work with it via ansible, but have not.
COMPONENT NAME
(https://docs.ansible.com/ansible/latest/collections/community/libvirt/virt_module.html)
ADDITIONAL INFORMATION
I suggest add new command, something like
The text was updated successfully, but these errors were encountered: