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

syslinux/iso2exe: add zimage support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Mar 06 19:57:41 2014 +0000 (2014-03-06)
parents 82c793b0a850
children d814ca840c3c
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 cdexe()
352 {
353 [ $(get 0 /media/cdrom/$1 2> /dev/null || echo 0) -ne 23117 ] &&
354 echo -en "\"$2\" \"$3\""
355 }
357 fddata()
358 {
359 [ $(get 28 /mnt/$ISO 1 2> /dev/null || echo 0) -ne 0 ] &&
360 echo -en "\"$1\" \"$2\""
361 }
363 fdbootstrap()
364 {
365 sz=$((512 * $(echo $(get 28 /mnt/$ISO 1))))
366 $DIALOG --clear \
367 --title " Create a floppy bootstrap " \
368 --yes-label "Continue" --yesno \
369 "\nThe floppy will install a driver to access the ISO file
370 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
371 Please insert a floppy in drive now.\n
372 " 10 70
373 [ $? -eq 0 ] || return
374 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=512 \
375 skip=$(( $(get 66 /mnt/$ISO) - $sz ))
376 echo "$ISO" | dd of=/dev/fd0 bs=512 seek=1 count=1
377 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=$sz seek=2 \
378 skip=$(( $(get 66 /mnt/$ISO) - $sz + 512 ))
379 }
381 usbdev()
382 {
383 sleep 5
384 DEV="$(grep -l 1 /sys/block/*/removable | \
385 sed 's|/sys/block/\(.*\)/removable|\1|')"
386 [ -n "$DEV" ] || return
387 exec 3>&1
388 device=`$DIALOG --clear \
389 --title " Select your USB key " \
390 --menu "\nPlease select the USB key according to its known size.\n\n" \
391 14 70 4 \
392 $(for i in $DEV ; do
393 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
394 done) \
395 2>&1 1>&3`
396 retval=$?
397 exec 3>&-
398 [ $retval -eq 0 ]
399 }
401 usbbootkey()
402 {
403 $DIALOG --clear \
404 --title " Create a USB boot key " \
405 --yes-label "Continue" --yesno \
406 "\nThe USB key will be used like a CD-ROM. You will not be able to write
407 any data on it.\n\n
408 You should choose 'USB key read/write installation' to be
409 able to save the package updates or your own configuration and data files.\n\n
410 Please plug your USB stick in now.\n
411 " 13 70
412 [ $? -eq 0 ] || return
413 usbdev || return
414 dd if=/mnt/$ISO of=$device
415 }
417 usbkey()
418 {
419 $DIALOG --clear \
420 --title " Create a SliTaz USB key " \
421 --yes-label "Continue" --yesno \
422 "\nUnlike a hard drive install, the filesystem is kept in a compressed
423 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
424 This should increase responsiveness, protect the filesystem against
425 accidental corruption and reduce read/writes to the USB drive.
426 Once setup, the tazusb utility can rewrite the root filesystem
427 with any changes you have made since booting up,
428 giving the effective benefits of a hard drive install.\n\n
429 /home is mounted on boot using the UUID of your particular flash drive.
430 Unlike a device name, the UUID has the benefit of never changing from machine
431 to machine.\n\n
432 Please plug your USB stick in now.\n
433 " 19 70
434 [ $? -eq 0 ] || return
435 usbdev || return
436 exec 3>&1
437 format=`$DIALOG --clear \
438 --title " Select the filesystem " \
439 --radiolist "\nPlease select the filesystem type to create.\n\n\
440 The filesystem creation will erase all the data \
441 in the USB key." 14 70 4 \
442 "none" "Do not erase the USB key" on \
443 "ext3" "Ext3 journaling filesystem" off \
444 "ext2" "Ext2 filesystem" off \
445 "fat32" "Windows FAT32 filesystem" off \
446 2>&1 1>&3`
447 retval=$?
448 exec 3>&-
449 [ $retval -eq 0 ] || return
450 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
451 tazusb gen-iso2usb /mnt/$ISO $device
452 }
454 mount_loram()
455 {
456 is_loram || return
457 insmod /lib/squashfs.ko* 2> /dev/null
458 if [ -d /media/cdrom/fs ]; then
459 ln -s /media/cdrom/fs /sqfs
460 else
461 mkdir /sqfs
462 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
463 fi
464 ln -s /sqfs/lib/* lib
465 ln -s /sqfs/usr /sqfs/var /
466 for i in dmesg basename tr od reboot poweroff getty sync ; do
467 ln -s /sqfs/bin/busybox /bin/$i
468 done
469 }
471 umount_loram()
472 {
473 is_loram || return
474 rm /var /usr
475 umount -d /sqfs
476 rmdir /sqfs
477 }
479 text()
480 {
481 umount_loram
482 umount -d /media/cdrom
483 umount /mnt
484 umount /proc
485 exec /init
486 }
488 live()
489 {
490 n=0
491 for i in $(ls -r /media/cdrom/boot/rootfs*); do
492 [ $((n++)) -eq 0 ] || uncpio $i
493 done
494 text
495 }
497 restart()
498 {
499 sync
500 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
501 reboot -f
502 }
504 stop()
505 {
506 sync
507 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
508 poweroff -f
509 }
511 shell()
512 {
513 getty -n -l /bin/ash 38400 tty1 || sh
514 }
516 BIN=bin/mount.posixovl
517 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
518 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN
519 mount -t proc /proc /proc
520 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
521 getiso
522 mount_loram
523 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
524 *install*) install ;;
525 *live*) live ;;
526 *text*) text ;;
527 esac
528 which $DIALOG || live
529 dmesg > /tmp/dmesg
531 while true; do
532 cat > /tmp/dialog <<EOT
533 $DIALOG --clear \
534 --title " Welcome to SliTaz " \
535 --menu "" 21 70 15 \
536 "live" "SliTaz RAM boot" \
537 "text" "SliTaz RAM boot (text mode only)" \
538 $(cdfile README "readme" "Show the README file") \
539 $(cdfile md5sum "md5" "Check ISO files") \
540 $(xfile mount.posixovl "install" "Hard disk installation (UMSDOS way)") \
541 $(xfile mount.posixovl "installtaz" "Hard disk installation (TAZUSB way)") \
542 $(xfile tazusb "usbkey" "USB key read/write installation") \
543 "usbbootkey" "USB boot key (read only)" \
544 $(fddata "fdbootstrap" "Floppy bootstrap") \
545 "tazboot" "Get tazboot.exe Linux loader" \
546 $(cdexe boot/memtest "memtest" "Get Memtest86") \
547 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
548 $(cdexe boot/gpxe "pxe" "Get SliTaz Web boot utility") \
549 $(cdfile boot/gpxe "fdpxe" "Create a SliTaz Web boot floppy") \
550 $(cdexe boot/ipxe "pxe" "Get SliTaz Web boot utility") \
551 $(cdfile boot/ipxe "fdpxe" "Create a SliTaz Web boot floppy") \
552 $(xfile reboot "restart" "Restart the computer") \
553 $(xfile poweroff "stop" "Power off") \
554 "bootlog" "Linux boot messages" \
555 "shell" "Shell prompt" \
557 EOT
558 exec 3>&1
559 value=$(sh /tmp/dialog 2>&1 1>&3)
560 retval=$?
561 exec 3>&-
562 [ $retval -eq 0 ] || continue
563 $value
564 done