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