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