forked from jonpanozzo/OpenELEC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenELEC-submit.php
More file actions
executable file
·229 lines (192 loc) · 8.09 KB
/
OpenELEC-submit.php
File metadata and controls
executable file
·229 lines (192 loc) · 8.09 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
<?php
readfile("/usr/local/emhttp/update.htm");
function write_log($string) {
syslog(LOG_INFO, "OpenELEC plugin: " . trim(str_replace('<br>', '', $string)));
}
function write_display($string, $failure = false) {
if ($failure) {
$string = "<span style='color: #FF0000'>" . $string . "</span>";
}
echo "<script>addLog(\"{$string}\");</script>\n";
@flush();
}
$varGPU = empty($_POST['gpu']) ? '' : $_POST['gpu'];
$varAudio = empty($_POST['audio']) ? '' : $_POST['audio'];
$varOtherList = empty($_POST['other']) ? [] : $_POST['other'];
$varUSBList = empty($_POST['usb']) ? [] : $_POST['usb'];
$varMAC = empty($_POST['mac']) ? '52:54:00:xx:xx:xx' : $_POST['mac'];
$varBridge = $_POST['bridge'];
$varReadonly = !empty($_POST['readonly']);
$varMemory = empty($_POST['memory']) ? 1024 : intval($_POST['memory']);
$varVCPUs = empty($_POST['vcpus']) ? 2 : intval($_POST['vcpus']);
$varMachineType = empty($_POST['machinetype']) ? 'q35' : $_POST['machinetype'];
// Ensure valid cpu core count
$intCPUCoreCount = intval(trim(shell_exec('nproc')));
if (empty($intCPUCoreCount)) {
$intCPUCoreCount = 1;
}
$varVCPUs = max(min($intCPUCoreCount, $varVCPUs), 1);
// Ensure valid machine type
$arrValidMachineTypes = ['q35', 'pc'];
if (!in_array($varMachineType, $arrValidMachineTypes)) {
$varMachineType = $arrValidMachineTypes[0];
}
// Ensure valid memory amount
$arrValidMemoryAmounts = [512, 1024, 1536, 2048];
if (!in_array($varMemory, $arrValidMemoryAmounts)) {
$varMemory = $arrValidMemoryAmounts[1];
}
// Replace wildcard chars in MAC
$varMACparts = explode(':', $varMAC);
$varMACparts = array_filter($varMACparts);
for ($i=0; $i < 6; $i++) {
if (empty($varMACparts[$i]) || stripos($varMACparts[$i], 'x') !== false) {
$varMACparts[$i] = str_pad(dechex(rand(0, 255)), 2, '0', STR_PAD_LEFT);
}
}
$varMAC = implode(':', $varMACparts);
// VFIO the GPU and Audio
$arrPassthruDevices = array_filter([$varGPU, $varAudio] + $varOtherList);
foreach ($arrPassthruDevices as $strPassthruDevice) {
// Ensure we have leading 0000:
$strPassthruDeviceShort = str_replace('0000:', '', $strPassthruDevice);
$strPassthruDeviceLong = '0000:' . $strPassthruDeviceShort;
// Determine the driver currently assigned to the device
$strDriverSymlink = @readlink('/sys/bus/pci/devices/' . $strPassthruDeviceLong . '/driver');
if ($strDriverSymlink !== false) {
// Device is bound to a Driver already
if (strpos($strDriverSymlink, 'vfio-pci') !== false) {
// Driver bound to vfio-pci already - nothing left to do for this device now regarding vfio
write_display('<br>Device ' . $strPassthruDeviceShort . ' already using vfio-pci driver');
continue;
}
// Driver bound to some other driver - attempt to unbind driver
write_display('<br>Unbinding device ' . $strPassthruDeviceShort . ' from current driver...');
if (file_put_contents('/sys/bus/pci/devices/' . $strPassthruDeviceLong . '/driver/unbind', $strPassthruDeviceLong) === false) {
write_display('FAILED', true);
write_log('ERROR: Failed to unbind device ' . $strPassthruDeviceShort . ' from current driver');
sleep(4);
exit(1);
} else {
write_display('Ok');
write_log('Unbound device ' . $strPassthruDeviceShort . ' from current driver');
}
}
// Get Vendor and Device IDs for the passthru device
$strVendor = file_get_contents('/sys/bus/pci/devices/' . $strPassthruDeviceLong . '/vendor');
$strDevice = file_get_contents('/sys/bus/pci/devices/' . $strPassthruDeviceLong . '/device');
// Attempt to bind driver to vfio-pci
write_display('<br>Binding device ' . $strPassthruDeviceShort . ' to vfio-pci driver...');
if (file_put_contents('/sys/bus/pci/drivers/vfio-pci/new_id', $strVendor . ' ' . $strDevice) === false) {
write_display('FAILED', true);
write_log('ERROR: Failed to bind device ' . $strPassthruDeviceShort . ' to vfio-pci driver');
sleep(4);
exit(1);
} else {
write_display('Ok');
write_log('Bound device ' . $strPassthruDeviceShort . ' to vfio-pci driver');
}
}
// Save configuration
$arrConfiguration = [
'gpu' => $varGPU,
'audio' => $varAudio,
'other' => $varOtherList,
'usb' => $varUSBList,
'mac' => $varMAC,
'bridge' => $varBridge,
'readonly' => $varReadonly,
'memory' => $varMemory,
'vcpus' => $varVCPUs,
'machinetype' => $varMachineType
];
write_display('<br>Saving boot configuration file...');
if (file_put_contents('/boot/config/plugins/OpenELEC/OpenELEC.conf', json_encode($arrConfiguration, JSON_PRETTY_PRINT)) === false) {
write_display('FAILED', true);
write_log('ERROR: Failed generating boot configuration file: /boot/config/plugins/OpenELEC/OpenELEC.conf');
sleep(4);
exit(1);
} else {
write_display('Ok');
write_log('Generated boot configuration file: /boot/config/plugins/OpenELEC/OpenELEC.conf');
}
// Replace variables - PCI Devices
$varPCIDevices = '';
if (!empty($varGPU)) {
$varPCIDevices .= "<qemu:arg value='-device'/>\n\t\t";
$varPCIDevices .= "<qemu:arg value='vfio-pci,host=" . $varGPU . ",bus=root.1,addr=00.0,multifunction=on,x-vga=on'/>\n\t\t";
}
if (!empty($varAudio)) {
$varPCIDevices .= "<qemu:arg value='-device'/>\n\t\t";
$varPCIDevices .= "<qemu:arg value='vfio-pci,host=" . $varAudio . ",bus=pci{{PCI_PCIE}}.0'/>\n\t\t";
}
if (!empty($varOtherList)) {
foreach ($varOtherList as $varOtherItem) {
$varPCIDevices .= "<qemu:arg value='-device'/><qemu:arg value='vfio-pci,host=" . $varOtherItem . ",bus=pci{{PCI_PCIE}}.0'/>\n\t\t";
}
}
$varPCIDevices = trim($varPCIDevices);
// Replace variables - USB Devices
$varUSBDevices = '';
if (!empty($varUSBList)) {
if ($varMachineType == 'q35') {
// Q35 needs a usb controller added, i440fx comes with one a default
$varUSBDevices .= "<controller type='usb' index='0'/>\n\n\t\t";
}
foreach ($varUSBList as $varUSBItem) {
list($vendor, $product) = explode(':', $varUSBItem);
if (empty($vendor) || empty($vendor)) {
continue;
}
$varUSBDevices .= "<hostdev mode='subsystem' type='usb'>\n\t\t";
$varUSBDevices .= " <source>\n\t\t";
$varUSBDevices .= " <vendor id='0x" . $vendor . "'/>\n\t\t";
$varUSBDevices .= " <product id='0x" . $product . "'/>\n\t\t";
$varUSBDevices .= " </source>\n\t\t";
$varUSBDevices .= "</hostdev>\n\n\t\t";
}
$varUSBDevices = trim($varUSBDevices);
}
// Build the CPU Tune section
$varCPUTune = '';
for ($i=0; $i < $varVCPUs; $i++) {
$varCPUTune .= "<vcpupin vcpu='" . $i . "' cpuset='" . ($intCPUCoreCount - ($i + 1)) . "'/>\n\t\t";
}
$varCPUTune = trim($varCPUTune);
// Open the seed xml and replace variables
write_display('<br>Parsing seed xml file...');
write_log('Parsing seed xml file: ' . __DIR__ . '/OpenELEC.xml');
$strXMLFile = file_get_contents(__DIR__ . '/OpenELEC.xml');
$strXMLFile = str_replace('{{PCI_DEVICES}}', $varPCIDevices, $strXMLFile);
$strXMLFile = str_replace('{{NET_MAC}}', $varMAC, $strXMLFile);
$strXMLFile = str_replace('{{NET_BRIDGE}}', $varBridge, $strXMLFile);
$strXMLFile = str_replace('{{MOUNT_READONLY}}', $varReadonly ? '<readonly/>' : '', $strXMLFile);
$strXMLFile = str_replace('{{USB_DEVICES}}', $varUSBDevices, $strXMLFile);
$strXMLFile = str_replace('{{MEMORY}}', $varMemory, $strXMLFile);
$strXMLFile = str_replace('{{VCPUS}}', $varVCPUs, $strXMLFile);
$strXMLFile = str_replace('{{CPU_TUNE}}', $varCPUTune, $strXMLFile);
$strXMLFile = str_replace('{{MACHINE_TYPE}}', $varMachineType, $strXMLFile);
$strXMLFile = str_replace('{{PCI_PCIE}}', $varMachineType == 'q35' ? 'e' : '', $strXMLFile);
// Save the modified xml to the tmp folder
write_display('<br>Saving generated xml file...');
if (file_put_contents('/tmp/OpenELEC.xml', $strXMLFile) === false) {
write_display('FAILED', true);
write_log('ERROR: Failed generating xml file: /tmp/OpenELEC.xml');
sleep(4);
exit(1);
} else {
write_display('Ok');
write_log('Generated xml file: /tmp/OpenELEC.xml');
}
// Ensure NODATACOW is set to all KVM images
// passthru('chattr +C /mnt/cache/.vms/kvm/');
// Start the VM
write_display('<br>Starting VM...');
write_log('Starting VM with "virsh create /tmp/OpenELEC.xml"');
$strOut = str_replace(["\n","\r","\t"], [' ','',''], shell_exec('virsh create /tmp/OpenELEC.xml 2>&1'));
if (stripos(trim($strOut), 'Domain OpenELEC created') !== 0) {
write_display('FAILED: ' . $strOut, true);
} else {
write_display('Ok');
}
sleep(5);