Bash scripts for Linux KVM labs. Only for educational purposes.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Update Dec 8, 2018 renommé : -> Mar 1, 2017 modifié : Mar 8, 2017


This suite of "quick and dirty" scripts are intended to mount virtual sandboxes for labs during Linux training courses with KVM/libvirtd (on Centos 7 or Debian Jessie hosts). The main goal is to spawn quickly linux guests for lab purposes. But we can find other subsequents objectives as coding/scripting, automation or virtual networks and system management, helping to understand virtual infrastructures technologies and architectures. Only for educational purposes.

Three groups of scripts :

  1. Native installation for Centos 7 Debian 9 and Ubuntu 18.04 and post-installation.
  2. Quickbuilder, guests spwaning with pre-builded images
  3. Devices management

If you already have images at stock, there exists beter ways to automate and manage libvirt guest like those nice scripts :

For native installation of Arch Linux : look at

Native installation and post-installation

Purposes : gold image auto-creation

  1. : prepare your system as virtualization host
  • : get iso distributions for fresh installations
  • : build a fresh Centos, Debian or Ubuntu system with http repos and kickstart files
  • : text user interface demo
  • : optimize space disk on the designated guest
  • : clone, sysprep and optimize builded guests
  • hosts-file : print the running guests and their ipv4 address
  • : nested virtualization installation on the physical host


Purposes : deploy quickly guests based on pre-builded with previous scripts. Some images are avaible on : centos7 centos7.5 debian7 debian8 ubuntu1604 ubuntu1804 kali metasploitable openwrt15.05.

  1. : deploy pre-builded images (like a quickbuilder, provisionner)
  • : get and start openwrt with two interfaces

But this is probably beter to build by yourself your appliance with the script. The root account and the password are in the preseed and kickstart templates included.

Devices management

Purposes : change RAM and vcpus, add block devices and network facilities

  1. : add RAM
  • : set vcpus count
  • : add an isolated or ipv4 nat/ipv6 ula libvirt bridge
  • : a new NIC on live guest to a bridged interface
  • : attach a live guest present NIC to a bridge
  • : detach a live guest from a bridge
  • : attach an empty bit disk by GB size
  • : start all the defined guests
  • : destroy, undefine all the guests with storage removing


First clone this project in your home directory :

git clone
cd virt-scripts

Step 1 : Verify your installation

Script :

Description : Setup KVM/Libvirtd/LibguestFS on RHEL7/Centos 7/Debian Jessie.

Usage :

# ./
This script will install all the necessary packages to use Libvirtd/KVM
Please reboot your host after this step
Are you sure? [y/N]

Step 2 : Get iso images (optionnal, outdated)

Script :

Description : Get latest iso of Centos 7, Debian Jessie and Ubuntu Xenial.

Usage :

# ./
Usage : ./ [ centos | debian | ubuntu ]

Step 3 : Build a guest automatically

Script :

Description : Centos 7, Debian Stretch or Ubuntu Bionic fully automatic installation by HTTP Repo and response file (preseed or kickstart) via local HTTP server.

Usage :

Please check all the parameters. For customization, you can adapt the kickstart or preseed templates inside the script.

# ./
Centos 7, Debian Jessie or Ubuntu Xenial fully automatic installation by HTTP Repos and response file via local HTTP.
Usage : ./ [ centos | debian | ubuntu ] nom_de_vm
Please provide one distribution centos, debian, ubuntu and one guest name: exit

Note : Escape character is ^] (CTRL+ ])

Step 4 : Sparse your native image

Script :

Description : Sparse a disk. Great gain on disk space !

Usage :

This script sparses an attached disk
Please provide a the guest name of a destroyed guest: exit
Usage : ./ <guest name>

Check the disk usage : 2,0G

# du -h /var/lib/libvirt/images/ubuntu-gold-31122016.qcow2
2,0G    /var/lib/libvirt/images/ubuntu-gold-31122016.qcow2

Sparsify operation

# ./ ubuntu-gold-31122016

Sparse disk optimization
[   0,1] Create overlay file in /tmp to protect source disk
[   0,1] Examine source disk
[   4,3] Fill free space in /dev/sda1 with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   6,9] Fill free space in /dev/u1-vg/root with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  70,6] Clearing Linux swap on /dev/u1-vg/swap_1
[  71,9] Copy to destination and make sparse
[ 191,4] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the
target disk boots and works correctly.

Check the disk usage : 432M

# du -h /var/lib/libvirt/images/ubuntu-gold-31122016.qcow2
432M    /var/lib/libvirt/images/ubuntu-gold-31122016.qcow2

Step 5 : Clone your guest

Script :

Description : Cloning a domain disk with sparsifying and Linux sysprep.

Usage :

This script clones, sparsifies and syspreps linux guest
Usage : './ <original guest> <destination guest>'
Please provide a the guest name of a destroyed guest: exit

Step 6 : Quickbuilder

Assume that you have installed three guests with :

~/virt-scripts# ./ centos7 centos
~/virt-scripts# ./ debian8 debian
~/virt-scripts# ./ ubuntu1604 ubuntu

And you can verify it :

~/virt-scripts# virsh list --all
 Id    Name                           State
 -     centos7                        shut off
 -     debian8                        shut off
 -     ubuntu1604                     shut off

Move those images disk into the virt-scripts directory and undefine original guests :

cd ~/virt-scripts
for x in centos7 debian8 ubuntu1604
mv /var/lib/libvirt/images/$x.qcow2 ./
virsh undefine $x

And you can deploy quicky builded and optimized guests based on those images :

~/virt-scripts# ./ c1 centos7

Début d'installation...
Création du domaine...                                                   |    0 B     00:00
Création du domaine terminée.  Vous pouvez redémarrer votre domaine en lançant :
  virsh --connect qemu:///system start c1
~/virt-scripts# ./ d1 debian8

Début d'installation...
Création du domaine...                                                   |    0 B     00:00
Création du domaine terminée.  Vous pouvez redémarrer votre domaine en lançant :
  virsh --connect qemu:///system start d1
~/virt-scripts# ./ u1 ubuntu1604

Début d'installation...
Création du domaine...                                                   |    0 B     00:00
Création du domaine terminée.  Vous pouvez redémarrer votre domaine en lançant :
  virsh --connect qemu:///system start u1

Also, I have prebuilded other images for training classes :

~/virt-scripts# ./
Usage : ./ <name> <image>
Please download one of those images :

Step 7 : Manage guests

Verify your running guests

~/virt-scripts# virsh list
 Id    Name                           State
 88    c1                             running
 89    d1                             running
 90    u1                             running

Access to the text console

~/virt-scripts# virsh console c1
Connected to domain c1
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-514.6.2.el7.x86_64 on an x86_64

centos7 login:

To exit from the text console execute CTRL ].

Step 8 : Add the guest hostname resolution

Script :

Description : Print a new /etc/resolv.conf with the ip address and the hostname of running guests.

Usage :


For example :

./ c1 d1 u1

To update your /etc/hosts :

./ >> /etc/hosts

SSH is enabled by default :

~/virt-scripts# ssh c1
The authenticity of host 'c1 (' can't be established.
ECDSA key fingerprint is 04:be:d2:e9:d9:9a:98:02:e3:a8:34:2d:3a:dd:26:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'c1,' (ECDSA) to the list of known hosts.
root@c1's password:
[root@centos7 ~]# exit
Connection to c1 closed.

Step 9 : Manage devices

Script :

Description : add an isolated or ipv4 nat/ipv6 ula libvirt bridge

Usage :

Description : This script create an isolated or a nat/ipv6 bridge
Usage       : ./ <name> <interface> <type, isolated or nat>
Example     : './ net1 virbr100 isolated' or './ lan101 virbr101 nat'

Script :

Description : attach an empty bit disk by GB size

Usage :

Description : This script attach a disk to a live guest
Usage       : ./ <guest name> <block device name> <size in GB>
Example     : './ guest1 vdb 4' add a vdb 4GB disk to guest1

To be continued ...

Next steps ...

  • Integrate kcli for guests management
  • Install ansible, add ssh hosts keys, create an ansible inventory and test your managed nodes.
  • Exploit snapshots and virtual storage
  • Exploit free-ipa, pacemaker, ovirt, openstack, gns3 (see kcli plans)


    • Fedora
  • : create local repo
  • Revise code and comment, comment, comment ...