diff options
Diffstat (limited to 'meta/classes/systemd.bbclass')
-rw-r--r-- | meta/classes/systemd.bbclass | 233 |
1 files changed, 0 insertions, 233 deletions
diff --git a/meta/classes/systemd.bbclass b/meta/classes/systemd.bbclass deleted file mode 100644 index db5d109545..0000000000 --- a/meta/classes/systemd.bbclass +++ /dev/null | |||
@@ -1,233 +0,0 @@ | |||
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 | SYSTEMD_PACKAGES_class-native ?= "" | ||
6 | SYSTEMD_PACKAGES_class-nativesdk ?= "" | ||
7 | |||
8 | # Whether to enable or disable the services on installation. | ||
9 | SYSTEMD_AUTO_ENABLE ??= "enable" | ||
10 | |||
11 | # This class will be included in any recipe that supports systemd init scripts, | ||
12 | # even if systemd is not in DISTRO_FEATURES. As such don't make any changes | ||
13 | # directly but check the DISTRO_FEATURES first. | ||
14 | python __anonymous() { | ||
15 | # If the distro features have systemd but not sysvinit, inhibit update-rcd | ||
16 | # from doing any work so that pure-systemd images don't have redundant init | ||
17 | # files. | ||
18 | if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): | ||
19 | d.appendVar("DEPENDS", " systemd-systemctl-native") | ||
20 | d.appendVar("PACKAGE_WRITE_DEPS", " systemd-systemctl-native") | ||
21 | if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d): | ||
22 | d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1") | ||
23 | } | ||
24 | |||
25 | systemd_postinst() { | ||
26 | if systemctl >/dev/null 2>/dev/null; then | ||
27 | OPTS="" | ||
28 | |||
29 | if [ -n "$D" ]; then | ||
30 | OPTS="--root=$D" | ||
31 | fi | ||
32 | |||
33 | if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then | ||
34 | for service in ${SYSTEMD_SERVICE_ESCAPED}; do | ||
35 | systemctl ${OPTS} enable "$service" | ||
36 | done | ||
37 | fi | ||
38 | |||
39 | if [ -z "$D" ]; then | ||
40 | systemctl daemon-reload | ||
41 | systemctl preset ${SYSTEMD_SERVICE_ESCAPED} | ||
42 | |||
43 | if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then | ||
44 | systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED} | ||
45 | fi | ||
46 | fi | ||
47 | fi | ||
48 | } | ||
49 | |||
50 | systemd_prerm() { | ||
51 | if systemctl >/dev/null 2>/dev/null; then | ||
52 | if [ -z "$D" ]; then | ||
53 | systemctl stop ${SYSTEMD_SERVICE_ESCAPED} | ||
54 | |||
55 | systemctl disable ${SYSTEMD_SERVICE_ESCAPED} | ||
56 | fi | ||
57 | fi | ||
58 | } | ||
59 | |||
60 | |||
61 | systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst" | ||
62 | systemd_populate_packages[vardepsexclude] += "OVERRIDES" | ||
63 | |||
64 | |||
65 | python systemd_populate_packages() { | ||
66 | import re | ||
67 | import shlex | ||
68 | |||
69 | if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): | ||
70 | return | ||
71 | |||
72 | def get_package_var(d, var, pkg): | ||
73 | val = (d.getVar('%s_%s' % (var, pkg)) or "").strip() | ||
74 | if val == "": | ||
75 | val = (d.getVar(var) or "").strip() | ||
76 | return val | ||
77 | |||
78 | # Check if systemd-packages already included in PACKAGES | ||
79 | def systemd_check_package(pkg_systemd): | ||
80 | packages = d.getVar('PACKAGES') | ||
81 | if not pkg_systemd in packages.split(): | ||
82 | bb.error('%s does not appear in package list, please add it' % pkg_systemd) | ||
83 | |||
84 | |||
85 | def systemd_generate_package_scripts(pkg): | ||
86 | bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg) | ||
87 | |||
88 | paths_escaped = ' '.join(shlex.quote(s) for s in d.getVar('SYSTEMD_SERVICE_' + pkg).split()) | ||
89 | d.setVar('SYSTEMD_SERVICE_ESCAPED_' + pkg, paths_escaped) | ||
90 | |||
91 | # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg | ||
92 | # variable. | ||
93 | localdata = d.createCopy() | ||
94 | localdata.prependVar("OVERRIDES", pkg + ":") | ||
95 | |||
96 | postinst = d.getVar('pkg_postinst_%s' % pkg) | ||
97 | if not postinst: | ||
98 | postinst = '#!/bin/sh\n' | ||
99 | postinst += localdata.getVar('systemd_postinst') | ||
100 | d.setVar('pkg_postinst_%s' % pkg, postinst) | ||
101 | |||
102 | prerm = d.getVar('pkg_prerm_%s' % pkg) | ||
103 | if not prerm: | ||
104 | prerm = '#!/bin/sh\n' | ||
105 | prerm += localdata.getVar('systemd_prerm') | ||
106 | d.setVar('pkg_prerm_%s' % pkg, prerm) | ||
107 | |||
108 | |||
109 | # Add files to FILES_*-systemd if existent and not already done | ||
110 | def systemd_append_file(pkg_systemd, file_append): | ||
111 | appended = False | ||
112 | if os.path.exists(oe.path.join(d.getVar("D"), file_append)): | ||
113 | var_name = "FILES_" + pkg_systemd | ||
114 | files = d.getVar(var_name, False) or "" | ||
115 | if file_append not in files.split(): | ||
116 | d.appendVar(var_name, " " + file_append) | ||
117 | appended = True | ||
118 | return appended | ||
119 | |||
120 | # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive | ||
121 | def systemd_add_files_and_parse(pkg_systemd, path, service, keys): | ||
122 | # avoid infinite recursion | ||
123 | if systemd_append_file(pkg_systemd, oe.path.join(path, service)): | ||
124 | fullpath = oe.path.join(d.getVar("D"), path, service) | ||
125 | if service.find('.service') != -1: | ||
126 | # for *.service add *@.service | ||
127 | service_base = service.replace('.service', '') | ||
128 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | ||
129 | if service.find('.socket') != -1: | ||
130 | # for *.socket add *.service and *@.service | ||
131 | service_base = service.replace('.socket', '') | ||
132 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys) | ||
133 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | ||
134 | for key in keys.split(): | ||
135 | # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files | ||
136 | cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, shlex.quote(fullpath), key) | ||
137 | pipe = os.popen(cmd, 'r') | ||
138 | line = pipe.readline() | ||
139 | while line: | ||
140 | line = line.replace('\n', '') | ||
141 | systemd_add_files_and_parse(pkg_systemd, path, line, keys) | ||
142 | line = pipe.readline() | ||
143 | pipe.close() | ||
144 | |||
145 | # Check service-files and call systemd_add_files_and_parse for each entry | ||
146 | def systemd_check_services(): | ||
147 | searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),] | ||
148 | searchpaths.append(d.getVar("systemd_system_unitdir")) | ||
149 | systemd_packages = d.getVar('SYSTEMD_PACKAGES') | ||
150 | |||
151 | keys = 'Also' | ||
152 | # scan for all in SYSTEMD_SERVICE[] | ||
153 | for pkg_systemd in systemd_packages.split(): | ||
154 | for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split(): | ||
155 | path_found = '' | ||
156 | |||
157 | # Deal with adding, for example, 'ifplugd@eth0.service' from | ||
158 | # 'ifplugd@.service' | ||
159 | base = None | ||
160 | at = service.find('@') | ||
161 | if at != -1: | ||
162 | ext = service.rfind('.') | ||
163 | base = service[:at] + '@' + service[ext:] | ||
164 | |||
165 | for path in searchpaths: | ||
166 | if os.path.exists(oe.path.join(d.getVar("D"), path, service)): | ||
167 | path_found = path | ||
168 | break | ||
169 | elif base is not None: | ||
170 | if os.path.exists(oe.path.join(d.getVar("D"), path, base)): | ||
171 | path_found = path | ||
172 | break | ||
173 | |||
174 | if path_found != '': | ||
175 | systemd_add_files_and_parse(pkg_systemd, path_found, service, keys) | ||
176 | else: | ||
177 | bb.fatal("Didn't find service unit '{0}', specified in SYSTEMD_SERVICE_{1}. {2}".format( | ||
178 | service, pkg_systemd, "Also looked for service unit '{0}'.".format(base) if base is not None else "")) | ||
179 | |||
180 | def systemd_create_presets(pkg, action): | ||
181 | presetf = oe.path.join(d.getVar("PKGD"), d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg) | ||
182 | bb.utils.mkdirhier(os.path.dirname(presetf)) | ||
183 | with open(presetf, 'a') as fd: | ||
184 | for service in d.getVar('SYSTEMD_SERVICE_%s' % pkg).split(): | ||
185 | fd.write("%s %s\n" % (action,service)) | ||
186 | d.appendVar("FILES_%s" % pkg, ' ' + oe.path.join(d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg)) | ||
187 | |||
188 | # Run all modifications once when creating package | ||
189 | if os.path.exists(d.getVar("D")): | ||
190 | for pkg in d.getVar('SYSTEMD_PACKAGES').split(): | ||
191 | systemd_check_package(pkg) | ||
192 | if d.getVar('SYSTEMD_SERVICE_' + pkg): | ||
193 | systemd_generate_package_scripts(pkg) | ||
194 | action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg) | ||
195 | if action in ("enable", "disable"): | ||
196 | systemd_create_presets(pkg, action) | ||
197 | elif action not in ("mask", "preset"): | ||
198 | bb.fatal("SYSTEMD_AUTO_ENABLE_%s '%s' is not 'enable', 'disable', 'mask' or 'preset'" % (pkg, action)) | ||
199 | systemd_check_services() | ||
200 | } | ||
201 | |||
202 | PACKAGESPLITFUNCS_prepend = "systemd_populate_packages " | ||
203 | |||
204 | python rm_systemd_unitdir (){ | ||
205 | import shutil | ||
206 | if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): | ||
207 | systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir')) | ||
208 | if os.path.exists(systemd_unitdir): | ||
209 | shutil.rmtree(systemd_unitdir) | ||
210 | systemd_libdir = os.path.dirname(systemd_unitdir) | ||
211 | if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)): | ||
212 | os.rmdir(systemd_libdir) | ||
213 | } | ||
214 | |||
215 | python rm_sysvinit_initddir (){ | ||
216 | import shutil | ||
217 | sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d")) | ||
218 | |||
219 | if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \ | ||
220 | not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \ | ||
221 | os.path.exists(sysv_initddir): | ||
222 | systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir')) | ||
223 | |||
224 | # If systemd_system_unitdir contains anything, delete sysv_initddir | ||
225 | if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)): | ||
226 | shutil.rmtree(sysv_initddir) | ||
227 | } | ||
228 | |||
229 | do_install[postfuncs] += "${RMINITDIR} " | ||
230 | RMINITDIR_class-target = " rm_sysvinit_initddir rm_systemd_unitdir " | ||
231 | RMINITDIR_class-nativesdk = " rm_sysvinit_initddir rm_systemd_unitdir " | ||
232 | RMINITDIR = "" | ||
233 | |||