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

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