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

isolinux/iso2exe: fix mkfloppy
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Dec 29 18:24:44 2014 +0100 (2014-12-29)
parents f889c01820d0
children 054f70cb9bec
line source
1 #!/bin/sh
3 DIALOG=dialog
5 ddq()
6 {
7 dd $@ 2> /dev/null
8 }
10 get()
11 {
12 od -j $1 -N ${3:-2} -t u${3:-2} -An $2 2> /dev/null ||
13 hexdump -s $1 -n ${3:-2} -e "\"\" 1/${3:-2} \"%d\"" $2
14 }
16 getarg()
17 {
18 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
19 }
21 mount_proc()
22 {
23 mount -t proc /proc /proc
24 mount -t sysfs /sys /sys
25 udevd --daemon 2> /dev/null && udevadm trigger && sleep 5
26 }
28 umount_proc()
29 {
30 killall udevd 2> /dev/null
31 umount /sys/fs/fuse/connections 2> /dev/null
32 umount /sys
33 umount /proc
34 }
36 bytes2bin()
37 {
38 for i in $@ ; do
39 printf '\\\\x%02X' $(($i&255)) | xargs echo -en
40 done
41 }
43 words2bin()
44 {
45 for i in $@ ; do
46 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
47 xargs echo -en
48 done
49 }
51 gettazboot()
52 {
53 echo "Creating $(basename $1) ..."
54 O=$(($(get 66 /mnt/$ISO) - 0xC0))
55 L=$((0x7EE0 - $(get 24 /mnt/$ISO) - $O))
56 S=$((32+$L))
57 P=$((($S+511)/512))
58 E=$((4096-(32*$P)))
59 words2bin 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
60 -2 0 256 -16 28 0x6C53 0x5469 0x7A61 > $1
61 ddq bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1
62 }
64 checkmagic()
65 {
66 [ -s $1 ] && [ $(getarg magic) == $(get 24 $1 4) ]
67 }
69 getiso()
70 {
71 mkdir -p /media/cdrom
72 blkid | while read dev info ; do
73 mount ${dev%:} /mnt
74 if checkmagic /mnt/$ISO; then
75 mount -o loop,ro /mnt/$ISO /media/cdrom
76 echo "Found $ISO on ${dev%:}"
77 break
78 fi
79 umount /mnt
80 done
81 }
83 uncpio()
84 {
85 echo -en "\n Extracting $(basename $1) ..."
86 case $(get 0 $1) in
87 *35615) ( zcat || gunzip ) ;;
88 *14333) unxz ;;
89 *\ 93) unlzma ;;
90 *) cat ;;
91 esac < $1 | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
92 }
94 dotwait()
95 {
96 echo -n "${1:-Install filesystem}.."
97 touch /tmp/wait
98 while [ -e /tmp/wait ]; do
99 echo -n "." > /dev/tty0
100 sleep 1
101 done &
102 }
104 getuuid()
105 {
106 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
107 blkid | sed "/$dev:/!d;s/.* UUID=.\\([^ ]*\\)\".*/\\1/"
108 }
110 tazusbinitfs()
111 {
112 PAD=$(($(stat -c %s $1) % 4))
113 [ $PAD -ne 0 ] && ddq if=/dev/zero bs=1 count=$((4 - $PAD)) >> $1
114 mkdir -p /tmp/fs/etc /tmp/fs/lib /tmp/fs/home
115 cp -a /etc/locale.conf /tmp/fs/etc 2> /dev/null
116 cp -a /etc/keymap.conf /tmp/fs/etc 2> /dev/null
117 sed 's/ .*//' /proc/modules | while read mod ; do
118 find /lib/modules/ | grep $mod.ko | \
119 sed 's|.*|cp & /tmp/fs/lib|' | sh
120 done
121 cat > /tmp/fs/init1 <<EOT
122 #!/bin/sh
123 sed -i 's|sbin/init|init2|' /init
124 exec /init
125 EOT
126 cat > /tmp/fs/init2 <<EOT
127 #!/bin/sh
129 mount -t proc /proc /proc
130 grep -q debug /proc/cmdline && debug=true && set -x
131 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
132 v=\$(sed '/\\/home=/!d;s|.*/home=\\([^ ]*\\).*|\\1|' /proc/cmdline /cmdline 2> /dev/null)
133 mount / -o remount,rw
134 mkdir /mnt/dos
135 rm -f /cmdline 2> /dev/null
136 mount / -o remount,ro
137 mnt=/mnt/dos/\${v#*/}
138 dev=\$( (blkid /dev/[sh]d* || blkid) | grep \${v%%/*} | sed 's/:.*//;q')
139 echo "Mount \$dev in /mnt/dos for \$v..." | tee -a /run/boot.log
140 mount \$dev /mnt/dos
141 mount.posixovl -F \$mnt -- -oallow_other -odefault_permissions -osuid
142 mount --bind \$mnt /home
143 [ "\$debug" = "true" ] && sh
144 umount /proc
145 exec /sbin/init
146 EOT
147 chmod 755 /tmp/fs/init?
148 cp -a /tmp/fs/* /
149 ( cd /tmp/fs ; find * | cpio -o -H newc ) | gzip -9 >> $1
150 }
152 mkinitrd()
153 {
154 dotwait "Creating $(basename $1) "
155 for i in bin lib dev proc tmp mnt etc ; do
156 mkdir -p /tmp/fs/$i
157 done
158 for i in /dev/console /dev/null /dev/tty /dev/tty0 /dev/tty1 /dev/tty2 \
159 /dev/fuse /dev/hd* /dev/sd* ; do
160 cp -a $i /tmp/fs$i
161 done
162 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
163 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
164 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
165 cp $i /tmp/fs/bin
166 done
167 cp -a /lib/ld-* /tmp/fs/lib
168 for i in $(busybox | sed '/Current/,$!d'); do
169 [ -e /tmp/fs/bin/${i%,} ] || ln -s busybox /tmp/fs/bin/${i%,}
170 done
171 ln -s /proc/mounts /tmp/fs/etc/mtab
172 sed 's/ .*//' /proc/modules | while read mod ; do
173 find /lib/modules/ | grep $mod.ko | \
174 sed 's|.*|cp & /tmp/fs/lib|' | sh
175 done
176 cat > /tmp/fs/init <<EOT
177 #!/bin/sh
179 arg()
180 {
181 grep -q \$1 /proc/cmdline &&
182 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
183 echo "\$2 \$val"
184 }
186 mount -t proc /proc /proc
187 arg debug "shell" && debug=true && set -x
188 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
189 arg mount "Mount device"
190 mount \$( (blkid /dev/[sh]d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
191 arg subroot "Change root to directory"
192 mount.posixovl -F /mnt/\$val -- -oallow_other -odefault_permissions -osuid
193 mount --bind /mnt /mnt/\$val/mnt/dos
194 mount -o size=0 -t tmpfs tmpfs /mnt/\$val/mnt/dos/\$val
195 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
196 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
197 [ "$debug" = "true" ] && sh
198 umount /proc
199 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
200 EOT
201 chmod +x /tmp/fs/init
202 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
203 rm -rf /tmp/fs /tmp/wait
204 }
206 is_loram()
207 {
208 [ -s /lib/squashfs.ko* ]
209 }
211 ls_r()
212 {
213 ls -r $@ 2> /dev/null || ls $@
214 }
216 doinstall()
217 {
218 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
219 if ! mount.posixovl -F /mnt/slitaz -- \
220 -oallow_other -odefault_permissions -osuid; then
221 echo "Can't install SliTaz. Abort."
222 sleep 5
223 return 1
224 fi
225 dowait "Install root filesystem in /slitaz.."
226 if [ "$1" ]; then
227 if [ -d /media/cdrom/fs ]; then
228 ( cd /mnt/slitaz/fs; find | cpio -o -H newc ) | gzip -9
229 else
230 ls_r /media/cdrom/boot/rootfs* | xargs cat
231 fi > /mnt/slitaz/boot/rootfs.gz
232 tazusbinitfs /mnt/slitaz/boot/rootfs.gz
233 initrd=rootfs.gz
234 extraargs="/home=$(getuuid)/slitaz rdinit=/init1"
235 else
236 if [ -d /media/cdrom/fs ]; then
237 cp -a /media/cdrom/fs/. /mnt/slitaz
238 elif is_loram ; then
239 for i in /media/cdrom/boot/rootfs?*.* ; do
240 [ -s $(basename $i) ] && continue
241 cpio -i $i
242 done
243 for i in $(ls_r /media/cdrom/boot/rootfs*); do
244 mount -o loop,ro $i /sqfs/mnt
245 cp -a /sqfs/mnt/. /mnt/slitaz
246 umount -d /sqfs/mnt
247 done
248 else
249 for i in $(ls_r /media/cdrom/boot/rootfs*); do
250 uncpio $i /mnt/slitaz
251 done
252 fi
253 cp -a /etc/locale.conf /tmp/slitaz/etc 2> /dev/null
254 cp -a /etc/keymap.conf /tmp/slitaz/etc 2> /dev/null
255 mkinitrd /mnt/slitaz/boot/initrd
256 initrd=initrd
257 extraargs="mount=$(getuuid) subroot=slitaz"
258 fi
259 echo -en "\nInstall boot files..."
260 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
261 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
262 /media/cdrom/README /media/cdrom/boot/memtest* ; do
263 [ -s $i ] && cp $i /mnt/slitaz/boot
264 done
265 for i in /mnt/slitaz/boot/memtest /mnt/slitaz/boot/*pxe* ; do
266 [ $(get 0 $i 2> /dev/null || echo 0) -eq 23117 ] &&
267 mv $i $i.exe
268 done
269 gettazboot /mnt/slitaz/boot/tazboot.exe
270 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
271 kernel=/slitaz/boot/bzimage
272 initrd=/slitaz/boot/$initrd
273 rw root=/dev/null $extraargs autologin
274 EOT
275 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
276 /mnt/slitaz/boot/README
277 [ -d /mnt/slitaz/usr/sbin -a ! -x /mnt/slitaz/usr/sbin/mount.posixovl ] &&
278 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
279 rm -f /tmp/wait
280 false &&
281 [ -s /mnt/boot.ini ] && ! grep -qs tazboot /mnt/boot.ini &&
282 echo "Update boot.ini ..." && unix2dos >> /mnt/boot.ini <<EOT
283 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
284 EOT
285 false &&
286 grep -qis menuitem /mnt/config.sys && ! grep -qi tazboot /mnt/config.sys &&
287 echo "Update config.sys ..." &&
288 sed -i 's/menudefault/menuitem SLITAZ, SliTaz\r\n&/' /mnt/config.sys &&
289 unix2dos >> /mnt/config.sys <<EOT