]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/actions/initializer.py
Improve robustness of the dynamic aidl version picker
[waydroid.git] / tools / actions / initializer.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 import logging
4 import os
5 from tools import helpers
6 import tools.config
7
8 def is_initialized(args):
9 return os.path.isfile(args.config) and os.path.isdir(tools.config.defaults["rootfs"])
10
11 def get_vendor_type(args):
12 vndk_str = helpers.props.host_get(args, "ro.vndk.version")
13 ret = "MAINLINE"
14 if vndk_str != "":
15 vndk = int(vndk_str)
16 if vndk > 19:
17 ret = "HALIUM_" + str(vndk - 19)
18
19 return ret
20
21 def setup_config(args):
22 cfg = tools.config.load(args)
23 args.arch = helpers.arch.host()
24 cfg["waydroid"]["arch"] = args.arch
25
26 preinstalled_images = tools.config.defaults["preinstalled_images_path"]
27 if not args.images_path:
28 if os.path.isdir(preinstalled_images):
29 if os.path.isfile(preinstalled_images + "/system.img") and os.path.isfile(preinstalled_images + "/vendor.img"):
30 args.images_path = preinstalled_images
31 else:
32 logging.error("Missing system or vendor on preinstalled images dir, fallback to default")
33 if not args.images_path:
34 args.images_path = tools.config.defaults["images_path"]
35 cfg["waydroid"]["images_path"] = args.images_path
36
37 channels_cfg = tools.config.load_channels()
38 if not args.system_channel:
39 args.system_channel = channels_cfg["channels"]["system_channel"]
40 if not args.vendor_channel:
41 args.vendor_channel = channels_cfg["channels"]["vendor_channel"]
42 if not args.rom_type:
43 args.rom_type = channels_cfg["channels"]["rom_type"]
44 if not args.system_type:
45 args.system_type = channels_cfg["channels"]["system_type"]
46
47 args.system_ota = args.system_channel + "/" + args.rom_type + \
48 "/waydroid_" + args.arch + "/" + args.system_type + ".json"
49 system_request = helpers.http.retrieve(args.system_ota)
50 if system_request[0] != 200:
51 if args.images_path != preinstalled_images:
52 raise ValueError(
53 "Failed to get system OTA channel: {}, error: {}".format(args.system_ota, system_request[0]))
54 else:
55 args.system_ota = "None"
56
57 device_codename = helpers.props.host_get(args, "ro.product.device")
58 args.vendor_type = None
59 for vendor in [device_codename, get_vendor_type(args)]:
60 vendor_ota = args.vendor_channel + "/waydroid_" + \
61 args.arch + "/" + vendor.replace(" ", "_") + ".json"
62 vendor_request = helpers.http.retrieve(vendor_ota)
63 if vendor_request[0] == 200:
64 args.vendor_type = vendor
65 args.vendor_ota = vendor_ota
66 break
67
68 if not args.vendor_type:
69 if args.images_path != preinstalled_images:
70 raise ValueError(
71 "Failed to get vendor OTA channel: {}".format(vendor_ota))
72 else:
73 args.vendor_ota = "None"
74 args.vendor_type = get_vendor_type(args)
75
76 cfg["waydroid"]["vendor_type"] = args.vendor_type
77 cfg["waydroid"]["system_ota"] = args.system_ota
78 cfg["waydroid"]["vendor_ota"] = args.vendor_ota
79 helpers.drivers.setupBinderNodes(args)
80 cfg["waydroid"]["binder"] = args.BINDER_DRIVER
81 cfg["waydroid"]["vndbinder"] = args.VNDBINDER_DRIVER
82 cfg["waydroid"]["hwbinder"] = args.HWBINDER_DRIVER
83 tools.config.save(args, cfg)
84
85 def init(args):
86 if not is_initialized(args) or args.force:
87 setup_config(args)
88 status = "STOPPED"
89 if os.path.exists(tools.config.defaults["lxc"] + "/waydroid"):
90 status = helpers.lxc.status(args)
91 if status != "STOPPED":
92 logging.info("Stopping container")
93 helpers.lxc.stop(args)
94 helpers.images.umount_rootfs(args)
95 if args.images_path != tools.config.defaults["preinstalled_images_path"]:
96 helpers.images.get(args)
97 if not os.path.isdir(tools.config.defaults["rootfs"]):
98 os.mkdir(tools.config.defaults["rootfs"])
99 helpers.lxc.setup_host_perms(args)
100 helpers.lxc.set_lxc_config(args)
101 helpers.lxc.make_base_props(args)
102 if status != "STOPPED":
103 logging.info("Starting container")
104 helpers.images.mount_rootfs(args, args.images_path)
105 helpers.lxc.start(args)
106
107 helpers.ipc.notify(channel="init", msg="done")
108 else:
109 logging.info("Already initialized")