-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcki
executable file
·243 lines (212 loc) · 8.06 KB
/
cki
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# -*- coding: utf-8 -*-
#
# cki - Condres Kernel Installer
# Copyright © 2017 Condres
#
# Loosely based on code from mhwd-kernel from Manjaro
#
# Cki is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Cki is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# The following additional terms are in effect as per Section 7 of the license:
#
# The preservation of all legal notices and author attributions in
# the material or in the Appropriate Legal Notices displayed
# by works containing it is required.
#
# You should have received a copy of the GNU General Public License
# along with Cki; If not, see <http://www.gnu.org/licenses/>.
set -euo pipefail
KERNEL_NAMES=""
KERNEL_AUR_NAMES=""
DATA_PATH="/usr/share/cki"
KERNEL_NAMES_FILE="kernels.txt"
KERNEL_AUR_NAMES_FILE="kernels-aur.txt"
AUR_HELPERS="yaourt packer pacaur"
# Colors
CSTART="\e[31m"
CEND="\e[0m"
fatal() {
printf "${CSTART}Error:${CEND} $1\n" 1>&2; exit 1
}
root_check() {
if [[ $EUID != 0 ]]; then
fatal "Please run as root."
fi
}
data_files_check() {
# First, we check if they're in the same directory
if [[ ! -f $KERNEL_NAMES_FILE ]] || [[ ! -f $KERNEL_AUR_NAMES_FILE ]]; then
# if not, we check in DATA_PATH
KERNEL_NAMES_FILE="$DATA_PATH/$KERNEL_NAMES_FILE"
KERNEL_AUR_NAMES_FILE="$DATA_PATH/$KERNEL_AUR_NAMES_FILE"
if [[ ! -f $KERNEL_NAMES_FILE ]] || [[ ! -f $KERNEL_AUR_NAMES_FILE ]]; then
fatal "Missing data files!"
fi
fi
}
args_check() {
[[ $1 -lt $2 || $1 -gt $3 ]] && fatal "Please use the right amount of arguments (use -h for help)."
}
usage() {
echo "Usage: cki [option] [kernel]
-h, --help Show this help message
-i, --install Install new kernel
-l, --list List all available kernels
-li, --list-installed List installed kernels
-r, --remove Remove kernel
-a, --aur Enable AUR kernels (disabled by default)
-n, --no-color Avoid showing ANSI colors in messages
"
}
load_kernel_names() {
KERNEL_NAMES=$(grep -h -v ^# ${KERNEL_NAMES_FILE} | grep -v '^[[:blank:]]*$' | cut -f1 -d ' ' | grep -v \\-docs | grep -v \\-headers | tr '\n' ' ')
if [[ "$AUR_ENABLED" == "y" ]]; then
KERNEL_AUR_NAMES=$(grep -h -v ^# ${KERNEL_AUR_NAMES_FILE} | grep -v '^[[:blank:]]*$' | cut -f1 -d ' ' | grep -v \\-docs | grep -v \\-headers | tr '\n' ' ')
fi
}
kernel_installed_list() {
printf "${CSTART}Currently running:${CEND} $(uname -r) (${current})\n"
echo "The following kernels are installed in your system:"
names=()
for kname in ${KERNEL_NAMES}; do
name=$(pacman -Qqs $kname | grep -w ^$kname$)
[[ ! -z "$name" ]] && echo $name
done
if [[ "$AUR_ENABLED" == "y" ]]; then
for kname in ${KERNEL_AUR_NAMES}; do
name=$($AUR_HELPER -Qqs $kname | grep -w ^$kname$)
[[ ! -z "$name" ]] && echo "$name (aur)"
done
fi
}
kernel_available_list() {
printf "${CSTART}Available kernels online:${CEND}\n"
for kname in ${KERNEL_NAMES}; do
name=$(pacman -Ssq $kname | grep -w ^$kname$)
[[ ! -z "$name" ]] && echo $name
done
if [[ "$AUR_ENABLED" == "y" ]]; then
for kname in ${KERNEL_AUR_NAMES}; do
name=$($AUR_HELPER -Ssq $kname | grep -w ^$kname$)
[[ ! -z "$name" ]] && echo "$name (aur)"
done
fi
}
kernel_remove() {
pkgremove=()
for kernel in "$@"; do
[[ -z "$kernel" ]] && fatal "Invalid argument (use -h for help)."
[[ ${KERNEL_NAMES} != *"$kernel"* ]] && [[ ${KERNEL_AUR_NAMES} != *"$kernel"* ]] && fatal "Please enter a valid kernel name.\n$(kernel_installed_list)"
[[ $current_kname = $kernel ]] && fatal "You can't remove your current kernel."
[[ -z $(pacman -Qqs "^$1$") ]] && fatal "Kernel not installed.\n$(kernel_installed_list)"
for pkg in $(pacman -Qqs "$kernel"); do
pkgremove+=("$pkg")
done
done
echo "pacman -R \"${pkgremove[@]}\""
}
kernel_install() {
set +euo pipefail
outofdate="$(pacman -Qqu | tr '\n' ' ')"
set -euo pipefail
if [[ -n $outofdate ]]; then
fatal "The following packages are out of date, please update your system first: $outofdate"
fi
pkginstall=()
use_aur="n"
kernel=${1:-}
[[ $current_kname = $kernel ]] && fatal "You can't reinstall your current kernel. Please use 'pacman -Syu' instead to update."
[[ ${KERNEL_NAMES} != *"$kernel"* ]] && [[ ${KERNEL_AUR_NAMES} != *"$kernel"* ]] && fatal "Please enter a valid kernel name.\n$(kernel_available_list)"
[[ ${KERNEL_NAMES} != *"$kernel"* ]] && [[ ${KERNEL_AUR_NAMES} == *"$kernel"* ]] && use_aur="y"
if [[ "$use_aur" != "y" ]]; then
root_check
echo Installing "$kernel"...
pacman -Sq $kernel
else
if [[ $EUID == 0 ]]; then
fatal "When installing from AUR you need NOT to run this script as root."
fi
echo Installing "$kernel" from AUR...
$AUR_HELPER -Sq $kernel
fi
# Once installed, let's try to rebuild Grub configuration
if [ -f /usr/bin/grub-mkconfig ]; then
echo "Rebuilding grub.cfg..."
sudo grub-mkconfig -o /boot/grub/grub.cfg
fi
}
check_aur_helper() {
AUR_HELPER=""
for helper in $AUR_HELPERS; do
if [[ "$(pacman -Qsq $helper)" != "" ]]; then
AUR_HELPER=$helper
return 0
fi
done
fatal "You need to install one of these AUR helpers ($AUR_HELPERS)"
}
###############################################################################
IFS=. read -r major minor _ <<< "$(uname -r)"
current_kname=$(uname -s)
current="$current_kname $major.$minor"
# convert string to lower case
current_kname=$(echo $current_kname | tr '[:upper:]' '[:lower:]')
#[[ $(uname -r) == *rt* ]] && [[ $current == $rt_base ]] && current=linux-rt-manjaro
#[[ $(uname -r) == *rt* ]] && [[ $current == $rt_lts_base ]] && current=linux-rt-lts-manjaro
check_aur_helper
for param in $@; do
if [[ "$param" == "--no-color" ]] || [[ "$param" == "-n" ]]; then
CSTART=""
CEND=""
fi
done
AUR_ENABLED="n"
for param in $@; do
if [[ "$param" == "--aur" ]] || [[ "$param" == "-a" ]]; then
AUR_ENABLED="y"
printf "Will use ${CSTART}$AUR_HELPER${CEND} as AUR helper if needed.\n"
fi
done
data_files_check
load_kernel_names
while :; do
case ${1:-"-h"} in
-a|--aur) shift ;;
-n|--no-color) shift ;;
-h|-\?|--help) usage
exit 0 ;;
-i|--install) if [ "${2:-}" ]; then
shift
kernel_install $1
exit 0
else
fatal "Invalid arguments (use -h for help)."
fi ;;
-l|--list-all) kernel_available_list
exit 0 ;;
-li|--list-installed) kernel_installed_list
exit 0 ;;
-r|--remove) if [ "${2:-}" ]; then
root_check
shift
kernel_remove $1
exit 0
else
fatal "Invalid arguments (use -h for help)."
fi ;;
--) shift
break ;;
*) echo "Invalid argument '${1:-}'."
usage
exit 0 ;;
esac
done