-
Notifications
You must be signed in to change notification settings - Fork 0
/
makevms.sh
318 lines (281 loc) · 7.65 KB
/
makevms.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
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
#!/bin/bash
# Author: Felipe Pfeifer Rubin
# Contact: felipe.rubin@edu.pucrs.br
# date +%s | md5sum | head -c 6 | sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/' | sed -e 's/^/52:54:00:/'
# perl -e 'for ($i=0;$i<5;$i++){@m[$i]=int(rand(256));} printf "02:%X:%X:%X:%X:%X\n",@m;'
op="$1"
name="$2"
PUBLIC_ADDRESS="10.32.45.215/24"
PUBLIC_GATEWAY="10.32.45.254"
PUBLIC_DNS="8.8.8.8,8.8.4.4"
create(){
name="$1"
case $name in
gateway)IP="10.0.0.2";;
storage)IP="10.0.0.3";;
controller)IP="10.0.0.4";;
kvm1)IP="10.0.0.5";;
kvm2)IP="10.0.0.6";;
esac
if [ ! -d iso ]; then
mkdir iso
fi
wget -Lnc https://cloud-images.ubuntu.com/releases/18.04/release/ubuntu-18.04-server-cloudimg-amd64.img -P iso/
# wget -Lnc https://releases.ubuntu.com/18.04/ubuntu-18.04.4-live-server-amd64.iso -P iso/
if [ ! -d vms/$name ]; then
mkdir -p vms/$name;
fi;
cp iso/ubuntu-18.04-server-cloudimg-amd64.img vms/$name/ubuntu18.qcow2
qemu-img resize vms/$name/ubuntu18.qcow2 32G
ssh-keygen -t rsa -b 4096 -N "" -f "vms/$name/key"
# Cloud-init files can be validated with:
# cloud-init devel schema --config-file <file.yaml>
# openssl passwd -6 password ubuntu -salt 4096
cat > vms/$name/meta-data <<EOF
local-hostname: $name
EOF
cat > vms/$name/user-data <<EOF
#cloud-config
users:
- name: ubuntu
ssh-authorized-keys:
- $(cat vms/$name/key.pub)
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash
passwd: $(openssl passwd -6 ubuntu)
lock_passwd: false
write_files:
- path: /etc/netplan/02-management.yaml
permissions: '0644'
content: |
network:
version: 2
renderer: networkd
ethernets:
ens4:
link-local: []
dhcp4: no
addresses: [$IP/24]
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
EOF
if [ "$name" == "gateway" ]; then
cat >> vms/$name/user-data <<EOF
- path: /etc/netplan/03-public-addr.yaml
permissions: '0644'
content: |
network:
version: 2
renderer: networkd
ethernets:
ens5:
dhcp4: no
link-local: []
addresses: [$PUBLIC_ADDRESS]
gateway4: $PUBLIC_GATEWAY
nameservers:
addresses: [$PUBLIC_DNS]
EOF
fi
cat >> vms/$name/user-data <<EOF
runcmd:
- echo "AllowUsers ubuntu" >> /etc/ssh/sshd_config
- sudo systemctl daemon-reload
- sudo systemctl restart ssh
- sudo truncate -s 0 /etc/machine-id
- sudo rm /var/lib/dbus/machine-id
- sudo ln -s /var/lib/dbus/machine-id /etc/machine-id
- sudo netplan apply
- sudo echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
- sudo echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf
- sudo echo 'net.ipv6.conf.lo.disable_ipv6 = 1' >> /etc/sysctl.conf
- sudo reboot
EOF
# - echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
# - systemd-machine-id-setup
chmod 600 vms/$name/key*
genisoimage \
-output vms/$name/$name-cidata.iso \
-volid cidata \
-joliet \
-rock vms/$name/user-data \
vms/$name/meta-data
}
destroy() {
name="$1"
virsh destroy $name && virsh undefine $name
rm -rf vms/$name;
case $name in
gateway)IP="10.0.0.2";;
storage)IP="10.0.0.3";;
controller)IP="10.0.0.4";;
kvm1)IP="10.0.0.5";;
kvm2)IP="10.0.0.6";;
esac
ssh-keygen -R $IP;
}
# ,mac=00:11
provision() {
name="$1"
if [ "$name" == "gateway" ]; then
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name $name \
--ram 2048 \
--vcpus=2 \
--os-type linux \
--os-variant ubuntu18.04 \
--disk path=vms/$name/ubuntu18.qcow2,format=virtio \
--disk vms/$name/$name-cidata.iso,format=raw,device=cdrom \
--import \
--network network=default,model=virtio,mac=02:99:E5:CB:35:F6 \
--network network=privatenet,model=virtio,mac=02:BC:B9:48:1C:DB \
--network bridge=br0,model=virtio,mac=02:EB:B6:5B:E5:3C \
--network bridge=br0,model=virtio,mac=02:6F:D5:DB:C2:1F \
--noautoconsole
elif [ "$name" == "storage" ]; then
# Storage
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name $name \
--ram 4096 \
--vcpus=2 \
--os-type linux \
--os-variant ubuntu18.04 \
--disk path=vms/$name/ubuntu18.qcow2,format=virtio \
--disk vms/$name/$name-cidata.iso,format=raw,device=cdrom \
--disk=pool=default,size=200,format=qcow2,bus=virtio \
--import \
--network network=default,model=virtio,mac=02:F7:19:76:5F:9C \
--network network=privatenet,model=virtio,mac=02:52:87:1F:6E:CB \
--network bridge=br0,model=virtio,mac=02:4:75:F:A9:D \
--network bridge=br0,model=virtio,mac=02:76:2:5B:CB:CC \
--noautoconsole
elif [ "$name" == "controller" ]; then
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name $name \
--ram 16384 \
--vcpus=4 \
--os-type linux \
--os-variant ubuntu18.04 \
--disk path=vms/$name/ubuntu18.qcow2,format=virtio \
--disk vms/$name/$name-cidata.iso,format=raw,device=cdrom \
--import \
--network network=default,model=virtio,mac=02:CE:57:2B:FB:C8 \
--network network=privatenet,model=virtio,mac=02:E5:83:72:DA:43 \
--network bridge=br0,model=virtio,mac=02:2F:4A:AB:29:29 \
--network bridge=br0,model=virtio,mac=02:CE:59:5D:5C:E3 \
--noautoconsole
# qemu -net nic,model=virtio,mac=... -net tap,ifname
elif [ "$name" == "kvm1" ]; then
# KVM 1
# --enable-kvm \
virt-install \
--connect qemu:///system \
--virt-type kvm \
--accelerate \
--cpu host-passthrough \
--name $name \
--ram 8192 \
--vcpus=4 \
--os-type linux \
--os-variant ubuntu18.04 \
--disk path=vms/$name/ubuntu18.qcow2,format=virtio \
--disk vms/$name/$name-cidata.iso,format=raw,device=cdrom \
--import \
--network network=default,model=virtio,mac=02:45:22:99:81:3C \
--network network=privatenet,model=virtio,mac=02:65:AC:8B:16:A2 \
--network bridge=br0,model=virtio,mac=02:24:5D:15:B5:ED \
--network bridge=br0,model=virtio,mac=02:50:9E:13:C8:0 \
--noautoconsole
elif [ "$name" == "kvm2" ]; then
virt-install \
--connect qemu:///system \
--virt-type kvm \
--accelerate \
--cpu host-passthrough \
--name $name \
--ram 8192 \
--vcpus=4 \
--os-type linux \
--os-variant ubuntu18.04 \
--disk path=vms/$name/ubuntu18.qcow2,format=virtio \
--disk vms/$name/$name-cidata.iso,format=raw,device=cdrom \
--import \
--network network=default,model=virtio,mac=02:DA:C5:C9:EA:90 \
--network network=privatenet,model=virtio,mac=02:6:32:DD:B8:69 \
--network bridge=br0,model=virtio,mac=02:31:F2:C5:F8:86 \
--network bridge=br0,model=virtio,mac=02:66:6B:D6:C1:3C \
--noautoconsole
fi
echo "virsh domifaddr $name"
# MAX_TRIES=3
# addr=$(virsh domifaddr $name | awk 'NR==3{print $4}' | cut -d '/' -f 1)
# virsh domifaddr $name
# while [ "$addr" == "" ] && [ "$MAX_TRIES" -gt 0 ]; do
# sleep 1
# addr=$(virsh domifaddr $name | awk 'NR==3{print $4}' | cut -d '/' -f 1)
# MAX_TRIES=$(($MAX_TRIES-1))
# done
}
true_op="$1"
true_name="$2"
if [ "$op" == "-c" ]; then
create $true_name
elif [ "$op" == '-d' ]; then
destroy $true_name
elif [ "$op" == "-p" ]; then
provision $true_name
elif [ "$op" == "-dd" ]; then
set +e
destroy gateway
destroy storage
destroy controller
destroy kvm1
destroy kvm2
rm -rf vms
elif [ "$op" == "-a" ]; then
set +e
destroy gateway
destroy storage
destroy controller
destroy kvm1
destroy kvm2
rm -rf vms
create gateway
create storage
create controller
create kvm1
create kvm2
virsh net-destroy default
virsh net-start default
provision gateway
provision storage
provision controller
provision kvm1
provision kvm2
set -e
elif [ "$op" == "-k" ]; then
set +e
destroy gateway
destroy storage
destroy controller
destroy kvm1
destroy kvm2
rm -rf vms
create gateway
create storage
create controller
create kvm1
create kvm2
provision gateway
provision storage
provision controller
provision kvm1
provision kvm2
fi