wok annotate syslinux/stuff/iso2exe/init @ rev 13697

syslinux/iso2exe: menu can create usbkey/floppy
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Dec 14 15:43:12 2012 +0100 (2012-12-14)
parents 87a217af01ea
children db0eb389448c
rev   line source
pascal@13691 1 #!/bin/sh
pascal@13691 2
pascal@13691 3 DIALOG=dialog
pascal@13691 4
pascal@13691 5 get()
pascal@13691 6 {
pascal@13691 7 od -j $1 -N ${3:-4} -t u${3:-4} -An $2
pascal@13691 8 }
pascal@13691 9
pascal@13691 10 getarg()
pascal@13691 11 {
pascal@13691 12 sed "/$1=/!d;s/.*$1=\\([^ ]*\\).*/\\1/" /proc/cmdline
pascal@13691 13 }
pascal@13691 14
pascal@13691 15 gettazboot()
pascal@13691 16 {
paul@13692 17 echo "Creating $(basename $1) ..."
pascal@13697 18 O=$(($(get 34 /mnt/$ISO 2) - 0xE0))
pascal@13691 19 L=$((0x8000 - $(get 28 /mnt/$ISO 2) - $O))
pascal@13691 20 S=$((32+$L))
pascal@13691 21 P=$((($S+511)/512))
pascal@13691 22 E=$((4096-(32*$P)))
pascal@13691 23 for i in 0x5A4D $(($S%512)) $P 0 2 $E -1 $((${2:-0}-16)) \
pascal@13691 24 -2 0 256 -16 28 0x6C53 0x5469 0x7A61; do
pascal@13691 25 printf '\\\\x%02X\\\\x%02X' $(($i&255)) $((($i>>8)&255)) | \
pascal@13691 26 xargs echo -en
pascal@13691 27 done > $1
pascal@13691 28 dd bs=1 count=$L skip=$(echo $O) if=/mnt/$ISO >> $1 2> /dev/null
pascal@13691 29 }
pascal@13691 30
pascal@13691 31 checkmagic()
pascal@13691 32 {
pascal@13691 33 [ -s $1 ] && [ $(getarg magic) == $(get 28 $1) ]
pascal@13691 34 }
pascal@13691 35
pascal@13691 36 getiso()
pascal@13691 37 {
pascal@13691 38 blkid | while read dev info ; do
pascal@13691 39 case "$info" in
pascal@13691 40 *dos*|*fat*|*ntfs*)
pascal@13691 41 mount ${dev%:} /mnt
pascal@13691 42 if checkmagic /mnt/$ISO; then
pascal@13691 43 mount -o loop,ro /mnt/$ISO /media/cdrom
pascal@13691 44 echo "Found $ISO on ${dev%:}"
pascal@13691 45 break
pascal@13691 46 fi
pascal@13691 47 umount /mnt ;;
pascal@13691 48 esac
pascal@13691 49 done
pascal@13691 50 }
pascal@13691 51
pascal@13691 52 uncpio()
pascal@13691 53 {
pascal@13691 54 echo "Extracting $(basename $1) ..."
pascal@13691 55 case $(get 0 $1 2) in
pascal@13691 56 *35615) zcat $1 ;;
pascal@13691 57 *\ 93) unlzma -c $1 ;;
pascal@13691 58 *) cat $1 ;;
pascal@13691 59 esac | ( cd ${2:-/} ; cpio -idmu > /dev/null 2>&1 )
pascal@13691 60 }
pascal@13691 61
pascal@13691 62 getuuid()
pascal@13691 63 {
pascal@13691 64 dev=$(mount | sed '/ \/mnt /!d;s/ .*//;s|/dev/||;q')
pascal@13691 65 blkid | sed "/$dev:/!d;s/.*UUID=.\\([^ ]*\\)\".*/\\1/"
pascal@13691 66 }
pascal@13691 67
pascal@13691 68 mkinitrd()
pascal@13691 69 {
paul@13692 70 echo "Creating $(basename $1) ..."
pascal@13691 71 for i in bin lib dev proc tmp mnt etc ; do
pascal@13691 72 mkdir -p /tmp/fs/$i
pascal@13691 73 done
pascal@13691 74 for i in /dev/console /dev/null /dev/tty /dev/tty1 /dev/fuse \
pascal@13691 75 /dev/hd* /dev/sd* ; do
pascal@13691 76 cp -a $i /tmp/fs$i
pascal@13691 77 done
pascal@13691 78 for i in /bin/busybox /usr/sbin/mount.posixovl $(which blkid); do
pascal@13691 79 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $i | \
pascal@13691 80 sed 's|.*=> \(.*/lib/l[^ ]*\).*|\1|;/^\//!d') /tmp/fs/lib
pascal@13691 81 cp $i /tmp/fs/bin
pascal@13691 82 done
pascal@13691 83 cp -a /lib/ld-* /tmp/fs/lib
pascal@13691 84 for i in $(busybox | sed '/Current/,$!d'); do
pascal@13691 85 ln -s busybox /tmp/fs/bin/${i%,}
pascal@13691 86 done
pascal@13691 87 ln -s /proc/mounts /tmp/fs/etc/mtab
pascal@13691 88 cat > /tmp/fs/init <<EOT
pascal@13691 89 #!/bin/sh
pascal@13691 90
pascal@13691 91 arg()
pascal@13691 92 {
pascal@13691 93 grep -q \$1 /proc/cmdline &&
pascal@13691 94 val="\$(sed "s/.*\$1=\\([^ ]*\\).*/\\1/" < /proc/cmdline)" &&
pascal@13691 95 echo "\$2 \$val"
pascal@13691 96 }
pascal@13691 97
pascal@13691 98 mount -t proc /proc /proc
pascal@13691 99 arg mount "Mount device"
pascal@13691 100 mount \$( (blkid /dev/?d* || blkid) | grep \$val | sed 's/:.*//;q') /mnt
pascal@13691 101 arg subroot "Change root to directory"
pascal@13691 102 mount.posixovl /mnt/\$val
pascal@13691 103 mount --bind /mnt /mnt/\$val/mnt/dos
pascal@13691 104 LDSO=\$(ls /mnt/\$val/lib/ld-* | sed q)
pascal@13691 105 umount /proc
pascal@13691 106 export LD_LIBRARY_PATH=\$val/lib:\$val/usr/lib:/lib
pascal@13691 107 exec /bin/switch_root /mnt \${LDSO#/mnt/} \$val/usr/sbin/chroot \$val /sbin/init
pascal@13691 108 EOT
pascal@13691 109 chmod +x /tmp/fs/init
pascal@13691 110 ( cd /tmp/fs ; find * | cpio -o -H newc ) | lzma e $1 -si 2> /dev/null
pascal@13691 111 rm -rf /tmp/fs
pascal@13691 112 }
pascal@13691 113
pascal@13691 114 doinstall()
pascal@13691 115 {
pascal@13691 116 mkdir /mnt/slitaz
pascal@13691 117 mount.posixovl /mnt/slitaz || return
pascal@13691 118 mkdir -p /mnt/slitaz/boot /mnt/slitaz/mnt/dos
pascal@13691 119 for i in $(ls -r /media/cdrom/boot/rootfs*); do
pascal@13691 120 uncpio $i /mnt/slitaz
pascal@13691 121 done
pascal@13691 122 for i in /media/cdrom/boot/bzImage /media/cdrom/boot/*pxe* \
pascal@13691 123 /media/cdrom/boot/isolinux/he* /media/cdrom/boot/isolinux/opt* \
pascal@13691 124 /media/cdrom/README /media/cdrom/boot/memtest* ; do
pascal@13691 125 [ -s $i ] && cp $i /mnt/slitaz/boot
pascal@13691 126 done
pascal@13691 127 umount -d /media/cdrom
pascal@13691 128 gettazboot /mnt/slitaz/boot/tazboot.exe
pascal@13691 129 mkinitrd /mnt/slitaz/boot/initrd
pascal@13691 130 cat > /mnt/slitaz/boot/tazboot.cmd <<EOT
pascal@13691 131 kernel=\\slitaz\\boot\\bzimage
pascal@13691 132 initrd=\\slitaz\\boot\\initrd
pascal@13691 133 rw root=/dev/null mount=$(getuuid) subroot=slitaz autologin
pascal@13691 134 EOT
pascal@13691 135 unix2dos /mnt/slitaz/boot/he* /mnt/slitaz/boot/opt* \
pascal@13691 136 /mnt/slitaz/boot/README /mnt/slitaz/boot/tazboot.cmd
pascal@13691 137 [ -x /mnt/slitaz/usr/sbin/mount.posixovl ] ||
pascal@13691 138 cp /usr/sbin/mount.posixovl /mnt/slitaz/usr/sbin
pascal@13691 139 ! grep -qs tazboot /mnt/boot.ini && cat >> /mnt/boot.ini <<EOT
pascal@13691 140 C:\\slitaz\\boot\\tazboot.exe="SliTaz"
pascal@13691 141 EOT
pascal@13691 142 grep -qs menuitem /mnt/config.sys && !grep -q tazboot /mnt/config.sys &&
pascal@13691 143 sed -i 's/menudefault=/menuitem=slitaz,SliTaz\n&/' /mnt/config.sys &&
pascal@13691 144 cat >> /mnt/config.sys <<EOT
pascal@13691 145 [slitaz]
pascal@13691 146 device=\\slitaz\\boot\\tazboot.exe
pascal@13691 147 EOT
pascal@13691 148 }
pascal@13691 149
pascal@13691 150 install()
pascal@13691 151 {
pascal@13691 152 $DIALOG --clear \
pascal@13691 153 --title " SliTaz UMSDOS like installation " \
pascal@13691 154 --yes-label "Install" --yesno \
pascal@13691 155 "\nSliTaz will be installed in the subdirectory \\slitaz of the current
pascal@13691 156 DOS/Windows partition. You will see your files from /mnt/dos.\n\n
paul@13692 157 You can start SliTaz with \\slitaz\\boot\\tazboot.exe\n\n
pascal@13691 158 To uninstall SliTaz, you have only to remove this directory.
pascal@13691 159 The file \\boot.ini or \\config.sys may be modified too.\n\n
pascal@13697 160 To do a traditional installation with disk partitioning,
pascal@13691 161 start SliTaz Live with 'SliTaz RAM boot' menu.\n
pascal@13691 162 " 16 70
pascal@13691 163 [ $? -eq 0 -a -x /usr/sbin/mount.posixovl ] || return
pascal@13691 164 doinstall
pascal@13691 165 umount /proc
pascal@13691 166 exec chroot /mnt/slitaz /sbin/init
pascal@13691 167 }
pascal@13691 168
pascal@13691 169 tazboot()
pascal@13691 170 {
pascal@13691 171 $DIALOG --clear \
pascal@13691 172 --title " SliTaz bootloader for DOS " \
pascal@13691 173 --yes-label "Install" --yesno \
paul@13692 174 "\nThe file TAZBOOT.EXE will be created in the top directory. It supports
pascal@13697 175 a bzImage linux kernel, multiple initramfs, a kernel command line and
paul@13692 176 an ISO image file loopback (retrieves files from an ISO file).\n\n
pascal@13691 177 Usage: tazboot.exe [[@commands]|[kernel=<bzimage>]
pascal@13691 178 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] cmdline args ...]\n\n
paul@13692 179 Defaults: tazboot @tazboot.cmd or tazboot kernel=bzImage auto\n\n\
pascal@13691 180 Examples for tazboot.cmd:\n\n\
pascal@13691 181 iso=\\isos\\slitaz-4.0.iso\n\
pascal@13691 182 kernel=boot/bzImage\n\
pascal@13691 183 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
pascal@13691 184 rw root=/dev/null autologin\n\n\
pascal@13691 185 kernel=\\slitaz\\vmlinuz root=/dev/sda5 ro\n\n
pascal@13691 186 Unlike GRUB4DOS, it doesn't require unfragmented ISO image files.\n
pascal@13691 187 " 24 78
pascal@13691 188 [ $? -eq 0 ] || return
pascal@13691 189 gettazboot /mnt/tazboot.exe
pascal@13691 190 }
pascal@13691 191
pascal@13697 192 md5()
pascal@13697 193 {
pascal@13697 194 ( cd /media/cdrom ; md5sum -c md5sum ) | tee /tmp/data
pascal@13697 195 $DIALOG --clear \
pascal@13697 196 --title " Checked files " \
pascal@13697 197 --textbox /tmp/data 24 78
pascal@13697 198 rm -f /tmp/data
pascal@13697 199 }
pascal@13697 200
pascal@13697 201 readme()
pascal@13697 202 {
pascal@13697 203 $DIALOG --clear \
pascal@13697 204 --title " Readme " \
pascal@13697 205 --textbox /media/cdrom/README 24 78
pascal@13697 206 }
pascal@13697 207
pascal@13697 208 bootlog()
pascal@13697 209 {
pascal@13697 210 $DIALOG --clear \
pascal@13697 211 --title " Linux boot messages " \
pascal@13697 212 --textbox /tmp/dmesg 24 78
pascal@13697 213 }
pascal@13697 214
pascal@13697 215 memtest()
pascal@13697 216 {
pascal@13697 217 $DIALOG --clear \
pascal@13697 218 --title " Memtest86 " \
pascal@13697 219 --yes-label "Install" --yesno \
pascal@13697 220 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
pascal@13697 221 computers. BIOS based memory tests are a quick, cursory check and often
pascal@13697 222 miss many of the failures that are detected by Memtest86.\n
pascal@13697 223 " 12 70
pascal@13697 224 [ $? -eq 0 ] || return
pascal@13697 225 cp /media/cdrom/boot/memtest /mnt
pascal@13697 226 }
pascal@13697 227
pascal@13697 228 fdmemtest()
pascal@13697 229 {
pascal@13697 230 $DIALOG --clear \
pascal@13697 231 --title " Create a Memtest86 boot floppy " \
pascal@13697 232 --yes-label "Create floppy" --yesno \
pascal@13697 233 "\nMemtest86 is a thorough, stand alone memory test for x86 architecture
pascal@13697 234 computers. BIOS based memory tests are a quick, cursory check and often
pascal@13697 235 miss many of the failures that are detected by Memtest86.\n\n
pascal@13697 236 Please insert a blank disk in floppy drive.\n
pascal@13697 237 " 12 70
pascal@13697 238 [ $? -eq 0 ] || return
pascal@13697 239 dd if=/media/cdrom/boot/memtest of=/dev/fd0
pascal@13697 240 }
pascal@13697 241
pascal@13697 242 gpxe()
pascal@13697 243 {
pascal@13697 244 $DIALOG --clear \
pascal@13697 245 --title " SliTaz Web boot " \
pascal@13697 246 --yes-label "Install" --yesno \
pascal@13697 247 "\nBoot your operating system from the internet and enjoy a full system
pascal@13697 248 working entirely in RAM with speed and stability in mind. The Linux Kernel
pascal@13697 249 and the complete SliTaz compressed root filesystem will be loaded into RAM
pascal@13697 250 from the Web using PXE and HTTP protocols.\n
pascal@13697 251 " 12 70
pascal@13697 252 [ $? -eq 0 ] || return
pascal@13697 253 cp /media/cdrom/boot/gpxe /mnt
pascal@13697 254 }
pascal@13697 255
pascal@13697 256 fdgpxe()
pascal@13697 257 {
pascal@13697 258 $DIALOG --clear \
pascal@13697 259 --title " Create a SliTaz Web boot floppy " \
pascal@13697 260 --yes-label "Create floppy" --yesno \
pascal@13697 261 "\nBoot your operating system from the internet and enjoy a full system
pascal@13697 262 working entirely in RAM with speed and stability in mind. The Linux Kernel
pascal@13697 263 and the complete SliTaz compressed root filesystem will be loaded into RAM
pascal@13697 264 from the Web using PXE and HTTP protocols.\n\n
pascal@13697 265 Please insert a blank disk in floppy drive.\n
pascal@13697 266 " 12 70
pascal@13697 267 [ $? -eq 0 ] || return
pascal@13697 268 dd if=/media/cdrom/boot/gpxe of=/dev/fd0
pascal@13697 269 }
pascal@13697 270
pascal@13697 271 cdfile()
pascal@13697 272 {
pascal@13697 273 [ -s /media/cdrom/$1 ] && echo -en "$2 ${3// /.}"
pascal@13697 274 }
pascal@13697 275
pascal@13697 276 usbkey()
pascal@13697 277 {
pascal@13697 278 $DIALOG --clear \
pascal@13697 279 --title " Create a SliTaz USB key " \
pascal@13697 280 --yes-label "Continue" --yesno \
pascal@13697 281 "\nUnlike a hard drive install, the filesystem is kept in a compressed
pascal@13697 282 rootfs.gz. The filesystem is loaded entirely into memory upon boot.
pascal@13697 283 This should increase responsiveness, protect the filesystem against
pascal@13697 284 accidental corruption and reduce read/writes to the USB drive.
pascal@13697 285 Once setup, the tazusb utility can rewrite the root filesystem
pascal@13697 286 with any changes you have made since booting up,
pascal@13697 287 giving the effective benefits of a hard drive install.\n\n
pascal@13697 288 /home is mounted on boot using the UUID of your particular flash drive.
pascal@13697 289 Unlike a device name, the UUID has the benefit of never changing from machine
pascal@13697 290 to machine.\n\n
pascal@13697 291 Please plug your USB stick in now.\n
pascal@13697 292 " 18 70
pascal@13697 293 [ $? -eq 0 ] || return
pascal@13697 294 DEV="$(grep -l 1 /sys/block/*/removable | \
pascal@13697 295 sed 's|/sys/block/\(.*\)/removable|\1|')"
pascal@13697 296 [ -n "$DEV" ] || return
pascal@13697 297 exec 3>&1
pascal@13697 298 sleep 5
pascal@13697 299 device=`$DIALOG --clear \
pascal@13697 300 --title " Select your USB key " \
pascal@13697 301 --menu "\nPlease select the USB key according its known size.\n\n" \
pascal@13697 302 14 70 4 \
pascal@13697 303 $(for i in $DEV ; do
pascal@13697 304 echo "/dev/$i $(( $(cat /sys/block/$i/size) / 1024 ))MB"
pascal@13697 305 done) \
pascal@13697 306 2>&1 1>&3`
pascal@13697 307 retval=$?
pascal@13697 308 exec 3>&-
pascal@13697 309 [ $retval -eq 0 ] || continue
pascal@13697 310 exec 3>&1
pascal@13697 311 format=`$DIALOG --clear \
pascal@13697 312 --title " Select the filesystem " \
pascal@13697 313 --menu "\nPlease select the filesystem type to create.\n\n\
pascal@13697 314 The filesystem creation will erase all the datas \
pascal@13697 315 in the USB key." 14 70 4 \
pascal@13697 316 "none" "Do not erase the USB key" \
pascal@13697 317 "ext3" "Ext3 journaling filesystem" \
pascal@13697 318 "ext2" "Ext2 filesystem" \
pascal@13697 319 "fat32" "Window FAT32 filesystem" \
pascal@13697 320 2>&1 1>&3`
pascal@13697 321 retval=$?
pascal@13697 322 exec 3>&-
pascal@13697 323 [ $retval -eq 0 ] || continue
pascal@13697 324 [ "$format" != "none" ] && tazusb format $device "SliTaz" $format
pascal@13697 325 tazusb gen-iso2usb /mnt/$ISO $device
pascal@13697 326 }
pascal@13697 327
pascal@13691 328 text()
pascal@13691 329 {
pascal@13691 330 umount -d /media/cdrom
pascal@13691 331 umount /mnt
pascal@13691 332 umount /proc
pascal@13691 333 exec /init
pascal@13691 334 }
pascal@13691 335
pascal@13691 336 live()
pascal@13691 337 {
pascal@13691 338 n=0
pascal@13691 339 for i in $(ls -r /media/cdrom/boot/rootfs*); do
pascal@13691 340 [ $((n++)) -eq 0 ] || uncpio $i
pascal@13691 341 done
pascal@13691 342 text
pascal@13691 343 }
pascal@13691 344
pascal@13691 345 reboot()
pascal@13691 346 {
pascal@13691 347 umount -d /media/cdrom
pascal@13691 348 umount /mnt
pascal@13691 349 /sbin/reboot -f
pascal@13691 350 }
pascal@13691 351
pascal@13691 352 poweroff()
pascal@13691 353 {
pascal@13691 354 umount -d /media/cdrom
pascal@13691 355 umount /mnt
pascal@13691 356 /sbin/poweroff -f
pascal@13691 357 }
pascal@13691 358
pascal@13697 359 shell()
pascal@13697 360 {
pascal@13697 361 /sbin/getty -n -l /bin/ash 38400 tty1
pascal@13697 362 }
pascal@13697 363
pascal@13691 364 [ -x /usr/sbin/mount.posixovl ] ||
pascal@13691 365 mv /usr/sbin/mount.posixovl.iso2exe /usr/sbin/mount.posixovl
pascal@13691 366 mount -t proc /proc /proc
pascal@13697 367 dmesg > /tmp/dmesg
pascal@13691 368 ISO="$(getarg iso | sed 's/.://;s|\\|/|g')"
pascal@13691 369 getiso
pascal@13691 370 case "$(basename $ISO | tr [A-Z] [a-z])$(getarg mode)" in
pascal@13691 371 *install*) install ;;
pascal@13691 372 *live*) live ;;
pascal@13691 373 *text*) text ;;
pascal@13691 374 esac
pascal@13691 375
pascal@13691 376 while true; do
pascal@13691 377 exec 3>&1
pascal@13691 378 value=`$DIALOG --clear \
pascal@13691 379 --title " Welcome to SliTaz " \
pascal@13697 380 --menu "\nPlease select" 17 70 9 \
pascal@13697 381 "live" "SliTaz RAM boot" \
pascal@13697 382 "text" "SliTaz RAM boot (text mode only)" \
pascal@13697 383 $(cdfile README "readme" "Show the README file") \
pascal@13697 384 $(cdfile md5sum "md5" "Check ISO files") \
pascal@13697 385 "install" "Hard disk installation" \
pascal@13697 386 "usbkey" "USB key installation" \
pascal@13697 387 "tazboot" "Get tazboot.exe Linux loader" \
pascal@13697 388 $(cdfile Xboot/memtest "memtest" "Get Memtest86") \
pascal@13697 389 $(cdfile boot/memtest "fdmemtest" "Create a Memtest86 boot floppy") \
pascal@13697 390 $(cdfile Xboot/gpxe "gpxe" "Get SliTaz Web boot utility") \
pascal@13697 391 $(cdfile boot/gpxe "fdgpxe" "Create a SliTaz Web boot floppy") \
pascal@13697 392 "reboot" "Restart the computer" \
pascal@13697 393 "poweroff" "Power off" \
pascal@13697 394 "bootlog" "Linux boot messages" \
pascal@13697 395 "shell" "Shell prompt" \
pascal@13691 396 2>&1 1>&3`
pascal@13691 397 retval=$?
pascal@13691 398 exec 3>&-
pascal@13691 399 [ $retval -eq 0 ] || continue
pascal@13691 400 $value
pascal@13691 401 done