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 # 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")
127 def set_lxc_config(args
):
128 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
129 config_file
= "config_2"
130 lxc_ver
= get_lxc_version(args
)
132 raise OSError("LXC is not installed")
134 config_file
= "config_1"
135 config_path
= tools
.config
.tools_src
+ "/data/configs/" + config_file
137 command
= ["mkdir", "-p", lxc_path
]
138 tools
.helpers
.run
.user(args
, command
)
139 command
= ["cp", "-fpr", config_path
, lxc_path
+ "/config"]
140 tools
.helpers
.run
.user(args
, command
)
141 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
142 tools
.helpers
.run
.user(args
, command
)
144 nodes
= generate_nodes_lxc_config(args
)
145 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
146 config_nodes
= open(config_nodes_tmp_path
, "w")
148 config_nodes
.write(node
+ "\n")
150 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
151 tools
.helpers
.run
.user(args
, command
)
154 def make_base_props(args
):
155 def find_hal(hardware
):
157 "ro.hardware." + hardware
,
162 for p
in hardware_props
:
163 prop
= tools
.helpers
.props
.host_get(args
, p
)
165 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
166 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
167 if os
.path
.isfile(hal_file
):
173 if not os
.path
.exists("/dev/ashmem"):
174 props
.append("sys.use_memfd=true")
176 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
178 gralloc
= find_hal("gralloc")
180 if os
.path
.exists("/dev/dri"):
186 props
.append("debug.stagefright.ccodec=0")
187 props
.append("ro.hardware.gralloc=" + gralloc
)
190 props
.append("ro.hardware.egl=" + egl
)
192 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
193 if media_profiles
!= "":
194 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
195 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
196 props
.append("media.settings.xml=" + media_profiles
)
198 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
200 props
.append("debug.stagefright.ccodec=" + ccodec
)
202 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
203 if ext_library
!= "":
204 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
205 ext_library
= ext_library
.replace("odm/", "odm_extra/")
206 props
.append("ro.vendor.extension_library=" + ext_library
)
208 vulkan
= find_hal("vulkan")
210 props
.append("ro.hardware.vulkan=" + vulkan
)
212 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
214 camera
= find_hal("camera")
216 props
.append("ro.hardware.camera=" + camera
)
218 if args
.vendor_type
== "MAINLINE":
219 props
.append("ro.hardware.camera=v4l2")
221 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
224 props
.append("ro.opengles.version=" + opengles
)
226 props
.append("waydroid.system_ota=" + args
.system_ota
)
227 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
228 props
.append("waydroid.tools_version=" + tools
.config
.version
)
230 if args
.vendor_type
== "MAINLINE":
231 props
.append("ro.vndk.lite=true")
233 for product
in ["brand", "device", "manufacturer", "model", "name"]:
234 prop_product
= tools
.helpers
.props
.host_get(
235 args
, "ro.product.vendor." + product
)
236 if prop_product
!= "":
237 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
239 if os
.path
.isfile("/proc/device-tree/" + product
):
240 with open("/proc/device-tree/" + product
) as f
:
241 f_value
= f
.read().strip().rstrip('\x00')
243 props
.append("ro.product.waydroid." +
244 product
+ "=" + f_value
)
246 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
248 props
.append("ro.build.fingerprint=" + prop_fp
)
250 # now append/override with values in [properties] section of waydroid.cfg
251 cfg
= tools
.config
.load(args
)
252 for k
, v
in cfg
["properties"].items():
253 for idx
, elem
in enumerate(props
):
256 props
.append(k
+"="+v
)
258 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
260 base_props
.write(prop
+ "\n")
264 def setup_host_perms(args
):
265 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
266 os
.mkdir(tools
.config
.defaults
["host_perms"])
268 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
272 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
275 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
276 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
277 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
279 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
280 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
281 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
284 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
285 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
287 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
289 for filename
in copy_list
:
290 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
293 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
294 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
295 os
.chmod(args
.log
, 0o666)
299 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
300 "-F", "-n", "waydroid", "--", "/init"]
301 tools
.helpers
.run
.user(args
, command
, output
="background")
304 command
= ["lxc-stop", "-P",
305 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
306 tools
.helpers
.run
.user(args
, command
)
309 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
310 tools
.helpers
.run
.user(args
, command
)
313 command
= ["lxc-unfreeze", "-P",
314 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
315 tools
.helpers
.run
.user(args
, command
)
318 if status(args
) != "RUNNING":
319 logging
.error("WayDroid container is {}".format(status(args
)))
321 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
322 "-n", "waydroid", "--"]
324 command
.append(args
.COMMAND
)
326 command
.append("/system/bin/sh")
327 subprocess
.run(command
, env
={"PATH": os.environ['PATH'] + ":/system/bin:/vendor/bin"}
)
330 if status(args
) != "RUNNING":
331 logging
.error("WayDroid container is {}".format(status(args
)))
333 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
334 "-n", "waydroid", "--", "/system/bin/logcat"]
335 subprocess
.run(command
)