| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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. | 
|  | 4 | SYSTEMD_PACKAGES ?= "${PN}" | 
|  | 5 | SYSTEMD_PACKAGES_class-native ?= "" | 
|  | 6 | SYSTEMD_PACKAGES_class-nativesdk ?= "" | 
|  | 7 |  | 
|  | 8 | # Whether to enable or disable the services on installation. | 
|  | 9 | SYSTEMD_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. | 
|  | 14 | python __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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | d.appendVar("PACKAGE_WRITE_DEPS", " systemd-systemctl-native") | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d): | 
|  | 22 | d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1") | 
|  | 23 | } | 
|  | 24 |  | 
|  | 25 | systemd_postinst() { | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | if type systemctl >/dev/null 2>/dev/null; then | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 27 | OPTS="" | 
|  | 28 |  | 
|  | 29 | if [ -n "$D" ]; then | 
|  | 30 | OPTS="--root=$D" | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | fi | 
|  | 32 |  | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 33 | if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then | 
|  | 34 | for service in ${SYSTEMD_SERVICE_ESCAPED}; do | 
|  | 35 | case "${service}" in | 
|  | 36 | *@*) | 
|  | 37 | systemctl ${OPTS} enable "${service}" | 
|  | 38 | ;; | 
|  | 39 | esac | 
|  | 40 | done | 
|  | 41 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 |  | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 43 | if [ -z "$D" ]; then | 
|  | 44 | systemctl daemon-reload | 
|  | 45 | systemctl preset ${SYSTEMD_SERVICE_ESCAPED} | 
|  | 46 |  | 
|  | 47 | if [ "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then | 
|  | 48 | systemctl --no-block restart ${SYSTEMD_SERVICE_ESCAPED} | 
|  | 49 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | fi | 
|  | 51 | fi | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | systemd_prerm() { | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | if type systemctl >/dev/null 2>/dev/null; then | 
|  | 56 | if [ -z "$D" ]; then | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 57 | systemctl stop ${SYSTEMD_SERVICE_ESCAPED} | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 |  | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 59 | systemctl disable ${SYSTEMD_SERVICE_ESCAPED} | 
|  | 60 | fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | fi | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 |  | 
|  | 65 | systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst" | 
|  | 66 | systemd_populate_packages[vardepsexclude] += "OVERRIDES" | 
|  | 67 |  | 
|  | 68 |  | 
|  | 69 | python systemd_populate_packages() { | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | import re | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 71 | import shlex | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 72 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): | 
|  | 74 | return | 
|  | 75 |  | 
|  | 76 | def get_package_var(d, var, pkg): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | val = (d.getVar('%s_%s' % (var, pkg)) or "").strip() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | if val == "": | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | val = (d.getVar(var) or "").strip() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | return val | 
|  | 81 |  | 
|  | 82 | # Check if systemd-packages already included in PACKAGES | 
|  | 83 | def systemd_check_package(pkg_systemd): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 84 | packages = d.getVar('PACKAGES') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | if not pkg_systemd in packages.split(): | 
|  | 86 | bb.error('%s does not appear in package list, please add it' % pkg_systemd) | 
|  | 87 |  | 
|  | 88 |  | 
|  | 89 | def systemd_generate_package_scripts(pkg): | 
|  | 90 | bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg) | 
|  | 91 |  | 
| Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 92 | paths_escaped = ' '.join(shlex.quote(s) for s in d.getVar('SYSTEMD_SERVICE_' + pkg).split()) | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 93 | d.setVar('SYSTEMD_SERVICE_ESCAPED_' + pkg, paths_escaped) | 
|  | 94 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg | 
|  | 96 | # variable. | 
|  | 97 | localdata = d.createCopy() | 
|  | 98 | localdata.prependVar("OVERRIDES", pkg + ":") | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 100 | postinst = d.getVar('pkg_postinst_%s' % pkg) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | if not postinst: | 
|  | 102 | postinst = '#!/bin/sh\n' | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 103 | postinst += localdata.getVar('systemd_postinst') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | d.setVar('pkg_postinst_%s' % pkg, postinst) | 
|  | 105 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 106 | prerm = d.getVar('pkg_prerm_%s' % pkg) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | if not prerm: | 
|  | 108 | prerm = '#!/bin/sh\n' | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | prerm += localdata.getVar('systemd_prerm') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | d.setVar('pkg_prerm_%s' % pkg, prerm) | 
|  | 111 |  | 
|  | 112 |  | 
|  | 113 | # Add files to FILES_*-systemd if existent and not already done | 
|  | 114 | def systemd_append_file(pkg_systemd, file_append): | 
|  | 115 | appended = False | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 116 | if os.path.exists(oe.path.join(d.getVar("D"), file_append)): | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | var_name = "FILES_" + pkg_systemd | 
|  | 118 | files = d.getVar(var_name, False) or "" | 
|  | 119 | if file_append not in files.split(): | 
|  | 120 | d.appendVar(var_name, " " + file_append) | 
|  | 121 | appended = True | 
|  | 122 | return appended | 
|  | 123 |  | 
|  | 124 | # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive | 
|  | 125 | def systemd_add_files_and_parse(pkg_systemd, path, service, keys): | 
|  | 126 | # avoid infinite recursion | 
|  | 127 | if systemd_append_file(pkg_systemd, oe.path.join(path, service)): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | fullpath = oe.path.join(d.getVar("D"), path, service) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | if service.find('.service') != -1: | 
|  | 130 | # for *.service add *@.service | 
|  | 131 | service_base = service.replace('.service', '') | 
|  | 132 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | 
|  | 133 | if service.find('.socket') != -1: | 
|  | 134 | # for *.socket add *.service and *@.service | 
|  | 135 | service_base = service.replace('.socket', '') | 
|  | 136 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys) | 
|  | 137 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) | 
|  | 138 | for key in keys.split(): | 
|  | 139 | # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 140 | cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, shlex.quote(fullpath), key) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | pipe = os.popen(cmd, 'r') | 
|  | 142 | line = pipe.readline() | 
|  | 143 | while line: | 
|  | 144 | line = line.replace('\n', '') | 
|  | 145 | systemd_add_files_and_parse(pkg_systemd, path, line, keys) | 
|  | 146 | line = pipe.readline() | 
|  | 147 | pipe.close() | 
|  | 148 |  | 
|  | 149 | # Check service-files and call systemd_add_files_and_parse for each entry | 
|  | 150 | def systemd_check_services(): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),] | 
|  | 152 | searchpaths.append(d.getVar("systemd_system_unitdir")) | 
|  | 153 | systemd_packages = d.getVar('SYSTEMD_PACKAGES') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 |  | 
|  | 155 | keys = 'Also' | 
|  | 156 | # scan for all in SYSTEMD_SERVICE[] | 
|  | 157 | for pkg_systemd in systemd_packages.split(): | 
|  | 158 | for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split(): | 
|  | 159 | path_found = '' | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 160 |  | 
|  | 161 | # Deal with adding, for example, 'ifplugd@eth0.service' from | 
|  | 162 | # 'ifplugd@.service' | 
|  | 163 | base = None | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 164 | at = service.find('@') | 
|  | 165 | if at != -1: | 
|  | 166 | ext = service.rfind('.') | 
|  | 167 | base = service[:at] + '@' + service[ext:] | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 168 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | for path in searchpaths: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | if os.path.exists(oe.path.join(d.getVar("D"), path, service)): | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | path_found = path | 
|  | 172 | break | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 173 | elif base is not None: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | if os.path.exists(oe.path.join(d.getVar("D"), path, base)): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 175 | path_found = path | 
|  | 176 | break | 
|  | 177 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 178 | if path_found != '': | 
|  | 179 | systemd_add_files_and_parse(pkg_systemd, path_found, service, keys) | 
|  | 180 | else: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 181 | bb.fatal("SYSTEMD_SERVICE_%s value %s does not exist" % (pkg_systemd, service)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 |  | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 183 | def systemd_create_presets(pkg, action): | 
|  | 184 | presetf = oe.path.join(d.getVar("PKGD"), d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg) | 
|  | 185 | bb.utils.mkdirhier(os.path.dirname(presetf)) | 
|  | 186 | with open(presetf, 'a') as fd: | 
|  | 187 | for service in d.getVar('SYSTEMD_SERVICE_%s' % pkg).split(): | 
|  | 188 | fd.write("%s %s\n" % (action,service)) | 
|  | 189 | d.appendVar("FILES_%s" % pkg, ' ' + oe.path.join(d.getVar("systemd_unitdir"), "system-preset/98-%s.preset" % pkg)) | 
|  | 190 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | # Run all modifications once when creating package | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 192 | if os.path.exists(d.getVar("D")): | 
|  | 193 | for pkg in d.getVar('SYSTEMD_PACKAGES').split(): | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 194 | systemd_check_package(pkg) | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 195 | if d.getVar('SYSTEMD_SERVICE_' + pkg): | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | systemd_generate_package_scripts(pkg) | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 197 | action = get_package_var(d, 'SYSTEMD_AUTO_ENABLE', pkg) | 
|  | 198 | if action in ("enable", "disable"): | 
|  | 199 | systemd_create_presets(pkg, action) | 
|  | 200 | elif action not in ("mask", "preset"): | 
|  | 201 | bb.fatal("SYSTEMD_AUTO_ENABLE_%s '%s' is not 'enable', 'disable', 'mask' or 'preset'" % (pkg, action)) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | systemd_check_services() | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | PACKAGESPLITFUNCS_prepend = "systemd_populate_packages " | 
|  | 206 |  | 
|  | 207 | python rm_systemd_unitdir (){ | 
|  | 208 | import shutil | 
|  | 209 | if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 210 | systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir')) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 211 | if os.path.exists(systemd_unitdir): | 
|  | 212 | shutil.rmtree(systemd_unitdir) | 
|  | 213 | systemd_libdir = os.path.dirname(systemd_unitdir) | 
|  | 214 | if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)): | 
|  | 215 | os.rmdir(systemd_libdir) | 
|  | 216 | } | 
|  | 217 | do_install[postfuncs] += "rm_systemd_unitdir " | 
|  | 218 |  | 
|  | 219 | python rm_sysvinit_initddir (){ | 
|  | 220 | import shutil | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 221 | sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d")) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 222 |  | 
|  | 223 | if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \ | 
|  | 224 | not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \ | 
|  | 225 | os.path.exists(sysv_initddir): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 226 | systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir')) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 |  | 
|  | 228 | # If systemd_system_unitdir contains anything, delete sysv_initddir | 
|  | 229 | if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)): | 
|  | 230 | shutil.rmtree(sysv_initddir) | 
|  | 231 | } | 
|  | 232 | do_install[postfuncs] += "rm_sysvinit_initddir " |