.nh .TH oci\-hooks(5) OCI Hooks Configuration W. Trevor King MAY 2018 .SH NAME .PP oci\-hooks \- OCI hooks configuration directories .SH SYNOPSIS .PP \fB\fC/usr/share/containers/oci/hooks.d/*.json\fR .SH DESCRIPTION .PP Provides a way for users to configure the intended hooks for Open Container Initiative containers so they will only be executed for containers that need their functionality, and then only for the stages where they're needed. .SH Directories .PP Hooks are configured with JSON files (ending with a \fB\fC\&.json\fR extension) in a series of hook directories. The default directory is \fB\fC/usr/share/containers/oci/hooks.d\fR, but tools consuming this format may change that default, include additional directories, or provide their callers with ways to adjust the configuration directories. .PP If multiple directories are configured, a JSON filename in a preferred directory masks entries with the same filename in directories with lower precedence. For example, if a consuming tool watches for hooks in \fB\fC/etc/containers/oci/hooks.d\fR and \fB\fC/usr/share/containers/oci/hooks.d\fR (in order of decreasing precedence), then a hook definition in \fB\fC/etc/containers/oci/hooks.d/01\-my\-hook.json\fR will mask any definition in \fB\fC/usr/share/containers/oci/hooks.d/01\-my\-hook.json\fR\&. .PP Tools consuming this format may also opt to monitor the hook directories for changes, in which case they will notice additions, changes, and removals to JSON files without needing to be restarted or otherwise signaled. When the tool monitors multiple hooks directories, the precedence discussed in the previous paragraph still applies. For example, if a consuming tool watches for hooks in \fB\fC/etc/containers/oci/hooks.d\fR and \fB\fC/usr/share/containers/oci/hooks.d\fR (in order of decreasing precedence), then writing a new hook definition to \fB\fC/etc/containers/oci/hooks.d/01\-my\-hook.json\fR will mask the hook previously loaded from \fB\fC/usr/share/containers/oci/hooks.d/01\-my\-hook.json\fR\&. Subsequent changes to \fB\fC/usr/share/containers/oci/hooks.d/01\-my\-hook.json\fR will have no effect on the consuming tool as long as \fB\fC/etc/containers/oci/hooks.d/01\-my\-hook.json\fR exists. Removing \fB\fC/etc/containers/oci/hooks.d/01\-my\-hook.json\fR will reload the hook from \fB\fC/usr/share/containers/oci/hooks.d/01\-my\-hook.json\fR\&. .PP Hooks are injected in the order obtained by sorting the JSON file names, after converting them to lower case, based on their Unicode code points. For example, a matching hook defined in \fB\fC01\-my\-hook.json\fR would be injected before matching hooks defined in \fB\fC02\-another\-hook.json\fR and \fB\fC01\-UPPERCASE.json\fR\&. It is strongly recommended to make the sort order unambiguous depending on an ASCII\-\&only prefix (like the \fB\fC01\fR/\fB\fC02\fR above). .PP Each JSON file should contain an object with one of the following schemas. .SH 1.0.0 Hook Schema .PP \fB\fCversion\fR (required string) Sets the hook\-definition version. For this schema version, the value be \fB\fC1.0.0\fR\&. .PP \fB\fChook\fR (required object) The hook to inject, with the hook\-entry schema defined by the 1.0.1 OCI Runtime Specification. .PP \fB\fCwhen\fR (required object) Conditions under which the hook is injected. The following properties can be specified, and at least one must be specified: .RS .IP \(bu 2 \fB\fCalways\fR (optional boolean) If set \fB\fCtrue\fR, this condition matches. .IP \(bu 2 \fB\fCannotations\fR (optional object) If all \fB\fCannotations\fR key/value pairs match a key/value pair from the configured annotations, this condition matches. Both keys and values must be POSIX extended regular expressions. .IP \(bu 2 \fB\fCcommands\fR (optional array of strings) If the configured \fB\fCprocess.args[0]\fR matches an entry, this condition matches. Entries must be POSIX extended regular expressions. .IP \(bu 2 \fB\fChasBindMounts\fR (optional boolean) If \fB\fChasBindMounts\fR is true and the caller requested host\-to\-container bind mounts, this condition matches. .RE .PP \fB\fCstages\fR (required array of strings) Stages when the hook must be injected. Entries must be chosen from the 1.0.1 OCI Runtime Specification hook stages or from extension stages supported by the package consumer. .PP If \fIall\fP of the conditions set in \fB\fCwhen\fR match, then the \fB\fChook\fR must be injected for the stages set in \fB\fCstages\fR\&. .SH 0.1.0 Hook Schema .PP \fB\fChook\fR (required string) Sets \fB\fCpath\fR in the injected hook. .PP \fB\fCarguments\fR (optional array of strings) Additional arguments to pass to the hook. The injected hook's \fB\fCargs\fR is \fB\fChook\fR with \fB\fCarguments\fR appended. .PP \fB\fCstages\fR (required array of strings) Stages when the hook must be injected. \fB\fCstage\fR is an allowed synonym for this property, but you must not set both \fB\fCstages\fR and \fB\fCstage\fR\&. Entries must be chosen from the 1.0.1 OCI Runtime Specification hook stages or from extension stages supported by the package consumer. .PP \fB\fCcmds\fR (optional array of strings) The hook must be injected if the configured \fB\fCprocess.args[0]\fR matches an entry. \fB\fCcmd\fR is an allowed synonym for this property, but you must not set both \fB\fCcmds\fR and \fB\fCcmd\fR\&. Entries must be POSIX extended regular expressions. .PP \fB\fCannotations\fR (optional array of strings) The hook must be injected if an \fB\fCannotations\fR entry matches a value from the configured annotations. \fB\fCannotation\fR is an allowed synonym for this property, but you must not set both \fB\fCannotations\fR and \fB\fCannotation\fR\&. Entries must be POSIX extended regular expressions. .PP \fB\fChasbindmounts\fR (optional boolean) The hook must be injected if \fB\fChasBindMounts\fR is true and the caller requested host\-to\-container bind mounts. .SH EXAMPLE .SH 1.0.0 Hook Schema .PP The following configuration injects \fB\fCoci\-systemd\-hook\fR in the pre\-start and post\-stop stages if \fB\fCprocess.args[0]\fR ends with \fB\fC/init\fR or \fB\fC/systemd\fR: .PP .RS .nf $ cat /etc/containers/oci/hooks.d/oci\-systemd\-hook.json { "version": "1.0.0", "hook": { "path": "/usr/libexec/oci/hooks.d/oci\-systemd\-hook" }, "when": { "commands": [".*/init$" , ".*/systemd$"] }, "stages": ["prestart", "poststop"] } .fi .RE .PP The following example injects \fB\fCoci\-umount \-\-debug\fR in the pre\-start stage if the container is configured to bind\-mount host directories into the container. .PP .RS .nf $ cat /etc/containers/oci/hooks.d/oci\-umount.json { "version": "1.0.0", "hook": { "path": "/usr/libexec/oci/hooks.d/oci\-umount", "args": ["oci\-umount", "\-\-debug"], }, "when": { "hasBindMounts": true }, "stages": ["prestart"] } .fi .RE .PP The following example injects \fB\fCnvidia\-container\-runtime\-hook prestart\fR with particular environment variables in the pre\-start stage if the container is configured with an \fB\fCannotations\fR entry whose key matches \fB\fC^com\\.example\\.department$\fR and whose value matches \fB\fC\&.*fluid\-\&dynamics.*\fR\&. .PP .RS .nf $ cat /etc/containers/oci/hooks.d/nvidia.json { "version": "1.0.0", "hook": { "path": "/usr/sbin/nvidia\-container\-runtime\-hook", "args": ["nvidia\-container\-runtime\-hook", "prestart"], "env": [ "NVIDIA\_REQUIRE\_CUDA=cuda>=9.1", "NVIDIA\_VISIBLE\_DEVICES=GPU\-fef8089b" ] }, "when": { "annotations": { "^com\\\\.example\\\\.department$": ".*fluid\-dynamics$" } }, "stages": ["prestart"] } .fi .RE .SH 0.1.0 Hook Schema .PP The following configuration injects \fB\fCoci\-systemd\-hook\fR in the pre\-start and post\-stop stages if \fB\fCprocess.args[0]\fR ends with \fB\fC/init\fR or \fB\fC/systemd\fR: .PP .RS .nf $ cat /etc/containers/oci/hooks.d/oci\-systemd\-hook.json { "cmds": [".*/init$" , ".*/systemd$"], "hook": "/usr/libexec/oci/hooks.d/oci\-systemd\-hook", "stages": ["prestart", "poststop"] } .fi .RE .PP The following example injects \fB\fCoci\-umount \-\-debug\fR in the pre\-start stage if the container is configured to bind\-mount host directories into the container. .PP .RS .nf $ cat /etc/containers/oci/hooks.d/oci\-umount.json { "hook": "/usr/libexec/oci/hooks.d/oci\-umount", "arguments": ["\-\-debug"], "hasbindmounts": true, "stages": ["prestart"] } .fi .RE .PP The following example injects \fB\fCnvidia\-container\-runtime\-hook prestart\fR in the pre\-start stage if the container is configured with an \fB\fCannotations\fR entry whose value matches \fB\fC\&.*fluid\-\&dynamics.*\fR\&. .PP .RS .nf $ cat /etc/containers/oci/hooks.d/osystemd\-hook.json { "hook": "/usr/sbin/nvidia\-container\-runtime\-hook", "arguments": ["prestart"], "annotations: [".*fluid\-dynamics.*"], "stages": ["prestart"] } .fi .RE .SH SEE ALSO .PP \fB\fCoci\-systemd\-hook(1)\fR, \fB\fCoci\-umount(1)\fR, \fB\fClocale(7)\fR .RS .IP \(bu 2 OCI Runtime Specification, 1.0.1, POSIX\-platform hooks \[la]https://github.com/opencontainers/runtime-spec/blob/v1.0.1/config.md#posix-platform-hooks\[ra] .IP \(bu 2 OCI Runtime Specification, 1.0.1, process \[la]https://github.com/opencontainers/runtime-spec/blob/v1.0.1/config.md#process\[ra] .IP \(bu 2 POSIX extended regular expressions (EREs) \[la]http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04\[ra] .RE