blob: 09ec52792d5874bc8d0591e035937e4e1a269786 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# The list of packages that should have systemd packaging scripts added. For
Patrick Williams213cb262021-08-07 19:21:33 -05002# each entry, optionally have a SYSTEMD_SERVICE:[package] that lists the service
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003# files in this package. If this variable isn't set, [package].service is used.
4SYSTEMD_PACKAGES ?= "${PN}"
Patrick Williams213cb262021-08-07 19:21:33 -05005SYSTEMD_PACKAGES:class-native ?= ""
6SYSTEMD_PACKAGES:class-nativesdk ?= ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
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() {
Andrew Geissler09209ee2020-12-13 08:44:15 -060026if 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
Brad Bishop64c979e2019-11-04 13:55:29 -050035 systemctl ${OPTS} enable "$service"
Brad Bishopc342db32019-05-15 21:57:59 -040036 done
37 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
Brad Bishopc342db32019-05-15 21:57:59 -040039 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 fi
47fi
48}
49
50systemd_prerm() {
Andrew Geissler09209ee2020-12-13 08:44:15 -060051if systemctl >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if [ -z "$D" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080053 systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
Brad Bishopc342db32019-05-15 21:57:59 -040055 systemctl disable ${SYSTEMD_SERVICE_ESCAPED}
56 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057fi
58}
59
60
61systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
62systemd_populate_packages[vardepsexclude] += "OVERRIDES"
63
64
65python systemd_populate_packages() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066 import re
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080067 import shlex
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050068
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
70 return
71
72 def get_package_var(d, var, pkg):
Patrick Williams213cb262021-08-07 19:21:33 -050073 val = (d.getVar('%s:%s' % (var, pkg)) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 if val == "":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 val = (d.getVar(var) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 return val
77
78 # Check if systemd-packages already included in PACKAGES
79 def systemd_check_package(pkg_systemd):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 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
Patrick Williams213cb262021-08-07 19:21:33 -050088 paths_escaped = ' '.join(shlex.quote(s) for s in d.getVar('SYSTEMD_SERVICE:' + pkg).split())
89 d.setVar('SYSTEMD_SERVICE_ESCAPED:' + pkg, paths_escaped)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080090
Patrick Williams213cb262021-08-07 19:21:33 -050091 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE:pkg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 # variable.
93 localdata = d.createCopy()
94 localdata.prependVar("OVERRIDES", pkg + ":")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
Patrick Williams213cb262021-08-07 19:21:33 -050096 postinst = d.getVar('pkg_postinst:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 if not postinst:
98 postinst = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 postinst += localdata.getVar('systemd_postinst')
Patrick Williams213cb262021-08-07 19:21:33 -0500100 d.setVar('pkg_postinst:%s' % pkg, postinst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
Patrick Williams213cb262021-08-07 19:21:33 -0500102 prerm = d.getVar('pkg_prerm:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 if not prerm:
104 prerm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 prerm += localdata.getVar('systemd_prerm')
Patrick Williams213cb262021-08-07 19:21:33 -0500106 d.setVar('pkg_prerm:%s' % pkg, prerm)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
108
Patrick Williams213cb262021-08-07 19:21:33 -0500109 # Add files to FILES:*-systemd if existent and not already done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 def systemd_append_file(pkg_systemd, file_append):
111 appended = False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 if os.path.exists(oe.path.join(d.getVar("D"), file_append)):
Patrick Williams213cb262021-08-07 19:21:33 -0500113 var_name = "FILES:" + pkg_systemd
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 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
Patrick Williams213cb262021-08-07 19:21:33 -0500120 # Add systemd files to FILES:*-systemd, parse for Also= and follow recursive
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 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)):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 fullpath = oe.path.join(d.getVar("D"), path, service)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800136 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, shlex.quote(fullpath), key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 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():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),]
148 searchpaths.append(d.getVar("systemd_system_unitdir"))
149 systemd_packages = d.getVar('SYSTEMD_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150
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 = ''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156
157 # Deal with adding, for example, 'ifplugd@eth0.service' from
158 # 'ifplugd@.service'
159 base = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160 at = service.find('@')
161 if at != -1:
162 ext = service.rfind('.')
163 base = service[:at] + '@' + service[ext:]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 for path in searchpaths:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 if os.path.exists(oe.path.join(d.getVar("D"), path, service)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 path_found = path
168 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169 elif base is not None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 if os.path.exists(oe.path.join(d.getVar("D"), path, base)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 path_found = path
172 break
173
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 if path_found != '':
175 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
176 else:
Patrick Williams213cb262021-08-07 19:21:33 -0500177 bb.fatal("Didn't find service unit '{0}', specified in SYSTEMD_SERVICE:{1}. {2}".format(
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600178 service, pkg_systemd, "Also looked for service unit '{0}'.".format(base) if base is not None else ""))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179
Brad Bishopc342db32019-05-15 21:57:59 -0400180 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:
Patrick Williams213cb262021-08-07 19:21:33 -0500184 for service in d.getVar('SYSTEMD_SERVICE:%s' % pkg).split():
Brad Bishopc342db32019-05-15 21:57:59 -0400185 fd.write("%s %s\n" % (action,service))
Patrick Williams213cb262021-08-07 19:21:33 -0500186 d.appendVar("FILES:%s" % pkg, ' ' + oe.path.join(d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg))
Brad Bishopc342db32019-05-15 21:57:59 -0400187
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 # Run all modifications once when creating package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500189 if os.path.exists(d.getVar("D")):
190 for pkg in d.getVar('SYSTEMD_PACKAGES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 systemd_check_package(pkg)
Patrick Williams213cb262021-08-07 19:21:33 -0500192 if d.getVar('SYSTEMD_SERVICE:' + pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 systemd_generate_package_scripts(pkg)
Brad Bishopc342db32019-05-15 21:57:59 -0400194 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"):
Patrick Williams213cb262021-08-07 19:21:33 -0500198 bb.fatal("SYSTEMD_AUTO_ENABLE:%s '%s' is not 'enable', 'disable', 'mask' or 'preset'" % (pkg, action))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 systemd_check_services()
200}
201
Patrick Williams213cb262021-08-07 19:21:33 -0500202PACKAGESPLITFUNCS:prepend = "systemd_populate_packages "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203
204python rm_systemd_unitdir (){
205 import shutil
206 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500207 systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 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}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214
215python rm_sysvinit_initddir (){
216 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500217 sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218
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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222 systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500223
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}
Brad Bishopc68388fc2019-08-26 01:33:31 -0400228
229do_install[postfuncs] += "${RMINITDIR} "
Patrick Williams213cb262021-08-07 19:21:33 -0500230RMINITDIR:class-target = " rm_sysvinit_initddir rm_systemd_unitdir "
231RMINITDIR:class-nativesdk = " rm_sysvinit_initddir rm_systemd_unitdir "
Brad Bishopc68388fc2019-08-26 01:33:31 -0400232RMINITDIR = ""
233