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