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