Script for Creating VMs from Template VM in KVM
-
While I'm cutting my teeth with BASH, I figured I'd share this script. I have a VM with two disks I'm using as a template, and this script will create clones based upon it.
https://gitlab.com/EddieJennings/bash-general/-/blob/master/create_vms_from_template.sh
#!/bin/bash # Clone VMs from a template and run virt-sysprep VM=("lab-ansible-web01" "lab-ansible-web02" "lab-ansible-db01" "lab-ansible-db02" "lab-ansible-host01") for i in ${VM[@]} ; do virt-clone --original rhelpractice1minimal --name $i \ --file "/kvm/vm/$i-1.qcow2" --file "/kvm/vm/$i-2.qcow2" #two files because original has two disks # Need to make this a bit more efficient # Need to look about having credentials stored in some kind of file virt-sysprep -d $i --hostname $i --password eddie:password:MYPASSWORD done
-
You are not cloning from a template, but from a full VM.
-
@JaredBusch said in Script for Creating VMs from Template in KVM:
You are not cloning from a template, but from a full VM.
A full VM acting as a template for other VMs, but yes, not a template as you'd see in vCenter or the like.
-
Not the exactly the same thing but you might want to look into how to create a VM from scratch.
Meaning a script that will set up a VM with vCPU, memory, storage, network etc and then boot it from iso and have it do an unattended install, create what users you want and install the packages you need.I think the days of the "golden image" are kind of over.
Even if you wanted to use a golden image you could at least have it built unattended. -
@Pete-S said in Script for Creating VMs from Template VM in KVM:
Not the exactly the same thing but you might want to look into how to create a VM from scratch.
Meaning a script that will set up a VM with vCPU, memory, storage, network etc and then boot it from iso and have it do an unattended install, create what users you want and install the packages you need.That's one of the next things I'm looking into.
-
@EddieJennings said in Script for Creating VMs from Template VM in KVM:
@Pete-S said in Script for Creating VMs from Template VM in KVM:
Not the exactly the same thing but you might want to look into how to create a VM from scratch.
Meaning a script that will set up a VM with vCPU, memory, storage, network etc and then boot it from iso and have it do an unattended install, create what users you want and install the packages you need.That's one of the next things I'm looking into.
@EddieJennings Also remember about things like kickstart in RedHat based operating systems. In Fedora/CentOS/RHOS you can use a kickstart file to automatically select all the install time options for the OS. A short time later you've got a fresh server and all the time it took you to setup was running the creation script on your hypervisor.
-
@EddieJennings using
virt-builder
andvirt-install
together is another nice option to create Linux VMs and its easy to script it too.
https://developer.fedoraproject.org/tools/virt-builder/about.html -
@travisdh1 said in Script for Creating VMs from Template VM in KVM:
@EddieJennings said in Script for Creating VMs from Template VM in KVM:
@Pete-S said in Script for Creating VMs from Template VM in KVM:
Not the exactly the same thing but you might want to look into how to create a VM from scratch.
Meaning a script that will set up a VM with vCPU, memory, storage, network etc and then boot it from iso and have it do an unattended install, create what users you want and install the packages you need.That's one of the next things I'm looking into.
@EddieJennings Also remember about things like kickstart in RedHat based operating systems. In Fedora/CentOS/RHOS you can use a kickstart file to automatically select all the install time options for the OS. A short time later you've got a fresh server and all the time it took you to setup was running the creation script on your hypervisor.
One of the things I'll need to figure out going the Kickstart route is setting the hostname what I want it to be at the time of installation. Likely not difficult to do, I just have to figure it out. Or perhaps, I can just truly take the approach of just making a clean minimal install, and then later configure to whatever specific thing I'm wanting the VM to do for my lab / testing.
-
@EddieJennings said in Script for Creating VMs from Template VM in KVM:
@travisdh1 said in Script for Creating VMs from Template VM in KVM:
@EddieJennings said in Script for Creating VMs from Template VM in KVM:
@Pete-S said in Script for Creating VMs from Template VM in KVM:
Not the exactly the same thing but you might want to look into how to create a VM from scratch.
Meaning a script that will set up a VM with vCPU, memory, storage, network etc and then boot it from iso and have it do an unattended install, create what users you want and install the packages you need.That's one of the next things I'm looking into.
@EddieJennings Also remember about things like kickstart in RedHat based operating systems. In Fedora/CentOS/RHOS you can use a kickstart file to automatically select all the install time options for the OS. A short time later you've got a fresh server and all the time it took you to setup was running the creation script on your hypervisor.
One of the things I'll need to figure out going the Kickstart route is setting the hostname what I want it to be at the time of installation. Likely not difficult to do, I just have to figure it out. Or perhaps, I can just truly take the approach of just making a clean minimal install, and then later configure to whatever specific thing I'm wanting the VM to do for my lab / testing.
Inside the kickstart file you'll find something like this:
network --hostname=centos8-4.example.com
We use debian as our goto and then it's called a preseed file. The only real thing that can be tricky is to tell the installation what kickstart/preseed file you want to use. You can do it in different ways. If you don't want to rely on dhcp/tftp/pxe etc you can roll your own iso file. I think the kickstart file can also be mounted as a drive that the installation will detect when it starts.
I think the best approach is to make an automated installation with same basic settings and some of those will get changed later in the installation. For example you can use a fixed hostname that is later changed from ansible.