blob: db7873fbe2bb374870b5f82600ee0286ebf380c2 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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.
4SYSTEMD_PACKAGES ?= "${PN}"
5SYSTEMD_PACKAGES_class-native ?= ""
6SYSTEMD_PACKAGES_class-nativesdk ?= ""
7
8# Whether to enable or disable the services on installation.
9SYSTEMD_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.
14python __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 if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
21 d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
22}
23
24systemd_postinst() {
25OPTS=""
26
27if [ -n "$D" ]; then
28 OPTS="--root=$D"
29fi
30
31if type systemctl >/dev/null 2>/dev/null; then
32 systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE}
33
34 if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
35 systemctl restart ${SYSTEMD_SERVICE}
36 fi
37fi
38}
39
40systemd_prerm() {
41OPTS=""
42
43if [ -n "$D" ]; then
44 OPTS="--root=$D"
45fi
46
47if type systemctl >/dev/null 2>/dev/null; then
48 if [ -z "$D" ]; then
49 systemctl stop ${SYSTEMD_SERVICE}
50 fi
51
52 systemctl $OPTS disable ${SYSTEMD_SERVICE}
53fi
54}
55
56
57systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
58systemd_populate_packages[vardepsexclude] += "OVERRIDES"
59
60
61python systemd_populate_packages() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050062 import re
63
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
65 return
66
67 def get_package_var(d, var, pkg):
68 val = (d.getVar('%s_%s' % (var, pkg), True) or "").strip()
69 if val == "":
70 val = (d.getVar(var, True) or "").strip()
71 return val
72
73 # Check if systemd-packages already included in PACKAGES
74 def systemd_check_package(pkg_systemd):
75 packages = d.getVar('PACKAGES', True)
76 if not pkg_systemd in packages.split():
77 bb.error('%s does not appear in package list, please add it' % pkg_systemd)
78
79
80 def systemd_generate_package_scripts(pkg):
81 bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
82
83 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
84 # variable.
85 localdata = d.createCopy()
86 localdata.prependVar("OVERRIDES", pkg + ":")
87 bb.data.update_data(localdata)
88
89 postinst = d.getVar('pkg_postinst_%s' % pkg, True)
90 if not postinst:
91 postinst = '#!/bin/sh\n'
92 postinst += localdata.getVar('systemd_postinst', True)
93 d.setVar('pkg_postinst_%s' % pkg, postinst)
94
95 prerm = d.getVar('pkg_prerm_%s' % pkg, True)
96 if not prerm:
97 prerm = '#!/bin/sh\n'
98 prerm += localdata.getVar('systemd_prerm', True)
99 d.setVar('pkg_prerm_%s' % pkg, prerm)
100
101
102 # Add files to FILES_*-systemd if existent and not already done
103 def systemd_append_file(pkg_systemd, file_append):
104 appended = False
105 if os.path.exists(oe.path.join(d.getVar("D", True), file_append)):
106 var_name = "FILES_" + pkg_systemd
107 files = d.getVar(var_name, False) or ""
108 if file_append not in files.split():
109 d.appendVar(var_name, " " + file_append)
110 appended = True
111 return appended
112
113 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
114 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
115 # avoid infinite recursion
116 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
117 fullpath = oe.path.join(d.getVar("D", True), path, service)
118 if service.find('.service') != -1:
119 # for *.service add *@.service
120 service_base = service.replace('.service', '')
121 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
122 if service.find('.socket') != -1:
123 # for *.socket add *.service and *@.service
124 service_base = service.replace('.socket', '')
125 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
126 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
127 for key in keys.split():
128 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
129 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key)
130 pipe = os.popen(cmd, 'r')
131 line = pipe.readline()
132 while line:
133 line = line.replace('\n', '')
134 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
135 line = pipe.readline()
136 pipe.close()
137
138 # Check service-files and call systemd_add_files_and_parse for each entry
139 def systemd_check_services():
140 searchpaths = [oe.path.join(d.getVar("sysconfdir", True), "systemd", "system"),]
141 searchpaths.append(d.getVar("systemd_system_unitdir", True))
142 systemd_packages = d.getVar('SYSTEMD_PACKAGES', True)
143
144 keys = 'Also'
145 # scan for all in SYSTEMD_SERVICE[]
146 for pkg_systemd in systemd_packages.split():
147 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
148 path_found = ''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149
150 # Deal with adding, for example, 'ifplugd@eth0.service' from
151 # 'ifplugd@.service'
152 base = None
153 if service.find('@') != -1:
154 base = re.sub('@[^.]+.', '@.', service)
155
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 for path in searchpaths:
157 if os.path.exists(oe.path.join(d.getVar("D", True), path, service)):
158 path_found = path
159 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160 elif base is not None:
161 if os.path.exists(oe.path.join(d.getVar("D", True), path, base)):
162 path_found = path
163 break
164
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 if path_found != '':
166 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
167 else:
168 raise bb.build.FuncFailed("SYSTEMD_SERVICE_%s value %s does not exist" % \
169 (pkg_systemd, service))
170
171 # Run all modifications once when creating package
172 if os.path.exists(d.getVar("D", True)):
173 for pkg in d.getVar('SYSTEMD_PACKAGES', True).split():
174 systemd_check_package(pkg)
175 if d.getVar('SYSTEMD_SERVICE_' + pkg, True):
176 systemd_generate_package_scripts(pkg)
177 systemd_check_services()
178}
179
180PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
181
182python rm_systemd_unitdir (){
183 import shutil
184 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
185 systemd_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_unitdir', True))
186 if os.path.exists(systemd_unitdir):
187 shutil.rmtree(systemd_unitdir)
188 systemd_libdir = os.path.dirname(systemd_unitdir)
189 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
190 os.rmdir(systemd_libdir)
191}
192do_install[postfuncs] += "rm_systemd_unitdir "
193
194python rm_sysvinit_initddir (){
195 import shutil
196 sysv_initddir = oe.path.join(d.getVar("D", True), (d.getVar('INIT_D_DIR', True) or "/etc/init.d"))
197
198 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
199 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
200 os.path.exists(sysv_initddir):
201 systemd_system_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_system_unitdir', True))
202
203 # If systemd_system_unitdir contains anything, delete sysv_initddir
204 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
205 shutil.rmtree(sysv_initddir)
206}
207do_install[postfuncs] += "rm_sysvinit_initddir "