-
Notifications
You must be signed in to change notification settings - Fork 2
/
virt-attach-disk.sh
executable file
·86 lines (76 loc) · 2.85 KB
/
virt-attach-disk.sh
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
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# create VM's using create_vm.sh and copy VM's with virt-copy.sh
# Create file systems (ext4, xfs) and mount to respective folders
# /home/ext4, /home/xfs. if disk volume will be attached, create
# vg using vgcreate disk vg_hdd.
# For ex: vgcreate /dev/sdc vg_hdd
# ./virt-attach-disk.sh <number of VM's> <fs> <prealloc> <image_format>
# For lvm: image_format not needed.
# For raw: prealloc=full
# For qcow: prealloc=off/metadata/falloc/full
#./virt-attach-disk.sh 16 ext4 full qcow2
nr_vms=$1
fs=$2
prealloc=$3
img=$4
BASE="/home/$fs"
size=5G
for i in `seq 1 $nr_vms`
do
for aio in native threads
do
if [ "$aio" = "native" ]
then
vdisk="vdb"
#cleanup previous disk
virsh detach-disk vm$i vdb --persistent
else
vdisk="vdc"
#cleanup previous disk
virsh detach-disk vm$i vdc --persistent
fi
if [ "$fs" = "lvm" ]
then
lvcreate -L $size -n lv$i-$aio vg_hdd
lvpath="/dev/vg_hdd/lv$i-$aio"
if [ "$prealloc" = "full" ]
then
dd if=/dev/zero of=/dev/vg_hdd/lv$i-$aio bs=1024k count=5000
fi
#disk xml file
echo "<disk type='block' device='disk'>" >> $BASE/$aio-disk.xml
echo " <driver name='qemu' type='raw' cache='none' io='$aio'/>" >> $BASE/$aio-disk.xml
echo " <source dev='$lvpath'/>" >> $BASE/$aio-disk.xml
echo " <target dev='$vdisk' bus='virtio'/>" >> $BASE/$aio-disk.xml
echo "</disk>" >> $BASE/$aio-disk.xml
else
if [ "$img" = "qcow2" ]
then
imgpath="$BASE/vm$i-$aio.qcow2"
qemu-img create -f qcow2 $imgpath -o preallocation=$prealloc 5G
#disk xml file
echo "<disk type='file' device='disk'>" >> $BASE/$aio-disk.xml
echo " <driver name='qemu' type='qcow2' cache='none' io='$aio'/>" >> $BASE/$aio-disk.xml
echo " <source file='$imgpath'/>" >> $BASE/$aio-disk.xml
echo " <target dev='$vdisk' bus='virtio'/>" >> $BASE/$aio-disk.xml
echo "</disk>" >> $BASE/$aio-disk.xml
else
imgpath="$BASE/vm$i-$aio.raw"
qemu-img create -f raw $imgpath 5G
if [ "$prealloc" = "full" ]
then
dd if=/dev/zero of=$imgpath bs=1024k count=5000
fi
#disk xml file
echo "<disk type='file' device='disk'>" >> $BASE/$aio-disk.xml
echo " <driver name='qemu' type='raw' cache='none' io='$aio'/>" >> $BASE/$aio-disk.xml
echo " <source file='$imgpath'/>" >> $BASE/$aio-disk.xml
echo " <target dev='$vdisk' bus='virtio'/>" >> $BASE/$aio-disk.xml
echo "</disk>" >> $BASE/$aio-disk.xml
fi
fi
#attach disk
virsh attach-device vm$i $BASE/$aio-disk.xml --persistent
rm -rf $BASE/$aio-disk.xml
done
done