Look at virt-sysprep from the guestfs-tools
package.
virt-sysprep
is a bash script to reset/unconfigure/sysprep a VM so clones can be made safely.
From the manual:
Virt-sysprep can reset or unconfigure a virtual machine so that clones can be made from it. Steps in this process include removing SSH host keys, removing persistent network MAC configuration, and removing user accounts. Virt-sysprep can also customize a virtual machine, for instance by adding SSH keys, users or logos. Each step can be enabled or disabled as required.
"Sysprep" stands for "system preparation" tool. The name comes from the Microsoft program sysprep.exe which is used to unconfigure Windows machines in preparation for cloning them. Having said that, virt-sysprep does not currently work on Microsoft Windows guests. We plan to support Windows sysprepping in a future version, and we already have code to do it.
Of course, make sure to read the warning:
Using "virt-sysprep" on live virtual machines, or concurrently with other disk editing tools, can be dangerous, potentially causing disk corruption. The virtual machine must be shut down before you use this command, and disk images must not be edited concurrently.
Ultimately your command would look something like this (where disk.img
is a disk image e.g. qcow2
):
virt-sysprep --add disk.img
sys-unconfig
?