blob: d1cb17dc8db3dd672b64eb8c363b2b6ffb0cddeb [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")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 d.appendVar("PACKAGE_WRITE_DEPS", " systemd-systemctl-native")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
22 d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
23}
24
25systemd_postinst() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026if type systemctl >/dev/null 2>/dev/null; then
Brad Bishopc342db32019-05-15 21:57:59 -040027 OPTS=""
28
29 if [ -n "$D" ]; then
30 OPTS="--root=$D"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 fi
32
Brad Bishopc342db32019-05-15 21:57:59 -040033 if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
34 for service in ${SYSTEMD_SERVICE_ESCAPED}; do
35 case "${service}" in
36 *@*)
37 systemctl ${OPTS} enable "${service}"
38 ;;
39 esac
40 done
41 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
Brad Bishopc342db32019-05-15 21:57:59 -040043 if [ -z "$D" ]; then
44 systemctl daemon-reload
45 systemctl preset ${SYSTEMD_SERVICE_ESCAPED}
46
47 if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
48 systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED}
49 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 fi
51fi
52}
53
54systemd_prerm() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055if type systemctl >/dev/null 2>/dev/null; then
56 if [ -z "$D" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058
Brad Bishopc342db32019-05-15 21:57:59 -040059 systemctl disable ${SYSTEMD_SERVICE_ESCAPED}
60 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061fi
62}
63
64
65systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
66systemd_populate_packages[vardepsexclude] += "OVERRIDES"
67
68
69python systemd_populate_packages() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 import re
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080071 import shlex
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
74 return
75
76 def get_package_var(d, var, pkg):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 val = (d.getVar('%s_%s' % (var, pkg)) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 if val == "":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 val = (d.getVar(var) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 return val
81
82 # Check if systemd-packages already included in PACKAGES
83 def systemd_check_package(pkg_systemd):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 if not pkg_systemd in packages.split():
86 bb.error('%s does not appear in package list, please add it' % pkg_systemd)
87
88
89 def systemd_generate_package_scripts(pkg):
90 bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
91
Brad Bishop977dc1a2019-02-06 16:01:43 -050092 paths_escaped = ' '.join(shlex.quote(s) for s in d.getVar('SYSTEMD_SERVICE_' + pkg).split())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080093 d.setVar('SYSTEMD_SERVICE_ESCAPED_' + pkg, paths_escaped)
94
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
96 # variable.
97 localdata = d.createCopy()
98 localdata.prependVar("OVERRIDES", pkg + ":")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 postinst = d.getVar('pkg_postinst_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 if not postinst:
102 postinst = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 postinst += localdata.getVar('systemd_postinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 d.setVar('pkg_postinst_%s' % pkg, postinst)
105
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 prerm = d.getVar('pkg_prerm_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 if not prerm:
108 prerm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 prerm += localdata.getVar('systemd_prerm')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 d.setVar('pkg_prerm_%s' % pkg, prerm)
111
112
113 # Add files to FILES_*-systemd if existent and not already done
114 def systemd_append_file(pkg_systemd, file_append):
115 appended = False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 if os.path.exists(oe.path.join(d.getVar("D"), file_append)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 var_name = "FILES_" + pkg_systemd
118 files = d.getVar(var_name, False) or ""
119 if file_append not in files.split():
120 d.appendVar(var_name, " " + file_append)
121 appended = True
122 return appended
123
124 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
125 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
126 # avoid infinite recursion
127 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500128 fullpath = oe.path.join(d.getVar("D"), path, service)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 if service.find('.service') != -1:
130 # for *.service add *@.service
131 service_base = service.replace('.service', '')
132 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
133 if service.find('.socket') != -1:
134 # for *.socket add *.service and *@.service
135 service_base = service.replace('.socket', '')
136 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
137 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
138 for key in keys.split():
139 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800140 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, shlex.quote(fullpath), key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 pipe = os.popen(cmd, 'r')
142 line = pipe.readline()
143 while line:
144 line = line.replace('\n', '')
145 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
146 line = pipe.readline()
147 pipe.close()
148
149 # Check service-files and call systemd_add_files_and_parse for each entry
150 def systemd_check_services():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),]
152 searchpaths.append(d.getVar("systemd_system_unitdir"))
153 systemd_packages = d.getVar('SYSTEMD_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155 keys = 'Also'
156 # scan for all in SYSTEMD_SERVICE[]
157 for pkg_systemd in systemd_packages.split():
158 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
159 path_found = ''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160
161 # Deal with adding, for example, 'ifplugd@eth0.service' from
162 # 'ifplugd@.service'
163 base = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500164 at = service.find('@')
165 if at != -1:
166 ext = service.rfind('.')
167 base = service[:at] + '@' + service[ext:]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 for path in searchpaths:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 if os.path.exists(oe.path.join(d.getVar("D"), path, service)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 path_found = path
172 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500173 elif base is not None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174 if os.path.exists(oe.path.join(d.getVar("D"), path, base)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500175 path_found = path
176 break
177
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178 if path_found != '':
179 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
180 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181 bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
Brad Bishopc342db32019-05-15 21:57:59 -0400183 def systemd_create_presets(pkg, action):
184 presetf = oe.path.join(d.getVar("PKGD"), d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg)
185 bb.utils.mkdirhier(os.path.dirname(presetf))
186 with open(presetf, 'a') as fd:
187 for service in d.getVar('SYSTEMD_SERVICE_%s' % pkg).split():
188 fd.write("%s %s\n" % (action,service))
189 d.appendVar("FILES_%s" % pkg, ' ' + oe.path.join(d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg))
190
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 # Run all modifications once when creating package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 if os.path.exists(d.getVar("D")):
193 for pkg in d.getVar('SYSTEMD_PACKAGES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 systemd_check_package(pkg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500195 if d.getVar('SYSTEMD_SERVICE_' + pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 systemd_generate_package_scripts(pkg)
Brad Bishopc342db32019-05-15 21:57:59 -0400197 action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg)
198 if action in ("enable", "disable"):
199 systemd_create_presets(pkg, action)
200 elif action not in ("mask", "preset"):
201 bb.fatal("SYSTEMD_AUTO_ENABLE_%s '%s' is not 'enable', 'disable', 'mask' or 'preset'" % (pkg, action))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 systemd_check_services()
203}
204
205PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
206
207python rm_systemd_unitdir (){
208 import shutil
209 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211 if os.path.exists(systemd_unitdir):
212 shutil.rmtree(systemd_unitdir)
213 systemd_libdir = os.path.dirname(systemd_unitdir)
214 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
215 os.rmdir(systemd_libdir)
216}
217do_install[postfuncs] += "rm_systemd_unitdir "
218
219python rm_sysvinit_initddir (){
220 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500221 sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222
223 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
224 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
225 os.path.exists(sysv_initddir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500226 systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227
228 # If systemd_system_unitdir contains anything, delete sysv_initddir
229 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
230 shutil.rmtree(sysv_initddir)
231}
232do_install[postfuncs] += "rm_sysvinit_initddir "