diff --git a/install.sh b/install.sh index ae679bd532..b65fdebb03 100755 --- a/install.sh +++ b/install.sh @@ -125,6 +125,13 @@ setup_env() { fi SERVICE_K3S=${SYSTEM_NAME}.service UNINSTALL_K3S_SH=${SYSTEM_NAME}-uninstall.sh + KILLALL_K3S_SH=k3s-killall.sh + + # --- use sudo if we are not already root --- + SUDO=sudo + if [ `id -u` = 0 ]; then + SUDO= + fi # --- use systemd type if defined or create default --- if [ -n "${INSTALL_K3S_TYPE}" ]; then @@ -161,13 +168,10 @@ setup_env() { FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env fi - # --- use sudo if we are not already root --- - SUDO=sudo - if [ `id -u` = 0 ]; then - SUDO= - fi - + # --- get hash of config & exec for currently installed k3s --- PRE_INSTALL_HASHES=`get_installed_hashes` + + # --- if bin directory is read only skip download --- if [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ]; then INSTALL_K3S_SKIP_DOWNLOAD=true fi @@ -342,6 +346,63 @@ create_symlinks() { fi } + +# --- create killall script --- +create_killall() { + [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return + info "Creating killall script ${BIN_DIR}/${KILLALL_K3S_SH}" + $SUDO tee ${BIN_DIR}/${KILLALL_K3S_SH} >/dev/null << \EOF +#!/bin/sh +set -x +[ `id -u` = 0 ] || exec sudo $0 $@ + +for bin in /var/lib/rancher/k3s/data/**/bin/; do + [ -d $bin ] && export PATH=$bin:$PATH +done + +for service in /etc/systemd/system/k3s*.service; do + [ -s $service ] && systemctl stop $(basename $service) +done + +for service in /etc/init.d/k3s*; do + [ -x $service ] && $service stop +done + +pstree() { + for pid in $@; do + echo $pid + pstree $(ps -o ppid= -o pid= | awk "\$1==$pid {print \$2}") + done +} + +killtree() { + [ $# -ne 0 ] && kill $(set +x; pstree $@; set -x) +} + +killtree $(lsof | sed -e 's/^[^0-9]*//g; s/ */\t/g' | grep -w 'k3s/data/[^/]*/bin/containerd-shim' | cut -f1 | sort -n -u) + +do_unmount() { + MOUNTS=`cat /proc/self/mounts | awk '{print $2}' | grep "^$1" | sort -r` + if [ -n "${MOUNTS}" ]; then + umount ${MOUNTS} + fi +} + +do_unmount '/run/k3s' +do_unmount '/var/lib/rancher/k3s' + +nets=$(ip link show | grep 'master cni0' | awk -F': ' '{print $2}' | sed -e 's|@.*||') +for iface in $nets; do + ip link delete $iface; +done +ip link delete cni0 +ip link delete flannel.1 +rm -rf /var/lib/cni/ +EOF + $SUDO chmod 755 ${BIN_DIR}/${KILLALL_K3S_SH} + $SUDO chown root:root ${BIN_DIR}/${KILLALL_K3S_SH} +} + # --- create uninstall script --- create_uninstall() { [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return @@ -349,12 +410,19 @@ create_uninstall() { $SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF #!/bin/sh set -x +[ \`id -u\` = 0 ] || exec sudo \$0 \$@ + +${BIN_DIR}/${KILLALL_K3S_SH} + if which systemctl; then - systemctl kill ${SYSTEM_NAME} systemctl disable ${SYSTEM_NAME} systemctl reset-failed ${SYSTEM_NAME} systemctl daemon-reload fi +if which rc-update; then + rc-update delete ${SYSTEM_NAME} default +fi + rm -f ${FILE_K3S_SERVICE} rm -f ${FILE_K3S_ENV} @@ -368,22 +436,6 @@ if (ls ${SYSTEMD_DIR}/k3s*.service || ls /etc/init.d/k3s*) >/dev/null 2>&1; then exit fi -do_unmount() { - MOUNTS=\`cat /proc/self/mounts | awk '{print \$2}' | grep "^\$1"\` - if [ -n "\${MOUNTS}" ]; then - umount \${MOUNTS} - fi -} -do_unmount '/run/k3s' -do_unmount '/var/lib/rancher/k3s' - -nets=\$(ip link show master cni0 | grep cni0 | awk -F': ' '{print \$2}' | sed -e 's|@.*||') -for iface in \$nets; do - ip link delete \$iface; -done -ip link delete cni0 -ip link delete flannel.1 - if [ -L ${BIN_DIR}/kubectl ]; then rm -f ${BIN_DIR}/kubectl fi @@ -394,6 +446,7 @@ fi rm -rf /etc/rancher/k3s rm -rf /var/lib/rancher/k3s rm -f ${BIN_DIR}/k3s +rm -f ${BIN_DIR}/${KILLALL_K3S_SH} EOF $SUDO chmod 755 ${BIN_DIR}/${UNINSTALL_K3S_SH} $SUDO chown root:root ${BIN_DIR}/${UNINSTALL_K3S_SH} @@ -492,7 +545,7 @@ create_service_file() { # --- get hashes of the current k3s bin and service files get_installed_hashes() { - sha256sum ${BIN_DIR}/k3s ${FILE_K3S_SERVICE} ${FILE_K3S_ENV} 2>&1 || true + $SUDO sha256sum ${BIN_DIR}/k3s ${FILE_K3S_SERVICE} ${FILE_K3S_ENV} 2>&1 || true } # --- enable and start systemd service --- @@ -542,6 +595,7 @@ service_enable_and_start() { setup_env ${INSTALL_K3S_EXEC} $@ download_and_verify create_symlinks + create_killall create_uninstall systemd_disable create_env_file