#!/bin/bash # Generador de ficheros "kernel" e "initrd.gz" para arranque de cliente OpenGnSys test "$(lsb_release -is 2>/dev/null)" == "Ubuntu" && DIST="$(lsb_release -cs)" DIST=${DIST:-"lucid"} # Si no se detecta, distribución Ubuntu por defecto. ARCH=$(arch) # Arquitectura del sistema: i386 (32 bits), amd64 (64 bits). ARCH=${ARCH:-"$(uname -m)"} # Corrección para Ubuntu Jaunty. ARCH=${ARCH/i[4-6]86/i386} ARCH=${ARCH/x86_64/amd64} URL=http://archive.ubuntu.com/ubuntu/dists/$DIST-updates/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH if [ "$TMP" = "" ] ; then TMP=/tmp ; fi TMPINITRD=$TMP/initrd NEWROOT=$TMPINITRD/newroot ANTERIORPWD=$PWD DEST=$PWD LINUX=1 CHROOTINITSCRIPT=/oginit INITSCRIPT=$NEWROOT$CHROOTINITSCRIPT # Comprueba los argumentos pasados para modificar los valores por defecto function parsearParametros { while [ $# -ne 0 ];do case $1 in ("-d") shift #URL=http://people.debian.org/~joeyh/d-i/images/daily/netboot/debian-installer/i386/ URL=http://ftp.nl.debian.org/debian/dists/testing/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH/ ;; ("-v") shift if [ $# -eq 0 ];then echo "Error en los argumentos" return -1 else DIST=$1 URL=http://archive.ubuntu.com/ubuntu/dists/$DIST-updates/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH shift fi ;; ("-l") shift ;; ("-t") shift if [ $# -eq 0 ];then echo "Error en los argumentos" return -1 else DEST=$1 shift fi ;; esac done } function descargar { # Borramos si existe el directorio temporal if [ -d $TMPINITRD ]; then rm -r $TMPINITRD fi # Creamos directorio temporal y nos vamos alli mkdir -p $TMPINITRD cd $TMPINITRD # Ficheros a descargar: Initrd y Kernel (si está habilitado). FILES="initrd.gz" [ $LINUX ] && FILES="$FILES linux" for f in $FILES; do # Borrar fichero anterior [ -f "$f" ] && rm -f $f # Descargar fichero (intentar primero actualizaciones). wget $URL/$f || wget ${URL/-updates/}/$f if [ $? != 0 ]; then echo "Error no se ha podido descargar el fichero $f" exit 1 fi done } # Descomprimimos el initrd function descomprimir { if [ ! -f $TMPINITRD/initrd.gz ]; then echo No se encuentra el initrd.gz exit -1 fi if [ -f $NEWROOT ];then rm -rf $NEWROOT fi mkdir -p $NEWROOT cd $NEWROOT gzip -dc $TMPINITRD/initrd.gz | cpio -id } # Borrar todos los scripts del directorio actual function borrarScripts { # Primero los hacemos con los enlaces para que no se produzcan errores si borramos a lo que apunta primero for i in `ls -F -1 | grep @ | awk -F @ '{print $1}'`; do if [ $(file $i -L | grep "shell script" | wc -l) -gt 0 ]; then rm $i fi done # Ahora lo hacemos con todos los ejecutables for i in `ls`; do if [ $(file $i | grep "shell script" | wc -l) -gt 0 ]; then rm $i fi done } # Borra todos los ejecutables que enlacen con la libreria debian-installer function borrarEjecutablesDebinstall { # Primero lo hacemos con los enlaces para que no se produzcan errores si borramos a lo que apunta primero for i in `ls -F -1 | grep @ | awk -F @ '{print $1}'`; do if [ $(ldd $i | grep -e libdebian-installer -e libdebconf | wc -l) -gt 0 ]; then rm $i fi done # Ahora lo hacemos con todos los ejecutables for i in `ls`; do if [ $(ldd $i | grep -e libdebian-installer -e libdebconf | wc -l) -gt 0 ]; then rm $i fi done } # Elimina todos los fichero innecesarios del initrd function purgarFicherosDebian { mkdir -p $NEWROOT cd $NEWROOT rm init # Pasamos por todos los directorios y vamos borrando lo innecesario cd bin/ borrarScripts borrarEjecutablesDebinstall cd ../etc/ rm -rf cdebconf.conf default-release lsb-release preseed_aliases udebs-source cd ../lib/ rm -rf chroot-setup.sh debian-installer* kickseed main-menu.d preseed libdebian-installer* cd ../sbin/ borrarScripts borrarEjecutablesDebinstall cd ../usr/bin/ borrarScripts borrarEjecutablesDebinstall cd ../lib/ rm -rf base-installer.d debian-installer finish-install.d libdebconfclient* cdebconf fetch-url net-retriever post-base-installer.d # Solo queda un enlace simbolico que ya no apunta a nada cd ../sbin/ rm -rf * cd ../share/ rm -rf debconf keyrings save-logs cd ../../var/ rm -rf cache/anna/ spool/kickseed/ cd lib/ rm -rf apt-install cdebconf dpkg cd ../.. } # Le agrega los archivos necesarios para que arranque de otra manera function agregarNuevoArranque { cd $NEWROOT cd etc/ # Script de arranque de OpenGnSys Client. perl -i -p -e "s/\/sbin\/debian-installer\$/${CHROOTINITSCRIPT//\//\/}/" inittab # Impedir usar shell en terminales 2 y 3 # (NOTA: comentar la siguiente línea para sistemas en pruebas). #perl -n -i -e "print unless /^tty[23]/" inittab # Script inicial que ejecuta el resto de scripts de /etc/rcS.d/ #echo "#! /bin/sh" >> rc #echo "for script in /etc/rcS.d/S[0-9][0-9]*; do if [ -x $script ]; then $script fi done" >> rc #chmod +x rc # Agregamos para que ejecute el script anterior lo primero #echo "::sysinit:/etc/init.d/rc" > inittab # Que ejecute el fichero /init cuando se reinicio el proceso init #echo "::restart:/sbin/init" >> inittab # Que funciona el control alt supr #echo "::ctrlaltdel:/sbin/reboot" >> inittab # Cosas que hacer si se apaga #echo "::shutdown:/bin/umount -a -r" >> inittab #echo "::shutdown:/sbin/swapoff -a" >> inittab # Primero ejecutamos el dhcp VERSION=$(cat /opt/opengnsys/doc/VERSION.txt 2>/dev/null) VERSION=${VERSION:-"OpenGnSys"} cat << FIN > $INITSCRIPT #! /bin/sh echo echo "Arranque cliente $VERSION" echo set -e # Exportando variables for i in \$(cat /proc/cmdline); do echo \$i | grep -q "=" && export \$i done # Configurar la red if [ "\$ip" == "dhcp" ]; then echo "Configurando red por DHCP" mkdir -p /var/state/dhcp /sbin/dhclient 2>/dev/null fi # Modo de trabajo del cliente: on-line/off-line status="\${status:-online}" echo "Comprobando modo de trabajo \$status" case "\$status" in online) if [ -z "\$repo" ] then SERVERIP=\$(grep -h dhcp-server-identifier /var/lib/dhcp3/dhclient.* | sed 's/[^0-9]*\(.*\);/\1/' | head -1) else SERVERIP="\$repo" fi echo "Preparando conexión con el Repositorio \$SERVERIP" # determinar el paramtro de boot para los permisos de los montajes de imagenes. BOOTMODE=\${boot:-"user"} case "\$BOOTMODE" in admin) MOUNTOPTS="rw" ;; user) MOUNTOPTS="ro" ;; *) echo "Aviso: Modo de arranque desconocido." BOOTMODE="user" MOUNTOPTS="ro" ;; esac # Montamos el resto de cosas necesarias echo "Montar repo en modo \$BOOTMODE" mkdir -p /opt/opengnsys DEFAULTOPTS="nolock,proto=tcp" if \$(mount | grep -q " /opt/opengnsys "); then DEFAULTOPTS="remount,\$DEFAULTOPTS" fi mount -t nfs -o "\$DEFAULTOPTS,ro" \$SERVERIP:/opt/opengnsys/client /opt/opengnsys mount -t nfs -o "\$DEFAULTOPTS,rw" \$SERVERIP:/opt/opengnsys/log/clients /opt/opengnsys/log mount -t nfs -o "\$DEFAULTOPTS,\$MOUNTOPTS" \$SERVERIP:/opt/opengnsys/images /opt/opengnsys/images ;; offline) echo "Off-line mode." ;; esac /opt/opengnsys/etc/preinit/default.sh FIN chmod +x $INITSCRIPT } # Función para corregir problemas detectados con módulos del kernel. function configurarModulos { cd $NEWROOT/lib/modules/* case "$DIST" in lucid) # Corregir problema de frame-buffer en Lucid. perl -p -i -e 's/vga16fb/vesafb/g' modules.alias ;; *) ;; esac } function comprimir { cd $NEWROOT if [ $? = 1 ] ; then exit -1 fi find ./ | cpio -H newc -o > $TMPINITRD/new-initrd cd $TMPINITRD gzip -9 new-initrd } function finalizar { cd $ANTERIORPWD mv $TMPINITRD/new-initrd.gz $DEST/initrd.gz if [ $LINUX ] ; then mv $TMPINITRD/linux $DEST/linux fi } parsearParametros $@ descargar descomprimir #purgarFicherosDebian agregarNuevoArranque configurarModulos comprimir finalizar