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(tools
.helpers
.gpu
.getDriNode(args
), "dev/dri/renderD128")
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 # TUN/TAP device node for VPN
78 make_entry("/dev/net/tun", "dev/tun")
80 # Low memory killer sys node
81 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
84 make_entry("tmpfs", "mnt", "tmpfs", "mode=0755,uid=0,gid=1000", False)
85 make_entry(tools
.config
.defaults
["data"], "data", options
="bind 0 0", check
=False)
87 # Mount host permissions
88 make_entry(tools
.config
.defaults
["host_perms"],
89 "vendor/etc/host-permissions", options
="bind,optional 0 0")
91 # Recursive mount /run to provide necessary host sockets
92 make_entry("/run", options
="rbind,create=dir 0 0")
94 # Necessary sw_sync node for HWC
95 make_entry("/dev/sw_sync")
96 make_entry("/sys/kernel/debug", options
="rbind,create=dir,optional 0 0")
99 make_entry("/sys/class/leds/vibrator",
100 options
="bind,create=dir,optional 0 0")
101 make_entry("/sys/devices/virtual/timed_output/vibrator",
102 options
="bind,create=dir,optional 0 0")
104 # Media dev nodes (for Mediatek)
105 make_entry("/dev/Vcodec")
106 make_entry("/dev/MTK_SMI")
107 make_entry("/dev/mdp_sync")
108 make_entry("/dev/mtk_cmdq")
111 make_entry("tmpfs", "mnt_extra", "tmpfs", "nodev 0 0", False)
112 make_entry("/mnt/wslg", "mnt_extra/wslg",
113 options
="rbind,create=dir,optional 0 0")
116 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
117 make_entry("/var/run", options
="rbind,create=dir,optional 0 0")
120 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
121 for n
in glob
.glob("/tmp/run-*"):
122 make_entry(n
, options
="rbind,create=dir,optional 0 0")
125 make_entry("/system/etc/libnfc-nci.conf", options
="bind,optional 0 0")
130 def set_lxc_config(args
):
131 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
132 config_file
= "config_2"
133 lxc_ver
= get_lxc_version(args
)
135 raise OSError("LXC is not installed")
137 config_file
= "config_1"
138 config_path
= tools
.config
.tools_src
+ "/data/configs/" + config_file
140 command
= ["mkdir", "-p", lxc_path
]
141 tools
.helpers
.run
.user(args
, command
)
142 command
= ["cp", "-fpr", config_path
, lxc_path
+ "/config"]
143 tools
.helpers
.run
.user(args
, command
)
144 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
145 tools
.helpers
.run
.user(args
, command
)
147 nodes
= generate_nodes_lxc_config(args
)
148 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
149 config_nodes
= open(config_nodes_tmp_path
, "w")
151 config_nodes
.write(node
+ "\n")
153 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
154 tools
.helpers
.run
.user(args
, command
)
157 def make_base_props(args
):
158 def find_hal(hardware
):
160 "ro.hardware." + hardware
,
165 for p
in hardware_props
:
166 prop
= tools
.helpers
.props
.host_get(args
, p
)
168 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
169 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
170 if os
.path
.isfile(hal_file
):
176 if not os
.path
.exists("/dev/ashmem"):
177 props
.append("sys.use_memfd=true")
179 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
181 gralloc
= find_hal("gralloc")
183 if tools
.helpers
.gpu
.getDriNode(args
):
189 props
.append("debug.stagefright.ccodec=0")
190 props
.append("ro.hardware.gralloc=" + gralloc
)
193 props
.append("ro.hardware.egl=" + egl
)
195 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
196 if media_profiles
!= "":
197 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
198 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
199 props
.append("media.settings.xml=" + media_profiles
)
201 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
203 props
.append("debug.stagefright.ccodec=" + ccodec
)
205 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
206 if ext_library
!= "":
207 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
208 ext_library
= ext_library
.replace("odm/", "odm_extra/")
209 props
.append("ro.vendor.extension_library=" + ext_library
)
211 vulkan
= find_hal("vulkan")
213 props
.append("ro.hardware.vulkan=" + vulkan
)
215 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
217 camera
= find_hal("camera")
219 props
.append("ro.hardware.camera=" + camera
)
221 if args
.vendor_type
== "MAINLINE":
222 props
.append("ro.hardware.camera=v4l2")
224 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
227 props
.append("ro.opengles.version=" + opengles
)
229 if args
.images_path
!= tools
.config
.defaults
["preinstalled_images_path"]:
230 props
.append("waydroid.system_ota=" + args
.system_ota
)
231 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
233 props
.append("waydroid.updater.disabled=true")
235 props
.append("waydroid.tools_version=" + tools
.config
.version
)
237 if args
.vendor_type
== "MAINLINE":
238 props
.append("ro.vndk.lite=true")
240 for product
in ["brand", "device", "manufacturer", "model", "name"]:
241 prop_product
= tools
.helpers
.props
.host_get(
242 args
, "ro.product.vendor." + product
)
243 if prop_product
!= "":
244 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
246 if os
.path
.isfile("/proc/device-tree/" + product
):
247 with open("/proc/device-tree/" + product
) as f
:
248 f_value
= f
.read().strip().rstrip('\x00')
250 props
.append("ro.product.waydroid." +
251 product
+ "=" + f_value
)
253 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
255 props
.append("ro.build.fingerprint=" + prop_fp
)
257 # now append/override with values in [properties] section of waydroid.cfg
258 cfg
= tools
.config
.load(args
)
259 for k
, v
in cfg
["properties"].items():
260 for idx
, elem
in enumerate(props
):
263 props
.append(k
+"="+v
)
265 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
267 base_props
.write(prop
+ "\n")
271 def setup_host_perms(args
):
272 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
273 os
.mkdir(tools
.config
.defaults
["host_perms"])
275 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
279 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
282 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
283 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
284 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
286 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
287 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
288 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
291 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
292 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
294 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
296 for filename
in copy_list
:
297 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
300 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
301 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
302 os
.chmod(args
.log
, 0o666)
306 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
307 "-F", "-n", "waydroid", "--", "/init"]
308 tools
.helpers
.run
.user(args
, command
, output
="background")
311 command
= ["lxc-stop", "-P",
312 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
313 tools
.helpers
.run
.user(args
, command
)
316 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
317 tools
.helpers
.run
.user(args
, command
)
320 command
= ["lxc-unfreeze", "-P",
321 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
322 tools
.helpers
.run
.user(args
, command
)
325 if status(args
) != "RUNNING":
326 logging
.error("WayDroid container is {}".format(status(args
)))
328 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
329 "-n", "waydroid", "--"]
331 command
.append(args
.COMMAND
)
333 command
.append("/system/bin/sh")
334 subprocess
.run(command
, env
={"PATH": os.environ['PATH'] + ":/system/bin:/vendor/bin"}
)
337 if status(args
) != "RUNNING":
338 logging
.error("WayDroid container is {}".format(status(args
)))
340 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
341 "-n", "waydroid", "--", "/system/bin/logcat"]
342 subprocess
.run(command
)