-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path.vm_functions
388 lines (335 loc) · 9.06 KB
/
.vm_functions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env bash
# Vm management
# start vm management daemons
vm () {
dock
sudo systemctl restart libvirtd.service
sudo systemctl restart vsftpd.service
sudo systemctl restart virtlogd.socket
sudo mount -o loop ~/iso/CentOS-7-x86_64-Minimal-1611.iso /srv/http/cent7
}
vm_start () {
echo ""
nodes=("$@")
for node in ${nodes[@]}; do
sudo virsh start $node
done
}
vm_restart () {
echo ""
nodes=("$@")
for node in ${nodes[@]}; do
sudo virsh reboot $node
done
}
vm_stop () {
nodes=("$@")
for node in ${nodes[@]}; do
sudo virsh shutdown $node
done
}
vm_shutdown () {
echo ""
nodes=("$@")
for node in ${nodes[@]}; do
sudo virsh destroy $node
done
}
vm_clean () {
echo ""
nodes=("$@")
for node in ${nodes[@]}; do
sudo virsh destroy $node 2>/dev/null
done
for node in ${nodes[@]}; do
sudo virsh undefine $node
done
sudo virsh pool-refresh default
for node in ${nodes[@]}; do
sudo virsh vol-delete --pool default $node.qcow2
done
}
# Stop all vms running
vm_stopall () {
for i in `sudo virsh list | grep running | awk '{print $2}'`
do
sudo virsh shutdown $i
done
}
nested () {
echo "options kvm_intel nested=1" | sudo tee /etc/modprobe.d/kvm-nested.conf
sudo modprobe -r kvm_intel && sudo modprobe kvm_intel # unload and reload kvm module
echo ""
echo "If output is Y, kvm module is loaded"
}
vm_edit () {
sudo EDITOR=vim virsh edit $1
}
vm_mem () {
local domain="$1"
local max_mem="$2"
sudo virsh setmaxmem $domain $max_mem --config
sudo virsh setmem $domain $max_mem --config
echo ""
virsh dominfo $domain
}
vm_vcpu () {
local domain="$1"
local vcpus="$2"
sudo virsh setvcpus --domain $1 --maximum 2 --config
sudo virsh setvcpus --domain $1 --count 2 --config
virsh dominfo $domain
}
# List all kvm vms
vm_list () {
sudo virsh list --all
}
# Volume management
vol_list () {
sudo virsh vol-list --pool default
}
vol_create (){
sudo virsh vol-create-as default ${1}.qcow2 ${2}
}
vol_delete () {
sudo virsh vol-delete ${1} --pool default
}
vol_attach () {
sudo virsh attach-disk --domain ${1} --source /var/lib/libvirt/images/${2}.qcow2 --persistent --target ${3}
# 3 e.g vdb
}
vol_detach () {
virsh detach-disk --domain ${1} --persistent --live --target ${2}
}
disk_grow () {
sudo qemu-img resize ${1} +${2}G
}
# Snapshot management
# Create a snaphost or a domaun
snap () {
while [[ $dom_name == "" || $snap_name == "" ]]; do
dom_name=${dom_name:-$1}
snap_name=${snap_name:-$2}
desc=${desc:-$3}
done
echo ""
sudo virsh snapshot-create-as --domain "$dom_name" \
--name "$snap_name" \
--description "$desc"
}
# List available snapshots for a domain
snaplist () {
sudo virsh snapshot-list --domain "$1"
}
# Display info about a snapshot
snapinfo () {
sudo virsh snapshot-info --domain "$1" "$2"
# $1 domain name
# $2 snapshot name
}
# Restore a snapshot
snaprestore () {
sudo virsh shutdown --domain "$1"
sudo virsh snapshot-revert --domain "$1" --snapshotname "$2" --running
}
# Delete a snapshot
snapdelete () {
sudo virsh snapshot-delete --domain "$1" --snapshotname "$2"
}
# Start CentOS 7 server
server () {
sudo virsh start server 2>/dev/null
sleep 30
TERM=xterm
ssh root@server 2>/dev/null
}
# Newtork Management
vm_attach_interface () {
DOMAIN=${DOMAIN:-"$1"}
INT_TYPE=${INT_TYPE:-"$2"}
SOURCE=${SOURCE:-"$3"}
if [[ $# -eq 0 ]]; then
echo ""
echo "USAGE: ${0##*/} <Argument>"
echo "Arguments:"
echo -e "\t1\tDomain name"
echo -e "\t2\tInterface type, e.g bridge,network"
echo -e "\t3\tInterface source, e.g br1"
else
sudo virsh attach-interface --domain $DOMAIN --type $INT_TYPE --source $SOURCE --model virtio --config --live
sudo virsh domiflist $DOMAIN
fi
}
# VM installer script
installer() {
local ISO_ DIR="/home/josepy/iso"
echo -en "Enter vm name: "
read VM_NAME
echo -en "Enter memory size: "
read MEM_SIZE
echo -en "Enter no of vcpus: "
read VCPUS
echo -en "Enter OS type: "
read OS_TYPE
echo -en "Enter virtual disk size : "
read DISK_SIZE
echo -en "Enter OS Variant e.g (rhel7,ubuntu16.04,debianjessie,wink12): "
read OS_VARIANT
echo -en "Enter Graphics type (none,vnc): "
read GRAPHICS_TYPE
echo -en "Specify ISO name (rhel7,cent7,deb8,ubuntu16,sles): "
read ISO_NAME
case $ISO_NAME in
rhel7)
ISO_FILE="/home/josepy/iso/rhel-server-7.3-x86_64-dvd.iso"
;;
cent7)
ISO_FILE="/home/josepy/iso/CentOS-7-x86_64-Minimal-1611.iso"
;;
deb8)
ISO_FILE="/home/josepy/iso/debian-8.6.0-amd64-CD-1.iso"
;;
ubuntu16)
ISO_FILE="/home/josepy/iso/ubuntu-16.04.1-server-amd64.iso"
;;
sles)
ISO_FILE="/home/josepy/iso/SLE-12-SP2-Server-DVD-x86_64-GM-DVD1.iso"
;;
esac
sudo virt-install \
--name ${VM_NAME} \
--memory=${MEM_SIZE} \
--vcpus=${VCPUS} \
--os-type ${OS_TYPE} \
--location ${ISO_FILE} \
--disk size=${DISK_SIZE} \
--network bridge=virbr0 --network bridge=docker0 \
--graphics=${GRAPHICS_TYPE} \
--hvm \
--os-variant=${OS_VARIANT}
#--console pty,target_type=serial \
#--extra-args 'console=ttyS0,115200n8 serial' \
# List os variants: virt-install --os-variant list, common are:
# rhel7,win2k8,win2k12,win7,opensuse12,ubuntu16.04
# --initrd-inject=/root/ks.cfg --extra-args "ks=file:/ks.cfg"
# --network bridge=br-ex --network bridge=br-ctlplane
#Live snapsho
# virsh snapshot-create-as --domain undercloud --disk-only --atomic --quiesce
# Take a copy of the (now read-only) QCOW backing file
# rsync --sparse -avh --progress /var/lib/libvirt/images/undercloud.qcow2 1.qcow2
# Merge the QCOW overlay file into the backing file and switch the undercloud VM back to using the original file:
# virsh blockcommit undercloud vda --active --verbose --pivot
}
cent_installer() {
echo ""
sudo cp ~/ks.cfg /srv/http/ks.cfg
# Kickstart update
echo -en "Enter Hostname: "
read HOST_NAME
echo -en "Enter IP Address: "
read IP_ADDRESS
HOST_NAME=${HOST_NAME:-$1}
IP_ADDRESS=${IP_ADDRESS:-$2}
sudo sed -i 's/server1/'$HOST_NAME'/g' /srv/http/ks.cfg
sudo sed -i 's/192.168.122.100/'$IP_ADDRESS'/g' /srv/http/ks.cfg
## VM configs
echo ""
echo -en "Enter vm name: "
read VM_NAME
local MEM_SIZE=1024
local VCPUS=1
local OS_TYPE="linux"
echo -en "Enter virtual disk size : "
read DISK_SIZE
local OS_VARIANT="rhel7"
local ISO_FILE="$HOME/iso/CentOS-7-x86_64-Minimal-1611.iso"
VM_NAME=${VM_NAME:-$3}
DISK_SIZE=${DISK_SIZE:-$4}
sudo virt-install \
--name ${VM_NAME} \
--memory=${MEM_SIZE} \
--vcpus=${VCPUS} \
--os-type ${OS_TYPE} \
--location ${ISO_FILE} \
--disk size=${DISK_SIZE} \
--network bridge=virbr0 \
--graphics=none \
--os-variant=${OS_VARIANT} \
--console pty,target_type=serial \
-x 'console=ttyS0,115200n8 serial' \
-x "ks=http://192.168.122.1:8090/ks.cfg"
}
vm_sysprep () {
if [[ $# -eq 0 ]]; then
echo ""
echo "USAGE: ${0##*/} Argument"
echo -e "\t1\tDomain"
echo -e "\t2\tHostname"
else
sudo virt-sysprep -c qemu:///system -d $1 \
--selinux-relabel --timezone Africa/Nairobi \
--root-password password --no-network \
--hostname $2
#-uninstall syslinux,xinetd,tftp-server \
#--update --install bash-completion,vim,htop,chrony
fi
}
ova2qcow2 () {
tar xvf $1
qemu-img convert -O qcow2 $1 $2
}
vm_clone () {
if [[ $# -eq 0 ]]; then
echo ""
echo "USAGE: ${0##*/} <Argument>"
echo "Arguments:"
echo -e "\t1\tOriginal domain"
echo -e "\t2\tTarget domain"
else
sudo virt-clone --connect qemu:///system \
--original $1 \
--name $2 \
--file /var/lib/libvirt/images/$2.qcow2 \
# sudo virt-sysprep -d $2
fi
}
vm_net_edit () {
sudo EDITOR=vim virsh net-edit $1
}
vm_mount () {
if [[ $# -eq 0 ]]; then
echo ""
echo "USAGE: ${0##*/} <Argument>"
echo "Arguments:"
echo -e "\t1\tDomain name"
echo -e "\t2\tMount Point"
else
sudo guestmount -d $1 -o nonempty -i $2
sudo ls $2
fi
}
ubuntu_installer () {
sudo virt-install \
--name $1 \
--ram 1024 \
--disk path=/var/lib/libvirt/images/$1.qcow2,size=10 \
--vcpus 1 \
--os-type linux \
--os-variant ubuntu14.04 \
--network bridge=virbr0 \
--graphics vnc \
--console pty,target_type=serial \
--cdrom '/home/josepy/Downloads/ubuntu-14.04.1-server-amd64.iso' \
}
vm_mac () {
MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; echo $MACADDR
}
iface_attach () {
echo -en "Enter domain name: "
read DOMAIN
echo -en "Enter host bridge name: eg br-em1: "
read source_bridge
echo -en "Enter interface type, e.g bridge: "
read iface_type
virsh attach-interface --domain $DOMAIN --type $iface_type --source $source_bridge --model virtio --config --live
}