]>
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/char", options
="bind,create=dir,optional 0 0")
53 make_entry("/dev/kgsl-3d0")
54 make_entry("/dev/mali0")
55 make_entry("/dev/pvr_sync")
56 make_entry("/dev/pmsg0")
57 make_entry("/dev/dxg")
58 render
, card
= tools
.helpers
.gpu
.getDriNode(args
)
59 make_entry(render
, "dev/dri/renderD128")
60 make_entry(card
, "dev/dri/card0")
62 for n
in glob
.glob("/dev/fb*"):
64 for n
in glob
.glob("/dev/graphics/fb*"):
66 for n
in glob
.glob("/dev/video*"):
70 make_entry("/dev/" + args
.BINDER_DRIVER
, "dev/binder", check
=False)
71 make_entry("/dev/" + args
.VNDBINDER_DRIVER
, "dev/vndbinder", check
=False)
72 make_entry("/dev/" + args
.HWBINDER_DRIVER
, "dev/hwbinder", check
=False)
74 if args
.vendor_type
!= "MAINLINE":
75 if not make_entry("/dev/hwbinder", "dev/host_hwbinder"):
76 raise OSError('Binder node "hwbinder" of host not found')
77 make_entry("/vendor", "vendor_extra", options
="bind,optional 0 0")
79 # Necessary device nodes for adb
80 make_entry("none", "dev/pts", "devpts", "defaults,mode=644,ptmxmode=666,create=dir 0 0", False)
81 make_entry("/dev/uhid")
83 # TUN/TAP device node for VPN
84 make_entry("/dev/net/tun", "dev/tun")
86 # Low memory killer sys node
87 make_entry("/sys/module/lowmemorykiller", options
="bind,create=dir,optional 0 0")
89 # Mount host permissions
90 make_entry(tools
.config
.defaults
["host_perms"],
91 "vendor/etc/host-permissions", options
="bind,optional 0 0")
93 # Necessary sw_sync node for HWC
94 make_entry("/dev/sw_sync")
95 make_entry("/sys/kernel/debug", options
="rbind,create=dir,optional 0 0")
98 make_entry("/sys/class/leds/vibrator",
99 options
="bind,create=dir,optional 0 0")
100 make_entry("/sys/devices/virtual/timed_output/vibrator",
101 options
="bind,create=dir,optional 0 0")
103 # Media dev nodes (for Mediatek)
104 make_entry("/dev/Vcodec")
105 make_entry("/dev/MTK_SMI")
106 make_entry("/dev/mdp_sync")
107 make_entry("/dev/mtk_cmdq")
110 make_entry("tmpfs", "mnt_extra", "tmpfs", "nodev 0 0", False)
111 make_entry("/mnt/wslg", "mnt_extra/wslg",
112 options
="rbind,create=dir,optional 0 0")
114 # Make a tmpfs at every possible rootfs mountpoint
115 make_entry("tmpfs", "tmp", "tmpfs", "nodev 0 0", False)
116 make_entry("tmpfs", "var", "tmpfs", "nodev 0 0", False)
117 make_entry("tmpfs", "run", "tmpfs", "nodev 0 0", False)
120 make_entry("/system/etc/libnfc-nci.conf", options
="bind,optional 0 0")
124 LXC_APPARMOR_PROFILE
= "lxc-waydroid"
125 def get_apparmor_status(args
):
127 if shutil
.which("aa-enabled"):
128 enabled
= (tools
.helpers
.run
.user(args
, ["aa-enabled", "--quiet"], check
=False) == 0)
129 if not enabled
and shutil
.which("systemctl"):
130 enabled
= (tools
.helpers
.run
.user(args
, ["systemctl", "is-active", "-q", "apparmor"], check
=False) == 0)
132 with open("/sys/kernel/security/apparmor/profiles", "r") as f
:
133 enabled
&= (LXC_APPARMOR_PROFILE
in f
.read())
138 def set_lxc_config(args
):
139 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
140 lxc_ver
= get_lxc_version(args
)
142 raise OSError("LXC is not installed")
143 config_paths
= tools
.config
.tools_src
+ "/data/configs/config_"
144 seccomp_profile
= tools
.config
.tools_src
+ "/data/configs/waydroid.seccomp"
146 config_snippets
= [ config_paths
+ "base" ]
147 # lxc v1 and v2 are bit special because some options got renamed later
149 config_snippets
.append(config_paths
+ "1")
151 for ver
in range(3, 5):
152 snippet
= config_paths
+ str(ver
)
153 if lxc_ver
>= ver
and os
.path
.exists(snippet
):
154 config_snippets
.append(snippet
)
156 command
= ["mkdir", "-p", lxc_path
]
157 tools
.helpers
.run
.user(args
, command
)
158 command
= ["sh", "-c", "cat {} > \"{}\"".format(' '.join('"{0}"'.format(w
) for w
in config_snippets
), lxc_path
+ "/config")]
159 tools
.helpers
.run
.user(args
, command
)
160 command
= ["sed", "-i", "s/LXCARCH/{}/".format(platform
.machine()), lxc_path
+ "/config"]
161 tools
.helpers
.run
.user(args
, command
)
162 command
= ["cp", "-fpr", seccomp_profile
, lxc_path
+ "/waydroid.seccomp"]
163 tools
.helpers
.run
.user(args
, command
)
164 if get_apparmor_status(args
):
165 command
= ["sed", "-i", "-E", "/lxc.aa_profile|lxc.apparmor.profile/ s/unconfined/{}/g".format(LXC_APPARMOR_PROFILE
), lxc_path
+ "/config"]
166 tools
.helpers
.run
.user(args
, command
)
168 nodes
= generate_nodes_lxc_config(args
)
169 config_nodes_tmp_path
= args
.work
+ "/config_nodes"
170 config_nodes
= open(config_nodes_tmp_path
, "w")
172 config_nodes
.write(node
+ "\n")
174 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
175 tools
.helpers
.run
.user(args
, command
)
178 open(os
.path
.join(lxc_path
, "config_session"), mode
="w").close()
180 def generate_session_lxc_config(args
, session
):
182 def make_entry(src
, dist
=None, mnt_type
="none", options
="rbind,create=file 0 0"):
183 if any(x
in src
for x
in ["\n", "\r"]):
184 logging
.warning("User-provided mount path contains illegal character")
186 if dist
is None and (not os
.path
.exists(src
) or
187 str(os
.stat(src
).st_uid
) != session
["user_id"]):
188 logging
.warning("User-provided mount path is not owned by user")
190 return add_node_entry(nodes
, src
, dist
, mnt_type
, options
, check
=False)
192 # Make sure XDG_RUNTIME_DIR exists
193 if not make_entry("tmpfs", session
["xdg_runtime_dir"], options
="create=dir 0 0"):
194 raise OSError("Failed to create XDG_RUNTIME_DIR mount point")
196 wayland_socket
= os
.path
.realpath(os
.path
.join(session
["xdg_runtime_dir"], session
["wayland_display"]))
197 if not make_entry(wayland_socket
):
198 raise OSError("Failed to bind Wayland socket")
200 pulse_socket
= os
.path
.join(session
["pulse_runtime_path"], "native")
201 make_entry(pulse_socket
)
203 if not make_entry(session
["waydroid_data"], "data", options
="rbind 0 0"):
204 raise OSError("Failed to bind userdata")
206 lxc_path
= tools
.config
.defaults
["lxc"] + "/waydroid"
207 config_nodes_tmp_path
= args
.work
+ "/config_session"
208 config_nodes
= open(config_nodes_tmp_path
, "w")
210 config_nodes
.write(node
+ "\n")
212 command
= ["mv", config_nodes_tmp_path
, lxc_path
]
213 tools
.helpers
.run
.user(args
, command
)
215 def make_base_props(args
):
216 def find_hal(hardware
):
218 "ro.hardware." + hardware
,
223 for p
in hardware_props
:
224 prop
= tools
.helpers
.props
.host_get(args
, p
)
226 for lib
in ["/odm/lib", "/odm/lib64", "/vendor/lib", "/vendor/lib64", "/system/lib", "/system/lib64"]:
227 hal_file
= lib
+ "/hw/" + hardware
+ "." + prop
+ ".so"
228 if os
.path
.isfile(hal_file
):
233 if args
.vendor_type
== "MAINLINE":
237 sm
= gbinder
.ServiceManager("/dev/hwbinder")
238 return intf
in sm
.list_sync()
244 if not os
.path
.exists("/dev/ashmem"):
245 props
.append("sys.use_memfd=true")
247 egl
= tools
.helpers
.props
.host_get(args
, "ro.hardware.egl")
248 dri
, _
= tools
.helpers
.gpu
.getDriNode(args
)
250 gralloc
= find_hal("gralloc")
252 if find_hidl("android.hardware.graphics.allocator@4.0::IAllocator/default"):
261 props
.append("debug.stagefright.ccodec=0")
262 props
.append("ro.hardware.gralloc=" + gralloc
)
265 props
.append("ro.hardware.egl=" + egl
)
267 media_profiles
= tools
.helpers
.props
.host_get(args
, "media.settings.xml")
268 if media_profiles
!= "":
269 media_profiles
= media_profiles
.replace("vendor/", "vendor_extra/")
270 media_profiles
= media_profiles
.replace("odm/", "odm_extra/")
271 props
.append("media.settings.xml=" + media_profiles
)
273 ccodec
= tools
.helpers
.props
.host_get(args
, "debug.stagefright.ccodec")
275 props
.append("debug.stagefright.ccodec=" + ccodec
)
277 ext_library
= tools
.helpers
.props
.host_get(args
, "ro.vendor.extension_library")
278 if ext_library
!= "":
279 ext_library
= ext_library
.replace("vendor/", "vendor_extra/")
280 ext_library
= ext_library
.replace("odm/", "odm_extra/")
281 props
.append("ro.vendor.extension_library=" + ext_library
)
283 vulkan
= find_hal("vulkan")
284 if not vulkan
and dri
:
285 vulkan
= tools
.helpers
.gpu
.getVulkanDriver(args
, os
.path
.basename(dri
))
287 props
.append("ro.hardware.vulkan=" + vulkan
)
289 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
291 camera
= find_hal("camera")
293 props
.append("ro.hardware.camera=" + camera
)
295 if args
.vendor_type
== "MAINLINE":
296 props
.append("ro.hardware.camera=v4l2")
298 opengles
= tools
.helpers
.props
.host_get(args
, "ro.opengles.version")
301 props
.append("ro.opengles.version=" + opengles
)
303 if args
.images_path
not in tools
.config
.defaults
["preinstalled_images_paths"]:
304 props
.append("waydroid.system_ota=" + args
.system_ota
)
305 props
.append("waydroid.vendor_ota=" + args
.vendor_ota
)
307 props
.append("waydroid.updater.disabled=true")
309 props
.append("waydroid.tools_version=" + tools
.config
.version
)
311 if args
.vendor_type
== "MAINLINE":
312 props
.append("ro.vndk.lite=true")
314 for product
in ["brand", "device", "manufacturer", "model", "name"]:
315 prop_product
= tools
.helpers
.props
.host_get(
316 args
, "ro.product.vendor." + product
)
317 if prop_product
!= "":
318 props
.append("ro.product.waydroid." + product
+ "=" + prop_product
)
320 if os
.path
.isfile("/proc/device-tree/" + product
):
321 with open("/proc/device-tree/" + product
) as f
:
322 f_value
= f
.read().strip().rstrip('\x00')
324 props
.append("ro.product.waydroid." +
325 product
+ "=" + f_value
)
327 prop_fp
= tools
.helpers
.props
.host_get(args
, "ro.vendor.build.fingerprint")
329 props
.append("ro.build.fingerprint=" + prop_fp
)
331 # now append/override with values in [properties] section of waydroid.cfg
332 cfg
= tools
.config
.load(args
)
333 for k
, v
in cfg
["properties"].items():
334 for idx
, elem
in enumerate(props
):
337 props
.append(k
+"="+v
)
339 base_props
= open(args
.work
+ "/waydroid_base.prop", "w")
341 base_props
.write(prop
+ "\n")
345 def setup_host_perms(args
):
346 if not os
.path
.exists(tools
.config
.defaults
["host_perms"]):
347 os
.mkdir(tools
.config
.defaults
["host_perms"])
349 treble
= tools
.helpers
.props
.host_get(args
, "ro.treble.enabled")
353 sku
= tools
.helpers
.props
.host_get(args
, "ro.boot.product.hardware.sku")
356 glob
.glob("/vendor/etc/permissions/android.hardware.nfc.*"))
357 if os
.path
.exists("/vendor/etc/permissions/android.hardware.consumerir.xml"):
358 copy_list
.append("/vendor/etc/permissions/android.hardware.consumerir.xml")
360 glob
.glob("/odm/etc/permissions/android.hardware.nfc.*"))
361 if os
.path
.exists("/odm/etc/permissions/android.hardware.consumerir.xml"):
362 copy_list
.append("/odm/etc/permissions/android.hardware.consumerir.xml")
365 glob
.glob("/odm/etc/permissions/sku_{}/android.hardware.nfc.*".format(sku
)))
366 if os
.path
.exists("/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
)):
368 "/odm/etc/permissions/sku_{}/android.hardware.consumerir.xml".format(sku
))
370 for filename
in copy_list
:
371 shutil
.copy(filename
, tools
.config
.defaults
["host_perms"])
374 command
= ["lxc-info", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid", "-sH"]
375 out
= subprocess
.run(command
, stdout
=subprocess
.PIPE
).stdout
.decode('utf-8').strip()
378 def wait_for_running(args
):
379 lxc_status
= status(args
)
381 while lxc_status
!= "RUNNING" and timeout
> 0:
382 lxc_status
= status(args
)
384 "waiting {} seconds for container to start...".format(timeout
))
385 timeout
= timeout
- 1
387 if lxc_status
!= "RUNNING":
388 raise OSError("container failed to start")
391 command
= ["lxc-start", "-P", tools
.config
.defaults
["lxc"],
392 "-F", "-n", "waydroid", "--", "/init"]
393 tools
.helpers
.run
.user(args
, command
, output
="background")
394 wait_for_running(args
)
395 # Workaround lxc-start changing stdout/stderr permissions to 700
396 os
.chmod(args
.log
, 0o666)
399 command
= ["lxc-stop", "-P",
400 tools
.config
.defaults
["lxc"], "-n", "waydroid", "-k"]
401 tools
.helpers
.run
.user(args
, command
)
404 command
= ["lxc-freeze", "-P", tools
.config
.defaults
["lxc"], "-n", "waydroid"]
405 tools
.helpers
.run
.user(args
, command
)
408 command
= ["lxc-unfreeze", "-P",
409 tools
.config
.defaults
["lxc"], "-n", "waydroid"]
410 tools
.helpers
.run
.user(args
, command
)
413 "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",
414 "ANDROID_ROOT": "/system",
415 "ANDROID_DATA": "/data",
416 "ANDROID_STORAGE": "/storage",
417 "ANDROID_ART_ROOT": "/apex/com.android.art",
418 "ANDROID_I18N_ROOT": "/apex/com.android.i18n",
419 "ANDROID_TZDATA_ROOT": "/apex/com.android.tzdata",
420 "ANDROID_RUNTIME_ROOT": "/apex/com.android.runtime",
423 def android_env_attach_options():
424 env
= [k
+ "=" + v
for k
, v
in ANDROID_ENV
.items()]
425 return [x
for var
in env
for x
in ("--set-var", var
)]
429 if state
== "FROZEN":
431 elif state
!= "RUNNING":
432 logging
.error("WayDroid container is {}".format(state
))
434 command
= ["lxc-attach", "-P", tools
.config
.defaults
["lxc"],
435 "-n", "waydroid", "--clear-env"]
436 command
.extend(android_env_attach_options())
439 command
.extend(args
.COMMAND
)
441 command
.append("/system/bin/sh")
442 subprocess
.run(command
)
443 if state
== "FROZEN":
447 args
.COMMAND
= ["/system/bin/logcat"]