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

syslinux/iso2exe: add partition table
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 01 17:47:04 2013 +0200 (2013-04-01)
parents bf8be127c60b
children d965b76fefd1
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) - 0xE0))
20 L=$((0x8000 - $(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 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 /mnt/slitaz
121 mount.posixovl -F /mnt/slitaz || return
122 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
123 if [ -d /media/cdrom/fs ]; then
124 cp -a /media/cdrom/fs/. /mnt/slitaz
125 elif is_loram ; then
126 for i in /media/cdrom/boot/rootfs?.* ; do
127 [ -s $(basename $i) ] && continue
128 cpio -i $i
129 done
130 for i in $(ls -r /media/cdrom/boot/rootfs*); do
131 mount -o loop,ro $i /sqfs/mnt
132 cp -a /sqfs/mnt/. /mnt/slitaz
133 umount -d /sqfs/mnt
134 done
135 else
136 for i in $(ls -r /media/cdrom/boot/rootfs*); do
137 uncpio $i /mnt/slitaz
138 done
139 fi
140 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
141 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
142 /media/cdrom/README /media/cdrom/boot/memtest* ; do
143 [ -s $i ] && cp $i /mnt/slitaz/boot
144 done
145 umount -d /media/cdrom
146 gettazboot /mnt/slitaz/boot/tazboot.exe
147 mkinitrd /mnt/slitaz/boot/initrd
148 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
149 kernel=\\slitaz\\boot\\bzimage
150 initrd=\\slitaz\\boot\\initrd
151 rw root=/dev/null mount=$(getuuid) subroot=slitaz autologin
152 EOT
153 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
154 /mnt/slitaz/boot/README
155 [ -x /mnt/slitaz/usr/sbin/mount.posixovl ] ||
156 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
157 ! grep -qs tazboot /mnt/boot.ini && unix2dos >> /mnt/boot.ini <<EOT
158 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
159 EOT
160 grep -qs menuitem /mnt/config.sys && !grep -q tazboot /mnt/config.sys &&
161 sed -i 's/menudefault=/menuitem=slitaz,SliTaz\n&/' /mnt/config.sys &&
162 cat >> /mnt/config.sys <<EOT
163 [slitaz]
164 device=\\slitaz\\boot\\tazboot.exe
165 EOT
166 unix2dos /mnt/config.sys
167 }
169 install()
170 {
171 $DIALOG --clear \
172 --title " SliTaz UMSDOS like installation " \
173 --yes-label "Install" --yesno \
174 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
175 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
176 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
177 To uninstall SliTaz, you have only to remove this directory.
178 The file \\boot.ini or \\config.sys may be modified too.\n\n
179 To do a traditional installation with disk partitioning,
180 start SliTaz Live with 'SliTaz RAM boot' menu.\n
181 " 16 70
182 [ $? -eq 0 -a -n "$(which mount.posixovl)" ] || return
183 doinstall
184 umount /proc
185 exec chroot /mnt/slitaz /sbin/init
186 }
188 tazboot()
189 {
190 $DIALOG --clear \
191 --title " SliTaz bootloader for DOS " \
192 --yes-label "Install" --yesno \
193 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
194 a bzImage linux kernel, multiple initramfs, a kernel command line and
195 an ISO image file loopback (retrieves files from an ISO file).\n\n
196 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
197 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
198 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
199 Examples for tazboot.cmd:\n\n\
200 iso=\\isos\\slitaz-4.0.iso\n\
201 kernel=boot/bzImage\n\
202 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
203 rw root=/dev/null autologin\n\n\
204 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
205 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
206 " 24 78
207 [ $? -eq 0 ] || return
208 gettazboot /mnt/tazboot.exe
209 }
211 md5()
212 {
213 echo "Checking files..."
214 ( cd /media/cdrom ; md5sum -c md5sum ) > /tmp/data
215 $DIALOG --clear \
216 --title " Checked files " \
217 --textbox /tmp/data 24 78
218 rm -f /tmp/data
219 }
221 readme()
222 {
223 $DIALOG --clear \
224 --title " Readme " \
225 --textbox /media/cdrom/README 24 78
226 }
228 bootlog()
229 {
230 $DIALOG --clear \
231 --title " Linux boot messages " \
232 --textbox /tmp/dmesg 24 78
233 }
235 memtest()
236 {
237 $DIALOG --clear \
238 --title " Memtest86 " \
239 --yes-label "Install" --yesno \
240 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
241 computers. BIOS based memory tests are a quick, cursory check and often
242 miss many of the failures that are detected by Memtest86.\n
243 " 12 70
244 [ $? -eq 0 ] || return
245 cp /media/cdrom/boot/memtest /mnt
246 }
248 fdmemtest()
249 {
250 $DIALOG --clear \
251 --title " Create a Memtest86 boot floppy " \
252 --yes-label "Create floppy" --yesno \
253 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
254 computers. BIOS based memory tests are a quick, cursory check and often
255 miss many of the failures that are detected by Memtest86.\n\n
256 Please insert a blank disk in floppy drive.\n
257 " 12 70
258 [ $? -eq 0 ] || return
259 dd if=/media/cdrom/boot/memtest of=/dev/fd0
260 }
262 gpxe()
263 {
264 $DIALOG --clear \
265 --title " SliTaz Web boot " \
266 --yes-label "Install" --yesno \
267 "\nBoot your operating system from the internet and enjoy a full system
268 working entirely in RAM with speed and stability in mind. The Linux Kernel
269 and the complete SliTaz compressed root filesystem will be loaded into RAM
270 from the Web using PXE and HTTP protocols.\n
271 " 12 70
272 [ $? -eq 0 ] || return
273 cp /media/cdrom/boot/gpxe /mnt
274 }
276 fdgpxe()
277 {
278 $DIALOG --clear \
279 --title " Create a SliTaz Web boot floppy " \
280 --yes-label "Create floppy" --yesno \
281 "\nBoot your operating system from the internet and enjoy a full system
282 working entirely in RAM with speed and stability in mind. The Linux Kernel
283 and the complete SliTaz compressed root filesystem will be loaded into RAM
284 from the Web using PXE and HTTP protocols.\n\n
285 Please insert a blank disk in floppy drive.\n
286 " 12 70
287 [ $? -eq 0 ] || return
288 dd if=/media/cdrom/boot/gpxe of=/dev/fd0
289 }
291 cdfile()
292 {
293 [ -s /media/cdrom/$1 ] && echo -en "$2 ${3// /.}"
294 }
296 fdbootstrap()
297 {
298 sz=$(echo $(get 28 /mnt/$ISO 1))
299 if [ 0$sz -eq 0 ]; then
300 $DIALOG --clear \
301 --title " No floppy bootstrap available " \
302 --msgbox \
303 "\nThe floppy bootstrap code is not available in the ISO file.\n\n
304 Can't create the floppy.\n
305 " 9 70
306 else
307 $DIALOG --clear \
308 --title " Create a floppy bootstrap " \
309 --yes-label "Continue" --yesno \
310 "\nThe floppy will install a driver to access the ISO file
311 on your hard disk and will emulate a CD-ROM during the boot process.\n\n
312 Please insert a floppy in drive now.\n
313 " 10 70
314 [ $? -eq 0 ] || return
315 dd if=/mnt/$ISO of=/dev/fd0 bs=1 count=$(($sz * 512)) \
316 skip=$(( $(get 66 /mnt/$ISO) - ($sz * 512) ))
317 echo "$ISO" | dd of=/dev/fd0 bs=512 seek=1
318 fi
319 }
321 usbdev()
322 {
323 sleep 5
324 DEV="$(grep -l 1 /sys/block/*/removable | \
325 sed 's|/sys/block/\(.*\)/removable|\1|')"
326 [ -n "$DEV" ] || return
327 exec 3>&1
328 device=`$DIALOG --clear \
329 --title " Select your USB key " \
330 --menu "\nPlease select the USB key according to its known size.\n\n" \
331 14 70 4 \
332 $(for i in $DEV ; do
333 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
334 done) \
335 2>&1 1>&3`
336 retval=$?
337 exec 3>&-
338 [ $retval -eq 0 ]
339 }
341 usbbootkey()
342 {
343 $DIALOG --clear \
344 --title " Create a USB boot key " \
345 --yes-label "Continue" --yesno \
346 "\nThe USB key will be used like a CD-ROM. You will not be able to write
347 any data on it.\n\n
348 You should choose 'USB key read/write installation' to be
349 able to save the package updates or your own configuration and data files.\n\n
350 Please plug your USB stick in now.\n
351 " 13 70
352 [ $? -eq 0 ] || return
353 usbdev || return
354 dd if=/mnt/$ISO of=$device
355 }
357 usbkey()
358 {
359 $DIALOG --clear \
360 --title " Create a SliTaz USB key " \
361 --yes-label "Continue" --yesno \
362 "\nUnlike a hard drive install, the filesystem is kept in a compressed
363 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
364 This should increase responsiveness, protect the filesystem against
365 accidental corruption and reduce read/writes to the USB drive.
366 Once setup, the tazusb utility can rewrite the root filesystem
367 with any changes you have made since booting up,
368 giving the effective benefits of a hard drive install.\n\n
369 /home is mounted on boot using the UUID of your particular flash drive.
370 Unlike a device name, the UUID has the benefit of never changing from machine
371 to machine.\n\n
372 Please plug your USB stick in now.\n
373 " 19 70
374 [ $? -eq 0 -a -n "$(which tazusb)" ] || return
375 usbdev || return
376 exec 3>&1
377 format=`$DIALOG --clear \
378 --title " Select the filesystem " \
379 --radiolist "\nPlease select the filesystem type to create.\n\n\
380 The filesystem creation will erase all the data \
381 in the USB key." 14 70 4 \
382 "none" "Do not erase the USB key" on \
383 "ext3" "Ext3 journaling filesystem" off \
384 "ext2" "Ext2 filesystem" off \
385 "fat32" "Windows FAT32 filesystem" off \
386 2>&1 1>&3`
387 retval=$?
388 exec 3>&-
389 [ $retval -eq 0 ] || return
390 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
391 tazusb gen-iso2usb /mnt/$ISO $device
392 }
394 mount_loram()
395 {
396 is_loram || return
397 insmod /lib/squashfs.ko* 2> /dev/null
398 if [ -d /media/cdrom/fs ]; then
399 ln -s /media/cdrom/fs /sqfs
400 else
401 mkdir /sqfs
402 mount -o loop,ro -t squashfs /rootfs*.gz /sqfs
403 fi
404 ln -s /sqfs/lib/* lib
405 ln -s /sqfs/usr /sqfs/var /
406 for i in dmesg basename tr od reboot poweroff getty sync ; do
407 ln -s /sqfs/bin/busybox /bin/$i
408 done
409 }
411 umount_loram()
412 {
413 is_loram || return
414 rm /var /usr
415 umount -d /sqfs
416 rmdir /sqfs
417 }
419 text()
420 {
421 umount_loram
422 umount -d /media/cdrom
423 umount /mnt
424 umount /proc
425 exec /init
426 }
428 live()
429 {
430 n=0
431 for i in $(ls -r /media/cdrom/boot/rootfs*); do
432 [ $((n++)) -eq 0 ] || uncpio $i
433 done
434 text
435 }
437 restart()
438 {
439 sync
440 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
441 reboot -f
442 }
444 stop()
445 {
446 sync
447 [ ! -L /sqfs ] && umount -d /media/cdrom && umount /mnt
448 poweroff -f
449 }
451 shell()
452 {
453 getty -n -l /bin/ash 38400 tty1 || sh
454 }
456 BIN=bin/mount.posixovl
457 [ -x /usr/s$BIN ] || mv /bin/mount.posixovl.iso2exe \
458 /usr/s$BIN 2> /dev/null || mv /bin/mount.posixovl.iso2exe /$BIN
459 mount -t proc /proc /proc
460 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
461 getiso
462 mount_loram
463 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
464 *install*) install ;;
465 *live*) live ;;
466 *text*) text ;;
467 esac
468 which $DIALOG || live
469 dmesg > /tmp/dmesg
471 while true; do
472 exec 3>&1
473 value=`$DIALOG --clear \
474 --title " Welcome to SliTaz " \
475 --menu "\nPlease select" 19 70 11 \
476 "live" "SliTaz RAM boot" \
477 "text" "SliTaz RAM boot (text mode only)" \
478 $(cdfile README "readme" "Show the README file") \
479 $(cdfile md5sum "md5" "Check ISO files") \
480 "install" "Hard disk installation" \
481 "usbkey" "USB key read/write installation" \
482 "usbbootkey" "USB boot key (read only)" \
483 "fdbootstrap" "Floppy bootstrap" \
484 "tazboot" "Get tazboot.exe Linux loader" \
485 $(cdfile Xboot/memtest "memtest" "Get Memtest86") \
486 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
487 $(cdfile Xboot/gpxe "gpxe" "Get SliTaz Web boot utility") \
488 $(cdfile boot/gpxe "fdgpxe" "Create a SliTaz Web boot floppy") \
489 "restart" "Restart the computer" \
490 "stop" "Power off" \
491 "bootlog" "Linux boot messages" \
492 "shell" "Shell prompt" \
493 2>&1 1>&3`
494 retval=$?
495 exec 3>&-
496 [ $retval -eq 0 ] || continue
497 $value
498 done