1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
12 import tools
.helpers
.run
15 def get_lxc_version(args
):
16 if shutil
.which("lxc-info") is not None:
17 command
= ["lxc-info", "--version"]
18 version_str
= tools
.helpers
.run
.user(args
, command
, output_return
=True)
19 return int(version_str
[0])
24 def generate_nodes_lxc_config(args
):
25 def make_entry(src
, dist
=None, mnt_type
="none", options
="bind,create=file,optional 0 0", check
=True):
26 if check
and not os
.path
.exists(src
):
28 entry
= "lxc.mount.entry = "
33 entry
+= mnt_type
+ " "
40 make_entry("tmpfs", "dev", "tmpfs", "nosuid 0 0", False)
41 make_entry("/dev/zero")
42 make_entry("/dev/null")
43 make_entry("/dev/full")
44 make_entry("/dev/ashmem", check
=False)
45 make_entry("/dev/fuse")
46 make_entry("/dev/ion")
47 make_entry("/dev/char", options
="bind,create=dir,optional 0 0")
50 make_entry("/dev/kgsl-3d0")
51 make_entry("/dev/mali0")
52 make_entry("/dev/pvr_sync")
53 make_entry("/dev/pmsg0")
54 make_entry("/dev/dxg")
55 make_entry(tools
.helpers
.gpu
.getDriNode(args
), "dev/dri/renderD128")
57 for n
in glob
.glob("/dev/fb*"):
59 for n
in glob
.glob("/dev/graphics/fb*"):
61 for n
in glob
.glob("/dev/video*"):
65 make_entry("/dev/" + args
.BINDER_DRIVER
, "dev/binder", check
=False)
66 make_entry("/dev/" + args
.VNDBINDER_DRIVER
, "dev/vndbinder", check
=False)
67 make_entry("/dev/" + args
.HWBINDER_DRIVER
, "dev/hwbinder", check
=False)
69 if args
.vendor_type
!= "MAINLINE":
70 if not make_entry("/dev/hwbinder", "dev/host_hwbinder"):
71 raise OSError('Binder node "hwbinder" of host not found')
72 make_entry("/vendor", "vendor_extra", options
="bind,optional 0 0")
74 # Necessary device nodes for adb
75 make_entry("none", "dev/pts", "devpts", "defaults,mode=644,ptmxmode=666,create=dir 0 0", False)
76 make_entry("/dev/uhid")
78 # TUN/TAP device node for VPN
79 make_entry("/dev/net/tun", "dev/tun")
81 # Low memory killer sys node
82 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
85 make_entry("tmpfs", "mnt", "tmpfs", "mode=0755,uid=0,gid=1000", False)
86 make_entry(tools
.config
.defaults
["data"], "data", options
="bind 0 0", check
=False)
88 # Mount host permissions
89 make_entry(tools
.config
.defaults
["host_perms"],
90 "vendor/etc/host-permissions", options
="bind,optional 0 0")
92 # Recursive mount /run to provide necessary host sockets
93 make_entry("/run", options
="rbind,create=dir 0 0")
95 # Necessary sw_sync node for HWC
96 make_entry("/dev/sw_sync")
97 make_entry("/sys/kernel/debug", options
="rbind,create=dir,optional 0 0")
100 make_entry("/sys/class/leds/vibrator",
101 options
="bind,create=dir,optional 0 0")
102 make_entry("/sys/devices/virtual/timed_output/vibrator",
103 options
="bind,create=dir,optional 0 0")
105 # Media dev nodes (for Mediatek)
106 make_entry("/dev/Vcodec")
107 make_entry("/dev/MTK_SMI")
108 make_entry("/dev/mdp_sync")
109 make_entry("/dev/mtk_cmdq")
112 make_entry("tmpfs", "mnt_extra", "tmpfs", "nodev 0 0", False)
113 make_entry("/mnt/wslg", "mnt_extra/wslg",
114 options
="rbind,create=dir,optional 0 0")
117 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
118 make_entry("/var/run", options
="rbind,create=dir,optional 0 0")
121 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
122 for n
in glob
.glob("/tmp/run-*"):
123 make_entry(n
, options
="rbind,create=dir,optional 0 0")
126 make_entry("/system/etc/libnfc-nci.conf", options
="bind,optional 0 0")
131 def set_lxc_config(args
):
132 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
133 config_file
= "config_2"
134 lxc_ver
= get_lxc_version(args
)
136 raise OSError("LXC is not installed")
138 config_file
= "config_1"
139 config_path
= tools
.config
.tools_src
+ "/data/configs/" + config_file
140 seccomp_profile
= tools
.config
.tools_src
+ "/data/configs/waydroid.seccomp"
142 command
= ["mkdir", "-p", lxc_path
]
143 tools
.helpers
.run
.user(args
, command
)
144 command
= ["cp", "-fpr", config_path
, lxc_path
+ "/config"]
145 tools
.helpers
.run
.user(args
, command
)
146 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
147 tools
.helpers
.run
.user(args
, command
)
148 command
= ["cp", "-fpr", seccomp_profile
, lxc_path
+ "/waydroid.seccomp"]
149 tools
.helpers
.run
.user(args
, command
)
151 nodes
= generate_nodes_lxc_config(args
)
152 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
153 config_nodes
= open(config_nodes_tmp_path
, "w")
155 config_nodes
.write(node
+ "\n")
157 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
158 tools
.helpers
.run
.user(args
, command
)
161 def make_base_props(args
):
162 def find_hal(hardware
):
164 "ro.hardware." + hardware
,
169 for p
in hardware_props
:
170 prop
= tools
.helpers
.props
.host_get(args
, p
)
172 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
173 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
174 if os
.path
.isfile(hal_file
):
179 if args
.vendor_type
== "MAINLINE":
183 sm
= gbinder
.ServiceManager("/dev/hwbinder")
184 return intf
in sm
.list_sync()
190 if not os
.path
.exists("/dev/ashmem"):
191 props
.append("sys.use_memfd=true")
193 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
194 dri
= tools
.helpers
.gpu
.getDriNode(args
)
196 gralloc
= find_hal("gralloc")
198 if find_hidl("android.hardware.graphics.allocator@4.0::IAllocator/default"):
207 props
.append("debug.stagefright.ccodec=0")
208 props
.append("ro.hardware.gralloc=" + gralloc
)
211 props
.append("ro.hardware.egl=" + egl
)
213 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
214 if media_profiles
!= "":
215 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
216 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
217 props
.append("media.settings.xml=" + media_profiles
)
219 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
221 props
.append("debug.stagefright.ccodec=" + ccodec
)
223 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
224 if ext_library
!= "":
225 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
226 ext_library
= ext_library
.replace("odm/", "odm_extra/")
227 props
.append("ro.vendor.extension_library=" + ext_library
)
229 vulkan
= find_hal("vulkan")
230 if not vulkan
and dri
:
231 vulkan
= tools
.helpers
.gpu
.getVulkanDriver(args
, os
.path
.basename(dri
))
233 props
.append("ro.hardware.vulkan=" + vulkan
)
235 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
237 camera
= find_hal("camera")
239 props
.append("ro.hardware.camera=" + camera
)
241 if args
.vendor_type
== "MAINLINE":
242 props
.append("ro.hardware.camera=v4l2")
244 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
247 props
.append("ro.opengles.version=" + opengles
)
249 if args
.images_path
!= tools
.config
.defaults
["preinstalled_images_path"]:
250 props
.append("waydroid.system_ota=" + args
.system_ota
)
251 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
253 props
.append("waydroid.updater.disabled=true")
255 props
.append("waydroid.tools_version=" + tools
.config
.version
)
257 if args
.vendor_type
== "MAINLINE":
258 props
.append("ro.vndk.lite=true")
260 for product
in ["brand", "device", "manufacturer", "model", "name"]:
261 prop_product
= tools
.helpers
.props
.host_get(
262 args
, "ro.product.vendor." + product
)
263 if prop_product
!= "":
264 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
266 if os
.path
.isfile("/proc/device-tree/" + product
):
267 with open("/proc/device-tree/" + product
) as f
:
268 f_value
= f
.read().strip().rstrip('\x00')
270 props
.append("ro.product.waydroid." +
271 product
+ "=" + f_value
)
273 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
275 props
.append("ro.build.fingerprint=" + prop_fp
)
277 # now append/override with values in [properties] section of waydroid.cfg
278 cfg
= tools
.config
.load(args
)
279 for k
, v
in cfg
["properties"].items():
280 for idx
, elem
in enumerate(props
):
283 props
.append(k
+"="+v
)
285 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
287 base_props
.write(prop
+ "\n")
291 def setup_host_perms(args
):
292 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
293 os
.mkdir(tools
.config
.defaults
["host_perms"])
295 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
299 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
302 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
303 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
304 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
306 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
307 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
308 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
311 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
312 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
314 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
316 for filename
in copy_list
:
317 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
320 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
321 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
322 os
.chmod(args
.log
, 0o666)
326 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
327 "-F", "-n", "waydroid", "--", "/init"]
328 tools
.helpers
.run
.user(args
, command
, output
="background")
331 command
= ["lxc-stop", "-P",
332 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
333 tools
.helpers
.run
.user(args
, command
)
336 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
337 tools
.helpers
.run
.user(args
, command
)
340 command
= ["lxc-unfreeze", "-P",
341 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
342 tools
.helpers
.run
.user(args
, command
)
345 if status(args
) != "RUNNING":
346 logging
.error("WayDroid container is {}".format(status(args
)))
348 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
349 "-n", "waydroid", "--"]
351 command
.append(args
.COMMAND
)
353 command
.append("/system/bin/sh")
354 subprocess
.run(command
, env
={"PATH": os.environ['PATH'] + ":/system/bin:/vendor/bin"}
)
357 if status(args
) != "RUNNING":
358 logging
.error("WayDroid container is {}".format(status(args
)))
360 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
361 "-n", "waydroid", "--", "/system/bin/logcat"]
362 subprocess
.run(command
)