Uklanjanje starih verzija kernela

Vijest o ranjivosti Linux kernela potakla je mnoge da provjere koja im je verzija jezgre instalirana, kako bi je zamijenili novom, "bezgriješnom". Usput je prilika da se razmisli što sa starijim, ranjivim inačicama kernela. Najbolje ih je ukloniti. Osim toga, ako koristite zasebnu boot particiju, ne želite reskirati da vam se zapuni brojnim zastarjelim verzijama jezgre.

Najbrži način provjere koje su verzije kernela instaliarne jest ovaj:

$ dpkg -l | grep linux-
ii  linux-firmware                                        1.127.2                                             all          Firmware for Linux kernel drivers
ii  linux-generic                              3.13.0.29.35                          amd64        Complete Generic Linux kernel and headers
ii  linux-headers-3.13.0-24                    3.13.0-24.47                          all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-24-generic            3.13.0-24.47                          amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-26                    3.13.0-26.48                          all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-26-generic            3.13.0-26.48                          amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-27                    3.13.0-27.50                          all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-27-generic            3.13.0-27.50                          amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-29                    3.13.0-29.52                          all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-29-generic            3.13.0-29.52                          amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-generic                      3.13.0.29.35                          amd64        Generic Linux kernel headers
rc  linux-image-3.11.0-12-generic              3.11.0-12.19                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-13-generic              3.11.0-13.20                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-14-generic              3.11.0-14.21                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-15-generic              3.11.0-15.25                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-17-generic              3.11.0-17.31                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-18-generic              3.11.0-18.32                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-3.11.0-19-generic              3.11.0-19.33                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-3.11.0-20-generic              3.11.0-20.34                          amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-24-generic              3.13.0-24.47                          amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-26-generic              3.13.0-26.48                          amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-27-generic              3.13.0-27.50                          amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-29-generic              3.13.0-29.52                          amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-23-generic               3.8.0-23.34                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-25-generic               3.8.0-25.37                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-26-generic               3.8.0-26.38                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-27-generic               3.8.0-27.40                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-28-generic               3.8.0-28.41                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-29-generic               3.8.0-29.42                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-30-generic               3.8.0-30.44                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-3.8.0-31-generic               3.8.0-31.46                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
ii  linux-image-3.8.0-32-generic               3.8.0-32.47                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-12-generic        3.11.0-12.19                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-13-generic        3.11.0-13.20                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-14-generic        3.11.0-14.21                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-15-generic        3.11.0-15.25                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-17-generic        3.11.0-17.31                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-18-generic        3.11.0-18.32                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
rc  linux-image-extra-3.11.0-19-generic        3.11.0-19.33                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-extra-3.11.0-20-generic        3.11.0-20.34                          amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-24-generic        3.13.0-24.47                          amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-26-generic        3.13.0-26.48                          amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-27-generic        3.13.0-27.50                          amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-29-generic        3.13.0-29.52                          amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-23-generic         3.8.0-23.34                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-25-generic         3.8.0-25.37                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-26-generic         3.8.0-26.38                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-27-generic         3.8.0-27.40                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-28-generic         3.8.0-28.41                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-29-generic         3.8.0-29.42                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-30-generic         3.8.0-30.44                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
rc  linux-image-extra-3.8.0-31-generic         3.8.0-31.46                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
ii  linux-image-extra-3.8.0-32-generic         3.8.0-32.47                           amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
ii  linux-image-generic                        3.13.0.29.35                          amd64        Generic Linux kernel image
ii  linux-libc-dev:amd64                       3.13.0-29.52                          amd64        Linux Kernel Headers for development
ii  linux-sound-base                           1.0.25+dfsg-0ubuntu4                  all          base package for ALSA and OSS sound systems
ii  syslinux-common                            3:4.05+dfsg-6+deb8u1                  all          collection of boot loaders (common files)
ii  syslinux-legacy                        

Gornji ispis je lista paketa koji zadovoljavaju kriterij na mom Ubuntu Linuxu 14.04. Većina će se, baš kao i ja, iznenaditi količinom paketa koji sadrže jezgru Linuxa. Tu su verzije od 3.8 do 3.13. Ispred nekih piše rc, što bi značilo removed-configured, odnosno da je paket deinstaliran, ali nije uklonjena njegova konfiguracija. No još je uvijek i više nego dovoljno paketa sa statusom ii, uspješno instaliranih.

Kod ostalih paketa normalno je da novi paket zamijeni stari, ali ne i kod jezgre. Uobičajena je praksa da se stara verzija jezgre zadrži još neko vrijeme, tako da se sustav može u slučaju problema podići i sa starom verzijom, ako s novom neke stvari ne rade kako treba.

Nepotrebne i zastarjele verzije paketa uklanjaju se naredbom

$ sudo apt-get autoclean

No to ne vrijedi za jezgru. Stare verzije kernela morat ćete sami ukloniti. Iz gornjeg ispisa vidljivo je da nazivi paketa s jezgrom počinju s linux-, ali ih ima nekoliko grupa:

linux-header
linux-image
linux-image-extra
linux-image-generic

Filter je pokupio i neke pakte koji ne sadrže jezgru:

ii  linux-libc-dev:amd64                     3.13.0-29.52                          amd64        Linux Kernel Headers for development
ii  linux-sound-base                         1.0.25+dfsg-0ubuntu4                  all          base package for ALSA and OSS sound systems
ii  syslinux-common                          3:4.05+dfsg-6+deb8u1                  all          collection of boot loaders (common files)
ii  syslinux-legacy                          2:3.63+dfsg-2ubuntu5                  amd64        Bootloader for Linux/i386 using MS-DOS floppies

Cilj nam je automatizacija brisanja starih verzija jezgre, da ne moramo deinstalirati svaki paket pojedinačno. Pritom treba biti oprezan da ne bismo uklonili i nešto što je trebalo ostaviti. Na primjer, ne smijemo ukloniti zadnju verziju jezgre, zar ne? :)

Prije nego se bacimo na pisanje skripte, kao svaki lijeni sistemac potražit ćemo da li je netko već riješio taj problem. Čemu otkrivati toplu vodu, zar ne? I zaista, nije trebalo dugo tražiti:

http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-headers-images-and-modules/

Tu se nalazi ovakav one-liner:

$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Nećemo ga samo tako pokrenuti na živom sustavu, vjerujući na slijepo da će sve ispasti dobro. Hajdemo najprije malo analizirati što skripta radi.

Prvi dio je jasan, tražimo pakete čiji nazivi počinju s linux:

$ dpkg -l 'linux-*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                   Version                    Architecture                Description
+++-======================================-=========================-===========================-=============================================================
un  linux-doc-3.11.0                      <none>                      <none>                      (no description available)
un  linux-doc-3.13.0                      <none>                      <none>                      (no description available)
un  linux-doc-3.8.0                       <none>                      <none>                      (no description available)
ii  linux-firmware                        1.127.2                     all                         Firmware for Linux kernel drivers
ii  linux-generic                         3.13.0.29.35                amd64                       Complete Generic Linux kernel and headers
un  linux-headers                         <none>                      <none>                      (no description available)
un  linux-headers-3.0                     <none>                      <none>                      (no description available)
un  linux-headers-3.11.0-20-generic       <none>                      <none>                      (no description available)
ii  linux-headers-3.13.0-24               3.13.0-24.47                all                         Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-24-generic       3.13.0-24.47                amd64                       Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-26               3.13.0-26.48                all                         Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-26-generic       3.13.0-26.48                amd64                       Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
...

Ovaj je način nešto bolji od našeg prvog primjera s grepom, jer ostaje zaglavlje koje nam olakšava razumijevanje

I ovo je lako:

$ uname -r
3.13.0-29-generic

Naredba ispisuje izdanje (release) instalirane verzije jezgre, koju moramo ostaviti na miru. Zapamtimo ovu numeraciju verzije: 3.13.0-29

Slijedi teži dio. sed je stream editor, naredba koja koristi pravilne izraze (regular expressions). Stari uniksaši će samo nasmiješiti, ali novim generacijama je ovo što slijedi kriptično. Hajdemo redom, od lakšeg prema težem.

$ dpkg -l 'linux-*' | sed '/^ii/!d'
ii  linux-firmware                             1.127.2                                             all          Firmware for Linux kernel drivers
ii  linux-generic                              3.13.0.29.35                                        amd64        Complete Generic Linux kernel and headers
ii  linux-headers-3.13.0-24                    3.13.0-24.47                                        all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-24-generic            3.13.0-24.47                                        amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-26                    3.13.0-26.48                                        all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-26-generic            3.13.0-26.48                                        amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-27                    3.13.0-27.50                                        all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-27-generic            3.13.0-27.50                                        amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-3.13.0-29                    3.13.0-29.52                                        all          Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-29-generic            3.13.0-29.52                                        amd64        Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-generic                      3.13.0.29.35                                        amd64        Generic Linux kernel headers
ii  linux-image-3.11.0-20-generic              3.11.0-20.34                                        amd64        Linux kernel image for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-24-generic              3.13.0-24.47                                        amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-26-generic              3.13.0-26.48                                        amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-27-generic              3.13.0-27.50                                        amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-29-generic              3.13.0-29.52                                        amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.8.0-32-generic               3.8.0-32.47                                         amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
ii  linux-image-extra-3.11.0-20-generic        3.11.0-20.34                                        amd64        Linux kernel extra modules for version 3.11.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-24-generic        3.13.0-24.47                                        amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-26-generic        3.13.0-26.48                                        amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-27-generic        3.13.0-27.50                                        amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-29-generic        3.13.0-29.52                                        amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.8.0-32-generic         3.8.0-32.47                                         amd64        Linux kernel image for version 3.8.0 on 64 bit x86 SMP
ii  linux-image-generic                        3.13.0.29.35                                        amd64        Generic Linux kernel image
ii  linux-libc-dev:amd64                       3.13.0-29.52                                        amd64        Linux Kernel Headers for development
ii  linux-sound-base                           1.0.25+dfsg-0ubuntu4                                all          base package for ALSA and OSS sound systems

Ovakva naredba eliminira sve pakete koji nemaju status "ii" (!d - not delete). No tu još ima paketa koje treba ostaviti na miru, a treba se riješiti i svih stupaca koji ne sadrže ime paketa. Sve to eliminira ostatak skripte:

$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
linux-headers-3.13.0-24
linux-headers-3.13.0-24-generic
linux-headers-3.13.0-26
linux-headers-3.13.0-26-generic
linux-headers-3.13.0-27
linux-headers-3.13.0-27-generic
linux-image-3.11.0-20-generic
linux-image-3.13.0-24-generic
linux-image-3.13.0-26-generic
linux-image-3.13.0-27-generic
linux-image-3.8.0-32-generic
linux-image-extra-3.11.0-20-generic
linux-image-extra-3.13.0-24-generic
linux-image-extra-3.13.0-26-generic
linux-image-extra-3.13.0-27-generic
linux-image-extra-3.8.0-32-generic

Sad su ostali samo paketi koji sadrže jezgru i zaglavlja (potrebna za kompiliranje). Još provjerimo da li je izostavljena zadnja verzija paketa (3.13.0-29). Nema je! Sad bismo mogli pokrenuti cijelu naredbu.

No prije toga odlučio sam obaviti još jedan test. Umjesto sed '/^ii/!d;/ stavio sam sed '/^rc/!d;/. Najprije ćemo eliminirati zaostale konfiguracije paketa s kernelom koje sam već ranije deinstalirao, a usput i provjeriti kako skripta radi.

$ dpkg -l 'linux-*' | sed '/^rc/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Naredba xargs čita parametre sa standardnog inputa i prosljeđuje ih naredbi apt-get koja slijedi.

Uspjelo je, deinstalirani paketi kernela više se na pojavljuju na popisu paketa. Sada možemo napraviti backup korisničkih podataka, za slučaj da će trebati nanovo instalirati Linux (šalimo se, ali samo napola!).

I evo:

$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Uslijedit će cijeli niz radnji koje su zadane pri deinstalaciji paketa s jezgrom, što uključuje pokretanje naredbe depmod i regeneriranje konfiguracije boot loadera. Na kraju, stanje izgleda ovako:

$ dpkg -l 'linux-*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                          Version                     Architecture                Description
+++-=============================================-===========================-===========================-===============================================================================================
un  linux-doc-3.13.0                              <none>                      <none>                      (no description available)
ii  linux-firmware                                1.127.2                     all                         Firmware for Linux kernel drivers
ii  linux-generic                                 3.13.0.29.35                amd64                       Complete Generic Linux kernel and headers
un  linux-headers                                 <none>                      <none>                      (no description available)
un  linux-headers-3.0                             <none>                      <none>                      (no description available)
ii  linux-headers-3.13.0-29                       3.13.0-29.52                all                         Header files related to Linux kernel version 3.13.0
ii  linux-headers-3.13.0-29-generic               3.13.0-29.52                amd64                       Linux kernel headers for version 3.13.0 on 64 bit x86 SMP
ii  linux-headers-generic                         3.13.0.29.35                amd64                       Generic Linux kernel headers
un  linux-image                                   <none>                      <none>                      (no description available)
un  linux-image-3.0                               <none>                      <none>                      (no description available)
ii  linux-image-3.13.0-29-generic                 3.13.0-29.52                amd64                       Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-29-generic           3.13.0-29.52                amd64                       Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-generic                           3.13.0.29.35                amd64                       Generic Linux kernel image
un  linux-initramfs-tool                          <none>                      <none>                      (no description available)
un  linux-kernel-headers                          <none>                      <none>                      (no description available)
un  linux-kernel-log-daemon                       <none>                      <none>                      (no description available)
ii  linux-libc-dev:amd64                          3.13.0-29.52                amd64                       Linux Kernel Headers for development
un  linux-restricted-common                       <none>                      <none>                      (no description available)
ii  linux-sound-base                              1.0.25+dfsg-0ubuntu4        all                         base package for ALSA and OSS sound systems
un  linux-source-3.13.0                           <none>                      <none>                      (no description available)
un  linux-tools                                   <none>                      <none>                      (no description available)

Situacija nije tako čista kako smo očekivali... Neki su paketi u statusu un - unknown, not installed.

Konačni test bit će reboot. Srećom, on prolazi bez problema. Usput, zauzeće diska smanjilo se za gotovo gigabajt.

Vote: 
0
No votes yet