Repository navigation
Expand file tree
/
Copy pathclonevm
More file actions
executable file
·76 lines (64 loc) · 2.53 KB
/
Copy pathclonevm
File metadata and controls
executable file
·76 lines (64 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
IFS="$(printf '\n\t')"
# $1 = source template
# $2 = dest vm name
# $3 = optional sub directory - e.g. cluster name to group all vms within a cluster.
# your base directory for your kvm vms
base_dir="/data/libvirtd_images"
cd $base_dir
error=0
if [ "$1" == "" ]; then
error=1
error_str="Source vm name is required. "
error_str+="Available templates are : $(virsh list --all |grep template |awk '{print $2}') "
fi
if [ "$2" == "" ]; then
error=1
error_str+="Destination vm name is required"
fi
# validate $1 - does not need to be -template as we want to be able to clone a full existing machine
virsh list --all |grep $1 > /dev/null
if [ "$?" == "1" ]; then
error_str="Template $1 does not exist. Templates should have dashes and not _"
error=1
fi
echo $2 |grep '_' > /dev/null
if [ "$?" == "0" ]; then
error_str="destination VM names should have dashes and not _"
error=1
fi
if [ $error -gt 0 ]; then
echo $error_str
exit;
fi
# if $3 is set, make it first, then cd into it.
# e.g. $3 = testcluster, we want /data/images/testcluster/vm_name
if [ "$3" != "" ]; then
mkdir $3
cd $3
fi
# first make a new directory for the vms disks - this colsolidates them all to keep them together
mkdir $2
# the virsh clone command produces the disks in the same directory so we cd there second
cd $2
echo "Working directory is `pwd`"
# based on the number of disks in $1 vm, need to create a virt-clone command to name that many new disks for the new vm we are cloning.
# assumptions
# -- ALL disks reside in the same directory
# -- cd to the directory where the disk images should be created
# -- using the source from dumpxml, the virt-clone command gets created with the full path to all disk files
# need to dynamically generate a command like this :
# virt-clone --original rhcs50-node01 --name rhcs50-admin --file /path/to/vms/$2/rhcs50-admin1.qcow2 --file /path/to/vms/$2/rhcs50-admin2.qcow2 --file /path/to/vms/$2/rhcs50-admin3.qcow2 --file /path/to/vms/$2/rhcs50-admin4.qcow2
# need to dynamically figure out how many disks we need based on the original vm
cmd_string="virt-clone --original $1 --name $2 "
for i in $(virsh dumpxml $1 |grep 'source file.*qcow'); do
echo "$i s/.*'\($base_dir.*\)'.*/\1/"
new=$(echo $i|sed "s%.*'\(${base_dir}.*\)'.*%\1%" | sed "s%$1%$2%g")
if [ "$3" != "" ]; then
# do an additional sed, change the first occurance to $3/$2/
new=$(echo $new | sed "s/$2/$3\/$2/");
fi
cmd_string="$cmd_string --file $new"
done
echo "$cmd_string"
eval "$cmd_string"