Advertisement
Guest User

kernelscript

a guest
Apr 21st, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo "# script PARA USUARIOS DO UBUNTU 11.10 baixa e compila kernel tudo automaticamente CRIADO PELOS USUÁRIOS DO FORUM UBUNTU BR:
  4. # http://ubuntuforum-br.org/index.php/topic,29799.3960.html
  5. #
  6. #
  7. # Com ele você compila o kernel, independente da versão (caso ela esteja disponível)
  8. # Escolhe os patchs do con kolivas
  9. # Adiciona os patchs da equipe do ubuntu
  10. # Altera as flags do compilador para melhor performance
  11. #
  12. ################################################################################################";
  13.  
  14.  
  15. # @Todo:
  16. #   * Verificar a existencia do arquivo de configuração do compilador e carregar as opções
  17. #   * Se ele não configurou alguma opção no arquivo de configuração, mostre um erro ou peça para ele configurar
  18. #       * Verificar a quantidade de patchs do con colivas para aquele kernel existente e exibir ao usuário (verifica se existe patchs como o bfs e pergunta se que usá-lo)
  19. #       * Dar a opção de usar um patch alternativo (passando o link do .patch)
  20. #       * Perguntar se deseja rodar o make menuconfig
  21. #
  22.  
  23.  
  24. #############
  25. # Funções
  26. #############
  27.  
  28. ###### Úteis
  29. ## Validaçao Sim/não
  30. function valida {
  31.     read opcao
  32.     if [ "$opcao" == "s" ]; then
  33.         return 0
  34.     else
  35.         return 1
  36.     fi
  37. }
  38.  
  39. ###### Sistema
  40. ## Função pra ler as configurações, caso exista um arquivo, leia-o e use suas configurações
  41. ## Se não existir, coloque algumas opções por padrão.
  42.  
  43. function init {
  44.     #Essa função inicia o sistema, começamos verificando se ele tem o arquivo de configuração, se tiver entendesse que deve prosseguir a configuração
  45.     if [ -d "config.json" ]; then
  46.         #Nada implementado ainda
  47.                 #confirmar
  48.                 echo ".."
  49.     else
  50.         #Se ela não existe usa essas aí:
  51.         kernel=3.3
  52.         patchkernel=3.3.2
  53.         ubuntu_patchs=true
  54. #       ck_patchs=true
  55. #       ck_patchs_bfs=true
  56. #       ck_patchs_dirty=true
  57. #       ck_patchs_1000mhz=true
  58.         config_file=$(locate /boot/config-* | sort -r | head -1)
  59.                 localmodcofig=true
  60.                 use_cflags=false
  61.                 arquivos=( "arch/x86/boot/compressed/Makefile" "arch/x86/boot/Makefile" "arch/x86/kernel/acpi/realmode/Makefile" "arch/x86/Kconfig.cpu" "arch/x86/Makefile" "arch/x86/Makefile_32.cpu")
  62.         #arquitetura: amd64 ou i386
  63.         arqt=$(dpkg --print-architecture)
  64.                 init_config
  65.     fi
  66.         confirmar
  67. }
  68.  
  69. #Funções de configuração
  70. #Init_config();
  71.  
  72. function init_config {
  73.     kernel
  74.     kernelpatch
  75.     if check_avancadas
  76.     then
  77.         patchs_ubuntu
  78.         patchs_ck
  79.         check_config
  80.         cflags
  81.         #patch_alternativo
  82.     fi
  83. }
  84.  
  85. #kernel
  86. function kernel {
  87.     #versão do kernel a ser compilado
  88.     echo "Digite a versão do kernel a ser compilada (versão disponivel: 3.3)"
  89.     read kernel
  90.     if ! [[ "$kernel" =~ ^[0-9]+([.][0-9]+)?$ ]] ;  then
  91.        echo "Erro: Versão inválida. Digite apenas pontos e números. Não digite a versão inteira, ao invés de 3.2.3, digite apenas 3.2";
  92.        kernel
  93.     fi
  94. }
  95.  
  96. #Kernelpatch
  97. function kernelpatch {
  98.     #patch de atualização
  99.     echo "Digite o patch de atualização desejado para a versão (ex: $kernel.2 )"
  100.     read patchkernel
  101.     if ! [[ "$patchkernel" =~ $kernel([.][0-9]+)?$ ]] ;  then
  102.        echo "Erro: Versão inválida. Digite apenas pontos e números. A versão do patch deve equivaler a versão do kernel escolhida.";
  103.        kernelpatch
  104.     fi
  105. }
  106.  
  107. #nome da versão
  108. function vsname {
  109.     #versão do kernel a ser compilado
  110.     echo "Digite o apelido da versão do ubuntu usado (versão disponivel: precise)"
  111.     read vsname
  112.     if ! [[ "$vsname" =~ ^[0-9]+([.][0-9]+)?$ ]] ;  then
  113.        echo "Erro: Nome inválido.";
  114.        vsname
  115.     fi
  116. }
  117.  
  118.  
  119. #check_avancadas
  120. function check_avancadas {
  121.     #patch ck
  122.     echo "Você deseja ver as opções avançadas de configuração? [s/n]"
  123.     if valida
  124.     then
  125.         return 0
  126.     else
  127.         return 1
  128.     fi
  129. }
  130.  
  131. ##Avançadas
  132. #Patchs do ubuntu
  133. function patchs_ubuntu {
  134.     echo "Deseja usar os patchs da equipe do Ubuntu? [s/n]"
  135.     if valida
  136.     then
  137.         ubuntu_patchs=true
  138.     else
  139.         ubuntu_patchs=false
  140.     fi
  141. }
  142.  
  143. #ck patchs
  144. function patchs_ck {
  145.     echo "Deseja usar os patchs do Con Kolivas? [s/n]"
  146.     if valida
  147.     then
  148.         ck_patchs=true
  149.         #config_ck_patchs
  150.                 #Desativado, não fiz ainda as funções que verificam quais
  151.                     #patchs especificos o usuário quer
  152.     fi
  153. }
  154.  
  155. #cflags
  156. function cflags {
  157.     echo "Deseja alterar as flags do C? [s/n]"
  158.     if valida
  159.     then
  160.         use_cflags=true
  161.     fi 
  162. }
  163.  
  164. #Arquivo .config
  165. function check_config {
  166.         echo "Escolha o .config que deseja usar"
  167.     echo "[1] Nenhum"
  168.     echo "[2] .config existente na pasta /boot"
  169.     echo "[3] .config do liquorix"
  170.     echo "[4] Desejo especificar o caminho de um .config"
  171.     echo "Qual .config você deseja usar? [1-4]:"
  172.     read opt
  173.     case $opt in
  174.     1)
  175.         echo 'Nenhum .config será usando'
  176.         config_file=false
  177.         ;;
  178.     2)
  179.         echo 'O script irá procurar o .config na pasta /boot'
  180.             config_file=$(locate /boot/config-* | sort -r | head -1)
  181.         ;;
  182.     3)
  183.         echo 'O script irá baixar o .config do site do liquorix'
  184.         config_file="/tmp/kernel-script-config"
  185.         wget -O $config_file "http://liquorix.net/sources/$kernel/config.$arqt"
  186.         ;;
  187.     4)
  188.         echo 'Especifique o caminho:'
  189.         read config_file
  190.         if ! [ -d $config_file ]; then
  191.             echo "Opps, não encontrei o .config no caminho especificado"
  192.         fi
  193.         check_config
  194.         ;;
  195.     *)
  196.         echo 'Opção inválida'
  197.         check_config
  198.         ;;
  199.     esac
  200.         echo "Deseja usar localmodconfig? (Use com muito cuidado) [s/n]"
  201.         if valida
  202.         then
  203.         echo 'localmodconfig será usado'
  204.         localmodcofig=true
  205.         fi
  206. }
  207.  
  208. #function config_ck_patchs {
  209.     #Essa função acessa a página do kolivas e verifica se ele tem patchs pro
  210.     #kernel escolhido, lista eles e pergunta quais quer usar
  211. #}
  212.  
  213. function confirmar {
  214.     echo "Você irá compilar o kernel  $kernel com o patch $kernelpatch"
  215.     [[ $ubuntu_patchs = true ]] && usepatch="" || usepatch="não "
  216.     echo "Você $usepatchirá usar os patchs dos desenvolvedores do ubuntu"
  217.     [[ $ck_patchs = true ]] && usepatch="" || usepatch="não "
  218.     echo "Você $usepatchusará os patchs do con kolivas"
  219.     [[ $use_cflags = true ]] && usepatch="" || usepatch="não "
  220.     echo "Você $usepatchirá alterar as cflags do computador"
  221.     echo "Você irá usar o seguinte arquivo de configuração: $config_file"
  222.     [[ $localmodcofig = true ]] && usepatch="" || usepatch="não "
  223.     echo "Você $usepatchirá usar o localmodconfig"
  224.     echo "Verifique se os dados estão corretos"
  225.     echo "Digite [P para Prosseguir, C para Corrigir ou S para Sair"
  226.     read opt
  227.     case $opt in
  228.         p)
  229.             compilar
  230.             ;;
  231.         c)
  232.             init
  233.             ;;
  234.         s)
  235.             exit 1
  236.             ;;
  237.         *)
  238.             echo "Opção inválida"
  239.             confirmar
  240.     esac
  241. }
  242.  
  243. function cflags {
  244.     sed "s/HOSTCFLAGS/HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -0fast -march=native -pipe \n#HOSTCFLAGS/g" Makefile > Makefile2
  245.     mv Makefile2 Makefile
  246.  
  247.     #Aqui eu faço um loop e altero os arquivos, removendo o -march=() por -march=native
  248.     for i in "${arquivos[@]}"
  249.     do
  250.             echo "Aplicando patch no arquivo:"$i
  251.             sed -e 's/-march=\([A-Z0-9a-z]\+\)/-march=native -Ofast/g'  $i > "$i.file_changed"
  252.             mv "$i.file_changed" $i
  253.     done
  254. }
  255.  
  256. function compilar {
  257.  
  258.     if use_cflags
  259.     then
  260.         cflags
  261.     fi
  262.  
  263.     patchck=$kernel-ck1
  264.  
  265.     #sufixo
  266.     ckk=ck1
  267.  
  268.     #CL=CONCURRENCY_LEVEL do processador
  269.     CL=$(grep -c processor /proc/cpuinfo)
  270.     ##################################
  271.  
  272.     sudo apt-get update
  273.     sudo apt-get install kernel-package gcc-4.6 libncurses5 libncurses5-dev build-essential patch -y
  274.  
  275.  
  276.     cd /usr/src
  277.     wget -c http://www.kernel.org/pub/linux/kernel/v3.x/linux-$kernel.tar.bz2
  278.  
  279.  
  280.     sudo tar -jxpvf /usr/src/linux-$kernel.tar.bz2
  281.     sudo mv /usr/src/linux-$kernel /usr/src/linux-$patchkernel-$ckk
  282.  
  283.     wget -c http://www.kernel.org/pub/linux/kernel/v3.x/patch-$patchkernel.bz2
  284.  
  285.     #ubuntu patch
  286.     wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$vsname/0001-base-packaging.patch
  287.     wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$vsname/0002-debian-changelog.patch
  288.     wget -c http://kernel.ubuntu.com/~kernel-ppa/mainline/v$patchkernel-$vsname/0003-default-configs.patch
  289.  
  290.     #ck
  291.     wget -c http://ck.kolivas.org/patches/3.0/$kernel/$patchck/patch-$patchck.bz2
  292.     sudo bunzip2 /usr/src/patch-$patchck.bz2
  293.  
  294.     sudo bunzip2 /usr/src/patch-$patchkernel.bz2
  295.     sudo bunzip2 /usr/src/patch-$patchck.bz2
  296.  
  297.     cd /usr/src/linux-$patchkernel-$ckk
  298.    
  299.     sudo patch -p1 < /usr/src/patch-$patchkernel
  300.     sudo patch -p1 < /usr/src/patch-$patchck    
  301.     sudo patch -p1 < /usr/src/0001-base-packaging.patch
  302.     sudo patch -p1 < /usr/src/0002-debian-changelog.patch
  303.     sudo patch -p1 < /usr/src/0003-default-configs.patch
  304.  
  305. #    if ck_patchs
  306. #    then
  307. #        cd /usr/src
  308. #        wget -c http://ck.kolivas.org/patches/3.0/$kernel/$patchck/patch-$patchck.bz2
  309. #        sudo bunzip2 /usr/src/patch-$patchck.bz2
  310. #        cd /usr/src/linux-$patchkernel-$ckk
  311. #        sudo patch -p1 < /usr/src/patch-$patchck
  312. #    fi
  313.  
  314.     sudo cp $config_file /usr/src/linux-$patchkernel-$ckk
  315.  
  316.     if localmodcofig
  317.     then
  318.         sudo make localmodconfig
  319.     fi
  320.     #########################
  321.  
  322.     #sudo gedit Makefile  
  323.  
  324.     sleep 3
  325.  
  326.     sudo make menuconfig
  327.  
  328.     sudo sed 's/CONFIG_RTS5139=m/# CONFIG_RTS5139 is not set/g' /usr/src/linux-$patchkernel-$ckk/.config > /usr/src/linux-$patchkernel-$ckk/cc && mv /usr/src/linux-$patchkernel-$ckk/cc /usr/src/linux-$patchkernel-$ckk/.config
  329.  
  330.  
  331.     sleep 3
  332.  
  333.  
  334. ######################################################
  335. ############ script do Stivekx #######################
  336.  
  337. #!/bin/bash
  338.  
  339. #Script para automatizar a mudança das flags na hora de compilar o kernel.
  340. #Mais informações sobre como compilar o kernel para seu processador:
  341. #http://ubuntuforum-br.org/index.php/topic,81718.0.html
  342.  
  343. #Modo de usar:
  344. #Após baixar o kernel, descompactar, criar um link simbólico para ele no /usr/src/linux:
  345. # cd /usr/src/linux
  346. # sudo su
  347. # chmod +x script.sh
  348. # ./script.sh
  349. #O script deve ser executado como root. Recomendo que use sudo su - e rode o script ao invés de sudo sh script.sh
  350.  
  351.  
  352. #Script criado por André Steinn
  353.  
  354. #Aqui é a lista de arquivos que ele vai dar replace no -march= por -march=native. Você pode mudar essa lista de arquivos passando a lista pela opção a e separados pro virgula
  355. #e.g.: ./script.sh -a arch/x86/boot/compressed/Makefile,arch/x86/boot/Makefile,arquivo1,arquivo2,arquivo3
  356.  
  357. arquivos=( "arch/x86/boot/compressed/Makefile" "arch/x86/boot/Makefile" "arch/x86/kernel/acpi/realmode/Makefile" "arch/x86/Kconfig.cpu" "arch/x86/Makefile" "arch/x86/Makefile_32.cpu")
  358.  
  359. #Aqui eu verifico se a pessoa passou algum argumento na hora de executar o script
  360. while getopts ":a:c" opt; do
  361.   case $opt in
  362.     a)
  363.       echo "Você setou uma lista de arquivos personalizada: $OPTARG" >&2
  364.     IFS=',' read -ra arquivos <<< "$OPTARG"
  365.       ;;
  366.     \?)
  367.       echo "Opção inválida: -$OPTARG" >&2
  368.       exit 1
  369.       ;;
  370.   esac
  371. done
  372.  
  373. #Aqui eu faço um loop e altero os arquivos, removendo o -march=() por -march=native
  374. for i in "${arquivos[@]}"
  375. do
  376.     echo "Path no arquivo:"$i
  377.     sed -e 's/-march=\([A-Z0-9a-z]\+\)/-march=native -Ofast/g'  $i > "$i.file_changed"
  378.     mv "$i.file_changed" $i
  379. done
  380.  
  381.  
  382.  
  383. ################################################
  384.  
  385. sleep 3
  386.  
  387.  
  388.  
  389. ####
  390.     #time sudo CONCURRENCY_LEVEL=2 make-kpkg --initrd kernel_image kernel_headers modules_image
  391.  
  392.     time sudo CONCURRENCY_LEVEL=$CL make-kpkg --initrd kernel_image kernel_headers modules_image
  393.  
  394.     cd /usr/src
  395.     sudo dpkg -i linux-image-$patchkernel-$ckk\_$patchkernel-$ckk-10.00.Custom_$arqt.deb linux-headers-$patchkernel-$ckk\_$patchkernel-$ckk-10.00.Custom_$arqt.deb
  396.  
  397.     sleep 3
  398.  
  399.     cd /lib/modules
  400.     sudo mkinitramfs -o /boot/initrd.img-$patchkernel-$ckk $patchkernel-$ckk
  401.     sudo update-grub
  402.  
  403.     sleep 5
  404.     echo 'tudo belezinha'
  405.     exit 0
  406. }
  407.  
  408. init
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement