blob: 1b134322fbcb57ae223ecdde8c2787e7bb155b37 [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
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 at = service.find('@')
158 if at != -1:
159 ext = service.rfind('.')
160 base = service[:at] + '@' + service[ext:]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500161
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 for path in searchpaths:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 if os.path.exists(oe.path.join(d.getVar("D"), path, service)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 path_found = path
165 break
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166 elif base is not None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 if os.path.exists(oe.path.join(d.getVar("D"), path, base)):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 path_found = path
169 break
170
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 if path_found != '':
172 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
173 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174 bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175
176 # Run all modifications once when creating package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 if os.path.exists(d.getVar("D")):
178 for pkg in d.getVar('SYSTEMD_PACKAGES').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 systemd_check_package(pkg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180 if d.getVar('SYSTEMD_SERVICE_' + pkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 systemd_generate_package_scripts(pkg)
182 systemd_check_services()
183}
184
185PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
186
187python rm_systemd_unitdir (){
188 import shutil
189 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190 systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 if os.path.exists(systemd_unitdir):
192 shutil.rmtree(systemd_unitdir)
193 systemd_libdir = os.path.dirname(systemd_unitdir)
194 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
195 os.rmdir(systemd_libdir)
196}
197do_install[postfuncs] += "rm_systemd_unitdir "
198
199python rm_sysvinit_initddir (){
200 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500201 sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202
203 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
204 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
205 os.path.exists(sysv_initddir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500206 systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207
208 # If systemd_system_unitdir contains anything, delete sysv_initddir
209 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
210 shutil.rmtree(sysv_initddir)
211}
212do_install[postfuncs] += "rm_sysvinit_initddir "