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

cpige: add man page for cook.slitaz.org
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Feb 13 16:56:42 2019 +0100 (21 months ago)
parents 48ae45dab042
children c89d25976dbe
line source
1 #!/bin/sh
3 DIALOG=dialog
5 ddq()
6 {
7 dd $@ 2> /dev/null
8 }
10 get()
11 {
12 od -v -j $1 -N ${4:-${3:-2}} -t u${3:-2} -w${3:-2} -An $2 2>/dev/null ||
13 hexdump -v -s $1 -n ${4:-${3:-2}} -e "\"\" 1/${3:-2} \" %d\n\"" $2
14 }
16 getarg()
17 {
18 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
19 }
21 clear()
22 {
23 echo -e "\x1B[1;1H\x1B[J"
24 }
26 xless()
27 {
28 [ $(wc -l < "$1") -gt 22 ] &&
29 sed 's/..3.;4.m/===/g;$s/.*/&\n---\nPress q to continue/' "$1" | less ||
30 { cat "$1"
31 [ "$2" ] || return
32 echo -e "$2"
33 read n
34 }
35 }
37 tinydialog()
38 {
39 clear
40 label=""
41 while [ "$1" ]; do
42 case "$1" in
43 --title) title=" \x1B[30;47m$2\x1B[37;40m\n"
44 echo -e $title; shift ;;
45 --yes-label) label="$2" ; shift ;;
46 --textbox)
47 xless "$2" "\nPress RETURN to continue."
48 break;;
49 --gauge)
50 t=" "
51 echo -e "$t$2\n"
52 while read pct ; do
53 s=" "
54 s="$s$pct%$s"
55 pct=$((($pct*63)/100))
56 echo -en "\r$t\x1B[30;47m$(echo "$s" | cut -c-$pct)\x1B[37;40m$(echo "$s" | cut -c$(($pct+1))-)"
57 done
58 break;;
59 --yesno)
60 while true; do
61 clear
62 echo "$2" | sed 's/\\n\\n/\\n/g;s/\\n/\n/g'
63 echo -en " <- 1:${label:-Yes} 2:Cancel\r"
64 read x
65 case "$x" in
66 ''|Y*|y*|1) return 0;;
67 N*|n*|2|0) return 1;;
68 esac
69 done ;;
70 --menu|--radiolist)
71 [ "$1" == "--menu" ] && shft=2 || shft=3
72 label=""
73 [ "$2" ] && label="\n$2"
74 shift 5
75 echo -e "$title$label\n0 Cancel" > /tmp/data
76 n=1
77 while [ "$1" ]; do
78 eval key_$n='$1'
79 echo "$((n++)) $2"
80 shift $shft
81 done >> /tmp/data
82 while ! grep -q "^$n " /tmp/data ; do
83 clear
84 xless /tmp/data
85 echo -en "\n <- Enter the selection number\r"
86 read n
87 done 2> /dev/null
88 rm -f /tmp/data
89 [ $n -eq 0 ] && return 1
90 eval echo -n \$key_$n 1>&2
91 return 0;;
92 esac
93 shift
94 done
95 }
97 mount_proc()
98 {
99 mount -t proc /proc /proc
100 mount -t sysfs /sys /sys
101 udevd --daemon 2> /dev/null && udevadm trigger && sleep 5
102 }
104 umount_proc()
105 {
106 killall udevd 2> /dev/null
107 umount /sys/fs/fuse/connections 2> /dev/null
108 umount /sys
109 umount /proc
110 }
112 bytes2bin()
113 {
114 for i in $@ ; do
115 printf '\\\\x%02X' $(($i&255))
116 done | xargs echo -en
117 }
119 words2bin()
120 {
121 for i in $@ ; do
122 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255))
123 done | xargs echo -en
124 }
126 gettazboot()
127 {
128 echo "Creating $(basename $1) ..."
129 O=$(($(get 64 /mnt/$ISO) - 0xC0))
130 L=$(($(get 20 /mnt/$ISO) - 0xC0 - $(get 24 /mnt/$ISO) - $O))
131 S=$((32+$L))
132 P=$((($S+511)/512))
133 E=$((4096-(32*$P)))
134 words2bin 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
135 -2 0 256 -16 28 0x6C53 0x5469 0x7A61 > $1
136 ddq bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1
137 }
139 checkmagic()
140 {
141 [ -s $1 ] && [ $(getarg magic) == $(get 18 $1 2) ]
142 }
144 getiso()
145 {
146 mkdir -p /media/cdrom
147 for dev in /sys/block/?d?/?d??* ; do
148 mount /dev/$(basename $dev) /mnt
149 if checkmagic /mnt/$ISO; then
150 mount -o loop,ro /mnt/$ISO /media/cdrom
151 echo "Found $ISO on $(basename $dev)"
152 return 0
153 fi
154 umount /mnt
155 done 2> /dev/null
156 return 1
157 }
159 uncpio()
160 {
161 [ -s "$1" ] || return
162 echo -en "\n Extracting $(basename $1) ..."
163 case $(get 0 $1) in
164 *35615) ( zcat || gunzip ) ;;
165 *14333) unxz ;;
166 *\ 93) unlzma ;;
167 *) cat ;;
168 esac < $1 | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
169 }
171 dotwait()
172 {
173 echo -n "${1:-Install filesystem}.."
174 touch /tmp/wait
175 while [ -e /tmp/wait ]; do
176 echo -n "." > /dev/tty0
177 sleep 1
178 done &
179 }
181 getuuid()
182 {
183 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
184 blkid | sed "/$dev:/!d;s/.* UUID=.\\([^ ]*\\)\".*/\\1/"
185 }
187 tazusbinitfs()
188 {
189 PAD=$(($(stat -c %s $1) % 4))
190 [ $PAD -ne 0 ] && ddq if=/dev/zero bs=1 count=$((4 - $PAD)) >> $1
191 mkdir -p /tmp/fs/etc /tmp/fs/lib /tmp/fs/home
192 cp -a /etc/locale.conf /tmp/fs/etc 2> /dev/null
193 cp -a /etc/keymap.conf /tmp/fs/etc 2> /dev/null
194 sed 's/ .*//' /proc/modules | while read mod ; do
195 find /lib/modules/ | grep $mod.ko | \
196 sed 's|.*|cp & /tmp/fs/lib|' | sh
197 done
198 cat > /tmp/fs/init1 <<EOT
199 #!/bin/sh
200 sed -i 's|sbin/init|init2|' /init
201 exec /init
202 EOT
203 cat > /tmp/fs/init2 <<EOT
204 #!/bin/sh
206 mount -t proc /proc /proc
207 grep -q debug /proc/cmdline && debug=true && set -x
208 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
209 v=\$(sed '/\\/home=/!d;s|.*/home=\\([^ ]*\\).*|\\1|' /proc/cmdline /cmdline 2> /dev/null)
210 mount / -o remount,rw
211 mkdir /mnt/dos
212 rm -f /cmdline 2> /dev/null
213 mount / -o remount,ro
214 mnt=/mnt/dos/\${v#*/}
215 dev=\$( (blkid /dev/[sh]d* || blkid) | grep \${v%%/*} | sed 's/:.*//;q')
216 echo "Mount \$dev in /mnt/dos for \$v..." | tee -a /run/boot.log
217 mount \$dev /mnt/dos
218 mount.posixovl -F \$mnt -- -oallow_other -odefault_permissions -osuid
219 mount --bind \$mnt /home
220 [ "\$debug" = "true" ] && sh
221 umount /proc
222 exec /sbin/init
223 EOT
224 chmod 755 /tmp/fs/init?
225 cp -a /tmp/fs/* /
226 ( cd /tmp/fs ; find * | cpio -o -H newc ) | gzip -9 >> $1
227 }
229 mkinitrd()
230 {
231 dotwait "Creating $(basename $1) "
232 for i in bin lib dev proc tmp mnt etc ; do
233 mkdir -p /tmp/fs/$i
234 done
235 for i in /dev/console /dev/null /dev/tty /dev/tty0 /dev/tty1 /dev/tty2 \
236 /dev/fuse /dev/hd* /dev/sd* ; do
237 cp -a $i /tmp/fs$i
238 done
239 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
240 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
241 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
242 cp $i /tmp/fs/bin
243 done
244 cp -a /lib/ld-* /tmp/fs/lib
245 for i in $(busybox | sed '/Current/,$!d'); do
246 [ -e /tmp/fs/bin/${i%,} ] || ln -s busybox /tmp/fs/bin/${i%,}
247 done
248 ln -s /proc/mounts /tmp/fs/etc/mtab
249 sed 's/ .*//' /proc/modules | while read mod ; do
250 find /lib/modules/ | grep $mod.ko | \
251 sed 's|.*|cp & /tmp/fs/lib|' | sh
252 done
253 cat > /tmp/fs/init <<EOT
254 #!/bin/sh
256 arg()
257 {
258 grep -q \$1 /proc/cmdline &&
259 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
260 echo "\$2 \$val"
261 }
263 mount -t proc /proc /proc
264 arg debug "shell" && debug=true && set -x
265 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
266 arg mount "Mount device"
267 mount \$( (blkid /dev/[sh]d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
268 arg subroot "Change root to directory"
269 mount.posixovl -F /mnt/\$val -- -oallow_other -odefault_permissions -osuid
270 mount --bind /mnt /mnt/\$val/mnt/dos
271 mount -o size=0 -t tmpfs tmpfs /mnt/\$val/mnt/dos/\$val
272 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
273 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
274 [ "$debug" = "true" ] && sh
275 umount /proc
276 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
277 EOT
278 chmod +x /tmp/fs/init
279 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
280 rm -rf /tmp/fs /tmp/wait
281 }
283 is_loram()
284 {
285 [ -s /lib/squashfs.ko* ]
286 }
288 ls_r()
289 {
290 ls -r $@ 2> /dev/null || ls $@
291 }
293 doinstall()
294 {
295 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
296 if ! mount.posixovl -F /mnt/slitaz -- \
297 -oallow_other -odefault_permissions -osuid; then
298 echo "Can't install SliTaz. Abort."
299 sleep 5
300 return 1
301 fi
302 dotwait "Install root filesystem in /slitaz.."
303 if [ "$1" ]; then
304 if [ -d /media/cdrom/fs ]; then
305 ( cd /mnt/slitaz/fs; find | cpio -o -H newc ) | gzip -9
306 else
307 ls_r /media/cdrom/boot/rootfs*gz | xargs cat
308 fi > /mnt/slitaz/boot/rootfs.gz
309 tazusbinitfs /mnt/slitaz/boot/rootfs.gz
310 initrd=rootfs.gz
311 extraargs="/home=$(getuuid)/slitaz rdinit=/init1"
312 else
313 if [ -d /media/cdrom/fs ]; then
314 cp -a /media/cdrom/fs/. /mnt/slitaz
315 elif is_loram ; then
316 for i in /media/cdrom/boot/rootfs?*.* ; do
317 [ -s $(basename $i) ] && continue
318 cpio -i $i
319 done
320 for i in $(ls_r /media/cdrom/boot/rootfs*gz); do
321 mount -o loop,ro $i /sqfs/mnt
322 cp -a /sqfs/mnt/. /mnt/slitaz
323 umount -d /sqfs/mnt
324 done
325 else
326 for i in $(ls_r /media/cdrom/boot/rootfs*gz); do
327 uncpio $i /mnt/slitaz
328 done
329 fi
330 cp -a /etc/locale.conf /mnt/slitaz/etc 2> /dev/null
331 cp -a /etc/keymap.conf /mnt/slitaz/etc 2> /dev/null
332 mkinitrd /mnt/slitaz/boot/initrd
333 initrd=initrd
334 extraargs="mount=$(getuuid) subroot=slitaz"
335 fi
336 echo -en "\nInstall boot files..."
337 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
338 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
339 /media/cdrom/README /media/cdrom/boot/memtest* ; do
340 [ -s $i ] && cp $i /mnt/slitaz/boot
341 done
342 for i in /mnt/slitaz/boot/memtest /mnt/slitaz/boot/*pxe* ; do
343 [ $(get 0 $i 2> /dev/null || echo 0) -eq 23117 ] &&
344 mv $i $i.exe
345 done
346 gettazboot /mnt/slitaz/boot/tazboot.exe
347 unix2dos > /mnt/slitaz/boot/tazboot.cmd <<EOT
348 kernel=/slitaz/boot/bzimage
349 initrd=/slitaz/boot/$initrd
350 rw root=/dev/null $extraargs autologin
351 EOT
352 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
353 /mnt/slitaz/boot/README
354 [ -d /mnt/slitaz/usr/sbin -a ! -x /mnt/slitaz/usr/sbin/mount.posixovl ] &&
355 cp $(which mount.posixovl) /mnt/slitaz/usr/sbin
356 rm -f /tmp/wait
357 false &&
358 [ -s /mnt/boot.ini ] && ! grep -qs tazboot /mnt/boot.ini &&
359 echo "Update boot.ini ..." && unix2dos >> /mnt/boot.ini <<EOT
360 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
361 EOT
362 false &&
363 grep -qis menuitem /mnt/config.sys && ! grep -qi tazboot /mnt/config.sys &&
364 echo "Update config.sys ..." &&
365 sed -i 's/menudefault/menuitem SLITAZ, SliTaz\r\n&/' /mnt/config.sys &&
366 unix2dos >> /mnt/config.sys <<EOT
367 [SLITAZ]
368 install=\\slitaz\\boot\\tazboot.exe
369 EOT
370 return 0
371 }
373 install()
374 {
375 $DIALOG --clear --title " SliTaz UMSDOS way installation " \
376 --yes-label "Install" --yesno \
377 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
378 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
379 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
380 To uninstall SliTaz, you have only to remove this directory.
381 The file \\boot.ini or \\config.sys may be modified too.\n\n
382 SliTaz may run slowly on the 'UMSDOS way' installation due to the
383 posixovl filesystem. The 'TAZUSB way' installation runs faster.\n\n
384 To do a traditional installation with disk partitioning,
385 start SliTaz Live with the 'SliTaz RAM boot' menu.\n" 19 70
386 [ $? -eq 0 ] || return
387 doinstall || return
388 [ -x /mnt/slitaz/sbin/init ] || return
389 umount -d /media/cdrom
390 umount_proc
391 exec chroot /mnt/slitaz /sbin/init
392 }
394 installtaz()
395 {
396 $DIALOG --clear --title " SliTaz TAZUSB way installation " \
397 --yes-label "Install" --yesno \
398 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
399 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
400 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
401 To uninstall SliTaz, you have only to remove this directory.
402 The file \\boot.ini or \\config.sys may be modified too.\n\n
403 The filesystem is loaded entirely into memory upon boot to
404 increase responsiveness. Only /home lands on the hard disk.\n\n
405 To do a traditional installation with disk partitioning,
406 start SliTaz Live with the 'SliTaz RAM boot' menu.\n" 19 70
407 [ $? -eq 0 ] || return
408 doinstall tazusblike || return
409 dotwait
410 if [ -d /media/cdrom/fs ]; then
411 cp -a /media/cdrom/fs/. /
412 else
413 for i in $(ls_r /media/cdrom/boot/rootfs*gz); do
414 uncpio $i
415 done
416 fi
417 cp /tmp/fs/etc/* /etc
418 echo "/home=$(getuuid)/slitaz" > /cmdline
419 rm -f /tmp/wait
420 [ -x /init1 ] || return
421 umount -d /media/cdrom
422 umount /mnt/slitaz
423 rm -f /dev/cdrom
424 umount /mnt
425 mkdir /mnt/dos
426 umount_proc
427 exec /init1
428 }