| 1 | #!/bin/bash |
|---|
| 2 | # isntallmodule - instalar módulo de kernel en Initrd de cliente ogLive. |
|---|
| 3 | # Uso: installmodule tarfile |
|---|
| 4 | # Nota: tarfile es un fichero tar.gz con el fichero .ko del módulo y un fichero "module.conf" |
|---|
| 5 | # para configuración de instalación (debe incluir nombre, fichero y camino del módulo). |
|---|
| 6 | # Autor: Ramón M. Gómez |
|---|
| 7 | # Fecha: 2015-12-03 |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | # Variables. |
|---|
| 11 | PROG=$(basename $0) |
|---|
| 12 | OPENGNSYS=/opt/opengnsys |
|---|
| 13 | INITRD=$OPENGNSYS/tftpboot/ogclient/oginitrd.img |
|---|
| 14 | TARFILE=$(realpath $1 2>/dev/null) |
|---|
| 15 | tmpmod=/tmp/module$$ |
|---|
| 16 | tmpinit=/tmp/initrd$$ |
|---|
| 17 | |
|---|
| 18 | # Comprobar errores. |
|---|
| 19 | if [ $# -ne 1 ]; then |
|---|
| 20 | echo "$PROG: Incorrect operand. Format: $PROG moduletarfile" >&2 |
|---|
| 21 | exit 1 |
|---|
| 22 | fi |
|---|
| 23 | if [ "$USER" != "root" ]; then |
|---|
| 24 | echo "$PROG: Need to be root." >&2 |
|---|
| 25 | exit 1 |
|---|
| 26 | fi |
|---|
| 27 | |
|---|
| 28 | # Mostrar ayuda. |
|---|
| 29 | if [ "$1" == "help" ]; then |
|---|
| 30 | cat << EOT |
|---|
| 31 | |
|---|
| 32 | $PROG: installs kernel module into ogLive image (initrd). |
|---|
| 33 | |
|---|
| 34 | Format: $PROG moduletarfile |
|---|
| 35 | |
|---|
| 36 | moduletarfile must be a tar.gz archive with 2 files: |
|---|
| 37 | - *.ko: compiled module |
|---|
| 38 | - module.conf: configuration file |
|---|
| 39 | |
|---|
| 40 | Configuration file format: |
|---|
| 41 | module=ModuleName |
|---|
| 42 | file=ModuleFile |
|---|
| 43 | path=ModulePath |
|---|
| 44 | |
|---|
| 45 | ModuleName must be a single word. |
|---|
| 46 | ModuleFile must be a kernel compiled module file (*.ko). |
|---|
| 47 | ModulePath must be the kernel target directory, started by "kernel/". |
|---|
| 48 | |
|---|
| 49 | EOT |
|---|
| 50 | exit 0 |
|---|
| 51 | fi |
|---|
| 52 | |
|---|
| 53 | # Comprobar acceso al fichero de módulos. |
|---|
| 54 | if [ ! -r "$TARFILE" ]; then |
|---|
| 55 | echo "$PROG: Cannot access module file." >&2 |
|---|
| 56 | exit 1 |
|---|
| 57 | fi |
|---|
| 58 | |
|---|
| 59 | pushd /tmp >/dev/null |
|---|
| 60 | |
|---|
| 61 | # Borrar al salir del programa. |
|---|
| 62 | trap "popd 2>/dev/null; rm -fr $tmpmod $tmpinit" 0 1 2 3 6 9 15 |
|---|
| 63 | |
|---|
| 64 | # Descompresión de módulos para el ogLive actual. |
|---|
| 65 | mkdir -p $tmpmod |
|---|
| 66 | cd $tmpmod |
|---|
| 67 | tar xvzf $TARFILE >/dev/null || exit |
|---|
| 68 | |
|---|
| 69 | # Fichero de configuración. |
|---|
| 70 | source module.conf || exit |
|---|
| 71 | [ -z "$module" ] && echo "Module not detected." && exit 1 |
|---|
| 72 | |
|---|
| 73 | # Descomprimir Initrd. |
|---|
| 74 | mkdir -p $tmpinit |
|---|
| 75 | cd $tmpinit |
|---|
| 76 | COMPRESS=$(file -b "$INITRD" | awk '{print tolower($1);}') |
|---|
| 77 | $COMPRESS -dc "$INITRD" | cpio -im 2>/dev/null |
|---|
| 78 | |
|---|
| 79 | # Versión del Kernel del Initrd. |
|---|
| 80 | KERNEL=$(ls -d lib/modules/[0-9]* | head -1) |
|---|
| 81 | [ -z "$KERNEL" ] && echo "Kernel not detected." && exit 1 |
|---|
| 82 | # Avisar si el Kernel del módulo es distinto del del Initred. |
|---|
| 83 | echo "$(basename $KERNEL) $(modinfo -F vermagic $tmpmod/$file | cut -f1 -d' ')" | awk '$1!=$2 {print "WARNING: installing module for Kernel",$1,"on Kernel",$2}' |
|---|
| 84 | |
|---|
| 85 | # Copiar módulo y reconstruir dependencias. |
|---|
| 86 | echo "Installing module: $module" |
|---|
| 87 | cp -a $tmpmod/$file $KERNEL/$path |
|---|
| 88 | depmod -b . -a $(basename $KERNEL) |
|---|
| 89 | |
|---|
| 90 | # Recomponer el Initrd. |
|---|
| 91 | find . | cpio -H newc -oa | gzip -9c >$INITRD |
|---|
| 92 | md5sum $INITRD | cut -f1 -d" " > $INITRD.sum |
|---|
| 93 | cp -a $INITRD $INITRD.sum $OPENGNSYS/tftpboot |
|---|
| 94 | |
|---|
| 95 | # Limpiar. |
|---|
| 96 | popd >/dev/null |
|---|
| 97 | rm -fr $tmpmod $tmpinit |
|---|
| 98 | |
|---|