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 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE} |
| 38 | |
| 39 | if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 40 | systemctl --no-block restart ${SYSTEMD_SERVICE} |
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 |
| 54 | systemctl stop ${SYSTEMD_SERVICE} |
| 55 | fi |
| 56 | |
| 57 | systemctl $OPTS disable ${SYSTEMD_SERVICE} |
| 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 |
| 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): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -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 | |
| 88 | # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg |
| 89 | # variable. |
| 90 | localdata = d.createCopy() |
| 91 | localdata.prependVar("OVERRIDES", pkg + ":") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | postinst = d.getVar('pkg_postinst_%s' % pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | if not postinst: |
| 95 | postinst = '#!/bin/sh\n' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 96 | postinst += localdata.getVar('systemd_postinst') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | d.setVar('pkg_postinst_%s' % pkg, postinst) |
| 98 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | prerm = d.getVar('pkg_prerm_%s' % pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | if not prerm: |
| 101 | prerm = '#!/bin/sh\n' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | prerm += localdata.getVar('systemd_prerm') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | d.setVar('pkg_prerm_%s' % pkg, prerm) |
| 104 | |
| 105 | |
| 106 | # Add files to FILES_*-systemd if existent and not already done |
| 107 | def systemd_append_file(pkg_systemd, file_append): |
| 108 | appended = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | if os.path.exists(oe.path.join(d.getVar("D"), file_append)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | var_name = "FILES_" + pkg_systemd |
| 111 | files = d.getVar(var_name, False) or "" |
| 112 | if file_append not in files.split(): |
| 113 | d.appendVar(var_name, " " + file_append) |
| 114 | appended = True |
| 115 | return appended |
| 116 | |
| 117 | # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive |
| 118 | def systemd_add_files_and_parse(pkg_systemd, path, service, keys): |
| 119 | # avoid infinite recursion |
| 120 | if systemd_append_file(pkg_systemd, oe.path.join(path, service)): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | fullpath = oe.path.join(d.getVar("D"), path, service) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | if service.find('.service') != -1: |
| 123 | # for *.service add *@.service |
| 124 | service_base = service.replace('.service', '') |
| 125 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) |
| 126 | if service.find('.socket') != -1: |
| 127 | # for *.socket add *.service and *@.service |
| 128 | service_base = service.replace('.socket', '') |
| 129 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys) |
| 130 | systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys) |
| 131 | for key in keys.split(): |
| 132 | # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files |
| 133 | cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key) |
| 134 | pipe = os.popen(cmd, 'r') |
| 135 | line = pipe.readline() |
| 136 | while line: |
| 137 | line = line.replace('\n', '') |
| 138 | systemd_add_files_and_parse(pkg_systemd, path, line, keys) |
| 139 | line = pipe.readline() |
| 140 | pipe.close() |
| 141 | |
| 142 | # Check service-files and call systemd_add_files_and_parse for each entry |
| 143 | def systemd_check_services(): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | searchpaths = [oe.path.join(d.getVar("sysconfdir"), "systemd", "system"),] |
| 145 | searchpaths.append(d.getVar("systemd_system_unitdir")) |
| 146 | systemd_packages = d.getVar('SYSTEMD_PACKAGES') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | |
| 148 | keys = 'Also' |
| 149 | # scan for all in SYSTEMD_SERVICE[] |
| 150 | for pkg_systemd in systemd_packages.split(): |
| 151 | for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split(): |
| 152 | path_found = '' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 153 | |
| 154 | # Deal with adding, for example, 'ifplugd@eth0.service' from |
| 155 | # 'ifplugd@.service' |
| 156 | base = None |
| 157 | if service.find('@') != -1: |
| 158 | base = re.sub('@[^.]+.', '@.', service) |
| 159 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 160 | for path in searchpaths: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 161 | if os.path.exists(oe.path.join(d.getVar("D"), path, service)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | path_found = path |
| 163 | break |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 164 | elif base is not None: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 165 | if os.path.exists(oe.path.join(d.getVar("D"), path, base)): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 166 | path_found = path |
| 167 | break |
| 168 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | if path_found != '': |
| 170 | systemd_add_files_and_parse(pkg_systemd, path_found, service, keys) |
| 171 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 172 | 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] | 173 | |
| 174 | # Run all modifications once when creating package |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 175 | if os.path.exists(d.getVar("D")): |
| 176 | for pkg in d.getVar('SYSTEMD_PACKAGES').split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | systemd_check_package(pkg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 178 | if d.getVar('SYSTEMD_SERVICE_' + pkg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | systemd_generate_package_scripts(pkg) |
| 180 | systemd_check_services() |
| 181 | } |
| 182 | |
| 183 | PACKAGESPLITFUNCS_prepend = "systemd_populate_packages " |
| 184 | |
| 185 | python rm_systemd_unitdir (){ |
| 186 | import shutil |
| 187 | if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 188 | systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | if os.path.exists(systemd_unitdir): |
| 190 | shutil.rmtree(systemd_unitdir) |
| 191 | systemd_libdir = os.path.dirname(systemd_unitdir) |
| 192 | if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)): |
| 193 | os.rmdir(systemd_libdir) |
| 194 | } |
| 195 | do_install[postfuncs] += "rm_systemd_unitdir " |
| 196 | |
| 197 | python rm_sysvinit_initddir (){ |
| 198 | import shutil |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 199 | 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] | 200 | |
| 201 | if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \ |
| 202 | not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \ |
| 203 | os.path.exists(sysv_initddir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 204 | 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] | 205 | |
| 206 | # If systemd_system_unitdir contains anything, delete sysv_initddir |
| 207 | if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)): |
| 208 | shutil.rmtree(sysv_initddir) |
| 209 | } |
| 210 | do_install[postfuncs] += "rm_sysvinit_initddir " |