-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUbuntu KVM guide.txt
486 lines (376 loc) · 12.6 KB
/
Ubuntu KVM guide.txt
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
Linux Guest Configuration:
CPU -> [] DO NOT Copy Host CPU configuration
Disk -> VirtIO
RAM -> 512 MB
Network -> VirtIO (20 GB)
CDROM -> SCSI
Video -> QXL
Boot Options ->
1. VirtIO Disk
2. SCSI CDROM
Windows Guest Configuration:
CPU -> Copy Host CPU configuration
RAM -> 3072 MB
Disk -> VirtIO (40 GB)
CDROM -> IDE
CDROM 2 -> IDE -> VirtIO Drivers ISO
Network -> VirtIO
Video -> QXL
Boot Options ->
1. VirtIO Disk
2. SCSI CDROM
WINDOWS: DO NOT FORGET TO ADD "virtio" DRIVERS ISO DOWNLOADABLE FROM HERE:
WINDOWS: LOAD "viostor" DRIVERS! FOR INSTALLATION!
https://fedoraproject.org/wiki/Windows_Virtio_Drivers
WINDOWS: AFTER INSTALLATION, INSTALL QXL GRAPHICS DRIVER FROM HERE:
http://people.redhat.com/~vrozenfe/qxlwddm/
WINDOWS: EDIT "video" RAM SIZE USING "virsh edit <VMNAME>" AND INCREASE IT TO
4 TIMES LARGER THAN VALUES:
<model type='qxl' ram='262144' vram='262144' vgamem='65536' heads='1'/>
GUIDE ON SAMBA: https://www.tecmint.com/setup-samba-file-sharing-for-linux-windows-clients/
=================================================================
# install cpu-checker, we check if we can safely use kvm
sudo apt-get install cpu-checker
# check if kvm can be used
kvm-ok
# install libvirt and qemu
sudo apt-get install qemu-kvm libvirt-bin
# add users to groups
sudo adduser arcana libvirtd
sudo adduser arcana libvirt
sudo adduser root libvirtd
sudo adduser root libvirt
# reboot
reboot
# install more libvirt tools
sudo apt install virtinst virt-viewer virt-manager
# change default networking configuration to allow static ip ranges
virsh net-edit default
> <range start='192.168.122.100' end='192.168.122.254'/>
# set domain name to "arcana.me"
# add bellow line below <mac address ....
virsh net-edit default
> <domain name="arcana.me"/>
# restart network
virsh net-destroy default
virsh net-start default
# add nested virtualization support
sudo vim /etc/modprobe.d/qemu-system-x86.conf
................
options kvm_intel nested=1
................
sudo vim /etc/modprobe.d/kvm_intel.conf
................
options kvm_intel nested=1
................
===================================================================
# increase size of image
qemu-img resize /var/lib/libvirt/images/web.qcow2 +2G
# stop kvm temporarily
sudo lsmod | grep kvm
sudo rmmod kvm_intel
sudo rmmod kvm
http://people.redhat.com/~vrozenfe/qxlwddm/
https://fedoraproject.org/wiki/Windows_Virtio_Drivers
## ENABLE SHARED FOLDER SUPPORT (view guide below)
=====================================================================
# view running instances
virsh list
# view all instances
virsh list --all
# view all configuration of instance
virsh dumpxml <NAME>
# list qemu instances
sudo ps aux | grep qemu
# allow for shared cloning
Base Machine -> Details -> Disk -> Shareable
# edit network
virsh net-edit default
# restart network
virsh net-destroy default
virsh net-start default
# export VM
virsh dumpxml > export.xml
# import VM
# remember that disks should exist
# so copy them!
virsh define export.xml
###### assign static ip address
virsh net-update default add ip-dhcp-host \
"<host mac='52:54:00:00:00:01' name='bob' ip='192.168.122.45' />" \
--live --config
###### remove static ip address
virsh net-update default delete ip-dhcp-host \
'<host mac="52:54:00:6f:78:f3" ip="192.168.122.222"/>' \
--live --config --parent-index
################ ENABLE SHARED FOLDER #############################
# first set user/group of qemu instances to local user
sudo vim /etc/libvirt/qemu.conf
> user = "arcana"
> group = "arcana"
# now restart libvirt
sudo systemctl restart libvirtd.service libvirt-guests.service libvirt-bin.service
### create a new filesystem device in virt-manager
# Driver -> Default
# Mode -> Mapped
# Source Path -> /
# Target path -> share
## MAP IN GUEST
sudo mkdir -p /share
sudo mount -t 9p -o trans=virtio,version=9p2000.L,rw share /share
##
## MAP IN GUEST USING FSTAB
# first create directory
sudo mkdir -p /share
# add modules to initramfs
sudo vi /etc/initramfs-tools/modules
> 9p
> 9pnet
> 9pnet_virtio
# regenerate initramfs
sudo update-initramfs -u
# add entry to fstab
sudo vim /etc/fstab
> share /share 9p trans=virtio,version=9p2000.L,rw 0 0
########################################################################
########################################################################
########################################################################
########################################################################
################ CREATE SAMBA SERVER #############################
## clone a machine (ubuntu 16.04 xenial)
Linux Guest Configuration:
CPU -> Copy Host CPU configuration
Name -> samba-server
Memory -> 512 MB
Disk -> VirtIO (20 GB)
Network -> VirtIO
CDROM -> SCSI
Video -> QXL
Boot Options ->
1. VirtIO Disk
2. SCSI CDROM
Add Hardware -> Filesystem ->
Driver -> Default
Mode -> Mapped
Source Path -> /
Target path -> share
# assign static ip address
virsh net-update default add ip-dhcp-host \
"<host mac='52:54:00:4b:56:5d' name='samba-server.arcana.me' ip='192.168.122.2' />" \
--live --config
####### GO TO GUEST CONSOLE
# first update system
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
# reboot
sudo reboot
# install some tools
sudo apt-get autoremove
sudo apt-get install vim nano screen htop psmisc dnsutils wget \
xz-utils openssh-server ufw mlocate \
uuid-runtime tcpdump man-db \
traceroute w3m bridge-utils iputils-arping \
dnsmasq git rar unrar
# configure firewall
sudo systemctl enable ufw
sudo systemctl restart ufw
sudo ufw enable
# configure ssh
sudo vim /etc/ssh/sshd_config
...
Port 2122
PermitRootLogin no
...
sudo ufw reload
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2122/tcp
sudo ufw reload
sudo systemctl enable sshd
sudo systemctl restart sshd
######## SSH INTO SAMBA SERVER!
# update mlocate
sudo updatedb
# set default editor to vim
sudo update-alternatives --config editor
# set hostname
sudo hostnamectl set-hostname samba-server
sudo vim /etc/hosts
> 127.0.0.1 samba-server
# reboot
sudo reboot
######### Enable shared folder
---> follow guide on above
# install samba server
sudo apt-get install samba samba-common python-glade2
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.org
# create shared folder configuration
sudo vim /etc/samba/smb.conf
.....
[general]
map to guest = bad user
[Public]
comment = Public
path = /share
browsable = yes
writable = yes
guest ok = yes
read only = no
force user = arcana
force group = arcana
.....
# enable and restart samba service
sudo systemctl restart smbd
sudo systemctl enable smbd
# allow ports through firewall
sudo ufw reload
sudo ufw allow 139/tcp
sudo ufw allow 445/tcp
sudo ufw reload
################ ENABLE GPU PASSTHROUGH (nvidia) #############################
###### REMEMBER TO HAVE 2 HDMI CARDS: ONE FOR "intel"
###### WHICH WILL BE USED ON LINUX DESKTOP
###### ONE CONNECTED TO GPU WHICH WILL BE PASSED THROUGH
###### TO WINDOWS VM.
# guide: https://scottlinux.com/2016/08/28/gpu-passthrough-with-kvm-and-debian-linux/
# guide: https://www.pugetsystems.com/labs/articles/Multiheaded-NVIDIA-Gaming-using-Ubuntu-14-04-KVM-585/
# guide: https://bbs.archlinux.org/viewtopic.php?id=162768
# guide: https://ycnrg.org/vga-passthrough-with-ovmf-vfio/
# add iommu support to GRUB_CMDLINE_LINUX_DEFAULT
# for intel: intel_iommu=on
# for amd: amd_iommu=on
sudo nano -w /etc/default/grub
....................................
GRUB_CMDLINE_LINUX_DEFAULT+="intel_iommu=on vfio_iommu_type1.allow_unsafe_interrupts=1 nogpumanager modprobe.blacklist=nouveau,radeon,amdgpu,nvidia,nvidia_drm,nvidia_uvm,nvidia_modeset blacklist=nouveau,radeon,amdgpu,nvidia,nvidia_drm,nvidia_uvm,nvidia_modeset nomodeset"
GRUB_CMDLINE_LINUX+="intel_iommu=on vfio_iommu_type1.allow_unsafe_interrupts=1 nogpumanager modprobe.blacklist=nouveau,radeon,amdgpu,nvidia,nvidia_drm,nvidia_uvm,nvidia_modeset blacklist=nouveau,radeon,amdgpu,nvidia,nvidia_drm,nvidia_uvm,nvidia_modeset nomodeset"
GRUB_GFXMODE=1920x1080
GRUB_GFXPAYLOAD_LINUX=keep
....................................
# regenerate grub.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg
# blacklist nvidia driver
sudo nano -w /etc/modprobe.d/blacklist.conf
....................................
# used for KVM passthrough
blacklist nouveau
blacklist radeon
blacklist amdgpu
blacklist nvidia
blacklist nvidia_drm
blacklist nvidia_uvm
blacklist nvidia_modeset
remove nvidia rmmod nvidia_uvm nvidia_drm nvidia_modeset nvidia
install nouveau /bin/false
install radeon /bin/false
install amdgpu /bin/false
install nvidia /bin/false
install nvidia_drm /bin/false
install nvidia_uvm /bin/false
install nvidia_modeset /bin/false
....................................
# comment nvidia drivers
sudo vim /etc/modprobe.d/nvidia-graphics-drivers.conf
....................................
# {comment all lines}
....................................
# load some necessary drivers
sudo nano -w /etc/modules
....................................
pci_stub
vfio
vfio_iommu_type1
vfio_pci
kvm
kvm_intel
vfio_virqfd
....................................
# get device ids (inside bracets []) from lscpi
lspci -nn | grep NV
######## SAMPLE OUTPUT:
## 01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GK104 [GeForce GTX 660 Ti] [10de:1183] (rev a1)
## 01:00.1 Audio device [0403]: NVIDIA Corporation GK104 HDMI Audio Controller [10de:0e0a] (rev a1)
##
## in this case we are interested in [10de:1183] and [10de:0e0a]
########
# blacklist nvidia drivers using pci stub
sudo nano -w /etc/initramfs-tools/modules
....................................
pci_stub ids=10de:1183,10de:0e0a
....................................
#####################################################################################
# restore drivers using:
# sudo cp -rfv /home/arcana/nvidia-backup/$(uname -r)/* /lib/modules/$(uname -r)/updates/dkms/
#####################################################################################
# verify where nvidia kernel modules are located
ll /lib/modules/$(uname -r)/updates/dkms/
# backup nvidia drivers
mkdir -p ~/nvidia-backup/$(uname -r)
cp -rfv /lib/modules/$(uname -r)/updates/dkms/nvidia* ~/nvidia-backup/$(uname -r)/
# remove nvidia drivers
sudo rm -rfv /lib/modules/$(uname -r)/updates/dkms/nvidia*
#####################################################################################
# regenerate initramfs
sudo update-initramfs -u
#####################################################################################
# verify initramfs no longer contains nvidia drivers
lsinitramfs /boot/initrd.img-$(uname -r) | grep nv
#####################################################################################
# also add pci stubs to bootloader
sudo nano -w /etc/default/grub
....................................
GRUB_CMDLINE_LINUX_DEFAULT+="pci-stub.ids=10de:1183,10de:0e0a"
GRUB_CMDLINE_LINUX+="pci-stub.ids=10de:1183,10de:0e0a"
....................................
# regenerate grub.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg
# disable nvidia services
sudo systemctl disable nvidia-persistenced.service
sudo systemctl disable nvidia-prime.service
sudo systemctl mask nvidia-persistenced.service
sudo systemctl mask nvidia-prime.service
sudo systemctl stop nvidia-persistenced.service
sudo systemctl stop nvidia-prime.service
sudo systemctl disable gpu-manager.service
sudo systemctl stop gpu-manager.service
# edit xorg.conf
sudo vim /etc/X11/xorg.conf
....................................
# change {Inactive "intel"} to {Inactive "nvidia"}
# change {Screen 0 "nvidia"} to {Screen 0 "intel"}
# comment all "nvidia" stuff
....................................
# set primary display to intel
sudo prime-select intel
# reboot system
sudo reboot
# change primary display in BIOS
# mine is "Asys Z87 Pro"
Advanced -> System Agent Configuration ->
Graphics Configuration -> Primary Display -> iGPU
# verify nvidia is not loaded
sudo lsmod | grep nvidia
# check if IOMMU is enabled
dmesg | grep -e DMAR -e IOMMU
# check iommu groups
ll /sys/kernel/iommu_groups/
# check devices claimed by stub
dmesg | grep stub
# add pci ids to vfio.conf
sudo nano -w /etc/modprobe.d/vfio.conf
....................................
options vfio-pci ids=10de:1183,10de:0e0a
....................................
# regenerate initramfs
sudo update-initramfs -u
# enable OVMF support
sudo nano -w /etc/libvirt/qemu.conf
....................................
nvram = [ "/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd",
"/usr/share/AAVMF/AAVMF_CODE.fd:/usr/share/AAVMF/AAVMF_VARS.fd"
]
user = "root"
group = "root"
....................................
# reboot system
sudo reboot