-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart
executable file
·162 lines (142 loc) · 4.47 KB
/
start
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
#!/bin/bash
set -e
err_report() {
echo "Error at $BASH_SOURCE on line $1!"
echo "GPU/VGPU_UUID: $GPU / $VGPU_UUID"
if [[ -n "$VGPU_UUID" ]]; then
echo "Deleting intel vGPU..."
delete_intel_vGPU
fi
}
trap 'err_report $LINENO' ERR
create_intel_vGPU() {
GPU=""
VGPU_UUID=""
# Finding the Intel GPU and choosing the one with highest weight value
for i in $(find /sys/devices/pci* -name 'mdev_supported_types'); do
for y in $(find "$i" -name 'description'); do
local WEIGHT=$(cat $y | tail -1 | cut -d ' ' -f 2)
if [ "$WEIGHT" -gt "0" ]; then
GPU=$(echo $y | cut -d '/' -f 1-7)
fi
done
done
if [[ -z "$GPU" ]]; then
echo "Note: No Intel GPU found" >&2
return
fi
# Now that GPU has been set, we can safely set VGPU_UUID
VGPU_UUID=$(uuidgen)
echo "$VGPU_UUID" > "$GPU/create"
if ! grep -o "$VGPU_UUID" >/dev/null < <(ls "$GPU/devices"); then
echo "Note: Could not create vGPU, $VGPU_UUID not found in $GPU/devices" >&2
VGPU_UUID=""
GPU=""
fi
}
delete_intel_vGPU() {
if [[ -z "$GPU" || -z "$VGPU_UUID" ]]; then
echo "Tried to delete vGPU, but invalid GPU,UUID pair: $GPU,$VGPU_UUID" >&2
return
fi
echo "1" > "$GPU/devices/$VGPU_UUID/remove"
VGPU_UUID=""
GPU=""
}
# If not already run as sudo, then run as sudo
if [[ "$EUID" != "0" ]]; then
sudo "$BASH_SOURCE" "$@"
exit 0
fi
# Check for intel when using Nvidia Optimus
if which prime-select >/dev/null && [[ "$(prime-select query)" != "intel" ]]; then
echo "\`prime-select query\` gave \"$(prime-select query)\". It must be \"intel\" for this to work!"
exit 1
fi
cd "$(dirname $([ -L "$BASH_SOURCE" ] && readlink -f "$BASH_SOURCE" || echo "$BASH_SOURCE"))"
# Setup /dev/md0 if it hasn't been setup already
if [[ ! -b /dev/md0 ]]; then
./create_raid_array
sleep 0.25 # Give time for /dev/md0p{1,2} to be created
fi
./verify_raid_array
# Use virtio (For networking essentially)
VIRTIO="true"
# Get vGPU VGPU_UUID if any, and mark vGPU desires
if [[ "$1" == "--no-igpu" ]]; then
USE_VGPU="false"
VGPU_UUID=""
else
USE_VGPU="true"
create_intel_vGPU # Sets VGPU_UUID on success
# Don't use vGPU is not vGPU VGPU_UUID was found
if [[ -z "$VGPU_UUID" ]]; then
echo "Could not find vGPU, using software emulated graphics for now"
USE_VGPU="false"
fi
fi
# Pass intel vGPU GVT-g device to the VM if the vGPU VGPU_UUID exists
if [[ -n "$VGPU_UUID" ]]; then
# Mount vGPU
MDEV="-device vfio-pci,id=hostdev0,sysfsdev=/sys/bus/mdev/devices/$VGPU_UUID"
# If we want to use the vGPU, mount with display=on
if [[ "$USE_VGPU" == "true" ]]; then
VGPU_ROM="./vbios_gvt_uefi.rom"
if [[ -f "$VGPU_ROM" ]]; then
MDEV="$MDEV,display=on,x-igd-opregion=on,ramfb=on,driver=vfio-pci-nohotplug,romfile=$VGPU_ROM"
else
# If we don't have the ROM, then we can still mount the vGPU, but we can't use it
echo "Tried to use iGPU, but $VGPU_ROM not found! Defaulting to software emulated graphics"
USE_VGPU="false"
fi
else
MDEV="$MDEV,display=off"
fi
else
# No vGPU found, so don't mount it
MDEV=""
fi
# No VGA if using vGPU, as the vGPU will be the display device
# Otherwise, use standard VGA
if [[ "$USE_VGPU" == "true" ]]; then
VGA="-vga none -display gtk,gl=on"
else
# Use virtio always since it has VGA fallback
#VGA="-vga virtio -display gtk"
#VGA="-device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1"
VGA="-display gtk -device VGA,vgamem_mb=32"
fi
# Pass audio to the PulseAudio shared-socket
if [[ -S /tmp/shared-pulse-socket ]]; then
AUDIO="-device ich9-intel-hda -device hda-micro,audiodev=snd0 -audiodev pa,id=snd0,server=unix:/tmp/shared-pulse-socket"
else
echo "/tmp/shared-pulse-socket not found, your VM will not have audio"
AUDIO=""
fi
# Use virt-manager bridge to access the internet
NIC="-net bridge,br=virbr0 -net nic"
if [[ "$VIRTIO" == "true" ]]; then
NIC="$NIC,model=virtio"
fi
# If possible, mount the virtio iso for installing virtio drivers
CDROMS=""
if [[ -f "./virtio-win.iso" ]]; then
CDROMS="$CDROMS -cdrom ./virtio-win.iso"
fi
# Run the emulator
QEMU_CMD=$(cat <<EOF
qemu-system-x86_64 \
-nodefaults \
-bios /usr/share/qemu/OVMF.fd \
-drive file=/dev/md0,media=disk,format=raw \
-cpu host -smp 6,sockets=1,cores=3,threads=2 -enable-kvm \
-m 8G \
-device virtio-balloon-pci,id=balloon0 \
$VGA $MDEV $AUDIO $NIC $CDROMS -msg timestamp=on
EOF
)
echo "$QEMU_CMD"
$QEMU_CMD
if [[ -n "$VGPU_UUID" ]]; then
delete_intel_vGPU
fi