blob: c7b784dea8e0bf3162982dcc52372ce63e878949 [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() {
26OPTS=""
27
28if [ -n "$D" ]; then
29 OPTS="--root=$D"
30fi
31
32if type systemctl >/dev/null 2>/dev/null; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033 if [ -z "$D" ]; then
34 systemctl daemon-reload
35 fi
36
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39 if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080040 systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 fi
42fi
43}
44
45systemd_prerm() {
46OPTS=""
47
48if [ -n "$D" ]; then
49 OPTS="--root=$D"
50fi
51
52if type systemctl >/dev/null 2>/dev/null; then
53 if [ -z "$D" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080054 systemctl stop ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 fi
56
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 systemctl $OPTS disable ${SYSTEMD_SERVICE_ESCAPED}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058fi
59}
60
61
62systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
63systemd_populate_packages[vardepsexclude] += "OVERRIDES"
64
65
66python systemd_populate_packages() {
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067 import re
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080068 import shlex
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
71 return
72
73 def get_package_var(d, var, pkg):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074 val = (d.getVar('%s_%s' % (var, pkg)) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 if val == "":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 val = (d.getVar(var) or "").strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 return val
78
79 # Check if systemd-packages already included in PACKAGES
80 def systemd_check_package(pkg_systemd):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 if not pkg_systemd in packages.split():
83 bb.error('%s does not appear in package list, please add it' % pkg_systemd)
84
85
86 def systemd_generate_package_scripts(pkg):
87 bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
88
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080089 paths_escaped = ' '.join(shlex.quote(s) for s in d.getVar('SYSTEMD_SERVICE_' + pkg, True).split())
90 d.setVar('SYSTEMD_SERVICE_ESCAPED_' + pkg, paths_escaped)
91
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
93 # variable.
94 localdata = d.createCopy()
95 localdata.prependVar("OVERRIDES", pkg + ":")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 postinst = d.getVar('pkg_postinst_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 if not postinst:
99 postinst = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 postinst += localdata.getVar('systemd_postinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 d.setVar('pkg_postinst_%s' % pkg, postinst)
102
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 prerm = d.getVar('pkg_prerm_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 if not prerm:
105 prerm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 prerm += localdata.getVar('systemd_prerm')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 d.setVar('pkg_prerm_%s' % pkg, prerm)
108
109
110 # Add files to FILES_*-systemd if existent and not already done
111 def systemd_append_file(pkg_systemd, file_append):
112 appended = False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 if os.path.exists(oe.path.join(d.getVar("D"), file_append)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 var_name = "FILES_" + pkg_systemd
115 files = d.getVar(var_name, False) or ""
116 if file_append not in files.split():
117 d.appendVar(var_name, " " + file_append)
118 appended = True
119 return appended
120
121 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
122 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
123 # avoid infinite recursion
124 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500125 fullpath = oe.path.join(d.getVar("D"), path, service)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 if service.find('.service') != -1:
127 # for *.service add *@.service
128 service_base = service.replace('.service', '')
129 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
130 if service.find('.socket') != -1:
131 # for *.socket add *.service and *@.service
132 service_base = service.replace('.socket', '')
133 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
134 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
135 for key in keys.split():
136 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800137 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, shlex.quote(fullpath), key)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 pipe = os.popen(cmd, 'r')
139 line = pipe.readline()
140 while line:
141 line = line.replace('\n', '')
142 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
143 line = pipe.readline()
144 pipe.close()
145
146 # Check service-files and call systemd_add_files_and_parse for each entry
147 def systemd_check_services():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),]
149 searchpaths.append(d.getVar("systemd_system_unitdir"))
150 systemd_packages = d.getVar('SYSTEMD_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
152 keys = 'Also'
153 # scan for all in SYSTEMD_SERVICE[]
154 for pkg_systemd in systemd_packages.split():
155 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
156 path_found = ''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157
158 # Deal with adding, for example, 'ifplugd@eth0.service' from
159 # 'ifplugd@.service'
160 base = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161 at = service.find('@')
162 if at != -1:
163 ext = service.rfind('.')
164 base = service[:at] + '@' + service[ext:]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 for path in searchpaths:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 if os.path.exists(oe.path.join(d.getVar("D"), path, service)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 path_found = path
169 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500170 elif base is not None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 if os.path.exists(oe.path.join(d.getVar("D"), path, base)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172 path_found = path
173 break
174
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175 if path_found != '':
176 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
177 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178 bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179
180 # Run all modifications once when creating package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181 if os.path.exists(d.getVar("D")):
182 for pkg in d.getVar('SYSTEMD_PACKAGES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 systemd_check_package(pkg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 if d.getVar('SYSTEMD_SERVICE_' + pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 systemd_generate_package_scripts(pkg)
186 systemd_check_services()
187}
188
189PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
190
191python rm_systemd_unitdir (){
192 import shutil
193 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 if os.path.exists(systemd_unitdir):
196 shutil.rmtree(systemd_unitdir)
197 systemd_libdir = os.path.dirname(systemd_unitdir)
198 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
199 os.rmdir(systemd_libdir)
200}
201do_install[postfuncs] += "rm_systemd_unitdir "
202
203python rm_sysvinit_initddir (){
204 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
207 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
208 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
209 os.path.exists(sysv_initddir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211
212 # If systemd_system_unitdir contains anything, delete sysv_initddir
213 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
214 shutil.rmtree(sysv_initddir)
215}
216do_install[postfuncs] += "rm_sysvinit_initddir "