Linked clone with qemu-img

As you would have seen in Virtualbox or vmware, there is option to create a linked clone. I wanted to use the same feature as “Snapshot” feature anyway does not look/work so great with virt-manager. So, I created a script to create a linked clone VM and here it is :

 

#!/bin/bash - 
#===============================================================================
#
#          FILE: qcow2-linked-clone.sh
# 
#         USAGE: ./qcow2-linked-clone.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka)
#  ORGANIZATION: Mobileum
#       CREATED: 01/05/2018 09:54
# Last modified: Wed Feb 28, 2018  04:39PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error
UP="amitag:amitag" #Here you need to put your username and group.

if [[ $# == 0 ]]
then
    read -p "Enter the source path :: " spath
    read -p "Enter the source disk :: " sdisk
    read -p "Enter the destin path :: " dpath
    read -p "Enter the destin disk :: " ddisk
    read -p "Enter new VMName :: " vmname
else
    spath=$(dirname $1)
    dpath=$spath
    sdisk=$(basename $1)
    ddisk=$2.qcow2
    vmname=$2
fi


sudo chown $UP "$spath/$sdisk"
qemu-img create -f qcow2 -b "$spath/$sdisk" "$dpath/$ddisk"

virt-install --disk $dpath/$ddisk --ram 512 \
    --virt-type kvm --vcpus 1 --name "$vmname" --import

The script will create a linked qcow2 and then create a VM with that image. Running it is simple, either provide command line options or just run and it will ask you for details.

 

Hope it is useful.

vagrant box to libvirtd (QEMU) VM

Like ova images, you can use box images as well with Qemu. After all, both have the disk images, so here is the script to do that. Just put the script somewhere in your path and run with ova or box image name :

 

#!/bin/bash - 
#===============================================================================
#
#          FILE: ova2vm.sh
# 
#         USAGE: ./ova2vm.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka),
#  ORGANIZATION: Mobileum
#       CREATED: 12/28/2017 13:59
# Last modified: Sun Mar 11, 2018  12:01PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error
dest='/mnt/Backup/VM'
ORIG=${PWD}

if [[ $# == 0 ]]
then
    echo "You need to provide ova/vmdk filename"
    exit
fi
if [[ $1 == *ova || $1 == *box ]]
then
    tmp=$(mktemp -d /tmp/amitXXXXXXX)
    cd  $tmp
    tar xvf $ORIG/$1
    file=$(echo $PWD/*vmdk)
else
    file=$1
    echo "Not a OVA file"
fi
dfile="$dest/$(basename $file)"

read -p "Enter the name for VM :: " vmname
qemu-img convert $file $dfile -p -c -O qcow2
virt-install --disk $dfile --ram 512 \
    --virt-type kvm --vcpus 1 --name "$vmname" --import

Add ova file as VM on Linux with libvirt (Qemu)

Although the commands are very simple and just 2-3 steps but I keep forgetting them and hence wrote the following script:

The script takes input as “ova” filename and then creates the qcow2 image and finally a VM for you.

#!/bin/bash - 
#===============================================================================
#
#          FILE: ova2vm.sh
# 
#         USAGE: ./ova2vm.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka), 
#  ORGANIZATION: Mobileum
#       CREATED: 12/28/2017 13:59
# Last modified: Thu Dec 28, 2017  02:17PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error

if [[ $# == 0 ]]
then
    echo "You need to provide ova/vmdk filename"
    exit
fi
if [[ $1 == *ova ]]
then
    tmp=$(mktemp -d /tmp/amitXXXXXXX)
    cd  $tmp
    tar xvf $1
    file=$(echo $PWD/*vmdk)
else
    file=$1
    echo "Not a OVA file"
fi
dfile="$dest/$(basename $file)"

read -p "Enter the name for VM" vmname
qemu-img convert $file $dfile -p -c -O qcow2
virt-install --disk $dfile --ram 512 \
    --virt-type kvm --vcpus 1 --name "$vmname" --import