1 | #!/bin/bash |
---|
2 | # Generador de ficheros "kernel" e "initrd.gz" para arranque de cliente OpenGnSys |
---|
3 | |
---|
4 | test "$(lsb_release -is 2>/dev/null)" == "Ubuntu" && DIST="(lsb_release -cs)" |
---|
5 | DIST=${DIST:-"lucid"} # Si no se detecta, distribución Ubuntu por defecto. |
---|
6 | ARCH=$(arch) # Arquitectura del sistema: i386 (32 bits), amd64 (64 bits). |
---|
7 | ARCH=${ARCH:-"$(uname -m)"} # Corrección para Ubuntu Jaunty. |
---|
8 | ARCH=${ARCH/i[4-6]86/i386} |
---|
9 | ARCH=${ARCH/x86_64/amd64} |
---|
10 | URL=http://archive.ubuntu.com/ubuntu/dists/$DIST/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH |
---|
11 | if [ "$TMP" = "" ] ; then TMP=/tmp ; fi |
---|
12 | TMPINITRD=$TMP/initrd |
---|
13 | NEWROOT=$TMPINITRD/newroot |
---|
14 | ANTERIORPWD=$PWD |
---|
15 | DEST=$PWD |
---|
16 | LINUX=1 |
---|
17 | CHROOTINITSCRIPT=/oginit |
---|
18 | INITSCRIPT=$NEWROOT$CHROOTINITSCRIPT |
---|
19 | |
---|
20 | # Comprueba los argumentos pasados para modificar los valores por defecto |
---|
21 | function parsearParametros |
---|
22 | { |
---|
23 | while [ $# -ne 0 ];do |
---|
24 | case $1 in |
---|
25 | ("-d") |
---|
26 | shift |
---|
27 | #URL=http://people.debian.org/~joeyh/d-i/images/daily/netboot/debian-installer/i386/ |
---|
28 | URL=http://ftp.nl.debian.org/debian/dists/testing/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH/ |
---|
29 | ;; |
---|
30 | ("-v") |
---|
31 | shift |
---|
32 | if [ $# -eq 0 ];then |
---|
33 | echo "Error en los argumentos" |
---|
34 | return -1 |
---|
35 | else |
---|
36 | DIST=$1 |
---|
37 | URL=http://archive.ubuntu.com/ubuntu/dists/$DIST/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH |
---|
38 | shift |
---|
39 | fi |
---|
40 | ;; |
---|
41 | ("-l") |
---|
42 | shift |
---|
43 | ;; |
---|
44 | ("-t") |
---|
45 | shift |
---|
46 | if [ $# -eq 0 ];then |
---|
47 | echo "Error en los argumentos" |
---|
48 | return -1 |
---|
49 | else |
---|
50 | DEST=$1 |
---|
51 | shift |
---|
52 | fi |
---|
53 | ;; |
---|
54 | esac |
---|
55 | done |
---|
56 | } |
---|
57 | |
---|
58 | function descargar |
---|
59 | { |
---|
60 | # Borramos si existe el directorio temporal |
---|
61 | if [ -d $TMPINITRD ]; then |
---|
62 | rm -r $TMPINITRD |
---|
63 | fi |
---|
64 | # Creamos directorio temporal y nos vamos alli |
---|
65 | mkdir -p $TMPINITRD |
---|
66 | cd $TMPINITRD |
---|
67 | |
---|
68 | # Borramos el initrd anterior si existe |
---|
69 | if [ -f initrd.gz ]; then rm initrd.gz; fi |
---|
70 | |
---|
71 | # Nos lo descargamos |
---|
72 | wget $URL/initrd.gz |
---|
73 | if [ $? = 1 ] ; then |
---|
74 | echo Error no se ha podido descarga el initrd.gz |
---|
75 | exit -1 |
---|
76 | fi |
---|
77 | |
---|
78 | # Si la opcion de descargar el nucleo tambien esta habilitado nos lo descargamos |
---|
79 | if [ $LINUX ] ; then |
---|
80 | if [ -f linux ] ; then |
---|
81 | rm linux |
---|
82 | fi |
---|
83 | wget $URL/linux |
---|
84 | if [ $? = 1 ] ; then |
---|
85 | echo Error no se ha podido descargar el nucleo linux |
---|
86 | exit -1 |
---|
87 | fi |
---|
88 | fi |
---|
89 | } |
---|
90 | |
---|
91 | # Descomprimimos el initrd |
---|
92 | function descomprimir |
---|
93 | { |
---|
94 | if [ ! -f $TMPINITRD/initrd.gz ]; then |
---|
95 | echo No se encuentra el initrd.gz |
---|
96 | exit -1 |
---|
97 | fi |
---|
98 | |
---|
99 | if [ -f $NEWROOT ];then |
---|
100 | rm -rf $NEWROOT |
---|
101 | fi |
---|
102 | |
---|
103 | mkdir -p $NEWROOT |
---|
104 | cd $NEWROOT |
---|
105 | |
---|
106 | gzip -dc $TMPINITRD/initrd.gz | cpio -id |
---|
107 | } |
---|
108 | |
---|
109 | # Borrar todos los scripts del directorio actual |
---|
110 | function borrarScripts |
---|
111 | { |
---|
112 | # Primero los hacemos con los enlaces para que no se produzcan errores si borramos a lo que apunta primero |
---|
113 | for i in `ls -F -1 | grep @ | awk -F @ '{print $1}'`; do |
---|
114 | if [ $(file $i -L | grep "shell script" | wc -l) -gt 0 ]; then |
---|
115 | rm $i |
---|
116 | fi |
---|
117 | done |
---|
118 | |
---|
119 | # Ahora lo hacemos con todos los ejecutables |
---|
120 | for i in `ls`; do |
---|
121 | if [ $(file $i | grep "shell script" | wc -l) -gt 0 ]; then |
---|
122 | rm $i |
---|
123 | fi |
---|
124 | done |
---|
125 | } |
---|
126 | |
---|
127 | # Borra todos los ejecutables que enlacen con la libreria debian-installer |
---|
128 | function borrarEjecutablesDebinstall |
---|
129 | { |
---|
130 | # Primero lo hacemos con los enlaces para que no se produzcan errores si borramos a lo que apunta primero |
---|
131 | for i in `ls -F -1 | grep @ | awk -F @ '{print $1}'`; do |
---|
132 | if [ $(ldd $i | grep -e libdebian-installer -e libdebconf | wc -l) -gt 0 ]; then |
---|
133 | rm $i |
---|
134 | fi |
---|
135 | done |
---|
136 | |
---|
137 | # Ahora lo hacemos con todos los ejecutables |
---|
138 | for i in `ls`; do |
---|
139 | if [ $(ldd $i | grep -e libdebian-installer -e libdebconf | wc -l) -gt 0 ]; then |
---|
140 | rm $i |
---|
141 | fi |
---|
142 | done |
---|
143 | } |
---|
144 | |
---|
145 | # Elimina todos los fichero innecesarios del initrd |
---|
146 | function purgarFicherosDebian |
---|
147 | { |
---|
148 | mkdir -p $NEWROOT |
---|
149 | cd $NEWROOT |
---|
150 | |
---|
151 | rm init |
---|
152 | |
---|
153 | # Pasamos por todos los directorios y vamos borrando lo innecesario |
---|
154 | cd bin/ |
---|
155 | borrarScripts |
---|
156 | borrarEjecutablesDebinstall |
---|
157 | |
---|
158 | cd ../etc/ |
---|
159 | rm -rf cdebconf.conf default-release lsb-release preseed_aliases udebs-source |
---|
160 | |
---|
161 | cd ../lib/ |
---|
162 | rm -rf chroot-setup.sh debian-installer* kickseed main-menu.d preseed libdebian-installer* |
---|
163 | |
---|
164 | cd ../sbin/ |
---|
165 | borrarScripts |
---|
166 | borrarEjecutablesDebinstall |
---|
167 | |
---|
168 | cd ../usr/bin/ |
---|
169 | borrarScripts |
---|
170 | borrarEjecutablesDebinstall |
---|
171 | |
---|
172 | cd ../lib/ |
---|
173 | rm -rf base-installer.d debian-installer finish-install.d libdebconfclient* cdebconf fetch-url net-retriever post-base-installer.d |
---|
174 | |
---|
175 | # Solo queda un enlace simbolico que ya no apunta a nada |
---|
176 | cd ../sbin/ |
---|
177 | rm -rf * |
---|
178 | |
---|
179 | cd ../share/ |
---|
180 | rm -rf debconf keyrings save-logs |
---|
181 | |
---|
182 | cd ../../var/ |
---|
183 | rm -rf cache/anna/ spool/kickseed/ |
---|
184 | |
---|
185 | cd lib/ |
---|
186 | rm -rf apt-install cdebconf dpkg |
---|
187 | |
---|
188 | cd ../.. |
---|
189 | } |
---|
190 | |
---|
191 | # Le agrega los archivos necesarios para que arranque de otra manera |
---|
192 | function agregarNuevoArranque |
---|
193 | { |
---|
194 | cd $NEWROOT |
---|
195 | |
---|
196 | cd etc/ |
---|
197 | # Script de arranque de OpenGnSys Client. |
---|
198 | perl -i -p -e "s/\/sbin\/debian-installer\$/${CHROOTINITSCRIPT//\//\/}/" inittab |
---|
199 | # Impedir usar shell en terminales 2 y 3 |
---|
200 | # (NOTA: comentar la siguiente línea para sistemas en pruebas). |
---|
201 | #perl -n -i -e "print unless /^tty[23]/" inittab |
---|
202 | |
---|
203 | # Script inicial que ejecuta el resto de scripts de /etc/rcS.d/ |
---|
204 | #echo "#! /bin/sh" >> rc |
---|
205 | #echo "for script in /etc/rcS.d/S[0-9][0-9]*; do if [ -x $script ]; then $script fi done" >> rc |
---|
206 | #chmod +x rc |
---|
207 | |
---|
208 | # Agregamos para que ejecute el script anterior lo primero |
---|
209 | #echo "::sysinit:/etc/init.d/rc" > inittab |
---|
210 | # Que ejecute el fichero /init cuando se reinicio el proceso init |
---|
211 | #echo "::restart:/sbin/init" >> inittab |
---|
212 | # Que funciona el control alt supr |
---|
213 | #echo "::ctrlaltdel:/sbin/reboot" >> inittab |
---|
214 | |
---|
215 | # Cosas que hacer si se apaga |
---|
216 | #echo "::shutdown:/bin/umount -a -r" >> inittab |
---|
217 | #echo "::shutdown:/sbin/swapoff -a" >> inittab |
---|
218 | |
---|
219 | # Primero ejecutamos el dhcp |
---|
220 | cat << FIN > $INITSCRIPT |
---|
221 | #! /bin/sh |
---|
222 | set -e |
---|
223 | #exportando variables |
---|
224 | GLOBAL="cat /proc/cmdline" |
---|
225 | for i in \`\${GLOBAL}\` |
---|
226 | do |
---|
227 | #export \$i |
---|
228 | echo \$i | grep "=" > /dev/null && export \$i |
---|
229 | done |
---|
230 | #configurar la red |
---|
231 | if grep -q "ip=dhcp" /proc/cmdline; then |
---|
232 | mkdir -p /var/state/dhcp |
---|
233 | /sbin/dhclient |
---|
234 | fi |
---|
235 | echo Configurada la red \$ip |
---|
236 | #Modo de Trabajo del cliente, on line <> off lne |
---|
237 | status="\${status:-online}" |
---|
238 | echo "Comprobando modo de trabajo \$status" |
---|
239 | case "\$status" in |
---|
240 | online) |
---|
241 | if [ -z \$repo ] |
---|
242 | then |
---|
243 | SERVERIP=\$(grep -h dhcp-server-identifier /var/lib/dhcp3/dhclient.* | sed 's/[^0-9]*\(.*\);/\1/' | head -1) |
---|
244 | else |
---|
245 | SERVERIP=\$repo |
---|
246 | fi |
---|
247 | echo "Preparando conexión con el Repositorio \$SERVERIP" |
---|
248 | # determinar el paramtro de boot para los permisos de los montajes de imagenes. |
---|
249 | BOOTMODE=\${boot:-"user"} |
---|
250 | case "\$BOOTMODE" in |
---|
251 | admin) MOUNTOPTS="rw" ;; |
---|
252 | user) MOUNTOPTS="ro" ;; |
---|
253 | *) # FIXME: Modo de arranque desconocido |
---|
254 | echo "\$MSG_ERRBOOTMODE" |
---|
255 | MOUNTOPTS="ro" ;; |
---|
256 | esac |
---|
257 | # Montamos el resto de cosas necesarias |
---|
258 | echo "Montar repo en modo \$BOOTMODE" |
---|
259 | mkdir -p /opt/opengnsys |
---|
260 | DEFAULTOPTS="nolock" |
---|
261 | if \$(mount | grep -q " /opt/opengnsys "); then |
---|
262 | DEFAULTOPTS="remount,\$DEFAULTOPTS" |
---|
263 | fi |
---|
264 | mount -t nfs -o "\$DEFAULTOPTS,ro" \$SERVERIP:/opt/opengnsys/client /opt/opengnsys |
---|
265 | mount -t nfs -o "\$DEFAULTOPTS,rw" \$SERVERIP:/opt/opengnsys/log/clients /opt/opengnsys/log |
---|
266 | mount -t nfs -o "\$DEFAULTOPTS,\$MOUNTOPTS" \$SERVERIP:/opt/opengnsys/images /opt/opengnsys/images |
---|
267 | ;; |
---|
268 | offline) |
---|
269 | echo "Off-line mode." |
---|
270 | ;; |
---|
271 | esac |
---|
272 | /opt/opengnsys/etc/preinit/default.sh |
---|
273 | FIN |
---|
274 | chmod +x $INITSCRIPT |
---|
275 | } |
---|
276 | |
---|
277 | # Función para corregir problemas detectados con módulos del kernel. |
---|
278 | function configurarModulos |
---|
279 | { |
---|
280 | cd $NEWROOT/lib/modules/* |
---|
281 | |
---|
282 | case "$DIST" in |
---|
283 | lucid) # Corregir problema de frame-buffer en Lucid. |
---|
284 | perl -p -i -e 's/vga16fb/vesafb/g' modules.alias ;; |
---|
285 | *) ;; |
---|
286 | esac |
---|
287 | } |
---|
288 | |
---|
289 | function comprimir |
---|
290 | { |
---|
291 | cd $NEWROOT |
---|
292 | |
---|
293 | if [ $? = 1 ] ; then |
---|
294 | exit -1 |
---|
295 | fi |
---|
296 | |
---|
297 | find ./ | cpio -H newc -o > $TMPINITRD/new-initrd |
---|
298 | cd $TMPINITRD |
---|
299 | gzip -9 new-initrd |
---|
300 | } |
---|
301 | |
---|
302 | function finalizar |
---|
303 | { |
---|
304 | cd $ANTERIORPWD |
---|
305 | mv $TMPINITRD/new-initrd.gz $DEST/initrd.gz |
---|
306 | if [ $LINUX ] ; then |
---|
307 | mv $TMPINITRD/linux $DEST/linux |
---|
308 | fi |
---|
309 | } |
---|
310 | |
---|
311 | parsearParametros $@ |
---|
312 | descargar |
---|
313 | descomprimir |
---|
314 | #purgarFicherosDebian |
---|
315 | agregarNuevoArranque |
---|
316 | configurarModulos |
---|
317 | comprimir |
---|
318 | finalizar |
---|