1
0

make-module-ipkgs.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. #
  3. # make-module-ipkgs.sh - scan through modules directory and create a package
  4. # for each of them automatically.
  5. #
  6. # Copyright (C) 2015 - Phil Sutter <phil@nwl.cc>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. # Usage:
  23. # $0 <ARCH> <KERNEL_VERSION> <LINUX_BUILD_DIR> <pkg-build-cmd> <PACKAGE_DIR>
  24. ARCH="$1"
  25. VER="$2"
  26. BUILD_DIR="$3"
  27. PKG_BUILD="$4"
  28. PACKAGE_DIR="$5"
  29. # declare associative arrays
  30. declare -A modpaths moddeps modlevels
  31. # recursively find a level for given module which is high enough so all
  32. # dependencies are in a lower level
  33. find_modlevel() { # (modname)
  34. local dep level=0
  35. for dep in ${moddeps[$1]}; do
  36. [[ -n "${modlevels[$dep]}" ]] || find_modlevel $dep
  37. [[ ${modlevels[$dep]} -lt $level ]] || level=$((modlevels[$dep] + 1))
  38. done
  39. modlevels[$1]=$level
  40. }
  41. # sanitize modname, ipkg does not allow uppercase or underscores
  42. pkgname() { # (modname)
  43. tr 'A-Z_' 'a-z-' <<< "kmod-$1"
  44. }
  45. for modpath in $(find ${BUILD_DIR}/modules -name \*.ko | xargs); do
  46. modname="$(basename $modpath .ko)"
  47. moddep="$(strings $modpath | awk -F= '/^depends=/{print $2}' | sed 's/,/ /g')"
  48. modpaths[$modname]="$modpath"
  49. moddeps[$modname]="$moddep"
  50. done
  51. #echo "modpaths:"
  52. #for modname in ${!modpaths[@]}; do
  53. # echo "$modname: ${modpaths[$modname]}"
  54. #done
  55. #echo
  56. #echo "moddeps:"
  57. #for modname in ${!moddeps[@]}; do
  58. # echo "$modname: ${moddeps[$modname]}"
  59. #done
  60. #echo
  61. for modname in ${!modpaths[@]}; do
  62. find_modlevel $modname
  63. ctrlfile=${BUILD_DIR}/kmod-control/kmod-${modname}.control
  64. ipkgdir=${BUILD_DIR}/linux-modules/ipkg/$modname
  65. cat >$ctrlfile <<-EOF
  66. Package: $(pkgname $modname)
  67. Priority: optional
  68. Section: sys
  69. Description: kernel module $modname
  70. EOF
  71. sh $(dirname $0)/make-ipkg-dir.sh $ipkgdir $ctrlfile $VER $ARCH
  72. depline="kernel ($VER)"
  73. for m in ${moddeps[$modname]}; do
  74. depline+=", $(pkgname ${m})"
  75. done
  76. echo "Depends: $depline" >>${ipkgdir}/CONTROL/control
  77. mkdir -p ${ipkgdir}/lib/modules/${VER}
  78. cp ${modpaths[$modname]} ${ipkgdir}/lib/modules/${VER}
  79. cat >${ipkgdir}/CONTROL/postinst <<EOF
  80. #!/bin/sh
  81. if [ -z \${IPKG_INSTROOT} ]; then
  82. . /etc/functions.sh
  83. load_modules /etc/modules.d/${modlevels[$modname]}-$modname
  84. fi
  85. EOF
  86. chmod 0755 ${ipkgdir}/CONTROL/postinst
  87. mkdir -p ${ipkgdir}/etc/modules.d
  88. echo $modname >${ipkgdir}/etc/modules.d/${modlevels[$modname]}-$modname
  89. env ${PKG_BUILD} ${ipkgdir} ${PACKAGE_DIR} || exit 1
  90. done