forked from FEX-Emu/FEX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallFEX.py
executable file
·404 lines (323 loc) · 11.8 KB
/
InstallFEX.py
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
#!/usr/bin/python3
import os
import subprocess
import sys
import tempfile
import re
_Arch = None
def GetArch():
global _Arch
if _Arch == None:
_Arch = subprocess.check_output(['uname', '-m']).decode("utf-8").strip()
return _Arch
_Distro = None
def GetDistro():
global _Distro
# Query files in order
# /etc/lsb-release
# /etc/os-release
if _Distro == None:
if os.path.exists("/etc/lsb-release"):
File = open("/etc/lsb-release", "r")
Lines = File.readlines()
File.close()
Found = 0
Distro = ""
Version = ""
for Line in Lines:
Key, Val = Line.split("=", 1)
if Key == "DISTRIB_ID":
Distro = Val.strip().lower()
Found+=1
if Key == "DISTRIB_RELEASE":
Version = Val.strip()
Found+=1
if Found == 2:
_Distro = [Distro, Version]
return _Distro
if os.path.exists("/etc/os-release"):
File = open("/etc/os-release", "r")
Lines = File.readlines()
File.close()
Found = 0
Distro = ""
Version = ""
for Line in Lines:
Key, Val = Line.split("=", 1)
if Key == "ID":
Distro = Val.strip()
Found+=1
if Key == "VERSION_ID":
# Strip the double quotes from the version id
Version = Val.strip()[1:-1]
Found+=1
if Found == 2:
_Distro = [Distro, Version]
return _Distro
# Unknown
_Distro = ["Unknown", "0.0"]
return _Distro
def IsSupportedArch():
Arch = GetArch()
return Arch == "aarch64"
def IsSupportedDistro():
Distro = GetDistro()
# We only support Ubuntu
if Distro[0] == "ubuntu":
# We only support what is available in ppa:fex-emu/fex
return Distro[1] == "22.04" or \
Distro[1] == "23.04" or \
Distro[1] == "23.10" or \
Distro[1] == "24.04"
return False
_ArchVersion = None
def ListContainsRequired(Features, RequiredFeatures):
for Req in RequiredFeatures:
if not Req in Features:
return False
return True
def GetCPUFeaturesVersion():
global _ArchVersion
# Also LOR but kernel doesn't expose this
v8_1Mandatory = ["atomics", "asimdrdm", "crc32"]
v8_2Mandatory = v8_1Mandatory + ["dcpop"]
v8_3Mandatory = v8_2Mandatory + ["fcma", "jscvt", "lrcpc", "paca", "pacg"]
v8_4Mandatory = v8_3Mandatory + ["asimddp", "flagm", "ilrcpc", "uscat"]
# fphp asimdhp asimddp
if _ArchVersion == None:
File = open("/proc/cpuinfo", "r")
Lines = File.readlines()
File.close()
# Minimum spec is ARMv8.0
_ArchVersion = "8.0"
for Line in Lines:
if "Features" in Line:
Features = Line.split(":")[1].strip().split(" ")
# We don't care beyond 8.4 right now
if ListContainsRequired(Features, v8_4Mandatory):
_ArchVersion = "8.4"
elif ListContainsRequired(Features, v8_3Mandatory):
_ArchVersion = "8.3"
elif ListContainsRequired(Features, v8_2Mandatory):
_ArchVersion = "8.2"
elif ListContainsRequired(Features, v8_1Mandatory):
_ArchVersion = "8.1"
break;
return _ArchVersion
_PPAInstalled = None
FEXPPA_REGEX = r".*\/fex-emu\/fex\/ubuntu$"
def GetPPAStatus():
global _PPAInstalled
if _PPAInstalled == None:
_PPAInstalled = False
CacheResults = subprocess.check_output(['apt-cache', 'policy']).decode("utf-8")
for Line in CacheResults.split("\n"):
if "http" in Line:
Line = Line.strip()
LineSplit = Line.split(" ")
# 'status' 'URL' 'series' 'arch' 'type'
if re.match(FEXPPA_REGEX, LineSplit[1]):
_PPAInstalled = True
break
return _PPAInstalled
def InstallPPA():
print ("Installing PPA: ppa:fex-emu/fex")
print ("This bit will ask for your password")
DidInstall = False
try:
CmdResult = subprocess.call(["sudo", "add-apt-repository", "-y", "ppa:fex-emu/fex"])
DidInstall = CmdResult == 0
except KeyboardInterrupt:
DidInstall = False
pass
if DidInstall:
print("PPA installed")
else:
print("PPA failed to install")
return DidInstall
ARMVersionToPackage = {
"8.0": "fex-emu-armv8.0",
"8.1": "fex-emu-armv8.0",
"8.2": "fex-emu-armv8.2",
"8.3": "fex-emu-armv8.2",
"8.4": "fex-emu-armv8.4",
}
def GetPackagesToInstall():
return [
ARMVersionToPackage[GetCPUFeaturesVersion()],
"fex-emu-binfmt32",
"fex-emu-binfmt64",
]
def UpdatePPA():
print ("Updating apt sources")
print ("This bit will ask for your password")
DidUpdate = False
try:
CmdResult = subprocess.call(["sudo", "apt-get", "update"])
DidUpdate = CmdResult == 0
except KeyboardInterrupt:
DidUpdate = False
pass
if DidUpdate:
print("PPA installed")
else:
print("PPA failed to install")
return DidUpdate
def CheckAndInstallPackageUpdates():
PackagesToInstall = GetPackagesToInstall()
for Package in PackagesToInstall[:]:
UpgradableStatus = subprocess.check_output(["apt", "list", "--upgradable", Package]).decode("utf-8")
Found = False
for Line in UpgradableStatus.split("\n"):
# If the package exists to be upgraded then it will appear in this list
# We need to check multiple lines
# $ apt list --upgradable <Package>
# With upgrade available
# Listing... Done
# <Package>/<Repo> <NewVersion> <arch> [upgradable from: <Installed version>]
# Without upgrade available
# Listing... Done
# <EOF>
if Package in Line and "upgradable" in Line:
Found = True
if Found == False:
PackagesToInstall.remove(Package)
if len(PackagesToInstall) > 0:
print ("Found updates for packages: {}".format(PackagesToInstall))
print ("This bit may ask for your password")
DidInstall = False
try:
CmdResult = subprocess.call(["sudo", "apt-get", "-y", "install"] + PackagesToInstall)
DidInstall = CmdResult == 0
except KeyboardInterrupt:
print ("Keyboard interrupt")
DidInstall = False
pass
if DidInstall:
print("Packages updated")
else:
print("Packages failed to update")
return DidInstall
return True
def CheckPackageInstallStatus():
PackagesToInstall = GetPackagesToInstall()
for Package in PackagesToInstall[:]:
CmdResult = subprocess.call(["dpkg", "-s", Package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if CmdResult == 0:
PackagesToInstall.remove(Package)
return PackagesToInstall
def InstallPackages(Packages):
print("Installing packages: {}".format(Packages))
DidInstall = False
try:
CmdResult = subprocess.call(["sudo", "apt-get", "-y", "install"] + Packages)
DidInstall = CmdResult == 0
except KeyboardInterrupt:
print ("Keyboard interrupt")
DidInstall = False
pass
if DidInstall:
print("Packages installed")
else:
print("Packages failed to install")
return DidInstall
_RootFSPath = None
def GetRootFSPath():
global _RootFSPath
if _RootFSPath == None:
# Follows the same logic as FEXCore::Config::GetDataDirectory()
HomeDir = os.getenv("HOME")
if HomeDir == None:
HomeDir = os.getenv("PWD")
if HomeDir == None:
HomeDir = "."
Path = HomeDir
DataXDG = os.getenv("XDG_DATA_HOME")
if DataXDG != None:
Path = DataXDG
Path = Path + "/.fex-emu"
DataOverride = os.getenv("FEX_APP_DATA_LOCATION")
if DataOverride != None:
Path = DataOverride
_RootFSPath = Path + "/RootFS/"
return _RootFSPath
def CheckRootFSInstallStatus():
Distro = GetDistro()[1] # Extract Ubuntu version number, e.g. "23.10"
DistroUnderscore = Distro.replace(".", "_")
Filename = "Ubuntu_{}.ero".format(DistroUnderscore)
if os.path.exists(GetRootFSPath() + Filename):
return True
Filename = "Ubuntu_{}.sqsh".format(DistroUnderscore)
if os.path.exists(GetRootFSPath() + Filename):
return True
# Couldn't find. Either no rootfs installed or unsupported distro.
return False
def TryInstallRootFS():
DidInstall = False
try:
CmdResult = subprocess.call(["FEXRootFSFetcher"])
DidInstall = CmdResult == 0
except KeyboardInterrupt:
print ("Keyboard interrupt")
DidInstall = False
pass
return DidInstall
def TryBasicProgramExecution():
return subprocess.call(["FEXInterpreter", "/usr/bin/uname", "-a"]) == 0
def ExitWithStatus(Status):
# Remove the cached credentials
subprocess.call(["sudo", "-K"])
sys.exit(Status)
def main():
# Only run on supported arch
if not IsSupportedArch():
print ( "{} is not a supported architecture".format(GetArch()))
ExitWithStatus(-1)
if not IsSupportedDistro():
Distro = GetDistro()
print ( "'{} {}' is not a supported distro".format(Distro[0], Distro[1]))
ExitWithStatus(-1)
if GetDistro()[0] == "ubuntu":
print ("Getting PPA status: {}".format(("NotInstalled", "Installed")[GetPPAStatus()]))
if GetPPAStatus():
if not UpdatePPA():
print ("apt sources failed to update. Not continuing")
ExitWithStatus(-1)
if not CheckAndInstallPackageUpdates():
print ("apt packages failed to update. Not continuing")
ExitWithStatus(-1)
else:
if not InstallPPA():
print ("PPA failed to install. Not continuing")
ExitWithStatus(-1)
Packages = CheckPackageInstallStatus()
if len(Packages) > 0:
if not InstallPackages(Packages):
print ("Failed to install packages. Not continuing")
ExitWithStatus(-1)
if not CheckRootFSInstallStatus():
print ("RootFS not found. Running FEXRootFSFetcher to get rootfs")
if not TryInstallRootFS():
print ("Failed to install RootFS. Not continuing")
ExitWithStatus(-1)
print ("FEX is now installed. Trying basic program run")
if not TryBasicProgramExecution():
print ("FEXInterpreter failed to run. Not continuing")
ExitWithStatus(-1)
print ("")
print ("===================================================")
print ("FEX test run executed. You should be set to run FEX")
print ("===================================================")
print ("Usage examples:")
print ("# steam is a bash script. Wrap with FEXBash")
print ("\tFEXBash steam")
print ("# Full path execution execution will wrap the application if it exists in the rootfs")
print ("\tFEXInterpreter /usr/bin/uname")
print ("# Freestanding x86/x86-64 programs can be executed directly. binfmt_misc will redirect to FEX")
print ("\t$HOME/PetalCrashOnline.AppImage")
print ("# If you need a terminal that emulates everything.")
print ("# Run FEXBash without arguments. Double check uname to see if running under FEX")
print ("\tFEXBash")
ExitWithStatus(0)
if __name__ == "__main__":
sys.exit(main())