Occasionally, I come to the point where I need to create a Virtual Machine for testing purposes. Either, to check something new (a new version of Plesk or cPanel, for instance) or just create a test environment for my Ansible playbooks. Virtualbox has been removed from my computer completely, in favour of QEMU/KVM. Despite the fact that you can use a GUI application like virt-manager (similar to VirtualBox GUI) to create a VM in KVM, I would prefer a more automated way of creating one.

The only prerequisite that I put on this, is that VM creation and installation must be unattended.
I resort to virt-install, part of virtinst package in Ubuntu. Thus, we end up with the following:

virt-install -n centos-test -r 768 --vcpus=2 --os-variant=rhel7 --accelerate \
-v -w network=default --disk path=/path/to/disk.img,size=4.5 \
-l ftp://ftp.cc.uoc.gr/CentOS/7.2.1511/os/x86_64/ --nographics \
-x "ks= console=ttyS0"

The above command creates a new Virtual Machine, named "centos-test", uses a CentOS 7 local mirror (here ftp.cc.uoc.gr) as its installation source, creates (if it does not exist) a new disk with size 4.5 GiB and installs CentOS 7 using ks.cfg kickstart file. Note that kickstart is served from a web server in, the IP address of the host when you use the default NAT configuration. In case you don't have a webserver in handy, you can start the simplest one, just by typing python -m SimpleHTTPServer in a different console window. A ks.cfg that I use is available on GitHub, here.