Skip to content

Instantly share code, notes, and snippets.

@cjvirtucio87
Created June 25, 2023 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjvirtucio87/6f97b2992c3d4f86618faebd911942e6 to your computer and use it in GitHub Desktop.
Save cjvirtucio87/6f97b2992c3d4f86618faebd911942e6 to your computer and use it in GitHub Desktop.
bash script for calling crictl on the containerd unix socket; useful when running k3s in rootless mode
#!/usr/bin/env bash
function main {
set -eo pipefail
local main_pid
main_pid="$(systemctl show --property MainPID --user k3s-rootless | cut -d '=' -f2 | tr -d '\n')"
local exe_pid
for f in /proc/*/status; do
local parent_pid
parent_pid="$(grep PPid "${f}" | awk '{print $2}' | tr -d '\n')"
if [[ "${main_pid}" -ne "${parent_pid}" ]]; then
continue
fi
local child_process_name
child_process_name="$(grep Name: "${f}" | awk '{print $2}' | tr -d '\n')"
if ! [[ "${child_process_name}" =~ exe ]]; then
continue
fi
exe_pid="$(grep -E '^Pid:' "${f}" | awk '{print $2}' | tr -d '\n')"
break
done
k3s crictl --runtime-endpoint "unix:///proc/${exe_pid}/root/run/k3s/containerd/containerd.sock" "$@"
}
main "$@"
@cjvirtucio87
Copy link
Author

just to credit the source of inspiration: k3s-io/k3s#5786 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment