1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
13 import tools
.helpers
.run
16 def get_lxc_version(args
):
17 if shutil
.which("lxc-info") is not None:
18 command
= ["lxc-info", "--version"]
19 version_str
= tools
.helpers
.run
.user(args
, command
, output_return
=True)
20 return int(version_str
[0])
25 def generate_nodes_lxc_config(args
):
26 def make_entry(src
, dist
=None, mnt_type
="none", options
="bind,create=file,optional 0 0", check
=True):
27 if check
and not os
.path
.exists(src
):
29 entry
= "lxc.mount.entry = "
34 entry
+= mnt_type
+ " "
41 make_entry("tmpfs", "dev", "tmpfs", "nosuid 0 0", False)
42 make_entry("/dev/zero")
43 make_entry("/dev/null")
44 make_entry("/dev/full")
45 make_entry("/dev/ashmem", check
=False)
46 make_entry("/dev/fuse")
47 make_entry("/dev/ion")
48 make_entry("/dev/char", options
="bind,create=dir,optional 0 0")
51 make_entry("/dev/kgsl-3d0")
52 make_entry("/dev/mali0")
53 make_entry("/dev/pvr_sync")
54 make_entry("/dev/pmsg0")
55 make_entry("/dev/dxg")
56 make_entry(tools
.helpers
.gpu
.getDriNode(args
), "dev/dri/renderD128")
58 for n
in glob
.glob("/dev/fb*"):
60 for n
in glob
.glob("/dev/graphics/fb*"):
62 for n
in glob
.glob("/dev/video*"):
66 make_entry("/dev/" + args
.BINDER_DRIVER
, "dev/binder", check
=False)
67 make_entry("/dev/" + args
.VNDBINDER_DRIVER
, "dev/vndbinder", check
=False)
68 make_entry("/dev/" + args
.HWBINDER_DRIVER
, "dev/hwbinder", check
=False)
70 if args
.vendor_type
!= "MAINLINE":
71 if not make_entry("/dev/hwbinder", "dev/host_hwbinder"):
72 raise OSError('Binder node "hwbinder" of host not found')
73 make_entry("/vendor", "vendor_extra", options
="bind,optional 0 0")
75 # Necessary device nodes for adb
76 make_entry("none", "dev/pts", "devpts", "defaults,mode=644,ptmxmode=666,create=dir 0 0", False)
77 make_entry("/dev/uhid")
79 # TUN/TAP device node for VPN
80 make_entry("/dev/net/tun", "dev/tun")
82 # Low memory killer sys node
83 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
86 make_entry("tmpfs", "mnt", "tmpfs", "mode=0755,uid=0,gid=1000", False)
87 make_entry(tools
.config
.defaults
["data"], "data", options
="bind 0 0", check
=False)
89 # Mount host permissions
90 make_entry(tools
.config
.defaults
["host_perms"],
91 "vendor/etc/host-permissions", options
="bind,optional 0 0")
93 # Recursive mount /run to provide necessary host sockets
94 make_entry("/run", options
="rbind,create=dir 0 0")
96 make_entry("/dev/shm", options
="rbind,create=dir,optional 0 0")
98 # Necessary sw_sync node for HWC
99 make_entry("/dev/sw_sync")
100 make_entry("/sys/kernel/debug", options
="rbind,create=dir,optional 0 0")
103 make_entry("/sys/class/leds/vibrator",
104 options
="bind,create=dir,optional 0 0")
105 make_entry("/sys/devices/virtual/timed_output/vibrator",
106 options
="bind,create=dir,optional 0 0")
108 # Media dev nodes (for Mediatek)
109 make_entry("/dev/Vcodec")
110 make_entry("/dev/MTK_SMI")
111 make_entry("/dev/mdp_sync")
112 make_entry("/dev/mtk_cmdq")
115 make_entry("tmpfs", "mnt_extra", "tmpfs", "nodev 0 0", False)
116 make_entry("/mnt/wslg", "mnt_extra/wslg",
117 options
="rbind,create=dir,optional 0 0")
120 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
121 make_entry("/var/run", options
="rbind,create=dir,optional 0 0")
124 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
125 for n
in glob
.glob("/tmp/run-*"):
126 make_entry(n
, options
="rbind,create=dir,optional 0 0")
129 make_entry("/system/etc/libnfc-nci.conf", options
="bind,optional 0 0")
133 LXC_APPARMOR_PROFILE
= "lxc-waydroid"
134 def get_apparmor_status(args
):
136 if shutil
.which("aa-enabled"):
137 enabled
= (tools
.helpers
.run
.user(args
, ["aa-enabled", "--quiet"], check
=False) == 0)
138 if not enabled
and shutil
.which("systemctl"):
139 enabled
= (tools
.helpers
.run
.user(args
, ["systemctl", "is-active", "-q", "apparmor"], check
=False) == 0)
141 with open("/sys/kernel/security/apparmor/profiles", "r") as f
:
142 enabled
&= (LXC_APPARMOR_PROFILE
in f
.read())
147 def set_lxc_config(args
):
148 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
149 lxc_ver
= get_lxc_version(args
)
151 raise OSError("LXC is not installed")
152 config_paths
= tools
.config
.tools_src
+ "/data/configs/config_"
153 seccomp_profile
= tools
.config
.tools_src
+ "/data/configs/waydroid.seccomp"
155 config_snippets
= [ config_paths
+ "base" ]
156 # lxc v1 and v2 are bit special because some options got renamed later
158 config_snippets
.append(config_paths
+ "1")
160 for ver
in range(3, 5):
161 snippet
= config_paths
+ str(ver
)
162 if lxc_ver
>= ver
and os
.path
.exists(snippet
):
163 config_snippets
.append(snippet
)
165 command
= ["mkdir", "-p", lxc_path
]
166 tools
.helpers
.run
.user(args
, command
)
167 command
= ["sh", "-c", "cat {} > \"{}\"".format(' '.join('"{0}"'.format(w
) for w
in config_snippets
), lxc_path
+ "/config")]
168 tools
.helpers
.run
.user(args
, command
)
169 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
170 tools
.helpers
.run
.user(args
, command
)
171 command
= ["cp", "-fpr", seccomp_profile
, lxc_path
+ "/waydroid.seccomp"]
172 tools
.helpers
.run
.user(args
, command
)
173 if get_apparmor_status(args
):
174 command
= ["sed", "-i", "-E", "/lxc.aa_profile|lxc.apparmor.profile/ s/unconfined/{}/g".format(LXC_APPARMOR_PROFILE
), lxc_path
+ "/config"]
175 tools
.helpers
.run
.user(args
, command
)
177 nodes
= generate_nodes_lxc_config(args
)
178 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
179 config_nodes
= open(config_nodes_tmp_path
, "w")
181 config_nodes
.write(node
+ "\n")
183 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
184 tools
.helpers
.run
.user(args
, command
)
187 def make_base_props(args
):
188 def find_hal(hardware
):
190 "ro.hardware." + hardware
,
195 for p
in hardware_props
:
196 prop
= tools
.helpers
.props
.host_get(args
, p
)
198 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
199 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
200 if os
.path
.isfile(hal_file
):
205 if args
.vendor_type
== "MAINLINE":
209 sm
= gbinder
.ServiceManager("/dev/hwbinder")
210 return intf
in sm
.list_sync()
216 if not os
.path
.exists("/dev/ashmem"):
217 props
.append("sys.use_memfd=true")
219 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
220 dri
= tools
.helpers
.gpu
.getDriNode(args
)
222 gralloc
= find_hal("gralloc")
224 if find_hidl("android.hardware.graphics.allocator@4.0::IAllocator/default"):
233 props
.append("debug.stagefright.ccodec=0")
234 props
.append("ro.hardware.gralloc=" + gralloc
)
237 props
.append("ro.hardware.egl=" + egl
)
239 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
240 if media_profiles
!= "":
241 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
242 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
243 props
.append("media.settings.xml=" + media_profiles
)
245 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
247 props
.append("debug.stagefright.ccodec=" + ccodec
)
249 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
250 if ext_library
!= "":
251 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
252 ext_library
= ext_library
.replace("odm/", "odm_extra/")
253 props
.append("ro.vendor.extension_library=" + ext_library
)
255 vulkan
= find_hal("vulkan")
256 if not vulkan
and dri
:
257 vulkan
= tools
.helpers
.gpu
.getVulkanDriver(args
, os
.path
.basename(dri
))
259 props
.append("ro.hardware.vulkan=" + vulkan
)
261 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
263 camera
= find_hal("camera")
265 props
.append("ro.hardware.camera=" + camera
)
267 if args
.vendor_type
== "MAINLINE":
268 props
.append("ro.hardware.camera=v4l2")
270 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
273 props
.append("ro.opengles.version=" + opengles
)
275 if args
.images_path
not in tools
.config
.defaults
["preinstalled_images_paths"]:
276 props
.append("waydroid.system_ota=" + args
.system_ota
)
277 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
279 props
.append("waydroid.updater.disabled=true")
281 props
.append("waydroid.tools_version=" + tools
.config
.version
)
283 if args
.vendor_type
== "MAINLINE":
284 props
.append("ro.vndk.lite=true")
286 for product
in ["brand", "device", "manufacturer", "model", "name"]:
287 prop_product
= tools
.helpers
.props
.host_get(
288 args
, "ro.product.vendor." + product
)
289 if prop_product
!= "":
290 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
292 if os
.path
.isfile("/proc/device-tree/" + product
):
293 with open("/proc/device-tree/" + product
) as f
:
294 f_value
= f
.read().strip().rstrip('\x00')
296 props
.append("ro.product.waydroid." +
297 product
+ "=" + f_value
)
299 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
301 props
.append("ro.build.fingerprint=" + prop_fp
)
303 # now append/override with values in [properties] section of waydroid.cfg
304 cfg
= tools
.config
.load(args
)
305 for k
, v
in cfg
["properties"].items():
306 for idx
, elem
in enumerate(props
):
309 props
.append(k
+"="+v
)
311 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
313 base_props
.write(prop
+ "\n")
317 def setup_host_perms(args
):
318 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
319 os
.mkdir(tools
.config
.defaults
["host_perms"])
321 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
325 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
328 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
329 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
330 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
332 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
333 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
334 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
337 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
338 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
340 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
342 for filename
in copy_list
:
343 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
346 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
347 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
350 def wait_for_running(args
):
351 lxc_status
= status(args
)
353 while lxc_status
!= "RUNNING" and timeout
> 0:
354 lxc_status
= status(args
)
356 "waiting {} seconds for container to start...".format(timeout
))
357 timeout
= timeout
- 1
359 if lxc_status
!= "RUNNING":
360 raise OSError("container failed to start")
363 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
364 "-F", "-n", "waydroid", "--", "/init"]
365 tools
.helpers
.run
.user(args
, command
, output
="background")
366 wait_for_running(args
)
367 # Workaround lxc-start changing stdout/stderr permissions to 700
368 os
.chmod(args
.log
, 0o666)
371 command
= ["lxc-stop", "-P",
372 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
373 tools
.helpers
.run
.user(args
, command
)
376 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
377 tools
.helpers
.run
.user(args
, command
)
380 command
= ["lxc-unfreeze", "-P",
381 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
382 tools
.helpers
.run
.user(args
, command
)
386 if state
== "FROZEN":
388 elif state
!= "RUNNING":
389 logging
.error("WayDroid container is {}".format(state
))
391 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
392 "-n", "waydroid", "--"]
394 command
.extend(args
.COMMAND
)
396 command
.append("/system/bin/sh")
397 subprocess
.run(command
, env
={"PATH": os.environ['PATH'] + ":/system/bin:/vendor/bin"}
)
398 if state
== "FROZEN":
403 if state
== "FROZEN":
405 elif state
!= "RUNNING":
406 logging
.error("WayDroid container is {}".format(state
))
408 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
409 "-n", "waydroid", "--", "/system/bin/logcat"]
410 subprocess
.run(command
)
411 if state
== "FROZEN":