-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkvm-setup.sh
More file actions
436 lines (398 loc) · 15.9 KB
/
Copy pathkvm-setup.sh
File metadata and controls
436 lines (398 loc) · 15.9 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# This contains a collection of commands to use for setting up KVM for building Virtual Machine on a Linux device
#
# BELOW INFO WAS COMPLETED USING THE BELOW OS
# uname -a
#Linux toborrocky-osbornepro-com 5.14.0-570.18.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Fri May 30 18:43:28 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
#
# cat /etc/os-release
#NAME="Rocky Linux"
#VERSION="9.6 (Blue Onyx)"
#ID="rocky"
#ID_LIKE="rhel centos fedora"
#VERSION_ID="9.6"
#PLATFORM_ID="platform:el9"
#PRETTY_NAME="Rocky Linux 9.6 (Blue Onyx)"
#ANSI_COLOR="0;32"
#LOGO="fedora-logo-icon"
#CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
#HOME_URL="https://rockylinux.org/"
#VENDOR_NAME="RESF"
#VENDOR_URL="https://resf.org/"
#BUG_REPORT_URL="https://bugs.rockylinux.org/"
#SUPPORT_END="2032-05-31"
#ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
#ROCKY_SUPPORT_PRODUCT_VERSION="9.6"
#REDHAT_SUPPORT_PRODUCT="Rocky Linux"
#REDHAT_SUPPORT_PRODUCT_VERSION="9.6"
# Install required packages
sudo dnf install -y epel-release
sudo dnf install -y qemu-kvm libvirt virt-manager virt-install bridge-utils virt-top libguestfs-tools bridge-utils virt-viewer
# Verify kvm module is loaded
sudo lsmod | grep kvm
# Tell libvirt service to start automatically and to start
sudo systemctl enable --now libvirtd
# Verify the service is running
systemctl status libvirtd
# Add system user to the KVM group
sudo usermod -aG libvirt $USER
# Create Network Bridge for KVM instances
# By default a network bridge with the name virbr0 is created for NAT
# VMs using this bridge do not have external network access
brctl show
# For external connections we need to create a network bridge
# View interfaces
# nmcli connection show
#NAME UUID TYPE DEVICE
#Wired connection 1 6418b003-7878-4cb3-9880-b2fcddacf906 ethernet enp5s0
#lo d57311b9-a8a3-46af-be55-6c12b932bbd8 loopback lo
#virbr0 45af469e-626d-4a55-9e70-10cc4f7ee379 bridge virbr0
# Define variables for the bridge connection
BR_NAME="br0"
BR_INT="$(ip route get 1.1.1.1 | awk '{print $5}')"
SUBNET_IP="$(hostname -I | cut -f1 -d' ')/24"
GW="$(ip route | awk '/default/ {print $3}')"
DNS1="1.1.1.2"
DNS2="1.0.0.2"
# Allow forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -p
# Verify value. If not what you expect modify `vim /etc/sysctl.conf'
sysctl net.ipv4.ip_forward
# Create the new internface bridge
sudo nmcli connection add type bridge autoconnect yes con-name ${BR_NAME} ifname ${BR_NAME}
#Connection 'br0' (be72a118-aa39-439b-ac11-e5645bfadcc7) successfully added.
#
nmcli con show
#NAME UUID TYPE DEVICE
#enp5s0 6418b003-7878-4cb3-9880-b2fcddacf906 ethernet enp5s0
#br0 6b5e075d-259a-4b74-a9bb-a3796fe4dc39 bridge br0
#lo d57311b9-a8a3-46af-be55-6c12b932bbd8 loopback lo
#virbr0 45af469e-626d-4a55-9e70-10cc4f7ee379 bridge virbr0
# Rename Wired connection 1
sudo nmcli con modify ${BR_INT} connection.id ${BR_INT}
sudo nmcli con down ${BR_INT} && sudo nmcli con up ${BR_INT}
sudo systemctl restart NetworkManager
# Configure the new br0 interface
sudo nmcli connection modify ${BR_NAME} ipv4.addresses ${SUBNET_IP} ipv4.method manual
sudo nmcli connection modify ${BR_NAME} ipv4.gateway ${GW}
sudo nmcli connection modify ${BR_NAME} ipv4.dns ${DNS1} +ipv4.dns ${DNS2}
# Make your main interface a slave to the bridge
sudo nmcli connection delete ${BR_INT}
#Connection 'Wired connection 1' (4555eaba-663a-4fec-a42c-5da716a7e859) successfully deleted.
nmcli con show
#NAME UUID TYPE DEVICE
#br0 6b5e075d-259a-4b74-a9bb-a3796fe4dc39 bridge br0
#lo d57311b9-a8a3-46af-be55-6c12b932bbd8 loopback lo
#virbr0 45af469e-626d-4a55-9e70-10cc4f7ee379 bridge virbr0
sudo nmcli connection add type bridge-slave autoconnect yes con-name ${BR_INT} ifname ${BR_INT} master ${BR_NAME}
#Connection 'Wired connection 1' (7c8fbe0e-a23e-4329-9dad-55a7f405f5ea) successfully added.
nmcli con show
#NAME UUID TYPE DEVICE
#enp5s0 cbcc99e1-c051-4a56-8f64-5dcbbe94786d ethernet enp5s0
#br0 6b5e075d-259a-4b74-a9bb-a3796fe4dc39 bridge br0
#lo d57311b9-a8a3-46af-be55-6c12b932bbd8 loopback lo
#virbr0 45af469e-626d-4a55-9e70-10cc4f7ee379 bridge virbr0
# Start the bridge connection
sudo nmcli connection up br0
#Connection successfully activated (controller waiting for ports) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
# Verify it works
sudo systemctl restart libvirtd
ping -I br0 1.1.1.1
#PING 1.1.1.1 (1.1.1.1) from 10.0.0.244 br0: 56(84) bytes of data.
#64 bytes from 1.1.1.1: icmp_seq=1 ttl=55 time=13.0 ms
#64 bytes from 1.1.1.1: icmp_seq=2 ttl=55 time=12.2 ms
# NOTE: If this ping fails delete the br0 internface and your starting interface. Then start these commands over
# nmcli con del br0 && nmcli con del ${BR_INT}
# Then in the desktop interface go up to the network icon and click Connect. This will get you connected to the internet again. Once that happens start this process over
# Configure the bridge interface
echo 'allow all' > /etc/qemu-kvm/bridge.conf
sudo systemctl restart libvirtd
# Set permissions for libvirt directory to use your setup qemu connection
sudo chown -R $USER:libvirt /var/lib/libvirt/
# If you have exernal storage for vms map it
# In your /etc/fstab file you should have something similar to these entries for your external drives
#/dev/sdd1 /mnt/vm_hosts xfs defaults 0 2
#/dev/sdb1 /mnt/iso_storage ext4 defaults 0 2
rm -rf /var/lib/libvirt/images # Delete the current folder so you can create your symlink
ln -s /mnt/vm_hosts /var/lib/libvirt/images # Where vm hosts get created
# If you have an external drive you save ISOs on I also did the below for accessibility
ln -s /mnt/iso_storage /var/lib/libvirt/iso_storage
sudo chmod 666 /var/lib/libvirt/iso_storage/*
# Command to create an image though GUI may be recommended
cd /var/lib/libvirt
# Make Alma VM from an ISO
sudo virt-install \
--name AlmaLinux10 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/almalinux-10.img,size=40,format=qcow2 \
--os-variant centos-stream9 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--location /var/lib/libvirt/iso_storage/AlmaLinux-10.0-x86_64-dvd.iso
# Make Rocky Linux VM from an ISO
sudo virt-install \
--name RockyLinux \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/rocky.img,size=40,format=qcow2 \
--os-variant rocky9.0 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/Rocky-9.3-x86_64-dvd.iso
# Make RHEL VM from an ISO
sudo virt-install \
--name RHEL9 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/rhel9.img,size=40,format=qcow2 \
--os-variant rhel9.0 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/rhel-9.3-x86_64-dvd.iso
# Make Fedora VM from a live ISO
sudo virt-install \
--name Fedora40 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/fedora40.img,size=40,format=qcow2 \
--os-variant fedora40 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/Fedora-Workstation-Live-x86_64-40-1.14.iso
# Make Ubuntu VM from a live ISO
sudo virt-install \
--name Ubuntu24 \
--ram 3072 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/ubuntu-24.04.02.img,size=40,format=qcow2 \
--os-variant ubuntu24.04 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/ubuntu-24.04.2-live-server-amd64.iso
# Make a CentOS VM from a live ISO
sudo virt-install \
--name CentOSStream9 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/centos-stream9.img,size=40,format=qcow2 \
--os-variant centos-stream9 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/CentOS-Stream-9-latest-x86_64-dvd1.iso
# Make a Zorin VM from an ISO
sudo virt-install \
--name ZorinOS \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/zorin.img,size=40,format=qcow2 \
--os-variant ubuntu20.04 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/Zorin-OS-16.3-Core-64-bit.iso
# Make Linux Mint VM from a live ISO
sudo virt-install \
--name LinuxMint \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/linuxmint.img,size=40,format=qcow2 \
--os-variant ubuntu22.04 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/linuxmint-21.3-cinnamon-64bit.iso
# Make a Pop VM from an ISO
sudo virt-install \
--name PopOS \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/popos.img,size=40,format=qcow2 \
--os-variant ubuntu22.04 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/pop-os_22.04_amd64.iso
# Make a Debian VM from an ISO
sudo virt-install \
--name Debian12 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/debian12.img,size=40,format=qcow2 \
--os-variant debian12 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/debian-12.5.0-amd64-netinst.iso
# Make an MX Linux VM from an ISO
sudo virt-install \
--name MXLinux \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/mxlinux.img,size=40,format=qcow2 \
--os-variant debian12 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/MX-23.2_x64.iso
# Make Kali VM from qemu image
virt-install \
--name Kali \
--memory 4096 \
--vcpus 2 \
--disk path=/mnt/iso_storage/kali-linux-2026.2-qemu-amd64.qcow2,format=qcow2,bus=virtio \
--os-variant debian12 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--import
# Make Kali VM from a live ISO
sudo virt-install \
--name Kali \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/kali-linux-2025.2.img,size=40,format=qcow2 \
--os-variant debian12 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/kali-linux-2025.2-installer-amd64.iso
# NOTE: On Kali you may need to install the below packages in order to make the resize work
sudo apt update
sudo apt install -y spice-vdagent qemu-guest-agent
# Make OpenSUSE VM from a live ISO
sudo virt-install \
--name OpenSUSE \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/opensuse.img,size=40,format=qcow2 \
--os-variant opensuse15.5 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/openSUSE-Leap-15.5-DVD-x86_64.iso
# Make ArchLinux VM from a live ISO
sudo virt-install \
--name ArchLinux \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/archlinux-2025.06.01.img,size=40,format=qcow2 \
--os-variant archlinux \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/archlinux-2025.06.01-x86_64.iso \
--boot cdrom,useserial=on
# Make a Manjaro VM from a live ISO
sudo virt-install \
--name Manjaro \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/manjaro.img,size=40,format=qcow2 \
--os-variant archlinux \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/manjaro-kde-24.0.1-minimal.iso
# Make a NixOS from a VM image
sudo virt-install \
--name NixOS \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/nixos.img,size=40,format=qcow2 \
--os-variant nixos-21.05 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/nixos-24.05.1234-x86_64-linux.iso
#===================
# WINDOWS DEVICES
#===================
# IMPORTANT: Your Windows server will not have a NIC when you first boot it
# To get internet to your Windows Server image you will need to
# 1.) Open Device Manager
# 2.) Right click on the drivers with a Warning icon over them that are unknown and select Update Drivers
# 3.) Select Search my Computer for Updates
# 4.) Select the moutned virtio drive (usually E drive)
# 5,) Now they have internet
# Make Windows Server 2022 from ISO
cd /var/lib/libvirt/iso_storage
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso
cd ..
sudo virt-install \
--name WinServer2022 \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/winserver2022.img,size=50,format=qcow2 \
--os-variant win2k22 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/WindowsServer2022_x64FRE_en-us.iso \
--disk path=/var/lib/libvirt/iso_storage/virtio-win.iso,device=cdrom
# Make Windows Server 2025 from ISO
cd /var/lib/libvirt/iso_storage
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso
cd ..
sudo virt-install \
--name WinServer2025 \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/winserver2025.img,size=50,format=qcow2 \
--os-variant win2k22 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /var/lib/libvirt/iso_storage/WindowsServer2025_x64FRE_en-us.iso \
--disk path=/var/lib/libvirt/iso_storage/virtio-win.iso,device=cdrom
#=====================
# GNS3 VIRTUALIZATION
#=====================
# Install required packages for using a GNS3 VM
sudo dnf install -y qt5-qtbase qt5-qtsvg qt5-qtwebsockets libvirt virt-install bridge-utils wireshark ubridge python3-qt5 python3-qt5-base python3-pyqt5-sip
# Download the latest VMWare Workstation and Fusion image from https://gns3.com/software/download-vm
# Extract the files
unzip GNS3.VM.VMware.Workstation.2.2.56.1.zip
# Remove compressed file
rm -rf -- GNS3.VM.VMware.Workstation.2.2.56.1.zip
# Convert ova file to libvirt preferred file type
tar -xvf 'GNS3 VM.ova'
qemu-img convert -f vmdk -O qcow2 GNS3_VM-disk1.vmdk gns3vm-disk1.qcow2
qemu-img convert -f vmdk -O qcow2 GNS3_VM-disk2.vmdk gns3vm-disk2.qcow2
# Ensure permissions allow qemu to read the location
sudo chown qemu:qemu gns3* GNS3*
# Build the VM
cd /var/lib/libvirt
sudo virt-install \
--connect qemu:///system \
--name GNS3-VM \
--memory 8192 \
--vcpus 4 \
--cpu host-passthrough \
--osinfo linux2022 \
--import \
--disk path=/var/lib/libvirt/iso_storage/gns3vm-disk1.qcow2,format=qcow2,bus=virtio \
--disk path=/var/lib/libvirt/iso_storage/gns3vm-disk2.qcow2,format=qcow2,bus=virtio \
--network network=default,model=virtio \
--graphics vnc \
--video virtio \
--noautoconsole
# Setup your GNS3 GUI tool
/usr/bin/python3 -m pip install --user gns3-gui==2.2.56.1 gns3-server==2.2.56.1
/usr/bin/python3 -m pip install --user --upgrade sip PyQt5-sip
# Verify the modules are there
/usr/bin/python3 -c "import sip; print('sip ok')"
/usr/bin/python3 -c "from PyQt5 import QtWidgets; print('PyQt5 ok')"
# If the packages are not there do
#mkdir -p ~/.local/lib/python3.9/site-packages
#cat > ~/.local/lib/python3.9/site-packages/sip.py <<'EOF'
#from PyQt5.sip import *
#EOF
# Run GNS3 and setup your remote connection to the VM
gns3 &