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