This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·282 lines (239 loc) · 7.42 KB
/
run.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
#!/usr/bin/env bash
export LC_ALL=C
version="2.0.1"
launcher=""
name=""
disk=""
ovmfPath="/usr/share/OVMF"
ovmfCode="${ovmfPath}/OVMF_CODE.fd"
ovmfVars="${ovmfPath}/OVMF_VARS.fd"
diskDir="$PWD/VMs"
winDir="$PWD/Windows"
macDir="$PWD/macOS"
openCoreVersion="16"
drives=""
machine="q35"
extra=""
audio="-audiodev pa,id=snd0 -device ich9-intel-hda -device hda-output,audiodev=snd0"
cpuTweaks=""
graphics="virtio-vga-gl"
peripherals="-device virtio-keyboard-pci -device virtio-tablet-pci"
usb="qemu-xhci,p2=8,p3=8"
network="virtio-net-pci"
diskInterface="-device virtio-blk-pci,drive=drive0,scsi=off"
cpuInfo=$(cat /proc/cpuinfo | grep Intel)
function usage()
{
echo "Virtainer v${version} - All valid options"
echo "<config>.conf - Starts a VM with the choosed configuration file."
exit 0
}
function loadDefaultConfig()
{
if [ -z ${ram} ]; then
ram="2G"
fi
if [ -z ${display} ]; then
display="sdl"
fi
if [ -z ${cores} ]; then
cores="2"
fi
if [ -z ${threads} ]; then
threads="2"
fi
if [ -z ${cpu} ]; then
cpu="max"
fi
if [ -z ${accelerated_graphics} ]; then
accelerated_graphics="on"
fi
if [ -z ${optimize_system} ]; then
optimize_system="linux"
fi
if [ -z ${firmware} ]; then
firmware="efi"
fi
if [ -z ${secure_boot} ]; then
secure_boot="off"
fi
if [ -z ${nested_virtualization} ]; then
nested_virtualization="off"
fi
if [ -z ${disk_size} ]; then
disk_size="40G"
fi
if [ -z ${disk_format} ]; then
disk_format="qcow2"
fi
if [ -z ${cache} ]; then
cache="none,aio=native"
fi
if [ -z ${snapshot} ]; then
snapshot="off"
fi
}
function enableNestedVirtualization()
{
if [[ $cpuInfo == *"Intel"* ]]; then
nestedIntel=$(cat /sys/module/kvm_intel/parameters/nested)
if [ ${nestedIntel} == "N" ] || [ ${nestedIntel} == "0" ]; then
sudo modprobe -r kvm_intel
sudo modprobe kvm_intel nested=1
fi
extra="${extra} -device intel-iommu"
else
nestedAmd=$(cat /sys/module/kvm_amd/parameters/nested)
if [ ${nestedAmd} == "N" ] || [ ${nestedAmd} == "0" ]; then
sudo modprobe -r kvm_amd
sudo modprobe kvm_amd nested=1
fi
extra="${extra} -device amd-iommu"
fi
}
function disableNestedVirtualization()
{
if [[ $cpuInfo == *"Intel"* ]]; then
nestedIntel=$(cat /sys/module/kvm_intel/parameters/nested)
if [ ${nestedIntel} == "Y" ] || [ ${nestedIntel} == "1" ]; then
sudo modprobe -r kvm_intel
sudo modprobe kvm_intel nested=0
fi
else
nestedAmd=$(cat /sys/module/kvm_amd/parameters/nested)
if [ ${nestedAmd} == "Y" ] || [ ${nestedAmd} == "1" ]; then
sudo modprobe -r kvm_amd
sudo modprobe kvm_amd nested=0
fi
fi
}
function setupDisk()
{
if [ ${optimize_system} != "macos" ]; then
drives="${drives} -drive media=cdrom,index=0,file=${boot_image}"
fi
}
function start()
{
echo "Virtainer v${version} - Starting VM..."
# Check if VM configuration file exists
if [ ! -e "${name}.conf" ]; then
echo "The selected configuration file does not exist!"
exit 0
fi
# Create VMs directory if it doesn't exist
if [ ! -e ${diskDir} ]; then
mkdir ${diskDir}
fi
# Use virtio-vga without VirGL if accelerated graphics are explicitly disabled
if [ ${accelerated_graphics} == "off" ]; then
graphics="virtio-vga"
fi
# Optimize settings for certain operating systems
if [ ${optimize_system} == "linux" ]; then
extra="-device virtio-rng-pci,rng=rng0 -object rng-random,id=rng0,filename=/dev/urandom"
elif [ ${optimize_system} == "windows" ]; then
# Create Windows directory if it doesn't exist
if [ ! -e ${winDir} ]; then
mkdir ${winDir}
fi
# Download VirtIO guest tools ISO if it doesn't exist
if [ ! -f "${winDir}/virtio-win.iso" ]; then
wget "https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/virtio-win.iso" -O "${winDir}/virtio-win.iso"
fi
drives="-drive media=cdrom,index=1,file=${winDir}/virtio-win.iso"
cpuTweaks=",hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time"
extra="-no-hpet"
elif [ ${optimize_system} == "macos" ]; then
# SMEP freezes the macOS installer! (Seen on Monterey)
cpuTweaks=",vendor=GenuineIntel,+kvm_pv_unhalt,+kvm_pv_eoi,+hypervisor,+invtsc,+movbe,+pcid,+sse3,+ssse3,+sse4.2,+popcnt,+aes,+avx2,+avx,+fma,+bmi1,+bmi2,+xsave,+xsavec,+xsaveopt,+xgetbv1,check"
if [[ $cpuInfo == *"AMD"* ]]; then
cpuTweaks="${cpuTweaks},+fma4"
fi
# pwetty pwease dowont steal owour wowork
extra="-device isa-applesmc,osk=ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
drives="-drive id=drive1,if=none,cache=${cache},format=raw,file=$PWD/macOS/OpenCore.iso -device virtio-blk-pci,drive=drive1,scsi=off -drive id=drive2,if=none,cache=${cache},format=dmg,file=${macDir}/BaseSystem.dmg -device virtio-blk-pci,drive=drive2,scsi=off"
if [ ! -e ${macDir} ]; then
mkdir ${macDir}
fi
if [ ! -f "${macDir}/OpenCore.iso" ]; then
wget "https://github.com/thenickdude/KVM-Opencore/releases/download/v${openCoreVersion}/OpenCore-v${openCoreVersion}.iso.gz" -O "${macDir}/OpenCore.iso.gz"
gzip -d -f "${macDir}/OpenCore.iso.gz"
fi
if [ ! -f "${macDir}/BaseSystem.dmg" ]; then
if [ ! -f "${macDir}/fetch.py" ]; then
wget "https://raw.githubusercontent.com/kholia/OSX-KVM/master/fetch-macOS-v2.py" -O "${macDir}/fetch.py"
pip3 install requests
pip3 install click
fi
python3 "${macDir}/fetch.py" --action download -o ${macDir} -os latest
fi
fi
# Enable nested virtualization if requested
if [ ${nested_virtualization} == "on" ]; then
enableNestedVirtualization
else
disableNestedVirtualization
fi
# Add snapshot support if requested
if [ ${snapshot} == "on" ]; then
extra="${extra} -snapshot"
fi
# Use EFI if requested
if [ ${firmware} == "efi" ]; then
extra="${extra} -drive if=pflash,format=raw,readonly=on,file=${ovmfCode} -drive if=pflash,format=raw,readonly=on,file=${ovmfVars}"
# Use Secure Boot if requested
if [ ${secure_boot} == "on" ]; then
ovmfCode="${ovmfPath}/OVMF_CODE.secboot.fd"
ovmfVars="${ovmfPath}/OVMF_VARS.secboot.fd"
fi
fi
# Setup virtual disk
if [ ! -f ${disk} ]; then
qemu-img create -q -f ${disk_format} ${disk} ${disk_size}
setupDisk
else
diskSize=$(stat -c%s ${disk})
if [ ${diskSize} -le $((197632 * 8)) ]; then
setupDisk
fi
fi
# Launch QEMU
qemu-system-x86_64 \
-name ${name},process=${name} \
-enable-kvm -machine ${machine},nvdimm=on -smbios type=2 \
-parallel none -serial none \
-cpu ${cpu},kvm=on${cpuTweaks} -smp sockets=1,dies=1,cores=${cores},threads=${threads} \
-m ${ram} -device virtio-balloon-pci \
-device ${graphics} -display ${display},gl=${accelerated_graphics} \
-device ${usb},id=usb ${peripherals} \
-netdev user,hostname=${name},id=nic -device ${network},netdev=nic \
-rtc base=localtime,clock=host \
-device virtio-serial-pci \
-chardev spicevmc,id=spicechannel0,name=vdagent \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev socket,path=/tmp/qga.sock,server=on,wait=off,id=spicechannel1 \
-device virtserialport,chardev=spicechannel1,name=org.qemu.guest_agent.0 \
-drive if=none,id=drive0,discard=unmap,cache=${cache},format=${disk_format},file=${disk} \
${extra} ${audio} ${drives} ${diskInterface} \
"${@}"
}
if [ $# -lt 1 ]; then
usage
else
while [ $# -gt 0 ]; do
case "${1}" in
*)
# Load default config
source "${2}"
loadDefaultConfig
# Populate some options
launcher=$(basename "${0}")
name=$(basename "${2}" .conf)
disk="${diskDir}/${name}.${disk_format}"
shift
shift;;
esac
done
fi
start