1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
11 import tools
.helpers
.run
14 def get_lxc_version(args
):
15 if shutil
.which("lxc-info") is not None:
16 command
= ["lxc-info", "--version"]
17 version_str
= tools
.helpers
.run
.user(args
, command
, output_return
=True)
18 return int(version_str
[0])
23 def generate_nodes_lxc_config(args
):
24 def make_entry(src
, dist
=None, mnt_type
="none", options
="bind,create=file,optional 0 0", check
=True):
25 if check
and not os
.path
.exists(src
):
27 entry
= "lxc.mount.entry = "
32 entry
+= mnt_type
+ " "
39 make_entry("tmpfs", "dev", "tmpfs", "nosuid 0 0", False)
40 make_entry("/dev/zero")
41 make_entry("/dev/null")
42 make_entry("/dev/full")
43 make_entry("/dev/ashmem", check
=False)
44 make_entry("/dev/fuse")
45 make_entry("/dev/ion")
46 make_entry("/dev/char", options
="bind,create=dir,optional 0 0")
49 make_entry("/dev/kgsl-3d0")
50 make_entry("/dev/mali0")
51 make_entry("/dev/pvr_sync")
52 make_entry("/dev/pmsg0")
53 make_entry("/dev/dxg")
54 make_entry("/dev/dri", options
="bind,create=dir,optional 0 0")
56 for n
in glob
.glob("/dev/fb*"):
58 for n
in glob
.glob("/dev/graphics/fb*"):
60 for n
in glob
.glob("/dev/video*"):
64 make_entry("/dev/" + args
.BINDER_DRIVER
, "dev/binder", check
=False)
65 make_entry("/dev/" + args
.VNDBINDER_DRIVER
, "dev/vndbinder", check
=False)
66 make_entry("/dev/" + args
.HWBINDER_DRIVER
, "dev/hwbinder", check
=False)
68 if args
.vendor_type
!= "MAINLINE":
69 if not make_entry("/dev/hwbinder", "dev/host_hwbinder"):
70 raise OSError('Binder node "hwbinder" of host not found')
71 make_entry("/vendor", "vendor_extra", options
="bind,optional 0 0")
73 # Necessary device nodes for adb
74 make_entry("none", "dev/pts", "devpts", "defaults,mode=644,ptmxmode=666,create=dir 0 0", False)
75 make_entry("/dev/uhid")
77 # Low memory killer sys node
78 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
81 make_entry("tmpfs", "mnt", "tmpfs", "mode=0755,uid=0,gid=1000", False)
82 make_entry(tools
.config
.defaults
["data"], "data", options
="bind 0 0", check
=False)
84 # Mount host permissions
85 make_entry(tools
.config
.defaults
["host_perms"],
86 "vendor/etc/host-permissions", options
="bind,optional 0 0")
88 # Recursive mount /run to provide necessary host sockets
89 make_entry("/run", options
="rbind,create=dir 0 0")
91 # Necessary sw_sync node for HWC
92 make_entry("/dev/sw_sync")
93 make_entry("/sys/kernel/debug", options
="rbind,create=dir,optional 0 0")
96 make_entry("/sys/class/leds/vibrator",
97 options
="bind,create=dir,optional 0 0")
98 make_entry("/sys/devices/virtual/timed_output/vibrator",
99 options
="bind,create=dir,optional 0 0")
101 # Media dev nodes (for Mediatek)
102 make_entry("/dev/Vcodec")
103 make_entry("/dev/MTK_SMI")
104 make_entry("/dev/mdp_sync")
105 make_entry("/dev/mtk_cmdq")
108 make_entry("tmpfs", "mnt_extra", "tmpfs", "nodev 0 0", False)
109 make_entry("/mnt/wslg", "mnt_extra/wslg",
110 options
="rbind,create=dir,optional 0 0")
113 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
114 make_entry("/var/run", options
="rbind,create=dir,optional 0 0")
117 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
118 for n
in glob
.glob("/tmp/run-*"):
119 make_entry(n
, options
="rbind,create=dir,optional 0 0")
124 def set_lxc_config(args
):
125 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
126 config_file
= "config_2"
127 lxc_ver
= get_lxc_version(args
)
129 raise OSError("LXC is not installed")
131 config_file
= "config_1"
132 config_path
= tools
.config
.tools_src
+ "/data/configs/" + config_file
134 command
= ["mkdir", "-p", lxc_path
]
135 tools
.helpers
.run
.user(args
, command
)
136 command
= ["cp", "-fpr", config_path
, lxc_path
+ "/config"]
137 tools
.helpers
.run
.user(args
, command
)
138 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
139 tools
.helpers
.run
.user(args
, command
)
141 nodes
= generate_nodes_lxc_config(args
)
142 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
143 config_nodes
= open(config_nodes_tmp_path
, "w")
145 config_nodes
.write(node
+ "\n")
147 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
148 tools
.helpers
.run
.user(args
, command
)
151 def make_base_props(args
):
152 def find_hal(hardware
):
154 "ro.hardware." + hardware
,
159 for p
in hardware_props
:
160 prop
= tools
.helpers
.props
.host_get(args
, p
)
162 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
163 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
164 if os
.path
.isfile(hal_file
):
169 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
171 gralloc
= find_hal("gralloc")
173 if os
.path
.exists("/dev/dri"):
179 props
.append("debug.stagefright.ccodec=0")
180 props
.append("ro.hardware.gralloc=" + gralloc
)
183 props
.append("ro.hardware.egl=" + egl
)
185 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
186 if media_profiles
!= "":
187 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
188 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
189 props
.append("media.settings.xml=" + media_profiles
)
191 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
193 props
.append("debug.stagefright.ccodec=" + ccodec
)
195 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
196 if ext_library
!= "":
197 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
198 ext_library
= ext_library
.replace("odm/", "odm_extra/")
199 props
.append("ro.vendor.extension_library=" + ext_library
)
201 vulkan
= find_hal("vulkan")
203 props
.append("ro.hardware.vulkan=" + vulkan
)
205 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
207 camera
= find_hal("camera")
209 props
.append("ro.hardware.camera=" + camera
)
211 if args
.vendor_type
== "MAINLINE":
212 props
.append("ro.hardware.camera=v4l2")
214 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
217 props
.append("ro.opengles.version=" + opengles
)
219 props
.append("waydroid.system_ota=" + args
.system_ota
)
220 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
221 props
.append("waydroid.tools_version=" + tools
.config
.version
)
223 if args
.vendor_type
== "MAINLINE":
224 props
.append("ro.vndk.lite=true")
226 for product
in ["brand", "device", "manufacturer", "model", "name"]:
227 prop_product
= tools
.helpers
.props
.host_get(
228 args
, "ro.product.vendor." + product
)
229 if prop_product
!= "":
230 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
232 if os
.path
.isfile("/proc/device-tree/" + product
):
233 with open("/proc/device-tree/" + product
) as f
:
234 f_value
= f
.read().strip()
236 props
.append("ro.product.waydroid." +
237 product
+ "=" + f_value
)
239 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
241 props
.append("ro.build.fingerprint=" + prop_fp
)
243 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
245 base_props
.write(prop
+ "\n")
249 def setup_host_perms(args
):
250 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
251 os
.mkdir(tools
.config
.defaults
["host_perms"])
253 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
257 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
260 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
261 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
262 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
264 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
265 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
266 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
269 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
270 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
272 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
274 for filename
in copy_list
:
275 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
278 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
279 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
280 os
.chmod(args
.log
, 0o666)
284 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
285 "-F", "-n", "waydroid", "--", "/init"]
286 tools
.helpers
.run
.user(args
, command
, output
="background")
289 command
= ["lxc-stop", "-P",
290 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
291 tools
.helpers
.run
.user(args
, command
)
294 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
295 tools
.helpers
.run
.user(args
, command
)
298 command
= ["lxc-unfreeze", "-P",
299 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
300 tools
.helpers
.run
.user(args
, command
)
303 if status(args
) != "RUNNING":
304 logging
.error("WayDroid container is {}".format(status(args
)))
306 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
307 "-n", "waydroid", "--"]
309 command
.append(args
.COMMAND
)
311 command
.append("/system/bin/sh")
312 subprocess
.run(command
, env
={"PATH": os.environ['PATH'] + ":/system/bin:/vendor/bin"}
)
315 if status(args
) != "RUNNING":
316 logging
.error("WayDroid container is {}".format(status(args
)))
318 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
319 "-n", "waydroid", "--", "/system/bin/logcat"]
320 subprocess
.run(command
)