Opting for a “quick start” method will actually install KVM hypervisor on your computer. Be absolutely sure this is what you want, because KVM can be resource intensive.
You should have your virtualization platform ready. If this is not the case, please skip to the first article in this series and get yourself familiar with the topic at hand. If you are lazy and don’t want to read, well here is a quick start method if you are using a Debain based Linux distribution. Open your terminal and download and execute my KVM Auto-Install script:
# wget -c http://www.zacks.eu/wp-content/uploads/files/kvm_autoinstall/kvm_autoinstall.tar.gz # ./kvm-autoinstall.sh
Please use sudo command if you’re not using a root user. This will set you up with KVM virtualization infrastructure in a minute or two. Once ready, you can try my super duper simple scripts I’m about to present in this article.
KVM Virtualization helper scripts
The set of scripts offered here is not much, but it will give you a nice idea on how you can automate and auto-scale operations related to your virtualization platform. Scripts offered here will help you with state change of your guest machines, snapshot manipulation and all-in-one build of guest machines. The details follow.
Build a guest machine
To build a guest machine you need a disk image and you need to pass several options to virt-install, like name of your guest machine, amount of memory, installation source etc. The following script will do all that for you, where you will only need to supply some basic info. Don’t worry, the script will ask you for it. When executed in terminal, it looks like this:
# ubuntu-barebone.sh Please enter VM name: Machine-001 Please enter disk size (MiB): 10240 Please set VCPU count: 1 Please set memory size (MiB): 1024 Please set network (choices are nat-dhcp virbr100 ): nat-dhcp Formatting '/var/lib/libvirt/images/Machine-001.qcow2', fmt=qcow2 size=10240 encryption=off cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16 Starting install... Creating domain... | 0 B 00:00:00 Domain installation still in progress. You can reconnect to the console to complete the installation process.
You noticed this script will initiate a build of Ubuntu based guest machine. If you want to use other operating system, you only need to change the installation source. Open the script in terminal and change the following line:
–cdrom /var/lib/libvirt/iso-images/ubuntu-xenial-mini.iso \
Of course you need to have desired operating system ISO image ready on your system.
Change the state of your guest machines
This script manipulates the state of your guest machines. It can either start a turned off machine, and perform graceful shutdown or reboot of your currently running machines (single, group or all). It will show you the state of all machines currently present in your virtualization platform and then call for action. When executed in terminal, it looks like this:
# vm_state.sh ## Current state of guest machines ## Id Name State ---------------------------------------------------- - Machine-001 shut off Please select state (start|reboot|shutdown): start Please select for which virtual machine do you want to change state (all or select from VM LIST: Machine-001 ): all Changing state on all virtual machines Domain Machine-001 started
Great thing with qcow2 disk image format is that you can create snapshot. Snapshot is sort of a “state freeze” operation. Let’s say you are working on a project where you need to test how a certain application(s) will work with a new release of your favorite operating system. You will install an operating system and all the services on your guest machine required for a desired application to work. At this stage, and before an actual install of an application, it would be wise to create a snapshot. Later, if something goes wrong, you have a known state where everything works and you can easily revert to, without having to start the whole project from the beginning. The following set of scripts will help you with snapshot operations. The first one will list snapshots currently available:
# vm_snapshot_list.sh Manipulate snapshots on all virtual machines or select (disk-image-001.qcow2): disk-image-001.qcow2 Listing snapshots for selected virtual machines: image: /var/lib/libvirt/images/disk-image-001.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.8M cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
As you can see from the output, there is no snapshots available for my guest machine, so I will create one. This is where my second script kicks in, the one that creates snapshots:
# vm_snapshot_create.sh Manipulate snapshots on all virtual machines or select (disk-image-001.qcow2): disk-image-001.qcow2 Create snapshots for selected virtual machines. Please enter snapshot name: Stage-001
Now if you again try to list available snapshot for your guest machine, you will notice a snapshot entry is created:
# vm_snapshot_list.sh Manipulate snapshots on all virtual machines or select (disk-image-001.qcow2): disk-image-001.qcow2 Listing snapshots for selected virtual machines: image: /var/lib/libvirt/images/disk-image-001.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.8M cluster_size: 65536 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 Stage-001 0 2017-10-20 12:58:07 00:00:00.000 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
Please use sudo if you are not running as root. Also it is recommended you turn of a guest machine before creating, applying or deleting a snapshot.
So now you have a snapshot of your image which marks the stage where you are certain everything works as expected. Now you continue to work on your guest machine and let’s say you mess something and it corrupts the system so it is not usable any more. Instead of cursing in anger and starting the whole project from the beginning, you can revert to a state where everything was working by applying a snapshot:
# vm_snapshot_apply.sh Manipulate snapshots on all virtual machines or select (disk-image-001.qcow2): disk-image-001.qcow2 Apply snapshots for selected virtual machines. Please enter snapshot name: Stage-001
Easy as that 🙂 You again come to the part where you messed up, and you want to make sure you don’t make a same mistake, so you create another snapshot. Smart thing to do right? Of course! Once again, you’re certain you have a snapshot which marks the stage where everything works as expected, so now you can delete the old snapshot:
# vm_snapshot_delete.sh Manipulate snapshots on all virtual machines or select (disk-image-001.qcow2): disk-image-001.qcow2 Delete snapshots for selected virtual machines. Please enter snapshot name: Stage-001
I hope this article gave you a nice idea how you can ease up and automate your KVM virtualization platform management. Thank you for reading.
Missed on other “Virtualization with KVM” articles?
Virtualization with KVM – Installation and configuration
Virtualization with KVM – Extended Customization
Virtualization with KVM – Creating Guest Machines