forked from crc-org/snc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_libvirt.template
230 lines (202 loc) · 6.07 KB
/
crc_libvirt.template
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
#!/bin/sh
set +x
prerequisites()
{
# Check if virtualization is supported
ls /dev/kvm 2> /dev/null
if [ $? -ne 0 ]
then
echo "Your system doesn't support virtualization"
exit 1
fi
# Install required dependencies
sudo yum install -y libvirt libvirt-devel libvirt-daemon-kvm qemu-kvm
# Enable IP forwarding
sudo sysctl net.ipv4.ip_forward=1
# Get active Firewall zone option
systemctl is-active firewalld
if [ $? -ne 0 ]
then
echo "Your system doesn't have firewalld service running"
exit 1
fi
activeZone=$(firewall-cmd --get-active-zones | head -n 1)
sudo firewall-cmd --zone=$activeZone --add-source=192.168.126.0/24
sudo firewall-cmd --zone=$activeZone --add-port=16509/tcp
# Configure default libvirt storage pool
sudo virsh pool-info 'default'
if [ $? -ne 0 ]
then
sudo virsh pool-define /dev/stdin <<EOF
<pool type='dir'>
<name>default</name>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool>
EOF
sudo virsh pool-start default
sudo virsh pool-autostart default
fi
# Set up NetworkManager DNS overlay
dnsconf=/etc/NetworkManager/conf.d/crc-libvirt-dnsmasq.conf
local dnschanged=""
if ! [ -f "${dnsconf}" ]; then
echo -e "[main]\ndns=dnsmasq" | sudo tee "${dnsconf}"
dnschanged=1
fi
dnsmasqconf=/etc/NetworkManager/dnsmasq.d/openshift.conf
if ! [ -f "${dnsmasqconf}" ]; then
echo server=/ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain/192.168.126.1 | sudo tee "${dnsmasqconf}"
echo address=/apps-ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain/192.168.126.11 | sudo tee -a "${dnsmasqconf}"
dnschanged=1
fi
if [ -n "$dnschanged" ]; then
sudo systemctl restart NetworkManager
fi
# Create an entry in the /etc/host
grep -q 'libvirt.default' /etc/hosts
if [ $? -ne 0 ]
then
echo '192.168.126.1 libvirt.default' | sudo tee --append /etc/hosts
fi
}
cluster_create()
{
sudo virsh net-define /dev/stdin <<EOF
<network>
<name>ReplaceMeWithCorrectVmName</name>
<uuid>b56e5d7f-296e-41ea-9bc3-5d374f8a168f</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='tt0' stp='on' delay='0'/>
<domain name='ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain' localOnly='yes'/>
<dns>
<srv service='etcd-server-ssl' protocol='tcp' domain='ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain' target='etcd-0.ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain' port='2380' weight='10'/>
<host ip='192.168.126.11'>
<hostname>api.ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain</hostname>
<hostname>api-int.ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain</hostname>
<hostname>etcd-0.ReplaceMeWithCorrectVmName.ReplaceMeWithCorrectBaseDomain</hostname>
</host>
</dns>
<ip family='ipv4' address='192.168.126.1' prefix='24'>
<dhcp>
ReplaceMeWithCorrectHost
</dhcp>
</ip>
</network>
EOF
sudo virsh net-start ReplaceMeWithCorrectVmName
size=$(stat -Lc%s ReplaceMeWithCorrectVmName.qcow2)
sudo virsh vol-create-as default ReplaceMeWithCorrectVmName $size --format qcow2
sudo virsh vol-upload --sparse --pool default ReplaceMeWithCorrectVmName ReplaceMeWithCorrectVmName.qcow2
sudo virsh define /dev/stdin <<EOF
<domain type='kvm'>
<name>crc</name>
<memory unit='GiB'>10</memory>
<vcpu>4</vcpu>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<cpu mode='host-passthrough' check='none'/>
<clock offset='utc'/>
<devices>
<disk type='volume' device='disk'>
<driver name='qemu' type='qcow2'/>
<source pool='default' volume='crc'/>
<target dev='vda' bus='virtio'/>
</disk>
<controller type='virtio-serial'/>
<interface type='network'>
ReplaceMeWithCorrectMac
<source network='ReplaceMeWithCorrectVmName'/>
<model type='virtio'/>
</interface>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='pty'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
</graphics>
<video>
<model type='cirrus'/>
</video>
<memballoon model='virtio'/>
<rng model='virtio'>
<backend model='random'>/dev/random</backend>
</rng>
</devices>
</domain>
EOF
echo "Cluster created successfully use '$0 start' to start it"
}
cluster_start()
{
sudo virsh start crc
echo "You need to wait around 4-5 mins till cluster is in healthy state"
echo "Use provided kubeconfig to check pods status before using this cluster"
}
cluster_stop()
{
sudo virsh shutdown crc
echo "Cluster stopped"
}
cluster_delete()
{
sudo virsh destroy crc
sudo virsh undefine crc
sudo virsh vol-delete --pool default crc
sudo virsh net-destroy ReplaceMeWithCorrectVmName
sudo virsh net-undefine ReplaceMeWithCorrectVmName
}
usage()
{
usage="$(basename "$0") [[create | start | stop | delete] | [-h]]
where:
create - Create the cluster resources
start - Start the cluster
stop - Stop the cluster
delete - Delete the cluster
-h - Usage message
"
echo "$usage"
}
main()
{
if [ "$#" -ne 1 ]; then
usage
exit 0
fi
while [ "$1" != "" ]; do
case $1 in
create ) prerequisites
cluster_create
;;
start ) cluster_start
;;
stop ) cluster_stop
;;
delete ) cluster_delete
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
}
main "$@"; exit