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

syslinux: add iso2exe
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 13 14:33:27 2012 +0100 (2012-12-13)
parents
children 87a217af01ea
line source
1 #!/bin/sh
3 DIALOG=dialog
5 get()
6 {
7 od -j $1 -N ${3:-4} -t u${3:-4} -An $2
8 }
10 getarg()
11 {
12 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
13 }
15 gettazboot()
16 {
17 echo "Create $(basename $1) ..."
18 O=$(($(get 36 /mnt/$ISO 2) - 0xE0))
19 L=$((0x8000 - $(get 28 /mnt/$ISO 2) - $O))
20 S=$((32+$L))
21 P=$((($S+511)/512))
22 E=$((4096-(32*$P)))
23 for i in 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
24 -2 0 256 -16 28 0x6C53 0x5469 0x7A61; do
25 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
26 xargs echo -en
27 done > $1
28 dd bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1 2> /dev/null
29 }
31 checkmagic()
32 {
33 [ -s $1 ] && [ $(getarg magic) == $(get 28 $1) ]
34 }
36 getiso()
37 {
38 blkid | while read dev info ; do
39 case "$info" in
40 *dos*|*fat*|*ntfs*)
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 esac
49 done
50 }
52 uncpio()
53 {
54 echo "Extracting $(basename $1) ..."
55 case $(get 0 $1 2) in
56 *35615) zcat $1 ;;
57 *\ 93) unlzma -c $1 ;;
58 *) cat $1 ;;
59 esac | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
60 }
62 getuuid()
63 {
64 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
65 blkid | sed "/$dev:/!d;s/.*UUID=.\\([^ ]*\\)\".*/\\1/"
66 }
68 mkinitrd()
69 {
70 echo "Create $(basename $1) ..."
71 for i in bin lib dev proc tmp mnt etc ; do
72 mkdir -p /tmp/fs/$i
73 done
74 for i in /dev/console /dev/null /dev/tty /dev/tty1 /dev/fuse \
75 /dev/hd* /dev/sd* ; do
76 cp -a $i /tmp/fs$i
77 done
78 for i in /bin/busybox /usr/sbin/mount.posixovl $(which blkid); do
79 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
80 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
81 cp $i /tmp/fs/bin
82 done
83 cp -a /lib/ld-* /tmp/fs/lib
84 for i in $(busybox | sed '/Current/,$!d'); do
85 ln -s busybox /tmp/fs/bin/${i%,}
86 done
87 ln -s /proc/mounts /tmp/fs/etc/mtab
88 cat > /tmp/fs/init <<EOT
89 #!/bin/sh
91 arg()
92 {
93 grep -q \$1 /proc/cmdline &&
94 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
95 echo "\$2 \$val"
96 }
98 mount -t proc /proc /proc
99 arg mount "Mount device"
100 mount \$( (blkid /dev/?d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
101 arg subroot "Change root to directory"
102 mount.posixovl /mnt/\$val
103 mount --bind /mnt /mnt/\$val/mnt/dos
104 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
105 umount /proc
106 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
107 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
108 EOT
109 chmod +x /tmp/fs/init
110 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
111 rm -rf /tmp/fs
112 }
114 doinstall()
115 {
116 mkdir /mnt/slitaz
117 mount.posixovl /mnt/slitaz || return
118 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
119 for i in $(ls -r /media/cdrom/boot/rootfs*); do
120 uncpio $i /mnt/slitaz
121 done
122 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
123 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
124 /media/cdrom/README /media/cdrom/boot/memtest* ; do
125 [ -s $i ] && cp $i /mnt/slitaz/boot
126 done
127 umount -d /media/cdrom
128 gettazboot /mnt/slitaz/boot/tazboot.exe
129 mkinitrd /mnt/slitaz/boot/initrd
130 cat > /mnt/slitaz/boot/tazboot.cmd <<EOT
131 kernel=\\slitaz\\boot\\bzimage
132 initrd=\\slitaz\\boot\\initrd
133 rw root=/dev/null mount=$(getuuid) subroot=slitaz autologin
134 EOT
135 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
136 /mnt/slitaz/boot/README /mnt/slitaz/boot/tazboot.cmd
137 [ -x /mnt/slitaz/usr/sbin/mount.posixovl ] ||
138 cp /usr/sbin/mount.posixovl /mnt/slitaz/usr/sbin
139 ! grep -qs tazboot /mnt/boot.ini && cat >> /mnt/boot.ini <<EOT
140 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
141 EOT
142 grep -qs menuitem /mnt/config.sys && !grep -q tazboot /mnt/config.sys &&
143 sed -i 's/menudefault=/menuitem=slitaz,SliTaz\n&/' /mnt/config.sys &&
144 cat >> /mnt/config.sys <<EOT
145 [slitaz]
146 device=\\slitaz\\boot\\tazboot.exe
147 EOT
148 }
150 install()
151 {
152 $DIALOG --clear \
153 --title " SliTaz UMSDOS like installation " \
154 --yes-label "Install" --yesno \
155 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
156 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
157 You will start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
158 To uninstall SliTaz, you have only to remove this directory.
159 The file \\boot.ini or \\config.sys may be modified too.\n\n
160 To do a traditionnal installation with disk partitioning
161 start SliTaz Live with 'SliTaz RAM boot' menu.\n
162 " 16 70
163 [ $? -eq 0 -a -x /usr/sbin/mount.posixovl ] || return
164 doinstall
165 umount /proc
166 exec chroot /mnt/slitaz /sbin/init
167 }
169 tazboot()
170 {
171 $DIALOG --clear \
172 --title " SliTaz bootloader for DOS " \
173 --yes-label "Install" --yesno \
174 "\nThe file TAZBOOT.EXE will be created in top directory. It supports
175 bzImage linux kernel, multiples initramfs, kernel command line and
176 ISO image file loopback (can retrieve files from an ISO file).\n\n
177 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
178 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
179 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
180 Examples for tazboot.cmd:\n\n\
181 iso=\\isos\\slitaz-4.0.iso\n\
182 kernel=boot/bzImage\n\
183 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
184 rw root=/dev/null autologin\n\n\
185 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
186 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
187 " 24 78
188 [ $? -eq 0 ] || return
189 gettazboot /mnt/tazboot.exe
190 }
192 text()
193 {
194 umount -d /media/cdrom
195 umount /mnt
196 umount /proc
197 exec /init
198 }
200 live()
201 {
202 n=0
203 for i in $(ls -r /media/cdrom/boot/rootfs*); do
204 [ $((n++)) -eq 0 ] || uncpio $i
205 done
206 text
207 }
209 reboot()
210 {
211 umount -d /media/cdrom
212 umount /mnt
213 /sbin/reboot -f
214 }
216 poweroff()
217 {
218 umount -d /media/cdrom
219 umount /mnt
220 /sbin/poweroff -f
221 }
223 [ -x /usr/sbin/mount.posixovl ] ||
224 mv /usr/sbin/mount.posixovl.iso2exe /usr/sbin/mount.posixovl
225 mount -t proc /proc /proc
226 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
227 getiso
228 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
229 *install*) install ;;
230 *live*) live ;;
231 *text*) text ;;
232 esac
234 while true; do
235 exec 3>&1
236 value=`$DIALOG --clear \
237 --title " Welcome to SliTaz " \
238 --menu "\nPlease select" 15 70 7 \
239 "live" "SliTaz RAM boot" \
240 "text" "SliTaz RAM boot (text mode only)" \
241 "install" "Hard disk installation" \
242 "tazboot" "Get tazboot.exe Linux loader" \
243 "reboot" "Restart the computer" \
244 "poweroff" "Power off" \
245 "ash" "Shell prompt" \
246 2>&1 1>&3`
247 retval=$?
248 exec 3>&-
249 [ $retval -eq 0 ] || continue
250 $value
251 done