blob: c4b4bb9b703d6985264eb3e6fa19d7b8ab78cbd9 [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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE}
38
39 if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
Brad Bishop37a0e4d2017-12-04 01:01:44 -050040 systemctl --no-block restart ${SYSTEMD_SERVICE}
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
54 systemctl stop ${SYSTEMD_SERVICE}
55 fi
56
57 systemctl $OPTS disable ${SYSTEMD_SERVICE}
58fi
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
68
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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -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
88 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
89 # variable.
90 localdata = d.createCopy()
91 localdata.prependVar("OVERRIDES", pkg + ":")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 postinst = d.getVar('pkg_postinst_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 if not postinst:
95 postinst = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050096 postinst += localdata.getVar('systemd_postinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 d.setVar('pkg_postinst_%s' % pkg, postinst)
98
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 prerm = d.getVar('pkg_prerm_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 if not prerm:
101 prerm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 prerm += localdata.getVar('systemd_prerm')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 d.setVar('pkg_prerm_%s' % pkg, prerm)
104
105
106 # Add files to FILES_*-systemd if existent and not already done
107 def systemd_append_file(pkg_systemd, file_append):
108 appended = False
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 if os.path.exists(oe.path.join(d.getVar("D"), file_append)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 var_name = "FILES_" + pkg_systemd
111 files = d.getVar(var_name, False) or ""
112 if file_append not in files.split():
113 d.appendVar(var_name, " " + file_append)
114 appended = True
115 return appended
116
117 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
118 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
119 # avoid infinite recursion
120 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 fullpath = oe.path.join(d.getVar("D"), path, service)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 if service.find('.service') != -1:
123 # for *.service add *@.service
124 service_base = service.replace('.service', '')
125 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
126 if service.find('.socket') != -1:
127 # for *.socket add *.service and *@.service
128 service_base = service.replace('.socket', '')
129 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
130 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
131 for key in keys.split():
132 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
133 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key)
134 pipe = os.popen(cmd, 'r')
135 line = pipe.readline()
136 while line:
137 line = line.replace('\n', '')
138 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
139 line = pipe.readline()
140 pipe.close()
141
142 # Check service-files and call systemd_add_files_and_parse for each entry
143 def systemd_check_services():
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144 searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),]
145 searchpaths.append(d.getVar("systemd_system_unitdir"))
146 systemd_packages = d.getVar('SYSTEMD_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 keys = 'Also'
149 # scan for all in SYSTEMD_SERVICE[]
150 for pkg_systemd in systemd_packages.split():
151 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
152 path_found = ''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153
154 # Deal with adding, for example, 'ifplugd@eth0.service' from
155 # 'ifplugd@.service'
156 base = None
157 if service.find('@') != -1:
158 base = re.sub('@[^.]+.', '@.', service)
159
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 for path in searchpaths:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 if os.path.exists(oe.path.join(d.getVar("D"), path, service)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 path_found = path
163 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164 elif base is not None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 if os.path.exists(oe.path.join(d.getVar("D"), path, base)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166 path_found = path
167 break
168
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 if path_found != '':
170 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
171 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600172 bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
174 # Run all modifications once when creating package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 if os.path.exists(d.getVar("D")):
176 for pkg in d.getVar('SYSTEMD_PACKAGES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177 systemd_check_package(pkg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178 if d.getVar('SYSTEMD_SERVICE_' + pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 systemd_generate_package_scripts(pkg)
180 systemd_check_services()
181}
182
183PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
184
185python rm_systemd_unitdir (){
186 import shutil
187 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500188 systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 if os.path.exists(systemd_unitdir):
190 shutil.rmtree(systemd_unitdir)
191 systemd_libdir = os.path.dirname(systemd_unitdir)
192 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
193 os.rmdir(systemd_libdir)
194}
195do_install[postfuncs] += "rm_systemd_unitdir "
196
197python rm_sysvinit_initddir (){
198 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200
201 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
202 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
203 os.path.exists(sysv_initddir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205
206 # If systemd_system_unitdir contains anything, delete sysv_initddir
207 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
208 shutil.rmtree(sysv_initddir)
209}
210do_install[postfuncs] += "rm_sysvinit_initddir "