Instalarea ultimului kernel Linux in Ubuntu/Mint

Configurare noua (How To)

Situatie

Dorim instalarea ultimei versiuni de kernel Linux in Ubuntu/Mint.

Cei de la Canonical distribuie pachete binare cu ultimele versiuni de Linux pentru a fi folosite in versiunile de Ubuntu ce vor fi lansate in viitor. La lansarea unei versiuni noi Ubuntu se foloseste un pachet mai vechi considerat mai stabil. Daca nu avem nevoie de stabilitate sau nu este necesara folosirea unui kernel cu suport disponibil mai multi ani si dorim sa avem mereu ultima versiune atunci cand se lanseaza, putem instala manual pachetele disponibile pe aceasta pagina.

Intotdeauna mi-a placut sa folosesc ultimele versiuni ale programelor, asa ca am creat un script ce ajuta utilizatorul sa instaleze in cativa pasi ultima versiune de kernel. Scriptul se foloseste de pachetele de pe pagina mentionata anterior, le descarca si le instaleaza automat. Tot ce trebuie sa faca utilizatorul este sa raspunda la cateva intrebari legate de tipul de kernel pe care doreste sa-l instaleze si versiunea acestuia.

Solutie

Pasi de urmat

Primul pas este sa copiam continutul scriptului intr-un fisier nou, sa salvam fisierul, sa-l facem executabil si sa-l rulam.

 

Screenshot from 2015-09-16 22:45:30

 

Deschidem editorul nostru de text preferat si salvam continutul de mai jos intr-un fisier pe nume kmd.sh:

vim kmd.sh

 

Scriptul:

#!/bin/bash
# © Stefanescu Cristian 2014
# This bash script checks the last kernel version available from Ubuntu mainline PPA
# and downloads the debs so you can install them easier.
# It's a stupid description and I'll probably change it in the near future.

# download and save the mainpage to a temporary location

ARCH=`uname -m`; if [[ $ARCH == "x86_64" ]]; then ARCH="amd64"; else ARCH="i386"; fi
URL="http://kernel.ubuntu.com/~kernel-ppa/mainline"
TEMP="/tmp/mainline.html"
STRIP="/tmp/mainline.strip"
SORTED="/tmp/mainline.sort"
STABLE="/tmp/mainline.stable"
RC="/tmp/mainline.rc"
KVPAGE="/tmp/kver.html"

echo "Downloading URL"
wget "$URL" -O "$TEMP" > /dev/null 2>&1
# get rid of html tags, print the first column and sort it in reverse
sed 's/<[^>]+>/ /g' -i "$TEMP"
awk '{print $1}' "$TEMP" > "$STRIP"
sort -rV "$STRIP" > "$SORTED"

# stable or rc?
# grep for rc if the user wants an rc version, exclude rc from searching if the user wants a stable version
# store the lines in an array, but no more then 10 kernel versions
# You can set the number of kernel versions in head -n10, but since we want the latest kernel, only 10 versions are fine
read -p "Do you want to install a stable version? [Y/n]
" kver
if [[ $kver == [Yy] || $kver == "" ]]; then
 grep -v "rc" "$SORTED" | grep -v "unstable" | head -n10 | awk '{for (f = 1; f <= NF; f++) { a[NR, f] = $f } } NF > nf { nf = NF } END { for (f = 1; f <= nf; f++) { for (r = 1; r <= NR; r++) { printf a[r, f] (r==NR ? RS : FS) } } }'> "$STABLE"
 OLD_IFS=$IFS
 IFS=$' '
 ver_arr=( $(cat "$STABLE") )
 IFS=$OLD_IFS
else
 grep "rc" "$SORTED" | grep "unstable" | head -n10 | awk '{for (f = 1; f <= NF; f++) { a[NR, f] = $f } } NF > nf { nf = NF } END { for (f = 1; f <= nf; f++) { for (r = 1; r <= NR; r++) { printf a[r, f] (r==NR ? RS : FS) } } }' > "$RC"
 OLD_IFS=$IFS
 IFS=$' '
 ver_arr=( $(cat "$RC") )
 IFS=$OLD_IFS
fi
# the following code doesn't depend on the previous answer, because we use the same array for each case (stable/rc)
# present the array in a friendly way so we can choose a number for a kernel version, if we don't want the latest kernel
echo "Select the version you want to install (Default is the latest, [0])"
for i in $(seq 0 $((${#ver_arr[@]} - 1))); do
 line="${ver_arr[$i]}"
 echo "[$i]. ${line}"
done

read -p "Enter the number correpsonding to the kernel version you want installed
" v
if [[ $v == "" ]]; then
 ver_id=$(echo ${ver_arr[0]} | awk '{ print $NF }')
else ver_id=$(echo ${ver_arr[$v]} | awk '{ print $NF }')
fi
# get the kernel's version page, so we can build the download links from the html
# save it as $KVPAGE
# grep through $KVPAGE for a generic or a lowlatency kernel
# grep for the headers
wget "$URL/$ver_id" -O "$KVPAGE" > /dev/null 2>&1

read -p "Do you want a lowlatency kernel? [N/y]
" latency
if [[ $latency == [Yy] ]]; then
 KERNEL=`grep -E "href.*image.*amd64.*latency.*deb" $KVPAGE | sed 's/<[^>]+>/ /g' | awk '{print $1}'`
else KERNEL=`grep -E "href.*image.*amd64.*generic.*deb" $KVPAGE | sed 's/<[^>]+>/ /g' | awk '{print $1}'`
fi

read -p "Download headers? [Y/n]
" headers
if [[ $headers == [Yy] || $headers == "" ]] && [[ $latency == [Yy] ]]; then
 HEADERS=`grep -E "href.*headers.*amd64.*latency.*deb" $KVPAGE | sed 's/<[^>]+>/ /g' | awk '{print $1}'`
else HEADERS=`grep -E "href.*headers.*amd64.*generic.*deb" $KVPAGE | sed 's/<[^>]+>/ /g' | awk '{print $1}'`
fi

echo "Downloading the files"
cd /tmp
wget -c "$URL"/"$ver_id"/"$KERNEL" --progress=bar:force 2>&1 | tail -f -n +6
if [[ $headers == [Yy] || $headers == "" ]]; then
 HEADERS_ALL=`grep -E "href.*headers.*all.*deb" $KVPAGE | sed 's/<[^>]+>/ /g' | awk '{print $1}'`
 wget -c "$URL"/"$ver_id"/"$HEADERS" --progress=bar:force 2>&1 | tail -f -n +6
 wget -c "$URL"/"$ver_id"/"$HEADERS_ALL" --progress=bar:force 2>&1 | tail -f -n +6
fi

read -p "Install the kernel? [Y/n]
" install
if [[ $install == [Yy] || $install == "" ]]; then 
 sudo dpkg -i $KERNEL
 if [[ $headers == [Yy] || $headers == "" ]]; then
 sudo dpkg -i $HEADERS_ALL $HEADERS
 fi
fi

read -p "Delete the *.deb files? [Y/n]
" deb
if [[ $deb == [Yy] || $deb == "" ]]; then
 rm -v $KERNEL
 if [[ -e $HEADERS ]]; then
 rm -v $HEADERS_ALL $HEADERS
 fi
fi

#cleanup
rm $TEMP $STRIP $SORTED $KVPAGE
if [[ -e "$STABLE" ]]; then rm $STABLE; else rm $RC; fi
unset ver_arr

Dupa salvare, facem fisierul executabil:

chmod u+x kmd.sh

Chmod are doua moduri de operare: cel clasic, in care se specifica permisiunile octal (chmod 644, chmod 755, chmod 777, chmod 400) si cel preferat de mine in care specificam ce permisie adaugam sau scoatem pentru user, group sau others. In cazul de mai sus am adaugat pentru user permisiunea de executie.

Aflati in aceeasi locatie in care am salvat fisierul, executam scriptul:

./kmd.sh

sau

bash kmd.sh

Raspundem la intrebari.

Screenshot from 2015-09-16 22:53:30

In principiu ar trebui doar sa apasam pe enter la fiecare intrebare (recomandat). Optiunile de raspuns sunt scrise intre paranteze patrate, iar optiunile ce vor fi selectate implicit cand apasam enter si nu tastam altceva sunt cele scrise cu litera mare in paranteze. Nu este nevoie sa tastam alt raspuns decat cel implicit daca nu dorim lucrul acesta.

Dupa descarcare, scriptul va intreaba daca doriti sa instalati pachetele. Vi se cere parola de root daca rulati scriptul ca utilizator obisnuit.

Screenshot from 2015-09-16 22:59:47

La sfarsit, scriptul va intreaba daca doriti sa stergeti fisierele descarcate. Fisierele le gasiti in /tmp in caz ca doriti sa le salvati. Eu am ales stergerea lor.

Screenshot from 2015-09-16 23:03:31

Ultimul pas este restartarea masinii si verificarea faptului ca rulam ultimul kernel:

uname -r

Tip solutie

Permanent

Plan de restaurare in caz de nefunctionare

Se sterg pachetele instalate cu ajutorul comenzii apt-get purge.

Voteaza

(3 out of 10 people found this article helpful)

Despre Autor

1 Comment

  1. Alex

    Eu cred ca v-ati complicat foarte tare. Update-ul de kernel se face din 2-3 comenzi foarte simple…se vede ca cine a scris nu foloseste Linux 🙂

Leave A Comment?