wok view syslinux/stuff/iso2exe/init @ rev 16013

syslinux/iso2exe/init: fix pxe names
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Mar 04 21:08:01 2014 +0000 (2014-03-04)
parents 3bb29e042c1c
children 592369f78d12
line source
1 #!/bin/sh
3 DIALOG=dialog
5 get()
6 {
7 od -j $1 -N ${3:-2} -t u${3:-2} -An $2 2> /dev/null ||
8 hexdump -s $1 -n ${3:-2} -e "\"\" 1/${3:-2} \"%d\"" $2
9 }
11 getarg()
12 {
13 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
14 }
16 gettazboot()
17 {
18 echo "Creating $(basename $1) ..."
19 O=$(($(get 66 /mnt/$ISO) - 0xC0))
20 L=$((0x7EE0 - $(get 24 /mnt/$ISO) - $O))
21 S=$((32+$L))
22 P=$((($S+511)/512))
23 E=$((4096-(32*$P)))
24 for i in 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
25 -2 0 256 -16 28 0x6C53 0x5469 0x7A61; do
26 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
27 xargs echo -en
28 done > $1
29 dd bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1 2> /dev/null
30 }
32 checkmagic()
33 {
34 [ -s $1 ] && [ $(getarg magic) == $(get 24 $1 4) ]
35 }
37 getiso()
38 {
39 mkdir -p /media/cdrom
40 blkid | while read dev info ; do
41 mount ${dev%:} /mnt
42 if checkmagic /mnt/$ISO; then
43 mount -o loop,ro /mnt/$ISO /media/cdrom
44 echo "Found $ISO on ${dev%:}"
45 break
46 fi
47 umount /mnt
48 done
49 }
51 uncpio()
52 {
53 echo "Extracting $(basename $1) ..."
54 case $(get 0 $1) in
55 *35615) zcat $1 ;;
56 *\ 93) unlzma -c $1 ;;
57 *) cat $1 ;;
58 esac | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
59 }
61 getuuid()
62 {
63 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
64 blkid | sed "/$dev:/!d;s/.*UUID=.\\([^ ]*\\)\".*/\\1/"
65 }
67 mkinitrd()
68 {
69 echo "Creating $(basename $1) ..."
70 for i in bin lib dev proc tmp mnt etc ; do
71 mkdir -p /tmp/fs/$i
72 done
73 for i in /dev/console /dev/null /dev/tty /dev/tty1 /dev/fuse \
74 /dev/hd* /dev/sd* ; do
75 cp -a $i /tmp/fs$i
76 done
77 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
78 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
79 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
80 cp $i /tmp/fs/bin
81 done
82 cp -a /lib/ld-* /tmp/fs/lib
83 for i in $(busybox | sed '/Current/,$!d'); do
84 [ -e /tmp/fs/bin/${i%,} ] || ln -s busybox /tmp/fs/bin/${i%,}
85 done
86 ln -s /proc/mounts /tmp/fs/etc/mtab
87 cat > /tmp/fs/init <<EOT
88 #!/bin/sh
90 arg()
91 {
92 grep -q \$1 /proc/cmdline &&
93 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
94 echo "\$2 \$val"
95 }
97 mount -t proc /proc /proc
98 arg mount "Mount device"
99 mount \$( (blkid /dev/?d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
100 arg subroot "Change root to directory"
101 mount.posixovl -F /mnt/\$val
102 mount --bind /mnt /mnt/\$val/mnt/dos
103 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
104 umount /proc
105 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
106 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
107 EOT
108 chmod +x /tmp/fs/init
109 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
110 rm -rf /tmp/fs
111 }
113 is_loram()
114 {
115 [ -s /lib/squashfs.ko* ]
116 }
118 doinstall()
119 {
120 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
121 mount.posixovl -F /mnt/slitaz || return
122 echo "Install root filesystem..."
123 if [ -n "$1" ]; then
124 if [ -d /media/cdrom/fs ]; then
125 ( cd /mnt/slitaz/fs; find | cpio -o -H newc ) | gzip -9
126 else
127 ls -r /media/cdrom/boot/rootfs* | xargs cat
128 fi > /mnt/slitaz/boot/rootfs.gz
129 mkdir /mnt/slitaz/home
130 initrd=rootfs.gz
131 extraargs="/home=$(getuuid)/slitaz/home" # FIXME /mnt/dos
132 else
133 if [ -d /media/cdrom/fs ]; then
134 cp -a /media/cdrom/fs/. /mnt/slitaz
135 elif is_loram ; then
136 for i in /media/cdrom/boot/rootfs?.* ; do
137 [ -s $(basename $i) ] && continue
138 cpio -i $i
139 done
140 for i in $(ls -r /media/cdrom/boot/rootfs*); do
141 mount -o loop,ro $i /sqfs/mnt
142 cp -a /sqfs/mnt/. /mnt/slitaz
143 umount -d /sqfs/mnt
144 done
145 else
146 for i in $(ls -r /media/cdrom/boot/rootfs*); do
147 uncpio $i /mnt/slitaz
148 done
149 fi
150 mkinitrd /mnt/slitaz/boot/initrd
151 initrd=initrd
152 extraargs="mount=$(getuuid) subroot=slitaz"
153 fi
154 echo "Install boot files..."
155 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
156 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
157 /media/cdrom/README /media/cdrom/boot/memtest* ; do
158 [ -s $i ] && cp $i /mnt/slitaz/boot
159 done
160 gettazboot /mnt/slitaz/boot/tazboot.exe
161 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
162 kernel=\\slitaz\\boot\\bzimage
163 initrd=\\slitaz\\boot\\$initrd
164 rw root=/dev/null $extraargs autologin
165 EOT
166 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
167 /mnt/slitaz/boot/README
168 [ -d /mnt/slitaz/usr/sbin -a ! -x /mnt/slitaz/usr/sbin/mount.posixovl ] &&
169 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
170 ! grep -qs tazboot /mnt/boot.ini && unix2dos >> /mnt/boot.ini <<EOT
171 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
172 EOT
173 grep -qs menuitem /mnt/config.sys && !grep -q tazboot /mnt/config.sys &&
174 sed -i 's/menudefault=/menuitem=slitaz,SliTaz\n&/' /mnt/config.sys &&
175 cat >> /mnt/config.sys <<EOT
176 [slitaz]
177 device=\\slitaz\\boot\\tazboot.exe
178 EOT
179 unix2dos /mnt/config.sys 2> /dev/null
180 }
182 install()
183 {
184 $DIALOG --clear \
185 --title " SliTaz UMSDOS way installation " \
186 --yes-label "Install" --yesno \
187 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
188 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
189 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
190 To uninstall SliTaz, you have only to remove this directory.
191 The file \\boot.ini or \\config.sys may be modified too.\n\n
192 SliTaz may run slowly on 'UMSDOS way' installation due to the
193 posixovl filesystem. The 'TAZUSB way' installation runs faster.\n\n
194 To do a traditional installation with disk partitioning,
195 start SliTaz Live with 'SliTaz RAM boot' menu.\n
196 " 19 70
197 [ $? -eq 0 ] || return
198 doinstall
199 [ -x /sbin/init ] || return
200 umount -d /media/cdrom
201 umount /proc
202 exec chroot /mnt/slitaz /sbin/init
203 }
205 installtaz()
206 {
207 $DIALOG --clear \
208 --title " SliTaz TAZUSB way installation " \
209 --yes-label "Install" --yesno \
210 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
211 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
212 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
213 To uninstall SliTaz, you have only to remove this directory.
214 The file \\boot.ini or \\config.sys may be modified too.\n\n
215 The filesystem is loaded entirely into memory upon boot to
216 increase responsiveness. Only /home lands on hard disk.\n\n
217 To do a traditional installation with disk partitioning,
218 start SliTaz Live with 'SliTaz RAM boot' menu.\n
219 " 19 70
220 [ $? -eq 0 ] || return
221 doinstall tazusblike
222 if [ -d /media/cdrom/fs ]; then
223 zcat /mnt/slitaz/boot/rootfs.gz | cpio -idmu
224 else
225 for i in $(ls -r /media/cdrom/boot/rootfs*); do
226 ( zcat $i 2> /dev/null || unlzma -c $i ) | cpio -idmu
227 done
228 fi
229 [ -x /sbin/init ] || return
230 umount -d /media/cdrom
231 mount --move /mnt /mnt/dos
232 ln -s /mnt/dos/slitaz/home /home
233 umount /proc
234 exec /sbin/init
235 }
237 tazboot()
238 {
239 $DIALOG --clear \
240 --title " SliTaz bootloader for DOS " \
241 --yes-label "Install" --yesno \
242 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
243 a bzImage linux kernel, multiple initramfs, a kernel command line and
244 an ISO image file loopback (retrieves files from an ISO file).\n\n
245 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
246 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
247 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
248 Examples for tazboot.cmd:\n\n\
249 iso=\\isos\\slitaz-4.0.iso\n\
250 kernel=boot/bzImage\n\
251 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
252 rw root=/dev/null autologin\n\n\
253 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
254 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
255 " 24 78
256 [ $? -eq 0 ] || return
257 gettazboot /mnt/tazboot.exe
258 }
260 md5()
261 {
262 echo "Checking files..."
263 ( cd /media/cdrom ; md5sum -c md5sum ) > /tmp/data
264 $DIALOG --clear \
265 --title " Checked files " \
266 --textbox /tmp/data 24 78
267 rm -f /tmp/data
268 }
270 readme()
271 {
272 $DIALOG --clear \
273 --title " Readme " \
274 --textbox /media/cdrom/README 24 78
275 }
277 bootlog()
278 {
279 $DIALOG --clear \
280 --title " Linux boot messages " \
281 --textbox /tmp/dmesg 24 78
282 }
284 memtest()
285 {
286 $DIALOG --clear \
287 --title " Memtest86 " \
288 --yes-label "Install" --yesno \
289 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
290 computers. BIOS based memory tests are a quick, cursory check and often
291 miss many of the failures that are detected by Memtest86.\n
292 " 12 70
293 [ $? -eq 0 ] || return
294 cp /media/cdrom/boot/memtest /mnt/memtest.exe
295 }
297 fdmemtest()
298 {
299 $DIALOG --clear \
300 --title " Create a Memtest86 boot floppy " \
301 --yes-label "Create floppy" --yesno \
302 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
303 computers. BIOS based memory tests are a quick, cursory check and often
304 miss many of the failures that are detected by Memtest86.\n\n
305 Please insert a blank disk in floppy drive.\n
306 " 12 70
307 [ $? -eq 0 ] || return
308 dd if=/media/cdrom/boot/memtest of=/dev/fd0
309 }
311 pxe()
312 {
313 $DIALOG --clear \
314 --title " SliTaz Web boot " \
315 --yes-label "Install" --yesno \
316 "\nBoot your operating system from the internet and enjoy a full system
317 working entirely in RAM with speed and stability in mind. The Linux Kernel
318 and the complete SliTaz compressed root filesystem will be loaded into RAM
319 from the Web using PXE and HTTP protocols.\n
320 " 12 70
321 [ $? -eq 0 ] || return
322 pxe=$(ls /media/cdrom/boot/?pxe)
323 cp $pxe /mnt/$(basename $pxe).exe
324 }
326 fdpxe()
327 {
328 $DIALOG --clear \
329 --title " Create a SliTaz Web boot floppy " \
330 --yes-label "Create floppy" --yesno \
331 "\nBoot your operating system from the internet and enjoy a full system
332 working entirely in RAM with speed and stability in mind. The Linux Kernel
333 and the complete SliTaz compressed root filesystem will be loaded into RAM
334 from the Web using PXE and HTTP protocols.\n\n
335 Please insert a blank disk in floppy drive.\n
336 " 12 70
337 [ $? -eq 0 ] || return
338 dd if=/media/cdrom/boot/?pxe of=/dev/fd0
339 }
341 xfile()
342 {
343 [ -n "$(which $1)" ] && echo -en "\"$2\" \"$3\""
344 }
346 cdfile()
347 {
348 [ -s /media/cdrom/$1 ] && echo -en "\"$2\" \"$3\""
349 }
351 fddata()
352 {
353 [ $(get 28 /mnt/$ISO 1 2> /dev/null || echo 0) -ne 0 ] &&
354 echo -en "\"$1\" \"$2\""
355 }
357 fdbootstrap()
358 {
359 sz=$((512 * $(echo $(get 28 /mnt/$ISO 1))))
360 $DIALOG --clear \
361 --title " Create a floppy bootstrap " \
362 --yes-label "Continue" --yesno \
363 "\nThe floppy will install a driver to access the ISO file
364 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
365 Please insert a floppy in drive now.\n
366 " 10 70
367 [ $? -eq 0 ] || return
368 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=512 \
369 skip=$(( $(get 66 /mnt/$ISO) - $sz ))
370 echo "$ISO" | dd of=/dev/fd0 bs=512 seek=1 count=1
371 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=$sz seek=2 \
372 skip=$(( $(get 66 /mnt/$ISO) - $sz + 512 ))
373 }
375 usbdev()
376 {
377 sleep 5
378 DEV="$(grep -l 1 /sys/block/*/removable | \
379 sed 's|/sys/block/\(.*\)/removable|\1|')"
380 [ -n "$DEV" ] || return
381 exec 3>&1
382 device=`$DIALOG --clear \
383 --title " Select your USB key " \
384 --menu "\nPlease select the USB key according to its known size.\n\n" \
385 14 70 4 \
386 $(for i in $DEV ; do
387 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
388 done) \
389 2>&1 1>&3`
390 retval=$?
391 exec 3>&-
392 [ $retval -eq 0 ]
393 }
395 usbbootkey()
396 {
397 $DIALOG --clear \
398 --title " Create a USB boot key " \
399 --yes-label "Continue" --yesno \
400 "\nThe USB key will be used like a CD-ROM. You will not be able to write
401 any data on it.\n\n
402 You should choose 'USB key read/write installation' to be
403 able to save the package updates or your own configuration and data files.\n\n
404 Please plug your USB stick in now.\n
405 " 13 70
406 [ $? -eq 0 ] || return
407 usbdev || return
408 dd if=/mnt/$ISO of=$device
409 }
411 usbkey()
412 {
413 $DIALOG --clear \
414 --title " Create a SliTaz USB key " \
415 --yes-label "Continue" --yesno \
416 "\nUnlike a hard drive install, the filesystem is kept in a compressed
417 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
418 This should increase responsiveness, protect the filesystem against
419 accidental corruption and reduce read/writes to the USB drive.
420 Once setup, the tazusb utility can rewrite the root filesystem
421 with any changes you have made since booting up,
422 giving the effective benefits of a hard drive install.\n\n
423 /home is mounted on boot using the UUID of your particular flash drive.
424 Unlike a device name, the UUID has the benefit of never changing from machine
425 to machine.\n\n
426 Please plug your USB stick in now.\n
427 " 19 70
428 [ $? -eq 0 ] || return
429 usbdev || return
430 exec 3>&1
431 format=`$DIALOG --clear \
432 --title " Select the filesystem " \
433 --radiolist "\nPlease select the filesystem type to create.\n\n\
434 The filesystem creation will erase all the data \
435 in the USB key." 14 70 4 \
436 "none" "Do not erase the USB key" on \
437 "ext3" "Ext3 journaling filesystem" off \
438 "ext2" "Ext2 filesystem" off \
439 "fat32" "Windows FAT32 filesystem" off \
440 2>&1 1>&3`
441 retval=$?
442 exec 3>&-
443 [ $retval -eq 0 ] || return
444 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
445 tazusb gen-iso2usb /mnt/$ISO $device
446 }
448 mount_loram()
449 {
450 is_loram || return
451 insmod /lib/squashfs.ko* 2> /dev/null
452 if [ -d /media/cdrom/fs ]; then
453 ln -s /media/cdrom/fs /sqfs
454 else
455 mkdir /sqfs
456 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
457 fi
458 ln -s /sqfs/lib/* lib
459 ln -s /sqfs/usr /sqfs/var /
460 for i in dmesg basename tr od reboot poweroff getty sync ; do
461 ln -s /sqfs/bin/busybox /bin/$i
462 done
463 }
465 umount_loram()
466 {
467 is_loram || return
468 rm /var /usr
469 umount -d /sqfs
470 rmdir /sqfs
471 }
473 text()
474 {
475 umount_loram
476 umount -d /media/cdrom
477 umount /mnt
478 umount /proc
479 exec /init
480 }
482 live()
483 {
484 n=0
485 for i in $(ls -r /media/cdrom/boot/rootfs*); do
486 [ $((n++)) -eq 0 ] || uncpio $i
487 done
488 text
489 }
491 restart()
492 {
493 sync
494 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
495 reboot -f
496 }
498 stop()
499 {
500 sync
501 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
502 poweroff -f
503 }
505 shell()
506 {
507 getty -n -l /bin/ash 38400 tty1 || sh
508 }
510 BIN=bin/mount.posixovl
511 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
512 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN
513 mount -t proc /proc /proc
514 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
515 getiso
516 mount_loram
517 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
518 *install*) install ;;
519 *live*) live ;;
520 *text*) text ;;
521 esac
522 which $DIALOG || live
523 dmesg > /tmp/dmesg
525 while true; do
526 cat > /tmp/dialog <<EOT
527 $DIALOG --clear \
528 --title " Welcome to SliTaz " \
529 --menu "" 21 70 15 \
530 "live" "SliTaz RAM boot" \
531 "text" "SliTaz RAM boot (text mode only)" \
532 $(cdfile README "readme" "Show the README file") \
533 $(cdfile md5sum "md5" "Check ISO files") \
534 $(xfile mount.posixovl "install" "Hard disk installation (UMSDOS way)") \
535 $(xfile mount.posixovl "installtaz" "Hard disk installation (TAZUSB way)") \
536 $(xfile tazusb "usbkey" "USB key read/write installation") \
537 "usbbootkey" "USB boot key (read only)" \
538 $(fddata "fdbootstrap" "Floppy bootstrap") \
539 "tazboot" "Get tazboot.exe Linux loader" \
540 $(cdfile boot/memtest "memtest" "Get Memtest86") \
541 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
542 $(cdfile Xboot/gpxe "pxe" "Get SliTaz Web boot utility") \
543 $(cdfile boot/gpxe "fdpxe" "Create a SliTaz Web boot floppy") \
544 $(cdfile boot/ipxe "pxe" "Get SliTaz Web boot utility") \
545 $(cdfile boot/ipxe "fdpxe" "Create a SliTaz Web boot floppy") \
546 $(xfile reboot "restart" "Restart the computer") \
547 $(xfile poweroff "stop" "Power off") \
548 "bootlog" "Linux boot messages" \
549 "shell" "Shell prompt" \
551 EOT
552 exec 3>&1
553 value=$(sh /tmp/dialog 2>&1 1>&3)
554 retval=$?
555 exec 3>&-
556 [ $retval -eq 0 ] || continue
557 $value
558 done