]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/lxc.py
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])
24 def add_node_entry(nodes
, src
, dist
, mnt_type
, options
, check
):
25 if check
and not os
.path
.exists(src
):
27 entry
= "lxc.mount.entry = "
32 entry
+= mnt_type
+ " "
37 def generate_nodes_lxc_config(args
):
39 def make_entry(src
, dist
=None, mnt_type
="none", options
="bind,create=file,optional 0 0", check
=True):
40 return add_node_entry(nodes
, src
, dist
, mnt_type
, options
, check
)
43 make_entry("tmpfs", "dev", "tmpfs", "nosuid 0 0", False)
44 make_entry("/dev/zero")
45 make_entry("/dev/null")
46 make_entry("/dev/full")
47 make_entry("/dev/ashmem")
48 make_entry("/dev/fuse")
49 make_entry("/dev/ion")
50 make_entry("/dev/tty")
51 make_entry("/dev/char", options
="bind,create=dir,optional 0 0")
54 make_entry("/dev/kgsl-3d0")
55 make_entry("/dev/mali0")
56 make_entry("/dev/pvr_sync")
57 make_entry("/dev/pmsg0")
58 make_entry("/dev/dxg")
59 render
, card
= tools
.helpers
.gpu
.getDriNode(args
)
60 make_entry(render
, "dev/dri/renderD128")
61 make_entry(card
, "dev/dri/card0")
63 for n
in glob
.glob("/dev/fb*"):
65 for n
in glob
.glob("/dev/graphics/fb*"):
67 for n
in glob
.glob("/dev/video*"):
71 make_entry("/dev/" + args
.BINDER_DRIVER
, "dev/binder", check
=False)
72 make_entry("/dev/" + args
.VNDBINDER_DRIVER
, "dev/vndbinder", check
=False)
73 make_entry("/dev/" + args
.HWBINDER_DRIVER
, "dev/hwbinder", check
=False)
75 if args
.vendor_type
!= "MAINLINE":
76 if not make_entry("/dev/hwbinder", "dev/host_hwbinder"):
77 raise OSError('Binder node "hwbinder" of host not found')
78 make_entry("/vendor", "vendor_extra", options
="bind,optional 0 0")
80 # Necessary device nodes for adb
81 make_entry("none", "dev/pts", "devpts", "defaults,mode=644,ptmxmode=666,create=dir 0 0", False)
82 make_entry("/dev/uhid")
84 # TUN/TAP device node for VPN
85 make_entry("/dev/net/tun", "dev/tun")
87 # Low memory killer sys node
88 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
90 # Mount host permissions
91 make_entry(tools
.config
.defaults
["host_perms"],
92 "vendor/etc/host-permissions", options
="bind,optional 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")
115 # Make a tmpfs at every possible rootfs mountpoint
116 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
117 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
118 make_entry("tmpfs", "run", "tmpfs", "nodev 0 0", False)
121 make_entry("/system/etc/libnfc-nci.conf", options
="bind,optional 0 0")
125 LXC_APPARMOR_PROFILE
= "lxc-waydroid"
126 def get_apparmor_status(args
):
128 if shutil
.which("aa-enabled"):
129 enabled
= (tools
.helpers
.run
.user(args
, ["aa-enabled", "--quiet"], check
=False) == 0)
130 if not enabled
and shutil
.which("systemctl"):
131 enabled
= (tools
.helpers
.run
.user(args
, ["systemctl", "is-active", "-q", "apparmor"], check
=False) == 0)
133 with open("/sys/kernel/security/apparmor/profiles", "r") as f
:
134 enabled
&= (LXC_APPARMOR_PROFILE
in f
.read())
139 def set_lxc_config(args
):
140 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
141 lxc_ver
= get_lxc_version(args
)
143 raise OSError("LXC is not installed")
144 config_paths
= tools
.config
.tools_src
+ "/data/configs/config_"
145 seccomp_profile
= tools
.config
.tools_src
+ "/data/configs/waydroid.seccomp"
147 config_snippets
= [ config_paths
+ "base" ]
148 # lxc v1 and v2 are bit special because some options got renamed later
150 config_snippets
.append(config_paths
+ "1")
152 for ver
in range(3, 5):
153 snippet
= config_paths
+ str(ver
)
154 if lxc_ver
>= ver
and os
.path
.exists(snippet
):
155 config_snippets
.append(snippet
)
157 command
= ["mkdir", "-p", lxc_path
]
158 tools
.helpers
.run
.user(args
, command
)
159 command
= ["sh", "-c", "cat {} > \"{}\"".format(' '.join('"{0}"'.format(w
) for w
in config_snippets
), lxc_path
+ "/config")]
160 tools
.helpers
.run
.user(args
, command
)
161 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
162 tools
.helpers
.run
.user(args
, command
)
163 command
= ["cp", "-fpr", seccomp_profile
, lxc_path
+ "/waydroid.seccomp"]
164 tools
.helpers
.run
.user(args
, command
)
165 if get_apparmor_status(args
):
166 command
= ["sed", "-i", "-E", "/lxc.aa_profile|lxc.apparmor.profile/ s/unconfined/{}/g".format(LXC_APPARMOR_PROFILE
), lxc_path
+ "/config"]
167 tools
.helpers
.run
.user(args
, command
)
169 nodes
= generate_nodes_lxc_config(args
)
170 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
171 config_nodes
= open(config_nodes_tmp_path
, "w")
173 config_nodes
.write(node
+ "\n")
175 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
176 tools
.helpers
.run
.user(args
, command
)
179 open(os
.path
.join(lxc_path
, "config_session"), mode
="w").close()
181 def generate_session_lxc_config(args
, session
):
183 def make_entry(src
, dist
=None, mnt_type
="none", options
="rbind,create=file 0 0"):
184 if any(x
in src
for x
in ["\n", "\r"]):
185 logging
.warning("User-provided mount path contains illegal character: " + src
)
187 if dist
is None and (not os
.path
.exists(src
) or
188 str(os
.stat(src
).st_uid
) != session
["user_id"]):
189 logging
.warning("User-provided mount path is not owned by user: " + src
)
191 return add_node_entry(nodes
, src
, dist
, mnt_type
, options
, check
=False)
193 # Make sure XDG_RUNTIME_DIR exists
194 if not make_entry("tmpfs", session
["xdg_runtime_dir"], options
="create=dir 0 0"):
195 raise OSError("Failed to create XDG_RUNTIME_DIR mount point")
197 wayland_socket
= os
.path
.realpath(os
.path
.join(session
["xdg_runtime_dir"], session
["wayland_display"]))
198 if not make_entry(wayland_socket
):
199 raise OSError("Failed to bind Wayland socket")
201 pulse_socket
= os
.path
.join(session
["pulse_runtime_path"], "native")
202 make_entry(pulse_socket
)
204 if not make_entry(session
["waydroid_data"], "data", options
="rbind 0 0"):
205 raise OSError("Failed to bind userdata")
207 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
208 config_nodes_tmp_path
= args
.work
+ "/config_session"
209 config_nodes
= open(config_nodes_tmp_path
, "w")
211 config_nodes
.write(node
+ "\n")
213 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
214 tools
.helpers
.run
.user(args
, command
)
216 def make_base_props(args
):
217 def find_hal(hardware
):
219 "ro.hardware." + hardware
,
224 for p
in hardware_props
:
225 prop
= tools
.helpers
.props
.host_get(args
, p
)
227 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
228 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
229 if os
.path
.isfile(hal_file
):
234 if args
.vendor_type
== "MAINLINE":
238 sm
= gbinder
.ServiceManager("/dev/hwbinder")
239 return intf
in sm
.list_sync()
245 if not os
.path
.exists("/dev/ashmem"):
246 props
.append("sys.use_memfd=true")
248 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
249 dri
, _
= tools
.helpers
.gpu
.getDriNode(args
)
251 gralloc
= find_hal("gralloc")
253 if find_hidl("android.hardware.graphics.allocator@4.0::IAllocator/default"):
262 props
.append("debug.stagefright.ccodec=0")
263 props
.append("ro.hardware.gralloc=" + gralloc
)
266 props
.append("ro.hardware.egl=" + egl
)
268 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
269 if media_profiles
!= "":
270 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
271 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
272 props
.append("media.settings.xml=" + media_profiles
)
274 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
276 props
.append("debug.stagefright.ccodec=" + ccodec
)
278 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
279 if ext_library
!= "":
280 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
281 ext_library
= ext_library
.replace("odm/", "odm_extra/")
282 props
.append("ro.vendor.extension_library=" + ext_library
)
284 vulkan
= find_hal("vulkan")
285 if not vulkan
and dri
:
286 vulkan
= tools
.helpers
.gpu
.getVulkanDriver(args
, os
.path
.basename(dri
))
288 props
.append("ro.hardware.vulkan=" + vulkan
)
290 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
292 camera
= find_hal("camera")
294 props
.append("ro.hardware.camera=" + camera
)
296 if args
.vendor_type
== "MAINLINE":
297 props
.append("ro.hardware.camera=v4l2")
299 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
302 props
.append("ro.opengles.version=" + opengles
)
304 if args
.images_path
not in tools
.config
.defaults
["preinstalled_images_paths"]:
305 props
.append("waydroid.system_ota=" + args
.system_ota
)
306 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
308 props
.append("waydroid.updater.disabled=true")
310 props
.append("waydroid.tools_version=" + tools
.config
.version
)
312 if args
.vendor_type
== "MAINLINE":
313 props
.append("ro.vndk.lite=true")
315 for product
in ["brand", "device", "manufacturer", "model", "name"]:
316 prop_product
= tools
.helpers
.props
.host_get(
317 args
, "ro.product.vendor." + product
)
318 if prop_product
!= "":
319 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
321 if os
.path
.isfile("/proc/device-tree/" + product
):
322 with open("/proc/device-tree/" + product
) as f
:
323 f_value
= f
.read().strip().rstrip('\x00')
325 props
.append("ro.product.waydroid." +
326 product
+ "=" + f_value
)
328 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
330 props
.append("ro.build.fingerprint=" + prop_fp
)
332 # now append/override with values in [properties] section of waydroid.cfg
333 cfg
= tools
.config
.load(args
)
334 for k
, v
in cfg
["properties"].items():
335 for idx
, elem
in enumerate(props
):
338 props
.append(k
+"="+v
)
340 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
342 base_props
.write(prop
+ "\n")
346 def setup_host_perms(args
):
347 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
348 os
.mkdir(tools
.config
.defaults
["host_perms"])
350 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
354 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
357 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
358 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
359 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
361 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
362 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
363 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
366 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
367 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
369 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
371 for filename
in copy_list
:
372 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
375 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
377 return tools
.helpers
.run
.user(args
, command
, output_return
=True).strip()
379 logging
.info("Couldn't get LXC status. Assuming STOPPED.")
382 def wait_for_running(args
):
383 lxc_status
= status(args
)
385 while lxc_status
!= "RUNNING" and timeout
> 0:
386 lxc_status
= status(args
)
388 "waiting {} seconds for container to start...".format(timeout
))
389 timeout
= timeout
- 1
391 if lxc_status
!= "RUNNING":
392 raise OSError("container failed to start")
395 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
396 "-F", "-n", "waydroid", "--", "/init"]
397 tools
.helpers
.run
.user(args
, command
, output
="background")
398 wait_for_running(args
)
399 # Workaround lxc-start changing stdout/stderr permissions to 700
400 os
.chmod(args
.log
, 0o666)
403 command
= ["lxc-stop", "-P",
404 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
405 tools
.helpers
.run
.user(args
, command
)
408 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
409 tools
.helpers
.run
.user(args
, command
)
412 command
= ["lxc-unfreeze", "-P",
413 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
414 tools
.helpers
.run
.user(args
, command
)
417 "PATH": "/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin",
418 "ANDROID_ROOT": "/system",
419 "ANDROID_DATA": "/data",
420 "ANDROID_STORAGE": "/storage",
421 "ANDROID_ART_ROOT": "/apex/com.android.art",
422 "ANDROID_I18N_ROOT": "/apex/com.android.i18n",
423 "ANDROID_TZDATA_ROOT": "/apex/com.android.tzdata",
424 "ANDROID_RUNTIME_ROOT": "/apex/com.android.runtime",
425 "BOOTCLASSPATH": "/apex/com.android.art/javalib/core-oj.jar:/apex/com.android.art/javalib/core-libart.jar:/apex/com.android.art/javalib/core-icu4j.jar:/apex/com.android.art/javalib/okhttp.jar:/apex/com.android.art/javalib/bouncycastle.jar:/apex/com.android.art/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/framework-atb-backward-compatibility.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.media/javalib/updatable-media.jar:/apex/com.android.mediaprovider/javalib/framework-mediaprovider.jar:/apex/com.android.os.statsd/javalib/framework-statsd.jar:/apex/com.android.permission/javalib/framework-permission.jar:/apex/com.android.sdkext/javalib/framework-sdkextensions.jar:/apex/com.android.wifi/javalib/framework-wifi.jar:/apex/com.android.tethering/javalib/framework-tethering.jar"
428 def android_env_attach_options():
429 env
= [k
+ "=" + v
for k
, v
in ANDROID_ENV
.items()]
430 return [x
for var
in env
for x
in ("--set-var", var
)]
434 if state
== "FROZEN":
436 elif state
!= "RUNNING":
437 logging
.error("WayDroid container is {}".format(state
))
439 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
440 "-n", "waydroid", "--clear-env"]
441 command
.extend(android_env_attach_options())
444 command
.extend(args
.COMMAND
)
446 command
.append("/system/bin/sh")
447 subprocess
.run(command
)
448 if state
== "FROZEN":
452 args
.COMMAND
= ["/system/bin/logcat"]