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

syslinux/iso2exe: check ISO md5 (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Jan 05 12:38:18 2015 +0100 (2015-01-05)
parents aa46cf9cc35a
children 00bb1e4137c6
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 tinydialog()
27 {
28 clear
29 label=""
30 while [ "$1" ]; do
31 case "$1" in
32 --title) title=" \x1B[30;47m$2\x1B[37;40m\n"
33 echo -e $title; shift ;;
34 --yes-label) label="$2" ; shift ;;
35 --textbox)
36 less "$2" < /dev/tty0 > /dev/tty0
37 break;;
38 --gauge)
39 t=" "
40 echo -e "$t$2\n"
41 while read pct ; do
42 s=" "
43 s="$s$pct%$s"
44 pct=$((($pct*63)/100))
45 echo -en "\r$t\x1B[30;47m$(echo "$s" | cut -c-$pct)\x1B[37;40m$(echo "$s" | cut -c$(($pct+1))-)"
46 done
47 break;;
48 --yesno)
49 while true; do
50 clear
51 echo "$2" | sed 's/\\n\\n/\\n/g;s/\\n/\n/g'
52 echo -en " <- 1:${label:-Yes} 2:Cancel\r"
53 read x
54 case "$x" in
55 ''|Y*|y*|1) return 0;;
56 N*|n*|2|0) return 1;;
57 esac
58 done ;;
59 --menu|--radiolist)
60 [ "$1" == "--menu" ] && shft=2 || shft=3
61 label=""
62 [ "$2" ] && label="\n$2"
63 shift 5
64 echo -e "$title$label\n0 Cancel" > /tmp/data
65 n=1
66 while [ "$1" ]; do
67 eval key_$n='$1'
68 echo "$((n++)) $2"
69 shift $shft
70 done >> /tmp/data
71 while ! grep -q "^$n " /tmp/data ; do
72 clear
73 [ $(wc -l < /tmp/data) -gt 20 ] &&
74 less /tmp/data || cat /tmp/data
75 echo -en "\n <- Enter the selection number\r"
76 read n
77 done 2> /dev/null
78 rm -f /tmp/data
79 [ $n -eq 0 ] && return 1
80 eval echo -n \$key_$n 1>&2
81 return 0;;
82 esac
83 shift
84 done
85 }
87 mount_proc()
88 {
89 mount -t proc /proc /proc
90 mount -t sysfs /sys /sys
91 udevd --daemon 2> /dev/null && udevadm trigger && sleep 5
92 }
94 umount_proc()
95 {
96 killall udevd 2> /dev/null
97 umount /sys/fs/fuse/connections 2> /dev/null
98 umount /sys
99 umount /proc
100 }
102 bytes2bin()
103 {
104 for i in $@ ; do
105 printf '\\\\x%02X' $(($i&255)) | xargs echo -en
106 done
107 }
109 words2bin()
110 {
111 for i in $@ ; do
112 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
113 xargs echo -en
114 done
115 }
117 gettazboot()
118 {
119 echo "Creating $(basename $1) ..."
120 O=$(($(get 66 /mnt/$ISO) - 0xC0))
121 L=$(($(get 20 /mnt/$ISO) - 0xC0 - $(get 24 /mnt/$ISO) - $O))
122 S=$((32+$L))
123 P=$((($S+511)/512))
124 E=$((4096-(32*$P)))
125 words2bin 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
126 -2 0 256 -16 28 0x6C53 0x5469 0x7A61 > $1
127 ddq bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1
128 }
130 checkmagic()
131 {
132 [ -s $1 ] && [ $(getarg magic) == $(get 24 $1 4) ]
133 }
135 getiso()
136 {
137 mkdir -p /media/cdrom
138 for dev in /sys/block/?d?/?d??* ; do
139 mount /dev/$(basename $dev) /mnt
140 if checkmagic /mnt/$ISO; then
141 mount -o loop,ro /mnt/$ISO /media/cdrom
142 echo "Found $ISO on $(basename $dev)"
143 break
144 fi
145 umount /mnt
146 done
147 }
149 uncpio()
150 {
151 [ -s "$1" ] || return
152 echo -en "\n Extracting $(basename $1) ..."
153 case $(get 0 $1) in
154 *35615) ( zcat || gunzip ) ;;
155 *14333) unxz ;;
156 *\ 93) unlzma ;;
157 *) cat ;;
158 esac < $1 | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
159 }
161 dotwait()
162 {
163 echo -n "${1:-Install filesystem}.."
164 touch /tmp/wait
165 while [ -e /tmp/wait ]; do
166 echo -n "." > /dev/tty0
167 sleep 1
168 done &
169 }
171 getuuid()
172 {
173 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
174 blkid | sed "/$dev:/!d;s/.* UUID=.\\([^ ]*\\)\".*/\\1/"
175 }
177 tazusbinitfs()
178 {
179 PAD=$(($(stat -c %s $1) % 4))
180 [ $PAD -ne 0 ] && ddq if=/dev/zero bs=1 count=$((4 - $PAD)) >> $1
181 mkdir -p /tmp/fs/etc /tmp/fs/lib /tmp/fs/home
182 cp -a /etc/locale.conf /tmp/fs/etc 2> /dev/null
183 cp -a /etc/keymap.conf /tmp/fs/etc 2> /dev/null
184 sed 's/ .*//' /proc/modules | while read mod ; do
185 find /lib/modules/ | grep $mod.ko | \
186 sed 's|.*|cp & /tmp/fs/lib|' | sh
187 done
188 cat > /tmp/fs/init1 <<EOT
189 #!/bin/sh
190 sed -i 's|sbin/init|init2|' /init
191 exec /init
192 EOT
193 cat > /tmp/fs/init2 <<EOT
194 #!/bin/sh
196 mount -t proc /proc /proc
197 grep -q debug /proc/cmdline && debug=true && set -x
198 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
199 v=\$(sed '/\\/home=/!d;s|.*/home=\\([^ ]*\\).*|\\1|' /proc/cmdline /cmdline 2> /dev/null)
200 mount / -o remount,rw
201 mkdir /mnt/dos
202 rm -f /cmdline 2> /dev/null
203 mount / -o remount,ro
204 mnt=/mnt/dos/\${v#*/}
205 dev=\$( (blkid /dev/[sh]d* || blkid) | grep \${v%%/*} | sed 's/:.*//;q')
206 echo "Mount \$dev in /mnt/dos for \$v..." | tee -a /run/boot.log
207 mount \$dev /mnt/dos
208 mount.posixovl -F \$mnt -- -oallow_other -odefault_permissions -osuid
209 mount --bind \$mnt /home
210 [ "\$debug" = "true" ] && sh
211 umount /proc
212 exec /sbin/init
213 EOT
214 chmod 755 /tmp/fs/init?
215 cp -a /tmp/fs/* /
216 ( cd /tmp/fs ; find * | cpio -o -H newc ) | gzip -9 >> $1
217 }
219 mkinitrd()
220 {
221 dotwait "Creating $(basename $1) "
222 for i in bin lib dev proc tmp mnt etc ; do
223 mkdir -p /tmp/fs/$i
224 done
225 for i in /dev/console /dev/null /dev/tty /dev/tty0 /dev/tty1 /dev/tty2 \
226 /dev/fuse /dev/hd* /dev/sd* ; do
227 cp -a $i /tmp/fs$i
228 done
229 for i in /bin/busybox $(which mount.posixovl) $(which blkid); do
230 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
231 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
232 cp $i /tmp/fs/bin
233 done
234 cp -a /lib/ld-* /tmp/fs/lib
235 for i in $(busybox | sed '/Current/,$!d'); do
236 [ -e /tmp/fs/bin/${i%,} ] || ln -s busybox /tmp/fs/bin/${i%,}
237 done
238 ln -s /proc/mounts /tmp/fs/etc/mtab
239 sed 's/ .*//' /proc/modules | while read mod ; do
240 find /lib/modules/ | grep $mod.ko | \
241 sed 's|.*|cp & /tmp/fs/lib|' | sh
242 done
243 cat > /tmp/fs/init <<EOT
244 #!/bin/sh
246 arg()
247 {
248 grep -q \$1 /proc/cmdline &&
249 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
250 echo "\$2 \$val"
251 }
253 mount -t proc /proc /proc
254 arg debug "shell" && debug=true && set -x
255 for i in /lib/*.ko* ; do insmod \$i 2> /dev/null ; done; sleep 2
256 arg mount "Mount device"
257 mount \$( (blkid /dev/[sh]d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
258 arg subroot "Change root to directory"
259 mount.posixovl -F /mnt/\$val -- -oallow_other -odefault_permissions -osuid
260 mount --bind /mnt /mnt/\$val/mnt/dos
261 mount -o size=0 -t tmpfs tmpfs /mnt/\$val/mnt/dos/\$val
262 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
263 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
264 [ "$debug" = "true" ] && sh
265 umount /proc
266 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
267 EOT
268 chmod +x /tmp/fs/init
269 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
270 rm -rf /tmp/fs /tmp/wait
271 }
273 is_loram()
274 {
275 [ -s /lib/squashfs.ko* ]
276 }
278 ls_r()
279 {
280 ls -r $@ 2> /dev/null || ls $@
281 }
283 doinstall()
284 {
285 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
286 if ! mount.posixovl -F /mnt/slitaz -- \
287 -oallow_other -odefault_permissions -osuid; then
288 echo "Can't install SliTaz. Abort."
289 sleep 5
290 return 1
291 fi
292 dowait "Install root filesystem in /slitaz.."
293 if [ "$1" ]; then
294 if [ -d /media/cdrom/fs ]; then
295 ( cd /mnt/slitaz/fs; find | cpio -o -H newc ) | gzip -9