daily pastebin goal
16%
SHARE
TWEET

BBB_update-kernel.sh

osteth Apr 7th, 2016 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh -e
  2. #
  3. # Copyright (c) 2014-2015 Robert Nelson <robertcnelson@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22.  
  23. if ! id | grep -q root; then
  24.     echo "must be run as root"
  25.     exit
  26. fi
  27.  
  28. scan_ti_kernels () {
  29.     if [ "x${SOC}" = "x" ] ; then
  30.         unset testvalue
  31.         testvalue=$(echo ${current_kernel} | grep ti-xenomai || true)
  32.         if [ ! "x${testvalue}" = "x" ] ; then
  33.             SOC="ti-xenomai"
  34.         fi
  35.     fi
  36.  
  37.     if [ "x${SOC}" = "x" ] ; then
  38.         unset testvalue
  39.         testvalue=$(echo ${current_kernel} | grep ti-rt || true)
  40.         if [ ! "x${testvalue}" = "x" ] ; then
  41.             SOC="ti-rt"
  42.         fi
  43.     fi
  44.  
  45.     if [ "x${SOC}" = "x" ] ; then
  46.         unset testvalue
  47.         testvalue=$(echo ${current_kernel} | grep ti || true)
  48.         if [ ! "x${testvalue}" = "x" ] ; then
  49.             SOC="ti"
  50.         fi
  51.     fi
  52. }
  53.  
  54. scan_bone_kernels () {
  55.     if [ "x${SOC}" = "x" ] ; then
  56.         unset testvalue
  57.         testvalue=$(echo ${current_kernel} | grep bone-rt || true)
  58.         if [ ! "x${testvalue}" = "x" ] ; then
  59.             SOC="bone-rt"
  60.         fi
  61.     fi
  62.     if [ "x${SOC}" = "x" ] ; then
  63.         unset testvalue
  64.         testvalue=$(echo ${current_kernel} | grep bone || true)
  65.         if [ ! "x${testvalue}" = "x" ] ; then
  66.             SOC="omap-psp"
  67.         fi
  68.     fi
  69. }
  70.  
  71. scan_armv7_kernels () {
  72.     if [ "x${SOC}" = "x" ] ; then
  73.         unset testvalue
  74.         testvalue=$(echo ${current_kernel} | grep lpae || true)
  75.         if [ ! "x${testvalue}" = "x" ] ; then
  76.             SOC="armv7-lpae"
  77.         fi
  78.     fi
  79.     if [ "x${SOC}" = "x" ] ; then
  80.         unset testvalue
  81.         testvalue=$(echo ${current_kernel} | grep armv7-rt || true)
  82.         if [ ! "x${testvalue}" = "x" ] ; then
  83.             SOC="armv7-rt"
  84.         fi
  85.     fi
  86.     if [ "x${SOC}" = "x" ] ; then
  87.         unset testvalue
  88.         testvalue=$(echo ${current_kernel} | grep armv7 || true)
  89.         if [ ! "x${testvalue}" = "x" ] ; then
  90.             SOC="armv7"
  91.         fi
  92.     fi
  93. }
  94.  
  95. get_device () {
  96.     machine=$(cat /proc/device-tree/model | sed "s/ /_/g")
  97.  
  98.     if [ "x${SOC}" = "x" ] ; then
  99.         case "${machine}" in
  100.         TI_AM335x_BeagleBone|TI_AM335x_BeagleBone_Black|TI_AM335x_BeagleBone_Green)
  101.             scan_ti_kernels
  102.             scan_bone_kernels
  103.             scan_armv7_kernels
  104.             es8="enabled"
  105.             ;;
  106.         TI_AM5728_BeagleBoard-X15)
  107.             scan_ti_kernels
  108.             scan_armv7_kernels
  109.             ;;
  110.         TI_OMAP5_uEVM_board)
  111.             scan_ti_kernels
  112.             scan_armv7_kernels
  113.             ;;
  114.         *)
  115.             echo "Machine: [${machine}]"
  116.             SOC="armv7"
  117.             ;;
  118.         esac
  119.     fi
  120.  
  121.     unset es8
  122.     unset kernel_headers
  123.     case "${machine}" in
  124.     TI_AM335x_BeagleBone|TI_AM335x_BeagleBone_Black|TI_AM335x_BeagleBone_Green)
  125.         es8="enabled"
  126.         ;;
  127.     TI_AM5728_BeagleBoard-X15)
  128.         kernel_headers="enabled"
  129.         ;;
  130.     TI_OMAP5_uEVM_board)
  131.         kernel_headers="enabled"
  132.         ;;
  133.     esac
  134. }
  135.  
  136. update_uEnv_txt () {
  137.     if [ ! -f /etc/kernel/postinst.d/zz-uenv_txt ] ; then
  138.         if [ -f /boot/uEnv.txt ] ; then
  139.             older_kernel=$(grep uname_r /boot/uEnv.txt | awk -F"=" '{print $2}')
  140.             sed -i -e "s:uname_r=$older_kernel:uname_r=$latest_kernel:g" /boot/uEnv.txt
  141.             echo "info: /boot/uEnv.txt: `grep uname_r /boot/uEnv.txt`"
  142.             if [ ! "x${older_kernel}" = "x${latest_kernel}" ] ; then
  143.                 echo "info: [${latest_kernel}] now installed and will be used on the next reboot..."
  144.             fi
  145.         fi
  146.     fi
  147.  
  148.     if [ "x${daily_cron}" = "xenabled" ] ; then
  149.         touch /tmp/daily_cron.reboot
  150.     fi
  151. }
  152.  
  153. check_dpkg () {
  154.     echo "Checking dpkg..."
  155.     unset deb_pkgs
  156.     LC_ALL=C dpkg --list | awk '{print $2}' | grep "^${pkg}$" >/dev/null || deb_pkgs="${pkg}"
  157. }
  158.  
  159. check_apt_cache () {
  160.     echo "Checking apt-cache..."
  161.     unset apt_cache
  162.     apt_cache=$(LC_ALL=C apt-cache search "^${pkg}$" | awk '{print $1}' || true)
  163. }
  164.  
  165. latest_version_repo () {
  166.     if [ ! "x${SOC}" = "x" ] ; then
  167.         cd /tmp/
  168.         if [ -f /tmp/LATEST-${SOC} ] ; then
  169.             rm -f /tmp/LATEST-${SOC} || true
  170.         fi
  171.  
  172.         echo "info: checking archive"
  173.         wget --no-verbose ${mirror}/${dist}-${arch}/LATEST-${SOC}
  174.         if [ -f /tmp/LATEST-${SOC} ] ; then
  175.  
  176.             echo "-----------------------------"
  177.             echo "Kernel Options:"
  178.             cat /tmp/LATEST-${SOC}
  179.             echo "-----------------------------"
  180.  
  181.             latest_kernel=$(cat /tmp/LATEST-${SOC} | grep ${kernel} | awk '{print $3}')
  182.             echo "info: you are running: [${current_kernel}], latest is: [${latest_kernel}] updating..."
  183.             if [ "x${latest_kernel}" = "x" ] ; then
  184.                 exit
  185.             fi
  186.  
  187.             if [ "x${current_kernel}" = "x${latest_kernel}" ] ; then
  188.                 if [ "x${daily_cron}" = "xenabled" ] ; then
  189.                     apt-get clean
  190.                     exit
  191.                 fi
  192.             fi
  193.             apt-get update
  194.  
  195.             pkg="linux-image-${latest_kernel}"
  196.             #is the package installed?
  197.             check_dpkg
  198.             #is the package even available to apt?
  199.             check_apt_cache
  200.             if [ "x${deb_pkgs}" = "x${apt_cache}" ] ; then
  201.                 if [ "x${kernel_headers}" = "xenabled" ] ; then
  202.                     pkg="${pkg} linux-headers-${latest_kernel}"
  203.                 fi
  204.                 echo "debug: installing: [${pkg}]"
  205.                 apt-get install -y ${pkg}
  206.                 update_uEnv_txt
  207.             elif [ "x${pkg}" = "x${apt_cache}" ] ; then
  208.                 echo "debug: reinstalling: [${pkg}]"
  209.                 apt-get install -y ${pkg} --reinstall
  210.                 update_uEnv_txt
  211.             else
  212.                 echo "info: [${pkg}] (latest) is currently unavailable on [rcn-ee.com/repos]"
  213.             fi
  214.         fi
  215.     fi
  216. }
  217.  
  218. #Only for the original May 2014 image, everything should use the repo's now..
  219. latest_version () {
  220.     if [ ! "x${SOC}" = "x" ] ; then
  221.         cd /tmp/
  222.         if [ -f /tmp/LATEST-${SOC} ] ; then
  223.             rm -f /tmp/LATEST-${SOC} || true
  224.         fi
  225.  
  226.         echo "info: checking archive"
  227.         wget --no-verbose ${mirror}/${dist}-${arch}/LATEST-${SOC}
  228.         if [ -f /tmp/LATEST-${SOC} ] ; then
  229.             latest_kernel=$(cat /tmp/LATEST-${SOC} | grep ${kernel} | awk '{print $3}')
  230.             echo "info: you are running: [${current_kernel}], latest is: [${latest_kernel}] updating..."
  231.             if [ "x${latest_kernel}" = "x" ] ; then
  232.                 exit
  233.             fi
  234.  
  235.             if [ ! "x${current_kernel}" = "x${latest_kernel}" ] ; then
  236.                 distro=$(lsb_release -is)
  237.                 if [ "x${distro}" = "xDebian" ] ; then
  238.                     wget --no-verbose -c https://rcn-ee.com/repos/debian/pool/main/l/linux-upstream/linux-image-${latest_kernel}_1${dist}_${arch}.deb
  239.                 else
  240.                     wget --no-verbose -c https://rcn-ee.com/repos/ubuntu/pool/main/l/linux-upstream/linux-image-${latest_kernel}_1${dist}_${arch}.deb
  241.                 fi
  242.                 if [ -f /tmp/linux-image-${latest_kernel}_1${dist}_${arch}.deb ] ; then
  243.                     dpkg -i /tmp/linux-image-${latest_kernel}_1${dist}_${arch}.deb
  244.                     sync
  245.  
  246.                     if [ -f /boot/vmlinuz-${latest_kernel} ] ; then
  247.                         bootdir="/boot/uboot"
  248.  
  249.                         if [ -f ${bootdir}/zImage_bak ] ; then
  250.                             rm ${bootdir}/zImage_bak
  251.                             sync
  252.                         fi
  253.  
  254.                         if [ -f ${bootdir}/zImage ] ; then
  255.                             echo "Backing up ${bootdir}/zImage as ${bootdir}/zImage_bak..."
  256.                             mv -v ${bootdir}/zImage ${bootdir}/zImage_bak
  257.                             sync
  258.                         fi
  259.  
  260.                         if [ -f ${bootdir}/initrd.bak ] ; then
  261.                             rm ${bootdir}/initrd.bak
  262.                             sync
  263.                         fi
  264.  
  265.                         if [ -f ${bootdir}/initrd.img ] ; then
  266.                             echo "Backing up ${bootdir}/initrd.img as ${bootdir}/initrd.bak..."
  267.                             mv -v ${bootdir}/initrd.img ${bootdir}/initrd.bak
  268.                             sync
  269.                         fi
  270.  
  271.                         if [ -d ${bootdir}/dtbs_bak/ ] ; then
  272.                             rm -rf ${bootdir}/dtbs_bak/ || true
  273.                             sync
  274.                         fi
  275.  
  276.                         if [ -d ${bootdir}/dtbs/ ] ; then
  277.                             echo "Backing up ${bootdir}/dtbs/ as ${bootdir}/dtbs_bak/..."
  278.                             mv ${bootdir}/dtbs/ ${bootdir}/dtbs_bak/ || true
  279.                             sync
  280.                         fi
  281.  
  282.                         if [ -d /boot/dtbs/${latest_kernel}/ ] ; then
  283.                             mkdir -p ${bootdir}/dtbs/
  284.                             cp /boot/dtbs/${latest_kernel}/*.dtb ${bootdir}/dtbs/ 2>/dev/null || true
  285.                             sync
  286.                         fi
  287.  
  288.                         if [ ! -f /boot/initrd.img-${latest_kernel} ] ; then
  289.                             echo "Creating /boot/initrd.img-${latest_kernel}"
  290.                             update-initramfs -c -k ${latest_kernel}
  291.                             sync
  292.                         else
  293.                             echo "Updating /boot/initrd.img-${latest_kernel}"
  294.                             update-initramfs -u -k ${latest_kernel}
  295.                             sync
  296.                         fi
  297.  
  298.                         cp -v /boot/vmlinuz-${latest_kernel} ${bootdir}/zImage
  299.                         cp -v /boot/initrd.img-${latest_kernel} ${bootdir}/initrd.img
  300.                     fi
  301.                 fi
  302.             fi
  303.         fi
  304.     fi
  305. }
  306.  
  307. specific_version_repo () {
  308.     latest_kernel=$(echo ${kernel_version})
  309.     apt-get update
  310.  
  311.     pkg="linux-image-${latest_kernel}"
  312.     #is the package installed?
  313.     check_dpkg
  314.     #is the package even available to apt?
  315.     check_apt_cache
  316.     if [ "x${deb_pkgs}" = "x${apt_cache}" ] ; then
  317.         if [ "x${kernel_headers}" = "xenabled" ] ; then
  318.             pkg="${pkg} linux-headers-${latest_kernel}"
  319.         fi
  320.         apt-get install -y ${pkg}
  321.         update_uEnv_txt
  322.     elif [ "x${pkg}" = "x${apt_cache}" ] ; then
  323.         apt-get install -y ${pkg} --reinstall
  324.         update_uEnv_txt
  325.     else
  326.         echo "error: [${pkg}] unavailable"
  327.     fi
  328. }
  329.  
  330. third_party_final () {
  331.     depmod -a ${latest_kernel}
  332.     update-initramfs -uk ${latest_kernel}
  333. }
  334.  
  335. third_party () {
  336.     if [ "x${SOC}" = "xomap-psp" ] ; then
  337.         #3.8 only...
  338.         if [ "x${kernel}" = "xSTABLE" ] ; then
  339.             apt-get install -o Dpkg::Options::="--force-overwrite" -y mt7601u-modules-${latest_kernel} || true
  340.         fi
  341.         if [ ! "x${kernel}" = "xSTABLE" ] ; then
  342.             if [ "x${es8}" = "xenabled" ] ; then
  343.                 apt-get install -y ti-sgx-es8-modules-${latest_kernel} || true
  344.             fi
  345.         fi
  346.         third_party_final
  347.     fi
  348.  
  349.     if [ "x${SOC}" = "xti" ] || [ "x${SOC}" = "xti-rt" ] || [ "x${SOC}" = "xti-xenomai" ] ; then
  350.         #3.14 only...
  351.         if [ "x${kernel}" = "xSTABLE" ] ; then
  352.             apt-get install -o Dpkg::Options::="--force-overwrite" -y mt7601u-modules-${latest_kernel} || true
  353.         fi
  354.         if [ "x${es8}" = "xenabled" ] ; then
  355.             apt-get install -y ti-sgx-es8-modules-${latest_kernel} || true
  356.         fi
  357.         third_party_final
  358.     fi
  359. }
  360.  
  361. checkparm () {
  362.     if [ "$(echo $1|grep ^'\-')" ] ; then
  363.         echo "E: Need an argument"
  364.         exit
  365.     fi
  366. }
  367.  
  368. if [ ! -f /usr/bin/lsb_release ] ; then
  369.     echo "install lsb-release"
  370.     exit
  371. fi
  372.  
  373. dist=$(lsb_release -cs | sed 's/\//_/g')
  374. arch=$(dpkg --print-architecture)
  375. current_kernel=$(uname -r)
  376.  
  377. #Debian testing...
  378. if [ "x${dist}" = "xn_a" ] ; then
  379.     deb_lsb_rs=$(lsb_release -rs | awk '{print $1}' | sed 's/\//_/g')
  380.  
  381.     #Distributor ID:    Debian
  382.     #Description:   Debian GNU/Linux testing/unstable
  383.     #Release:   testing/unstable
  384.     #Codename:  n/a
  385.  
  386.     if [ "x${deb_lsb_rs}" = "xtesting_unstable" ] ; then
  387.         dist="stretch"
  388.     fi
  389. fi
  390.  
  391. kernel="STABLE"
  392. mirror="https://rcn-ee.com/repos/latest"
  393. unset kernel_version
  394. unset daily_cron
  395. # parse commandline options
  396. while [ ! -z "$1" ] ; do
  397.     case $1 in
  398.     --kernel)
  399.         checkparm $2
  400.         kernel_version="$2"
  401.         ;;
  402.     --daily-cron)
  403.         daily_cron="enabled"
  404.         ;;
  405.     --lts-kernel|--lts)
  406.         kernel="LTS"
  407.         ;;
  408.     --stable-kernel|--stable)
  409.         kernel="STABLE"
  410.         ;;
  411.     --beta-kernel|--beta|--testing-kernel|--testing)
  412.         kernel="TESTING"
  413.         ;;
  414.     --exp-kernel|--exp)
  415.         kernel="EXPERIMENTAL"
  416.         ;;
  417.     --armv7-channel)
  418.         SOC="armv7"
  419.         ;;
  420.     --armv7-rt-channel)
  421.         SOC="armv7-rt"
  422.         ;;
  423.     --bone-kernel|--bone-channel)
  424.         SOC="omap-psp"
  425.         ;;
  426.     --bone-rt-kernel|--bone-rt-channel)
  427.         SOC="bone-rt"
  428.         ;;
  429.     --bone-xenomai-kernel|--bone-xenomai-channel)
  430.         SOC="xenomai"
  431.         kernel="STABLE"
  432.         ;;
  433.     --multiv7-channel)
  434.         SOC="multiv7"
  435.         kernel="STABLE"
  436.         ;;
  437.     --omap2plus-channel)
  438.         SOC="omap2plus"
  439.         kernel="STABLE"
  440.         ;;
  441.     --ti-kernel|--ti-channel)
  442.         SOC="ti"
  443.         ;;
  444.     --ti-rt-kernel|--ti-rt-channel)
  445.         SOC="ti-rt"
  446.         ;;
  447.     --ti-xenomai-kernel|--ti-xenomai-channel)
  448.         SOC="ti-xenomai"
  449.         ;;
  450.     --ti-omap2plus-channel)
  451.         SOC="ti-omap2plus"
  452.         kernel="TESTING"
  453.         ;;
  454.     esac
  455.     shift
  456. done
  457.  
  458.  
  459. if [ ! -f /lib/systemd/system/systemd-timesyncd.service ] ; then
  460.     if [ -f /usr/sbin/ntpdate ] ; then
  461.         echo "syncing local clock to pool.ntp.org"
  462.         ntpdate -s pool.ntp.org || true
  463.     fi
  464. fi
  465.  
  466. test_rcnee=$(cat /etc/apt/sources.list | grep repos.rcn-ee || true)
  467. if [ ! "x${test_rcnee}" = "x" ] ; then
  468.     net_rcnee=$(cat /etc/apt/sources.list | grep repos.rcn-ee.net || true)
  469.     if [ ! "x${net_rcnee}" = "x" ] ; then
  470.         sed -i -e 's:repos.rcn-ee.net:repos.rcn-ee.com:g' /etc/apt/sources.list
  471.     fi
  472.     get_device
  473.  
  474.     if [ "x${kernel_version}" = "x" ] ; then
  475.         latest_version_repo
  476.     else
  477.         specific_version_repo
  478.     fi
  479.     third_party
  480.     apt-get clean
  481. else
  482.     get_device
  483.     latest_version
  484. fi
  485. #third_party
  486. #
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top