|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Copyright (c) 2019 by Emmett1 (emmett1.2miligrams@gmail.com) |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | +# |
| 19 | +# script to detect broken kernel modules after kernel update |
| 20 | +# need to use with 'scratchpkg' |
| 21 | +# |
| 22 | + |
| 23 | +export LANG=C |
| 24 | + |
| 25 | +get_perlmodules() { |
| 26 | + command -v perl >/dev/null || return |
| 27 | + perlpath=$(dirname $(perl -V:sitearch | grep -o "'.*'" | sed "s/'//g")) |
| 28 | + for i in $(dirname $perlpath)/*; do |
| 29 | + [ "$perlpath" = "$i" ] && continue |
| 30 | + brokenpkg="$brokenpkg $(scratch provide $i/$ | awk '{print $1}')" |
| 31 | + done |
| 32 | +} |
| 33 | + |
| 34 | +get_modules() { |
| 35 | + [ -f /lib/modules/KERNELVERSION ] || return |
| 36 | + KERVER=$(cat /lib/modules/KERNELVERSION) |
| 37 | + for i in /lib/modules/*; do |
| 38 | + case $i in |
| 39 | + /lib/modules/KERNELVERSION|/lib/modules/$KERVER) continue ;; |
| 40 | + esac |
| 41 | + brokenpkg="$brokenpkg $(scratch provide $i/$ | awk '{print $1}')" |
| 42 | + done |
| 43 | +} |
| 44 | + |
| 45 | +get_rubygem() { |
| 46 | + command -v gem >/dev/null || return |
| 47 | + gempath=$(gem env gemdir) |
| 48 | + for i in $(dirname $gempath)/*; do |
| 49 | + [ "$gempath" = "$i" ] && continue |
| 50 | + brokenpkg="$brokenpkg $(scratch provide $i/$ | awk '{print $1}')" |
| 51 | + done |
| 52 | +} |
| 53 | + |
| 54 | +sort_modules() { |
| 55 | + for all in $(scratch deplist $brokenpkg | cut -d ' ' -f2); do |
| 56 | + for r in $brokenpkg; do |
| 57 | + if [ $r = $all ]; then |
| 58 | + if [ -z "$order" ]; then |
| 59 | + order="$all" |
| 60 | + else |
| 61 | + order="$order $all" |
| 62 | + fi |
| 63 | + break |
| 64 | + fi |
| 65 | + done |
| 66 | + done |
| 67 | +} |
| 68 | + |
| 69 | +confirm() { |
| 70 | + printf "$1 (Y/n) " |
| 71 | + read -r response |
| 72 | + case "$response" in |
| 73 | + [Nn][Oo]|[Nn]) echo "$2"; return 2 ;; |
| 74 | + *) : ;; |
| 75 | + esac |
| 76 | + return 0 |
| 77 | +} |
| 78 | + |
| 79 | +usage() { |
| 80 | + cat << EOF |
| 81 | +Usage: |
| 82 | + $(basename $0) [options] |
| 83 | + |
| 84 | +Options: |
| 85 | + -r rebuild & reinstall broken package |
| 86 | + -y dont ask user confirmation to rebuild package (use with -r) |
| 87 | + -h print this help message |
| 88 | +
|
| 89 | +EOF |
| 90 | +} |
| 91 | + |
| 92 | +parse_opt() { |
| 93 | + while [ "$1" ]; do |
| 94 | + case $1 in |
| 95 | + -r) REBUILD=1 ;; |
| 96 | + -y) YES=1 ;; |
| 97 | + -h) usage; exit 0 ;; |
| 98 | + *) echo "Invalid option ($1)"; exit 1 ;; |
| 99 | + esac |
| 100 | + shift |
| 101 | + done |
| 102 | +} |
| 103 | + |
| 104 | +parse_opt $@ |
| 105 | + |
| 106 | +if [ "$REBUILD" ] && [ "$(id -u)" != 0 ]; then |
| 107 | + echo "Rebuild broken packages required root!" |
| 108 | + exit 1 |
| 109 | +fi |
| 110 | + |
| 111 | +get_modules |
| 112 | +get_perlmodules |
| 113 | +get_rubygem |
| 114 | + |
| 115 | +if [ "$brokenpkg" ]; then |
| 116 | + sort_modules |
| 117 | +else |
| 118 | + echo "No broken packages found." |
| 119 | + exit 0 |
| 120 | +fi |
| 121 | + |
| 122 | +if [ "$REBUILD" = 1 ]; then |
| 123 | + [ "$YES" ] || { |
| 124 | + echo |
| 125 | + echo "Package will be rebuild & reinstall by this order:" |
| 126 | + echo " $order" |
| 127 | + echo |
| 128 | + confirm "Continue rebuild & reinstall broken packages?" "Operation cancelled." |
| 129 | + } |
| 130 | + for p in $order; do |
| 131 | + scratch build -f $p && scratch install -r $p || exit 1 |
| 132 | + done |
| 133 | +else |
| 134 | + echo "Broken packages:" |
| 135 | + for p in $order; do |
| 136 | + echo " $p" |
| 137 | + done |
| 138 | +fi |
| 139 | + |
| 140 | +exit 0 |
0 commit comments