| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | # Copyright (C) 2014-2026 Daniel Baumann <[email protected]> |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-3.0+ |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 3 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | |
| 20 | set -e |
| 21 | |
| 22 | PROJECT="open-infrastructure" |
| 23 | SOFTWARE="compute-tools" |
| 24 | PROGRAM="container" |
| 25 | COMMAND="$(basenamescip-shell system . . basename. ${0})" |
| 26 | |
| 27 | HOOKS="/etc/${SOFTWARE}/hooks" |
| 28 | MACHINES="/var/lib/machinesscip-shell filesystem . . `/var/lib/machines`." |
| 29 | |
| 30 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). () |
| 31 | { |
| 32 | GETOPT_LONGOPTIONS="name:,blockio-device-weight:,blockio-read-bandwidth:,blockio-weight:,blockio-write-bandwidth:,cpu-quota:,cpu-shares:,memory-limit:,tasks-max:," |
| 33 | GETOPT_OPTIONS="n:b:c:m:t:," |
| 34 | |
| 35 | PARAMETERS="$(getoptscip-shell system . . getopt. --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" |
| 36 | |
| 37 | if [scip-shell system . . `[`. "${?}" != "0" ] |
| 38 | then |
| 39 | echoscip-shell system . . echo. "'${COMMAND}': getopt exit" >&2 |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | eval set -- "${PARAMETERS}" |
| 44 | |
| 45 | while truescip-shell system . . true. |
| 46 | do |
| 47 | case "${1}" in |
| 48 | -n|--name) |
| 49 | NAME="${2}" |
| 50 | shift 2 |
| 51 | ;; |
| 52 | |
| 53 | -c|--cpu-quota) |
| 54 | CPU_QUOTA="${2}" |
| 55 | shift 2 |
| 56 | ;; |
| 57 | |
| 58 | --cpu-shares) |
| 59 | CPU_SHARES="${2}" |
| 60 | shift 2 |
| 61 | ;; |
| 62 | |
| 63 | -m|--memory-limit) |
| 64 | MEMORY_LIMIT="${2}" |
| 65 | shift 2 |
| 66 | ;; |
| 67 | |
| 68 | -t|--tasks-max) |
| 69 | TASKS_MAX="${2}" |
| 70 | shift 2 |
| 71 | ;; |
| 72 | |
| 73 | --blockio-device-weight) |
| 74 | BLOCK_IO_DEVICE_WEIGHT="${2}" |
| 75 | shift 2 |
| 76 | ;; |
| 77 | |
| 78 | --blockio-read-bandwidth) |
| 79 | BLOCK_IO_READ_BANDWIDTH="${2}" |
| 80 | shift 2 |
| 81 | ;; |
| 82 | |
| 83 | -b|--blockio-weight) |
| 84 | BLOCK_IO_WEIGHT="${2}" |
| 85 | shift 2 |
| 86 | ;; |
| 87 | |
| 88 | --blockio-write-bandwidth) |
| 89 | BLOCK_IO_WRITE_BANDWIDTH="${2}" |
| 90 | shift 2 |
| 91 | ;; |
| 92 | |
| 93 | --) |
| 94 | shift 1 |
| 95 | break |
| 96 | ;; |
| 97 | |
| 98 | *) |
| 99 | echoscip-shell system . . echo. "'${COMMAND}': getopt error" >&2 |
| 100 | exit 1 |
| 101 | ;; |
| 102 | esac |
| 103 | done |
| 104 | } |
| 105 | |
| 106 | Usagescip-shell shell shell-project 0.0.0 Usage(). () |
| 107 | { |
| 108 | echoscip-shell system . . echo. "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--blockio-device-weight \"DEVICE WEIGHT\"] [--blockio-read-bandwidth \"DEVICE BYTES\"] [-b|--blockio-weight WEIGHT] [--blockio-write-bandwidth \"DEVICE BYTES\"] [-c|--cpu-quota QUOTA] [--cpu-shares SHARES] [-m|--memory-limit BYTES] [-t|--tasks-max NUMBER]" >&2 |
| 109 | echoscip-shell system . . echo. |
| 110 | echoscip-shell system . . echo. "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." |
| 111 | |
| 112 | exit 1 |
| 113 | } |
| 114 | |
| 115 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). "${@}" |
| 116 | |
| 117 | if [scip-shell system . . `[`. -z "${NAME}" ] |
| 118 | then |
| 119 | Usagescip-shell shell shell-project 0.0.0 Usage(). |
| 120 | fi |
| 121 | |
| 122 | if [scip-shell system . . `[`. ! -e "${MACHINES}/${NAME}" ] |
| 123 | then |
| 124 | echoscip-shell system . . echo. "'${NAME}': no such container" >&2 |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | STATE="$(machinectl show ${NAME} 2>&1 | awkscip-shell system . . awk. -FState= '/^State=/ { print $2 }scip-shell filesystem . . `/^State=/ { print $2 }`.')" |
| 129 | |
| 130 | case "${STATE}" in |
| 131 | running) |
| 132 | ;; |
| 133 | |
| 134 | *) |
| 135 | echoscip-shell system . . echo. "'${NAME}': container is not running" >&2 |
| 136 | exit 1 |
| 137 | ;; |
| 138 | esac |
| 139 | |
| 140 | if [scip-shell system . . `[`. -n "${BLOCK_IO_DEVICE_WEIGHT}" ] |
| 141 | then |
| 142 | BLOCK_IO_DEVICE_WEIGHT="BlockIODeviceWeight=${BLOCK_IO_DEVICE_WEIGHT}" |
| 143 | SET_PROPERTY="true" |
| 144 | fi |
| 145 | |
| 146 | if [scip-shell system . . `[`. -n "${BLOCK_IO_READ_BANDWIDTH}" ] |
| 147 | then |
| 148 | BLOCK_IO_READ_BANDWIDTH="BlockIOReadBandwidth=${BLOCK_IO_READ_BANDWIDTH}" |
| 149 | SET_PROPERTY="true" |
| 150 | fi |
| 151 | |
| 152 | if [scip-shell system . . `[`. -n "${BLOCK_IO_WEIGHT}" ] |
| 153 | then |
| 154 | BLOCK_IO_WEIGHT="BlockIOWeight=${BLOCK_IO_WEIGHT}" |
| 155 | SET_PROPERTY="true" |
| 156 | fi |
| 157 | |
| 158 | if [scip-shell system . . `[`. -n "${BLOCK_IO_WRITE_BANDWIDTH}" ] |
| 159 | then |
| 160 | BLOCK_IO_WRITE_BANDWIDTH="BlockIOReadBandwidth=${BLOCK_IO_WRITE_BANDWIDTH}" |
| 161 | SET_PROPERTY="true" |
| 162 | fi |
| 163 | |
| 164 | if [scip-shell system . . `[`. -n "${CPU_QUOTA}" ] |
| 165 | then |
| 166 | CPU_QUOTA="CPUQuota=${CPU_QUOTA}" |
| 167 | SET_PROPERTY="true" |
| 168 | fi |
| 169 | |
| 170 | if [scip-shell system . . `[`. -n "${CPU_SHARES}" ] |
| 171 | then |
| 172 | CPU_SHARES="CPUShares=${CPU_SHARES}" |
| 173 | SET_PROPERTY="true" |
| 174 | fi |
| 175 | |
| 176 | if [scip-shell system . . `[`. -n "${MEMORY_LIMIT}" ] |
| 177 | then |
| 178 | MEMORY_LIMIT="MemoryLimit=${MEMORY_LIMIT}" |
| 179 | SET_PROPERTY="true" |
| 180 | fi |
| 181 | |
| 182 | if [scip-shell system . . `[`. -n "${TASKS_MAX}" ] |
| 183 | then |
| 184 | TASKS_MAX="TasksMax=${TASKS_MAX}" |
| 185 | SET_PROPERTY="true" |
| 186 | fi |
| 187 | |
| 188 | if [scip-shell system . . `[`. "${SET_PROPERTY}" != "true" ] |
| 189 | then |
| 190 | Usagescip-shell shell shell-project 0.0.0 Usage(). |
| 191 | fi |
| 192 | |
| 193 | # Pre hooks |
| 194 | for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" |
| 195 | do |
| 196 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 197 | then |
| 198 | "${FILE}" |
| 199 | fi |
| 200 | done |
| 201 | |
| 202 | # Run |
| 203 | systemctl --runtime set-property ${NAME} ${BLOCK_IO_DEVICE_WEIGHT} ${BLOCK_IO_READ_BANDWIDTH} ${BLOCK_IO_WEIGHT} ${BLOCK_IO_WRITE_BANDWIDTH} ${CPU_QUOTA} ${CPU_SHARES} ${MEMORY_LIMIT} ${TASKS_MAX} |
| 204 | |
| 205 | # Post hooks |
| 206 | for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" |
| 207 | do |
| 208 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 209 | then |
| 210 | "${FILE}" |
| 211 | fi |
| 212 | done |