summaryrefslogtreecommitdiffstats
path: root/recipes-containers/k3s/k3s/k3s-killall.sh
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-containers/k3s/k3s/k3s-killall.sh')
-rw-r--r--recipes-containers/k3s/k3s/k3s-killall.sh82
1 files changed, 82 insertions, 0 deletions
diff --git a/recipes-containers/k3s/k3s/k3s-killall.sh b/recipes-containers/k3s/k3s/k3s-killall.sh
new file mode 100644
index 00000000..9e726153
--- /dev/null
+++ b/recipes-containers/k3s/k3s/k3s-killall.sh
@@ -0,0 +1,82 @@
1#!/bin/sh
2
3# Based on: k3s-killall.sh installed when running Rancher Lab's K3S install.sh
4# In open-source project: https://github.com/k3s-io/k3s
5#
6# Original file: Copyright (c) 2021 Rancher Labs and Contributors.
7# Modifications: Copyright (c) 2021 Arm Limited and Contributors. All rights reserved.
8#
9# Modifications:
10# - Change systemd service directory location
11# - Fix PID parsing to run on core image
12# - Remove service stopping code (as this is intended to run as part of service
13# stop)
14# - Changes to resolve warnings from the ShellCheck static analysis tool
15#
16# SPDX-License-Identifier: Apache License 2.0
17
18[ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@"
19
20for bin in /var/lib/rancher/k3s/data/**/bin/; do
21 [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux
22done
23
24set -x
25
26pschildren() {
27 ps -e -o ppid= -o pid= | sed -e 's/^\s*//g; s/\s\s*/\t/g;' | grep -w "^$1" | cut -f2
28}
29
30pstree() {
31 for pid in "$@"; do
32 echo "$pid"
33 for child in $(pschildren "$pid"); do
34 pstree "$child"
35 done
36 done
37}
38
39killtree() {
40 while read -r pid; do
41 if [ -n "${pid}" ]; then
42 kill -9 "${pid}" 2>/dev/null
43 fi
44 done <<EOF
45$({ set +x; } 2>/dev/null; pstree "$@"; set -x;)
46EOF
47}
48
49getshims() {
50 ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w '[^/]*/bin/containerd-shim' | cut -f1
51}
52
53killtree "$({ set +x; } 2>/dev/null; getshims; set -x)"
54
55# shellcheck disable=SC2016
56do_unmount_and_remove() {
57 set +x
58 while read -r _ path _; do
59 case "$path" in $1*) echo "$path" ;; esac
60 done < /proc/self/mounts | sort -r | xargs -r -t -n 1 sh -c 'umount "$0" && rm -rf "$0"'
61 set -x
62}
63
64do_unmount_and_remove '/run/k3s'
65do_unmount_and_remove '/var/lib/rancher/k3s'
66do_unmount_and_remove '/var/lib/kubelet/pods'
67do_unmount_and_remove '/var/lib/kubelet/plugins'
68do_unmount_and_remove '/run/netns/cni-'
69
70# Remove CNI namespaces
71ip netns show 2>/dev/null | grep cni- | xargs -r -t -n 1 ip netns delete
72
73# Delete network interface(s) that match 'master cni0'
74ip link show 2>/dev/null | grep 'master cni0' | while read -r _ iface _; do
75 iface=${iface%%@*}
76 [ -z "$iface" ] || ip link delete "$iface"
77done
78ip link delete cni0
79ip link delete flannel.1
80ip link delete flannel-v6.1
81rm -rf /var/lib/cni/
82iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restore