-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_vm.sh
executable file
·201 lines (182 loc) · 4.25 KB
/
create_vm.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
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
#!/bin/sh
if [[ "$(id -u)" -ne "0" ]]
then
echo "You need to run this script as root."
exit 1
fi
if [ "$#" -ne 3 ]
then
# echo "Usage: ./create_vm vm_name template_name home_image_size owner_name"
# echo "\nExample: ./create_vm vm1 debian11 5G user1"
echo "Usage: ./create_vm vm_name template_name owner_name"
echo "\nExample: ./create_vm vm1 debian11 user1"
exit 1
fi
. ./var_setup.sh
IFCFOLD=/tmp/if-old
IFCFNEW=/tmp/if-new
VMNAME=${1}-vm
TEMPLATENAME=${2}
#HOMESIZE=${3}
OWNERNAME=${3}
TEMPLATEOS="$TEMPLATENAME".qcow2
TEMPLATEHOME="$TEMPLATENAME"-home.qcow2
TEMPLATEOSP="$TEMPLATEPATH"/"$TEMPLATEOS"
TEMPLATEHOMEP="$TEMPLATEPATH"/"$TEMPLATEHOME"
VMOS="$VMNAME".qcow2
VMHOME="$VMNAME"-home.qcow2
VMOSP="$IMAGESPATH"/"$VMOS"
VMHOMEP="$IMAGESPATH"/"$VMHOME"
CONF="$CONFIGPATH"/"$VMNAME".conf
cleanup() {
echo "VM creation failed."
if [[ -f "$VMOSP" ]]
then
rm "$VMOSP"
fi
if [[ -f "$VMHOMEP" ]]
then
rm "$VMHOMEP"
fi
exit 1;
}
# Check if required directories exist
if ! [[ -d "$TEMPLATEPATH" ]]
then
echo "$TEMPLATEPATH does not exist. Did you run setup.sh?"
exit 1
fi
if ! [[ -d "$IMAGESPATH" ]]
then
echo "$IMAGESPATH does not exist. Did you run setup.sh?"
exit 1
fi
if ! [[ -d "$ISOPATH" ]]
then
echo "$ISOPATH does not exist. Did you run setup.sh?"
exit 1
fi
if ! [[ -d "$CONFIGPATH" ]]
then
echo "$CONFIGPATH does not exist. Did you run setup.sh?"
exit 1
fi
# Check if vm with same name exists
if [[ -f "$VMOSP" || -f "$VMHOMEP" ]]
then
echo "VM images with same name already exist. Please change vm name."
exit 1
elif [[ -f "$CONF" ]]
then
echo "VM config file for with that VM name already exists. Please change vm name."
exit 1
#else
#grep -v "^\\"$VMNAME"," "$CONFIGPATH" > "$CONFIGPATH" # Clear config
fi
# Check if given template exists
if ! [[ -f "$TEMPLATEOSP" || -f "$TEMPLATEHOMEP" ]]
then
echo "\nGiven template does not exist."
echo "Avilable Templates:"
./list_templates.sh
exit 1
fi
# Check if user exists
if ! id "$OWNERNAME" >/dev/null 2>&1;
then
echo "User $OWNERNAME does not exist."
exit 1
fi
# Create vm images from templates
# vmctl returns 0 on success and >0 on error.
# Check if vmd is enabled.
echo "Creating VM OS image..."
vmctl create -b "$TEMPLATEOSP" "$VMOSP" > /dev/null 2>&1
if ! [[ $? -eq 0 ]]
then
echo "Error while creating OS image... Cleaning up."
cleanup;
fi
echo "Creating VM persistent(private) image..."
cp "$TEMPLATEHOMEP" "$VMHOMEP"
# generate a MAC address
MAC="$(hexdump -n3 -e'/3 "00:60:2F" 3/1 ":%02X"' /dev/random)"
# Get new ID
# TODO: Fix finding unused number as this finds bigest one and
# there may be some unused
ID=0
for vmconf in $CONFIGPATH/*
do
TEMPID=$(grep -E '^#id:.*[0-9]$' "$vmconf" | cut -d':' -f 2)
if [[ $TEMPID > $ID ]]
then
ID=$TEMPID
fi
done
ID=$((ID+1))
echo "ID: $ID"
# Backup old config
# cat /etc/vm.conf > $VMCFOLD
# Create vm config
echo "Creating config..."
touch $CONF
cat <<EOF >>$CONF
#id:$ID
vm "${VMNAME}" {
disk $VMOSP
disk $VMHOMEP
owner $OWNERNAME
local interface tap$ID locked lladdr $MAC
memory 1G
disable
#template:$TEMPLATENAME
}
EOF
# set config premissions
chmod 640 $CONF
# Check if tap interface exists
if ! [[ -f /dev/tap$ID ]]
then
echo "Creating new tap interface (there is not one with given id $ID)."
(cd /dev; sh MAKEDEV tap$ID)
fi
# reload vmd
vmctl load $CONF > /dev/null 2>&1
if ! [[ $? -eq 0 ]]
then
echo "New config load failed."
# Restore old config
#cat $VMCFOLD > /etc/vm.conf
rm $CONF
cleanup;
exit 1
fi
echo "Starting vm for the first time to finish configuration..."
# Get vm ip by detecting change in ifconfig
ifconfig > $IFCFOLD
vmctl start $VMNAME
sleep 1
ifconfig > $IFCFNEW
sleep 1
IFIP=$(diff /tmp/if-old /tmp/if-new | grep inet | awk -F' ' '{print $3}')
VMIP="${IFIP%.2}".3
echo "VMs IP: $VMIP"
# Copy ssh key to vm
#printf "Copying ssh keys"
#while true;
#do
# ssh -o StrictHostKeyChecking=no user@$VMIP -t "mkdir ~/.ssh"
# if [[ $? -eq 0 ]]
# then
# printf "."
# sleep 1
# fi
#done
#scp -o StrictHostKeyChecking=no ~/.ssh/id_ed25519.pub user@$VMIP:~/.ssh/authorized_keys
vmctl stop -w $VMNAME > /dev/null
# Save vm info
#echo "$VMNAME,$TEMPLATENAME,$VMIP" >> "$CONFIGPATH" # Put template an ip info
#echo "$VMNAME,$TEMPLATENAME" >> "$CONFIGPATH" # Put template an ip info
# Cleanup
rm $IFCFOLD $IFCFNEW
exit 0