summaryrefslogtreecommitdiffstats
path: root/meta/classes/systemd.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/systemd.bbclass')
-rw-r--r--meta/classes/systemd.bbclass198
1 files changed, 198 insertions, 0 deletions
diff --git a/meta/classes/systemd.bbclass b/meta/classes/systemd.bbclass
new file mode 100644
index 0000000..2bd63a4
--- /dev/null
+++ b/meta/classes/systemd.bbclass
@@ -0,0 +1,198 @@
1# 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 oe.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
19 d.appendVar("DEPENDS", " systemd-systemctl-native")
20 if not oe.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
21 d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
22}
23
24systemd_postinst() {
25OPTS=""
26
27if [ -n "$D" ]; then
28 OPTS="--root=$D"
29fi
30
31if type systemctl >/dev/null 2>/dev/null; then
32 systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE}
33
34 if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
35 systemctl restart ${SYSTEMD_SERVICE}
36 fi
37fi
38}
39
40systemd_prerm() {
41OPTS=""
42
43if [ -n "$D" ]; then
44 OPTS="--root=$D"
45fi
46
47if type systemctl >/dev/null 2>/dev/null; then
48 if [ -z "$D" ]; then
49 systemctl stop ${SYSTEMD_SERVICE}
50 fi
51
52 systemctl $OPTS disable ${SYSTEMD_SERVICE}
53fi
54}
55
56
57systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
58
59python systemd_populate_packages() {
60 if not oe.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
61 return
62
63 def get_package_var(d, var, pkg):
64 val = (d.getVar('%s_%s' % (var, pkg), True) or "").strip()
65 if val == "":
66 val = (d.getVar(var, True) or "").strip()
67 return val
68
69 # Check if systemd-packages already included in PACKAGES
70 def systemd_check_package(pkg_systemd):
71 packages = d.getVar('PACKAGES', True)
72 if not pkg_systemd in packages.split():
73 bb.error('%s does not appear in package list, please add it' % pkg_systemd)
74
75
76 def systemd_generate_package_scripts(pkg):
77 bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
78
79 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
80 # variable.
81 localdata = d.createCopy()
82 localdata.prependVar("OVERRIDES", pkg + ":")
83 bb.data.update_data(localdata)
84
85 postinst = d.getVar('pkg_postinst_%s' % pkg, True)
86 if not postinst:
87 postinst = '#!/bin/sh\n'
88 postinst += localdata.getVar('systemd_postinst', True)
89 d.setVar('pkg_postinst_%s' % pkg, postinst)
90
91 prerm = d.getVar('pkg_prerm_%s' % pkg, True)
92 if not prerm:
93 prerm = '#!/bin/sh\n'
94 prerm += localdata.getVar('systemd_prerm', True)
95 d.setVar('pkg_prerm_%s' % pkg, prerm)
96
97
98 # Add files to FILES_*-systemd if existent and not already done
99 def systemd_append_file(pkg_systemd, file_append):
100 appended = False
101 if os.path.exists(oe.path.join(d.getVar("D", True), file_append)):
102 var_name = "FILES_" + pkg_systemd
103 files = d.getVar(var_name, False) or ""
104 if file_append not in files.split():
105 d.appendVar(var_name, " " + file_append)
106 appended = True
107 return appended
108
109 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
110 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
111 # avoid infinite recursion
112 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
113 fullpath = oe.path.join(d.getVar("D", True), path, service)
114 if service.find('.service') != -1:
115 # for *.service add *@.service
116 service_base = service.replace('.service', '')
117 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
118 if service.find('.socket') != -1:
119 # for *.socket add *.service and *@.service
120 service_base = service.replace('.socket', '')
121 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
122 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
123 for key in keys.split():
124 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
125 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key)
126 pipe = os.popen(cmd, 'r')
127 line = pipe.readline()
128 while line:
129 line = line.replace('\n', '')
130 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
131 line = pipe.readline()
132 pipe.close()
133
134 # Check service-files and call systemd_add_files_and_parse for each entry
135 def systemd_check_services():
136 searchpaths = [oe.path.join(d.getVar("sysconfdir", True), "systemd", "system"),]
137 searchpaths.append(oe.path.join(d.getVar("nonarch_base_libdir", True), "systemd", "system"))
138 searchpaths.append(oe.path.join(d.getVar("exec_prefix", True), d.getVar("nonarch_base_libdir", True), "systemd", "system"))
139 systemd_packages = d.getVar('SYSTEMD_PACKAGES', True)
140 has_exactly_one_service = len(systemd_packages.split()) == 1
141 if has_exactly_one_service:
142 has_exactly_one_service = len(get_package_var(d, 'SYSTEMD_SERVICE', systemd_packages).split()) == 1
143
144 keys = 'Also' # Conflicts??
145 if has_exactly_one_service:
146 # single service gets also the /dev/null dummies
147 keys = 'Also Conflicts'
148 # scan for all in SYSTEMD_SERVICE[]
149 for pkg_systemd in systemd_packages.split():
150 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
151 path_found = ''
152 for path in searchpaths:
153 if os.path.exists(oe.path.join(d.getVar("D", True), path, service)):
154 path_found = path
155 break
156 if path_found != '':
157 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
158 else:
159 raise bb.build.FuncFailed("SYSTEMD_SERVICE_%s value %s does not exist" % \
160 (pkg_systemd, service))
161
162 # Run all modifications once when creating package
163 if os.path.exists(d.getVar("D", True)):
164 for pkg in d.getVar('SYSTEMD_PACKAGES', True).split():
165 systemd_check_package(pkg)
166 if d.getVar('SYSTEMD_SERVICE_' + pkg, True):
167 systemd_generate_package_scripts(pkg)
168 systemd_check_services()
169}
170
171PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
172
173python rm_systemd_unitdir (){
174 import shutil
175 if not oe.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
176 systemd_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_unitdir', True))
177 if os.path.exists(systemd_unitdir):
178 shutil.rmtree(systemd_unitdir)
179 systemd_libdir = os.path.dirname(systemd_unitdir)
180 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
181 os.rmdir(systemd_libdir)
182}
183do_install[postfuncs] += "rm_systemd_unitdir "
184
185python rm_sysvinit_initddir (){
186 import shutil
187 sysv_initddir = oe.path.join(d.getVar("D", True), (d.getVar('INIT_D_DIR', True) or "/etc/init.d"))
188
189 if oe.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
190 not oe.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
191 os.path.exists(sysv_initddir):
192 systemd_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_unitdir', True), "system")
193
194 # If systemd_unitdir contains anything, delete sysv_initddir
195 if (os.path.exists(systemd_unitdir) and os.listdir(systemd_unitdir)):
196 shutil.rmtree(sysv_initddir)
197}
198do_install[postfuncs] += "rm_sysvinit_initddir "