diff options
Diffstat (limited to 'recipes-containers/k3s/k3s/k3s-killall.sh')
-rw-r--r-- | recipes-containers/k3s/k3s/k3s-killall.sh | 82 |
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 | |||
20 | for bin in /var/lib/rancher/k3s/data/**/bin/; do | ||
21 | [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux | ||
22 | done | ||
23 | |||
24 | set -x | ||
25 | |||
26 | pschildren() { | ||
27 | ps -e -o ppid= -o pid= | sed -e 's/^\s*//g; s/\s\s*/\t/g;' | grep -w "^$1" | cut -f2 | ||
28 | } | ||
29 | |||
30 | pstree() { | ||
31 | for pid in "$@"; do | ||
32 | echo "$pid" | ||
33 | for child in $(pschildren "$pid"); do | ||
34 | pstree "$child" | ||
35 | done | ||
36 | done | ||
37 | } | ||
38 | |||
39 | killtree() { | ||
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;) | ||
46 | EOF | ||
47 | } | ||
48 | |||
49 | getshims() { | ||
50 | ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w '[^/]*/bin/containerd-shim' | cut -f1 | ||
51 | } | ||
52 | |||
53 | killtree "$({ set +x; } 2>/dev/null; getshims; set -x)" | ||
54 | |||
55 | # shellcheck disable=SC2016 | ||
56 | do_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 | |||
64 | do_unmount_and_remove '/run/k3s' | ||
65 | do_unmount_and_remove '/var/lib/rancher/k3s' | ||
66 | do_unmount_and_remove '/var/lib/kubelet/pods' | ||
67 | do_unmount_and_remove '/var/lib/kubelet/plugins' | ||
68 | do_unmount_and_remove '/run/netns/cni-' | ||
69 | |||
70 | # Remove CNI namespaces | ||
71 | ip 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' | ||
74 | ip link show 2>/dev/null | grep 'master cni0' | while read -r _ iface _; do | ||
75 | iface=${iface%%@*} | ||
76 | [ -z "$iface" ] || ip link delete "$iface" | ||
77 | done | ||
78 | ip link delete cni0 | ||
79 | ip link delete flannel.1 | ||
80 | ip link delete flannel-v6.1 | ||
81 | rm -rf /var/lib/cni/ | ||
82 | iptables-save | grep -v KUBE- | grep -v CNI- | iptables-restore | ||