forked from OndrejHome/fast-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-vm-helper.sh
executable file
·94 lines (85 loc) · 2.56 KB
/
fast-vm-helper.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
87
88
89
90
91
92
93
94
#!/bin/bash
# fast-vm helper script for privileged root actions
DEBUG_LOG=/tmp/fast-vm-helper.debug.log
if [ ! $(whoami) == 'root' ];
then
echo "[err] this must be run as root"
exit 1
fi
if [ ! -f '/etc/fast-vm.conf' ]; then
echo "[err] global configuration file /etc/fast-vm.conf not found"
exit 1
fi
# load global configuration file
. /etc/fast-vm.conf
function check_empty {
var_name="$1"
var_value="$2"
if [ -z "$var_value" ]; then
echo "[err] variable $var_name not declared in global configuration"
echo " run configure-fast-vm again or fix manually"
exit 2
fi
}
check_empty "VM_PREFIX" "$VM_PREFIX"
check_empty "THINPOOL_VG" "$THINPOOL_VG"
check_empty "THINPOOL_LV" "$THINPOOL_LV"
check_empty "LIBVIRT_NETWORK" "$LIBVIRT_NETWORK"
check_empty "SUBNET_NUMBER" "$SUBNET_NUMBER"
read action arg1 arg2 arg3
case "$action" in
lvcreate)
arg2=$(echo "$arg2"|egrep '^[a-zA-Z0-9.-]+$')
if [ -z "$arg2" ]; then
echo "[err] LV name validation failed"
exit 1
fi
arg3=$(echo "$arg3"|egrep '^[a-zA-Z0-9.-]+$')
if [ "$arg1" == "newvm" ]; then
if [ -z "$arg3" ] || [ ! -b "/dev/$THINPOOL_VG/$VM_PREFIX$arg2" ]; then
echo "[err] newvm LV name validation failed"
exit 1
fi
fi
case "$arg1" in
base)
lvcreate -n $VM_PREFIX$arg2 -V 10G --thinpool $THINPOOL_VG/$THINPOOL_LV >>$DEBUG_LOG 2>&1
;;
newvm)
lvcreate -k n -s --thinpool $THINPOOL_VG/$THINPOOL_LV /dev/$THINPOOL_VG/$VM_PREFIX$arg2 --name $VM_PREFIX$arg3 >>$DEBUG_LOG 2>&1
;;
*)
echo "[err] wrong action for lvcreate"
exit 1
esac
;;
lvremove)
arg1=$(echo "$arg1" | egrep "/dev/$THINPOOL_VG/$VM_PREFIX[a-zA-Z0-9.-]+$")
if [ -z "$arg1" ] || [ ! -b "$arg1" ]; then
echo "[err] LV not found, not a block device or not allowed to be removed"
exit 1
fi
lvremove -f "$arg1" >>$DEBUG_LOG 2>&1
;;
chgrp)
arg1=$(echo "$arg1" | egrep "/dev/$THINPOOL_VG/$VM_PREFIX[a-zA-Z0-9.-]+$")
if [ -z "$arg1" ] || [ ! -b "$arg1" ]; then
echo "[err] LV not found, not a block device or not allowed to be chgrp"
exit 1
fi
chgrp libvirt "$arg1" >>$DEBUG_LOG 2>&1
;;
dhcp_release)
arg1=$(echo "$arg1"| egrep '^[0-9]+$')
arg2=$(echo "$arg2"| egrep '^[a-f0-9]{2,2}:[a-f0-9]{2,2}:[a-f0-9]{2,2}:[a-f0-9]{2,2}:[a-f0-9]{2,2}:[a-f0-9]{2,2}$')
if [ -z "$arg1" ] || [ -z "$arg2" ] || [ "$arg1" -lt 20 ] || [ "$arg1" -gt 220 ]; then
echo "[err] validation of VM number or mac address failed"
exit 1
fi
dhcp_release "$LIBVIRT_NETWORK" "192.168.$SUBNET_NUMBER.$arg1" "$arg2" >>$DEBUG_LOG 2>&1
;;
*)
echo "[err] unknown action"
exit 3
;;
esac