diff options
Diffstat (limited to 'meta')
-rw-r--r-- | meta/classes/systemd.bbclass | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/meta/classes/systemd.bbclass b/meta/classes/systemd.bbclass new file mode 100644 index 0000000000..e0ea65c87e --- /dev/null +++ b/meta/classes/systemd.bbclass | |||
@@ -0,0 +1,158 @@ | |||
1 | # The list of packages that should have systemd packaging scripts added. For | ||
2 | # each entry, optionally have a SYSTEMD_SERVICE_[package] that lists the service | ||
3 | # files in this package. If this variable isn't set, [package].service is used. | ||
4 | SYSTEMD_PACKAGES ?= "${PN}" | ||
5 | |||
6 | # Whether to enable or disable the services on installation. | ||
7 | SYSTEMD_AUTO_ENABLE ??= "enable" | ||
8 | |||
9 | # This class will be included in any recipe that supports systemd init scripts, | ||
10 | # even if the systemd DISTRO_FEATURE isn't enabled. As such don't make any | ||
11 | # changes directly but check the DISTRO_FEATURES first. | ||
12 | python __anonymous() { | ||
13 | if oe.utils.contains ('DISTRO_FEATURES', 'systemd', True, False, d): | ||
14 | d.appendVar("DEPENDS", " systemd-systemctl-native") | ||
15 | # Set a variable so that update-rcd.bbclass knows we're active and can | ||
16 | # disable itself. | ||
17 | d.setVar("SYSTEMD_BBCLASS_ENABLED", "1") | ||
18 | } | ||
19 | |||
20 | systemd_postinst() { | ||
21 | OPTS="" | ||
22 | |||
23 | if [ -n "$D" ]; then | ||
24 | OPTS="--root=$D" | ||
25 | fi | ||
26 | |||
27 | systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE} | ||
28 | |||
29 | if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then | ||
30 | systemctl start ${SYSTEMD_SERVICE} | ||
31 | fi | ||
32 | } | ||
33 | |||
34 | systemd_prerm() { | ||
35 | if [ -z "$D" ]; then | ||
36 | systemctl stop ${SYSTEMD_SERVICE} | ||
37 | fi | ||
38 | |||
39 | systemctl disable ${SYSTEMD_SERVICE} | ||
40 | } | ||
41 | |||
42 | def systemd_populate_packages(d): | ||
43 | def get_package_var(d, var, pkg): | ||
44 | val = (d.getVar('%s_%s' % (var, pkg), True) or "").strip() | ||
45 | if val == "": | ||
46 | val = (d.getVar(var, True) or "").strip() | ||
47 | return val | ||
48 | |||
49 | |||
50 | # Add a runtime dependency on systemd to pkg | ||
51 | def systemd_add_rdepends(pkg): | ||
52 | rdepends = d.getVar('RDEPENDS_' + pkg, True) or "" | ||
53 | if not 'systemd' in rdepends.split(): | ||
54 | rdepends = '%s %s' % (rdepends, 'systemd') | ||
55 | d.setVar('RDEPENDS_' + pkg, rdepends) | ||
56 | |||
57 | |||
58 | def systemd_generate_package_scripts(pkg): | ||
59 | bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg) | ||
60 | |||
61 | # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg | ||
62 | # variable. | ||
63 | localdata = d.createCopy() | ||
64 | localdata.prependVar("OVERRIDES", pkg + ":") | ||
65 | bb.data.update_data(localdata) | ||
66 | |||
67 | postinst = d.getVar('pkg_postinst_%s' % pkg, True) | ||
68 | if not postinst: | ||
69 | postinst = '#!/bin/sh\n' | ||
70 | postinst += localdata.getVar('systemd_postinst', True) | ||
71 | d.setVar('pkg_postinst_%s' % pkg, postinst) | ||
72 | |||
73 | prerm = d.getVar('pkg_prerm_%s' % pkg, True) | ||
74 | if not prerm: | ||
75 | prerm = '#!/bin/sh\n' | ||
76 | prerm += localdata.getVar('systemd_prerm', True) | ||
77 | d.setVar('pkg_prerm_%s' % pkg, prerm) | ||
78 | |||
79 | |||
80 | # Add files to FILES_*-systemd if existent and not already done | ||
81 | def systemd_append_file(pkg_systemd, file_append): | ||
82 | appended = False | ||
83 | if os.path.exists(oe.path.join(d.getVar("D", True), file_append)): | ||
84 | var_name = "FILES_" + pkg_systemd | ||
85 | files = d.getVar(var_name, False) or "" | ||
86 | if file_append not in files.split(): | ||
87 | d.appendVar(var_name, " " + file_append) | ||
88 | appended = True | ||
89 | return appended | ||
90 | |||
91 | # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive | ||
92 | def systemd_add_files_and_parse(pkg_systemd, path, service, keys): | ||
93 | # avoid infinite recursion | ||
94 | if systemd_append_file(pkg_systemd, oe.path.join(path, service)): | ||
95 | fullpath = oe.path.join(d.getVar("D", True), path, service) | ||
96 | if service.find('.service') != -1: | ||
97 | # for *.service add *@.service | ||
98 | service_base = service.replace('.service', '') | ||
99 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | ||
100 | if service.find('.socket') != -1: | ||
101 | # for *.socket add *.service and *@.service | ||
102 | service_base = service.replace('.socket', '') | ||
103 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys) | ||
104 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | ||
105 | for key in keys.split(): | ||
106 | # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files | ||
107 | cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key) | ||
108 | pipe = os.popen(cmd, 'r') | ||
109 | line = pipe.readline() | ||
110 | while line: | ||
111 | line = line.replace('\n', '') | ||
112 | systemd_add_files_and_parse(pkg_systemd, path, line, keys) | ||
113 | line = pipe.readline() | ||
114 | pipe.close() | ||
115 | |||
116 | # Check service-files and call systemd_add_files_and_parse for each entry | ||
117 | def systemd_check_services(): | ||
118 | base_libdir = d.getVar('base_libdir', True) | ||
119 | searchpaths = [oe.path.join(d.getVar("sysconfdir", True), "systemd", "system"),] | ||
120 | searchpaths.append(oe.path.join(d.getVar("base_libdir", True), "systemd", "system")) | ||
121 | searchpaths.append(oe.path.join(d.getVar("libdir", True), "systemd", "system")) | ||
122 | searchpaths.append(oe.path.join(d.getVar("libdir", True), "systemd", "user")) | ||
123 | systemd_packages = d.getVar('SYSTEMD_PACKAGES', True) | ||
124 | has_exactly_one_service = len(systemd_packages.split()) == 1 | ||
125 | if has_exactly_one_service: | ||
126 | has_exactly_one_service = len(get_package_var(d, 'SYSTEMD_SERVICE', systemd_packages).split()) == 1 | ||
127 | |||
128 | keys = 'Also' # Conflicts?? | ||
129 | if has_exactly_one_service: | ||
130 | # single service gets also the /dev/null dummies | ||
131 | keys = 'Also Conflicts' | ||
132 | # scan for all in SYSTEMD_SERVICE[] | ||
133 | for pkg_systemd in systemd_packages.split(): | ||
134 | for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split(): | ||
135 | path_found = '' | ||
136 | for path in searchpaths: | ||
137 | if os.path.exists(oe.path.join(d.getVar("D", True), path, service)): | ||
138 | path_found = path | ||
139 | break | ||
140 | if path_found != '': | ||
141 | systemd_add_files_and_parse(pkg_systemd, path_found, service, keys) | ||
142 | else: | ||
143 | raise bb.build.FuncFailed, "\n\nSYSTEMD_SERVICE_%s value %s does not exist" % \ | ||
144 | (pkg_systemd, service) | ||
145 | |||
146 | # Run all modifications once when creating package | ||
147 | if os.path.exists(d.getVar("D", True)): | ||
148 | for pkg in d.getVar('SYSTEMD_PACKAGES', True).split(): | ||
149 | if d.getVar('SYSTEMD_SERVICE_' + pkg, True): | ||
150 | systemd_generate_package_scripts(pkg) | ||
151 | systemd_add_rdepends(pkg) | ||
152 | systemd_check_services() | ||
153 | |||
154 | |||
155 | python populate_packages_prepend () { | ||
156 | if oe.utils.contains ('DISTRO_FEATURES', 'systemd', True, False, d): | ||
157 | systemd_populate_packages (d) | ||
158 | } | ||