wok-6.x view syslinux/stuff/iso2exe/init @ rev 14271

syslinux/iso2exe: refresh init
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Apr 05 13:11:51 2013 +0200 (2013-04-05)
parents ff85ea851c53
children 353dc1b968e5
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 /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 run 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 umount -d /media/cdrom
200 umount /proc
201 exec chroot /mnt/slitaz /sbin/init
202 }
204 installtaz()
205 {
206 $DIALOG --clear \
207 --title " SliTaz TAZUSB way installation " \
208 --yes-label "Install" --yesno \
209 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
210 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
211 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
212 To uninstall SliTaz, you have only to remove this directory.
213 The file \\boot.ini or \\config.sys may be modified too.\n\n
214 The filesystem is loaded entirely into memory upon boot to
215 increase responsiveness. Only /home lands on hard disk.\n\n
216 To do a traditional installation with disk partitioning,
217 start SliTaz Live with 'SliTaz RAM boot' menu.\n
218 " 19 70
219 [ $? -eq 0 ] || return
220 doinstall tazusblike
221 if [ -d /media/cdrom/fs ]; then
222 zcat /mnt/slitaz/boot/rootfs.gz | cpio -idmu
223 else
224 for i in $(ls -r /media/cdrom/boot/rootfs*); do
225 ( zcat $i 2> /dev/null || unlzma -c $i ) | cpio -idmu
226 done
227 fi
228 umount -d /media/cdrom
229 mkdir /dos
230 mount --move /mnt /dos
231 ln -s /dos/slitaz/home /home
232 umount /proc
233 exec /sbin/init
234 }
236 tazboot()
237 {
238 $DIALOG --clear \
239 --title " SliTaz bootloader for DOS " \
240 --yes-label "Install" --yesno \
241 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
242 a bzImage linux kernel, multiple initramfs, a kernel command line and
243 an ISO image file loopback (retrieves files from an ISO file).\n\n
244 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
245 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
246 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
247 Examples for tazboot.cmd:\n\n\
248 iso=\\isos\\slitaz-4.0.iso\n\
249 kernel=boot/bzImage\n\
250 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
251 rw root=/dev/null autologin\n\n\
252 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
253 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
254 " 24 78
255 [ $? -eq 0 ] || return
256 gettazboot /mnt/tazboot.exe
257 }
259 md5()
260 {
261 echo "Checking files..."
262 ( cd /media/cdrom ; md5sum -c md5sum ) > /tmp/data
263 $DIALOG --clear \
264 --title " Checked files " \
265 --textbox /tmp/data 24 78
266 rm -f /tmp/data
267 }
269 readme()
270 {
271 $DIALOG --clear \
272 --title " Readme " \
273 --textbox /media/cdrom/README 24 78
274 }
276 bootlog()
277 {
278 $DIALOG --clear \
279 --title " Linux boot messages " \
280 --textbox /tmp/dmesg 24 78
281 }
283 memtest()
284 {
285 $DIALOG --clear \
286 --title " Memtest86 " \
287 --yes-label "Install" --yesno \
288 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
289 computers. BIOS based memory tests are a quick, cursory check and often
290 miss many of the failures that are detected by Memtest86.\n
291 " 12 70
292 [ $? -eq 0 ] || return
293 cp /media/cdrom/boot/memtest /mnt
294 }
296 fdmemtest()
297 {
298 $DIALOG --clear \
299 --title " Create a Memtest86 boot floppy " \
300 --yes-label "Create floppy" --yesno \
301 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
302 computers. BIOS based memory tests are a quick, cursory check and often
303 miss many of the failures that are detected by Memtest86.\n\n
304 Please insert a blank disk in floppy drive.\n
305 " 12 70
306 [ $? -eq 0 ] || return
307 dd if=/media/cdrom/boot/memtest of=/dev/fd0
308 }
310 gpxe()
311 {
312 $DIALOG --clear \
313 --title " SliTaz Web boot " \
314 --yes-label "Install" --yesno \
315 "\nBoot your operating system from the internet and enjoy a full system
316 working entirely in RAM with speed and stability in mind. The Linux Kernel
317 and the complete SliTaz compressed root filesystem will be loaded into RAM
318 from the Web using PXE and HTTP protocols.\n
319 " 12 70
320 [ $? -eq 0 ] || return
321 cp /media/cdrom/boot/gpxe /mnt
322 }
324 fdgpxe()
325 {
326 $DIALOG --clear \
327 --title " Create a SliTaz Web boot floppy " \
328 --yes-label "Create floppy" --yesno \
329 "\nBoot your operating system from the internet and enjoy a full system
330 working entirely in RAM with speed and stability in mind. The Linux Kernel
331 and the complete SliTaz compressed root filesystem will be loaded into RAM
332 from the Web using PXE and HTTP protocols.\n\n
333 Please insert a blank disk in floppy drive.\n
334 " 12 70
335 [ $? -eq 0 ] || return
336 dd if=/media/cdrom/boot/gpxe of=/dev/fd0
337 }
339 xfile()
340 {
341 [ -n "$(which $1)" ] && echo -en "\"$2\" \"$3\""
342 }
344 cdfile()
345 {
346 [ -s /media/cdrom/$1 ] && echo -en "\"$2\" \"$3\""
347 }
349 fddata()
350 {
351 [ $(get 28 /mnt/$ISO 1 2> /dev/null || echo 0) -ne 0 ] &&
352 echo -en "\"$1\" \"$2\""
353 }
355 fdbootstrap()
356 {
357 sz=$((512 * $(echo $(get 28 /mnt/$ISO 1))))
358 $DIALOG --clear \
359 --title " Create a floppy bootstrap " \
360 --yes-label "Continue" --yesno \
361 "\nThe floppy will install a driver to access the ISO file
362 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
363 Please insert a floppy in drive now.\n
364 " 10 70
365 [ $? -eq 0 ] || return
366 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=512 \
367 skip=$(( $(get 66 /mnt/$ISO) - $sz ))
368 echo "$ISO" | dd of=/dev/fd0 bs=512 seek=1 count=1
369 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=$sz seek=2 \
370 skip=$(( $(get 66 /mnt/$ISO) - $sz + 512 ))
371 }
373 usbdev()
374 {
375 sleep 5
376 DEV="$(grep -l 1 /sys/block/*/removable | \
377 sed 's|/sys/block/\(.*\)/removable|\1|')"
378 [ -n "$DEV" ] || return
379 exec 3>&1
380 device=`$DIALOG --clear \
381 --title " Select your USB key " \
382 --menu "\nPlease select the USB key according to its known size.\n\n" \
383 14 70 4 \
384 $(for i in $DEV ; do
385 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
386 done) \
387 2>&1 1>&3`
388 retval=$?
389 exec 3>&-
390 [ $retval -eq 0 ]
391 }
393 usbbootkey()
394 {
395 $DIALOG --clear \
396 --title " Create a USB boot key " \
397 --yes-label "Continue" --yesno \
398 "\nThe USB key will be used like a CD-ROM. You will not be able to write
399 any data on it.\n\n
400 You should choose 'USB key read/write installation' to be
401 able to save the package updates or your own configuration and data files.\n\n
402 Please plug your USB stick in now.\n
403 " 13 70
404 [ $? -eq 0 ] || return
405 usbdev || return
406 dd if=/mnt/$ISO of=$device
407 }
409 usbkey()
410 {
411 $DIALOG --clear \
412 --title " Create a SliTaz USB key " \
413 --yes-label "Continue" --yesno \
414 "\nUnlike a hard drive install, the filesystem is kept in a compressed
415 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
416 This should increase responsiveness, protect the filesystem against
417 accidental corruption and reduce read/writes to the USB drive.
418 Once setup, the tazusb utility can rewrite the root filesystem
419 with any changes you have made since booting up,
420 giving the effective benefits of a hard drive install.\n\n
421 /home is mounted on boot using the UUID of your particular flash drive.
422 Unlike a device name, the UUID has the benefit of never changing from machine
423 to machine.\n\n
424 Please plug your USB stick in now.\n
425 " 19 70
426 [ $? -eq 0 ] || return
427 usbdev || return
428 exec 3>&1
429 format=`$DIALOG --clear \
430 --title " Select the filesystem " \
431 --radiolist "\nPlease select the filesystem type to create.\n\n\
432 The filesystem creation will erase all the data \
433 in the USB key." 14 70 4 \
434 "none" "Do not erase the USB key" on \
435 "ext3" "Ext3 journaling filesystem" off \
436 "ext2" "Ext2 filesystem" off \
437 "fat32" "Windows FAT32 filesystem" off \
438 2>&1 1>&3`
439 retval=$?
440 exec 3>&-
441 [ $retval -eq 0 ] || return
442 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
443 tazusb gen-iso2usb /mnt/$ISO $device
444 }
446 mount_loram()
447 {
448 is_loram || return
449 insmod /lib/squashfs.ko* 2> /dev/null
450 if [ -d /media/cdrom/fs ]; then
451 ln -s /media/cdrom/fs /sqfs
452 else
453 mkdir /sqfs
454 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
455 fi
456 ln -s /sqfs/lib/* lib
457 ln -s /sqfs/usr /sqfs/var /
458 for i in dmesg basename tr od reboot poweroff getty sync ; do
459 ln -s /sqfs/bin/busybox /bin/$i
460 done
461 }
463 umount_loram()
464 {
465 is_loram || return
466 rm /var /usr
467 umount -d /sqfs
468 rmdir /sqfs
469 }
471 text()
472 {
473 umount_loram
474 umount -d /media/cdrom
475 umount /mnt
476 umount /proc
477 exec /init
478 }
480 live()
481 {
482 n=0
483 for i in $(ls -r /media/cdrom/boot/rootfs*); do
484 [ $((n++)) -eq 0 ] || uncpio $i
485 done
486 text
487 }
489 restart()
490 {
491 sync
492 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
493 reboot -f
494 }
496 stop()
497 {
498 sync
499 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
500 poweroff -f
501 }
503 shell()
504 {
505 getty -n -l /bin/ash 38400 tty1 || sh
506 }
508 BIN=bin/mount.posixovl
509 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
510 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN
511 mount -t proc /proc /proc
512 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
513 getiso
514 mount_loram
515 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
516 *install*) install ;;
517 *live*) live ;;
518 *text*) text ;;
519 esac
520 which $DIALOG || live
521 dmesg > /tmp/dmesg
523 while true; do
524 cat > /tmp/dialog <<EOT
525 $DIALOG --clear \
526 --title " Welcome to SliTaz " \
527 --menu "" 21 70 15 \
528 "live" "SliTaz RAM boot" \
529 "text" "SliTaz RAM boot (text mode only)" \
530 $(cdfile README "readme" "Show the README file") \
531 $(cdfile md5sum "md5" "Check ISO files") \
532 $(xfile mount.posixovl "install" "Hard disk installation (UMSDOS way)") \
533 $(xfile mount.posixovl "installtaz" "Hard disk installation (TAZUSB way)") \
534 $(xfile tazusb "usbkey" "USB key read/write installation") \
535 "usbbootkey" "USB boot key (read only)" \
536 $(fddata "fdbootstrap" "Floppy bootstrap") \
537 "tazboot" "Get tazboot.exe Linux loader" \
538 $(cdfile Xboot/memtest "memtest" "Get Memtest86") \
539 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
540 $(cdfile Xboot/gpxe "gpxe" "Get SliTaz Web boot utility") \
541 $(cdfile boot/gpxe "fdgpxe" "Create a SliTaz Web boot floppy") \
542 $(xfile reboot "restart" "Restart the computer") \
543 $(xfile poweroff "stop" "Power off") \
544 "bootlog" "Linux boot messages" \
545 "shell" "Shell prompt" \
547 EOT
548 exec 3>&1
549 value=$(sh /tmp/dialog 2>&1 1>&3)
550 retval=$?
551 exec 3>&-
552 [ $retval -eq 0 ] || continue
553 $value
554 done