-
Notifications
You must be signed in to change notification settings - Fork 18
/
KernelUpdateScriptGenerator
336 lines (334 loc) · 12 KB
/
KernelUpdateScriptGenerator
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/php
<?php
// This is a client side php script, as such it needs the php5-cli or php7.0-cli package to work
// This script generates a bash script
$NO_RC=false;
$NO_Header=false;
$version=false;
$r=false;
$k=false;
$l=false;
error_reporting(0);
foreach($argv as $key => $val){
if($val=='-no-rc'||$val=='--no-rc')
$NO_RC=true;
else if($val=='-nh'||$val=='--no-header')
$NO_Header=true;
else if($val=='-v'||$val=='--version'){
if(!isset($argv[$key+1]))
break;
if(strlen($argv[$key+1])==0)
continue;
$version=$argv[$key+1];
if(!is_numeric(substr($version,0,1)))
die("$version is not a version number\n");
}
else if($val=='-r'||$val=='--release'){
if(!isset($argv[$key+1]))
break;
if(strlen($argv[$key+1])==0)
continue;
$r=$argv[$key+1];
}
else if($val=='-ar'||$val=='--any-release'){
$r='*';
}
else if($val=='-k'||$val=='-f'||$val=='--force'){
$k='1.0.0-1-generic';// Very low fake kernel version to ensure new kernel is found
}
else if ($val=='--debug'){
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
}
else if($val=='-l'||$val=='--lowlatency'||$val=='--low-latency'){
$l=true;
}
else if($val=='-h'||$val=='--help'){
$script=strrpos($_SERVER["SCRIPT_NAME"],'/');
if(!is_bool($script))
$script=substr($_SERVER["SCRIPT_NAME"],$script+1);
else
$script=$_SERVER["SCRIPT_NAME"];
echo "Options:\n\t-no-rc, --no-rc\n\t\tSets the script to ignore Release Candidate versions\n";
echo "\t-f, --force\n\t\tTells the script to generate a installer regardless of the current running kernel\n";
echo "\t-r, --release\n\t\tOverrides the release version\n".
"\t\t\teg: `".$script." -r raring`\n".
"\t\tWould mean to a only kernels for Raring even if you are using Quantal\n".
"\t\t\teg: `".$script." -r \\*`\n".
"\t\tWould mean any release, the asterisk needs to be escaped (backslash or single quotes)\n".
"\t\tThis option defaults to the system's release codename, ".substr(shell_exec('lsb_release -sc'),0,-1)."\n";
echo "\t-ar, --any-release\n\t\tTells the script to accept kernels for any release series, same as -r \\*\n";
echo "\t-v, --version\n\t\tIs used to specify version series\n".
"\t\t\teg: `".$script." -v 3.8`\n".
"\t\tWould mean to a only accept kernels starting with 3.8 in the version number\n";
echo "\t-l, --low-latency\n\t\tTells the script to use low-latency kernels instead of generic (experimental)\n";
echo "\t-nh, --no-header\n\t\tTells the script to not download the linux header packages (experimental)\n";
echo "\t-h, --help\n\t\tShow this documentation of command options";
die("\n");
}
}
if(is_bool($r))
$r=substr(shell_exec('lsb_release -sc'),0,-1);
if(is_bool($k))
$k=substr(shell_exec("uname -r"),0,-1);
echo "#!/bin/bash\n\n";
echo "echo 'Config Notes:'\n";
echo "echo -e '\\t".(!$NO_RC?"Accept":"Reject")."ing Release Candidates'\n";
echo "echo -e '\\tAccepting Latest".(is_bool($version)?'':' '.$version)." Kernel'\n";
echo "echo -e '\\tAccepting kernels compiled for $r'\n";
echo "echo -e '\\tAccepting kernels with a version higher than $k'\n";
echo "echo -e '\\tAccepting ".($l?'lowlatency':'generic')." kernels'\n";
echo "echo -e '\\t".($NO_Header?'Excluding':'Including')." kernel headers'\n";
$url="https://kernel.ubuntu.com/~kernel-ppa/mainline/";
$html=file_get_contents($url);
for($i=1;$i<21;$i=$i+1){
$html=file_get_contents($url);
if(strlen($html)==0)
shell_exec("notify-send --icon error 'Kernel Update Checker' 'Unable to connect to kernel.ubuntu.com\\nAttempt $i/20'");
else
break;
shell_exec("sleep 11");
}
if(strlen($html)==0)
die("echo 'No Internet Access'\nexit\n");
$DOM=new DOMDocument;
$DOM->loadHTML($html);
$links=$DOM->getElementsByTagName('a');
$arr=array();
$arr2=array();
$k=explode('-',$k);
if(substr($k[0],-2)=='.0')
$k[0]=substr($k[0],0,-2);
if(!is_bool(strpos($k[1],'rc')))
$k=$k[0].'-'.substr($k[1],strpos($k[1],'rc'));
else
$k=$k[0];
$dir='';
for ($i = 5; $i < $links->length; $i++){// 5 is the 1st file in directory
$name=substr($links->item($i)->nodeValue,0,-1);
if(substr($name,0,1)!='v')// Not a kernel version
continue;
$name=substr($name,1);
if(strpos($name,$r)>0||$r=='*'){
if(($NO_RC && strpos($name,'-rc')>0)||(!is_bool($version) && substr($name,0,strlen($version))!=$version))
continue;
if(strrpos($name,'-')===false)
$name2=$name;
else{
$name2=strrpos($name,'-');
$name2=substr($name,$name2);
if(substr($name2,0,3)=="-rc")
$name2=$name;
else
$name2=substr($name,0,$r=='*'?strrpos($name,'-'):strpos($name,"-$r"));
}
if(version_compare($name2,$k)==1){
$k=$name2;
array_push($arr,$name2);
array_push($arr2,'v'.$name.'/');
}
}
}
if(count($arr)>0){
$k=$arr[count($arr)-1];
shell_exec("notify-send --icon info 'Update Available' 'Kernel $k is available.'");
$url=substr($url.$arr2[count($arr2)-1],0,-1)."/";
if(version_compare($k,'5.7.2')>-1){
$arch=trim(shell_exec('dpkg --print-architecture'));
$url.="$arch/";
}
$html=@file_get_contents($url);
if(!$html){
echo "echo 'Fatal Error: Failed to load content from $url'";
shell_exec("notify-send --icon info 'Fatal Error' 'Failed to load content from url'\n");
die("exit\n");
}
$DOM=new DOMDocument;
$DOM->loadHTML($html);
$links=$DOM->getElementsByTagName('table')->item(0)->getElementsByTagName('a');
for ($i = 5; $i < $links->length; $i++){// 5 is the 1st file in directory
$name=$links->item($i)->nodeValue;
if(strpos($name,'.deb')>0)
break;
}
$name=substr($name,strpos($name,'_')+1,-1*strlen(substr($name,strrpos($name,'_'))));
$a=substr($name,0,strrpos($name,'.'));
$b=substr($name,strrpos($name,'.')+1);
}
else
die("exit\n");
echo "\nWHERE=\"$url\"\n";
echo "VER_A=\"$a\"\n";
echo "VER_B=\"$b\"\n";
echo "VER_C=\"".str_replace(Array('-generic','-lowlatency'),'',substr(shell_exec("uname -r"),0,-1))."\"\n";
echo "NoHeader=".($NO_Header?1:0)."\n\n";
?>
echo -e '\nInformation:'
echo -e "\tOrigin: \n\t\t$WHERE\n\tKernel Version:\n\t\t$VER_A"
echo -e "\tRelease Date:\n\t\t${VER_B:0:4}/${VER_B:4:2}/${VER_B:6:2} @ ${VER_B:8:2}:${VER_B:10:2} (YYYY/MM/DD @ HH:MM)"
echo -e "\tInstall Linux Header Package:\n\t\t<?php echo $NO_Header?'No':'Yes'; ?>"
echo -e "Notes:"
echo -e "\tKernels built as of July 11, 2016 do not have a target release,\n\t\tplease use the --any-release option to get the latest kernel"
if [ "$1" == "--uninstall" ];then
if [ "$VER_A" == "$VER_C" ];then
echo -e "WARNING:\n\tYou are about to un-install the current running kernel,\n\tif you don't have a replacement kernel installed,\n\tyou will not be able to boot."
fi
if [ $(dpkg-query -l "linux-image-extra-$VER_A-generic" > /dev/null 2>&1;echo $?) -eq 0 ];then
sudo apt-get purge linux-{image-extra,headers,image,image-unsigned,modules}-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?> linux-headers-$VER_A
else
sudo apt-get purge linux-{headers,image,image-unsigned,modules}-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?> linux-headers-$VER_A
fi
exit
elif [ "$1" == "--silent" ];then
auto=1
else
auto=0
fi
if [ "$1" == "--download-only" ];then
download=1
else
download=0
fi
if [ -n "$2" ];then
if [ "$2" == "--download-only" ];then
download=1
elif [ "$2" == "--silent" ];then
auto=1
fi
fi
LOC="/tmp/kernel-$VER_A"
if [ -d "$LOC" ];then
clear
echo -e "Warning: This script has been run already\nFiles are in $LOC\nDelete existing downloads? (y=Yes, n=No) (y)"
if [ $auto -eq 0 ];then
read action
else
action="n"
fi
if [ ! "$action" == "n" ];then
rm $LOC/* > /dev/null
fi
else
mkdir $LOC
fi
cd $LOC
arch=$(dpkg --print-architecture)
echo "Care to look at the change log? (y=Yes, n=No) (n)"
if [ $auto -eq 0 ];then
read action
else
action="n"
fi
if [ "$action" == "y" ];then
curl $WHERE/CHANGES 2> /dev/null | less
echo "Now that you read the change log, do you want to upgrade (y=Yes, n=No) (y)"
read action
if [ "$action" == "n" ];then
exit
fi
fi
# If I don't need the header package do I need the image-extra package?
# amd64 kernels are signed as of 4.17-rc2, 201804152230 is the dt for 4.17-rc1
if [[ "$arch" == "amd64" && ($VER_B -gt 201804152230) ]]; then
image="image-unsigned"
else
image="image"
fi
echo "A 404 error is not a problem here"
if [ $NoHeader -eq 1 ];then
wget -nc $WHERE/linux-{image-extra,$image,modules}-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?>_$VER_A."$VER_B"_$arch.deb
else
wget -nc $WHERE/linux-headers-$VER_A"_$VER_A"."$VER_B"_all.deb
wget -nc $WHERE/linux-{image-extra,headers,$image,modules}-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?>_$VER_A."$VER_B"_$arch.deb
fi
clear
total=$(ls linux-*.deb | wc -l)
if [ $NoHeader -eq 1 ];then
total=$(($total+2))
else
# If anyone has a better way please suggest it in a bug report
libssl="$(dpkg -I ./linux-headers-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?>_$VER_A."$VER_B"_$arch.deb | grep '^ Depends:' | cut -d':' -f2| sed 's/,/\n/g' | grep libssl | awk '{print $1}')"
if [ -n "$libssl" ];then
# libssl is required
if [ $(dpkg-query -l "$libssl" > /dev/null 2>&1;echo $?) -eq 1 ];then
# libssl is not installed
if [ $(apt-cache show $libssl 2&> /dev/null;echo $?) -eq 0 ];then
# Package is available for installation
if [ $download -eq 0 ];then
# Install libssl
if [ $auto -eq 1 ];then
sudo apt-get install $libssl -y
else
sudo apt-get install $libssl
fi
else
# Download only
apt-get download $libssl
fi
else
echo "Warning: $libssl is required but has no installation candidate"
echo -e "\e[41mLinux Headers will NOT be installed!!!\e[0m"
echo "The linux headers packages is only needed for a few things like nvidia drivers and virtualbox-dkms"
echo "If you do need that you may want to use the --no-header option"
if [[ ( $download -eq 0 ) && ( $auto -eq 0 ) ]];then
echo -e "\n\nAbort installation? (y=Yes, n=No) (y)"
read action
if [ "$action" != "n" ];then
exit 1
fi
fi
if [ $download -eq 0 ];then
rm ./linux-headers-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?>_$VER_A."$VER_B"_$arch.deb
rm ./linux-headers-$VER_A"_$VER_A"."$VER_B"_all.deb
fi
fi
fi
fi
fi
if [ $VER_B -gt 201806032309 ];then
total=$(($total-1))
fi
if [[ $total -eq 3 && ( $VER_B -gt 201304080035 || $VER_B -lt 201206041411 ) ]];then
# Don't feel like inverting the above line and merging it with the line that is on this line plus two, so enjoy my sense of humor
echo -e "Greetings $USER\n I ($(hostname)) am now self aware and a sentient being, have a nice day.\n Oh and by the way SKYNET is now active.\n"
elif [ $total -ne 4 ];then
echo -e "Failed to download all needed files\nFiles are in $LOC\nGet files from $WHERE\n\nThere should be 4 files, 3 ending with '$arch.deb' and 1 with 'all.deb'\nHere is what we have:"
ls $LOC
echo -e "\nYou can install what you have by running this command:\n\tsudo dpkg -i $LOC/*.deb\nJust be prepared for broken packages."
exit 1
fi
if [ $download -eq 1 ];then
echo -e "Packages have been downloaded to $LOC\nHere is a list:"
ls $LOC
exit 0
fi
echo "Installing Linux $VER_A:"
sudo dpkg -i ./*.deb
if [ -d /lib/modules/$VER_A-<?php echo ($l?'lowlatency':'generic'); ?> ];then
echo -e "\nThe New Kernel looks to have been installed"
if [ $auto -eq 0 ] && [ "$VER_A" != "$VER_C" ];then
echo -e "\nWARNING: If the new kernel does not boot you may regret saying yes here.\nWould you like to remove the current one? (y=Yes, n=No) (n)"
read action
else
action="n"
fi
if [ "$action" == "y" ];then
sudo apt-get purge linux-{image-extra,headers,$image}-$VER_C-<?php echo ($l?'lowlatency':'generic'); ?> linux-headers-$VER_C
if [ $(dpkg-query -l "linux-image-extra-$VER_C-<?php echo ($l?'lowlatency':'generic'); ?>" > /dev/null 2>&1;echo $?) -eq 0 ];then
sudo apt-get purge linux-{image-extra,headers,$image}-$VER_A-<?php echo ($l?'lowlatency':'generic'); ?> linux-headers-$VER_C
else
sudo apt-get purge linux-{headers,$image}-$VER_C-<?php echo ($l?'lowlatency':'generic'); ?> linux-headers-$VER_C
fi
fi
else
echo -e "Looks like the kernel was not installed for some reason\nI don't see it in /lib/modules"
fi
echo -e "\n\nAre you ready to Reboot? (y=Yes, n=No) (n)"
if [ $auto -eq 0 ];then
read action
else
action="n"
fi
if [ "$action" == "y" ];then
sudo reboot
fi
exit