1
0

make-module-ipkgs.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 -A modpaths moddeps modlevels
  30. # recursively find a level for given module which is high enough so all
  31. # dependencies are in a lower level
  32. find_modlevel() { # (modname)
  33. local dep level=0
  34. for dep in ${moddeps[$1]}; do
  35. [[ -n "${modlevels[$dep]}" ]] || find_modlevel $dep
  36. [[ ${modlevels[$dep]} -lt $level ]] || level=$((modlevels[$dep] + 1))
  37. done
  38. modlevels[$1]=$level
  39. }
  40. # sanitize modname, ipkg does not allow uppercase or underscores
  41. pkgname() { # (modname)
  42. tr 'A-Z_' 'a-z-' <<< "kmod-$1"
  43. }
  44. for modpath in $(find ${BUILD_DIR}/modules -name \*.ko | xargs); do
  45. modname="$(basename $modpath .ko)"
  46. moddep="$(modinfo $modpath | awk '/^depends:/{print $2}' | sed 's/,/ /g')"
  47. modpaths[$modname]="$modpath"
  48. moddeps[$modname]="$moddep"
  49. done
  50. #echo "modpaths:"
  51. #for modname in ${!modpaths[@]}; do
  52. # echo "$modname: ${modpaths[$modname]}"
  53. #done
  54. #echo
  55. #echo "moddeps:"
  56. #for modname in ${!moddeps[@]}; do
  57. # echo "$modname: ${moddeps[$modname]}"
  58. #done
  59. #echo
  60. for modname in ${!modpaths[@]}; do
  61. find_modlevel $modname
  62. ctrlfile=${BUILD_DIR}/kmod-control/kmod-${modname}.control
  63. ipkgdir=${BUILD_DIR}/linux-modules/ipkg/$modname
  64. cat >$ctrlfile <<-EOF
  65. Package: $(pkgname $modname)
  66. Priority: optional
  67. Section: sys
  68. Description: kernel module $modname
  69. EOF
  70. sh $(dirname $0)/make-ipkg-dir.sh $ipkgdir $ctrlfile $VER $ARCH
  71. depline="kernel ($VER)"
  72. for m in ${moddeps[$modname]}; do
  73. depline+=", $(pkgname ${m})"
  74. done
  75. echo "Depends: $depline" >>${ipkgdir}/CONTROL/control
  76. mkdir -p ${ipkgdir}/lib/modules/${VER}
  77. cp ${modpaths[$modname]} ${ipkgdir}/lib/modules/${VER}
  78. cat >${ipkgdir}/CONTROL/postinst <<EOF
  79. #!/bin/sh
  80. if [ -z \${IPKG_INSTROOT} ]; then
  81. . /etc/functions.sh
  82. load_modules /etc/modules.d/${modlevels[$modname]}-$modname
  83. fi
  84. EOF
  85. chmod 0755 ${ipkgdir}/CONTROL/postinst
  86. mkdir -p ${ipkgdir}/etc/modules.d
  87. echo $modname >${ipkgdir}/etc/modules.d/${modlevels[$modname]}-$modname
  88. env ${PKG_BUILD} ${ipkgdir} ${PACKAGE_DIR} || exit 1
  89. done