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