1
0

make-module-ipkgs.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. # start with empty directory, avoid leftovers after version change
  62. rm -rf ${BUILD_DIR}/linux-modules
  63. for modname in ${!modpaths[@]}; do
  64. find_modlevel $modname
  65. ctrlfile=${BUILD_DIR}/kmod-control/kmod-${modname}.control
  66. ipkgdir=${BUILD_DIR}/linux-modules/ipkg/$modname
  67. cat >$ctrlfile <<-EOF
  68. Package: $(pkgname $modname)
  69. Priority: optional
  70. Section: sys
  71. Description: kernel module $modname
  72. EOF
  73. bash $(dirname $0)/make-ipkg-dir.sh $ipkgdir $ctrlfile $VER $ARCH
  74. depline="kernel ($VER)"
  75. for m in ${moddeps[$modname]}; do
  76. depline+=", $(pkgname ${m})"
  77. done
  78. echo "Depends: $depline" >>${ipkgdir}/CONTROL/control
  79. mkdir -p ${ipkgdir}/lib/modules/${VER}
  80. cp ${modpaths[$modname]} ${ipkgdir}/lib/modules/${VER}
  81. cat >${ipkgdir}/CONTROL/postinst <<EOF
  82. #!/bin/sh
  83. if [ -z \${IPKG_INSTROOT} ]; then
  84. . /etc/functions.sh
  85. load_modules /etc/modules.d/${modlevels[$modname]}-$modname
  86. fi
  87. EOF
  88. chmod 0755 ${ipkgdir}/CONTROL/postinst
  89. mkdir -p ${ipkgdir}/etc/modules.d
  90. echo $modname >${ipkgdir}/etc/modules.d/${modlevels[$modname]}-$modname
  91. ${PKG_BUILD} ${ipkgdir} ${PACKAGE_DIR} || exit 1
  92. done