wok-6.x annotate busybox/stuff/init @ rev 4545

busybox/init: add loop & crypto support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 03 17:58:06 2009 +0100 (2009-12-03)
parents ed4e00c9b798
children 1e76f9aab153
rev   line source
pascal@1703 1 #!/bin/sh
pascal@1703 2
pascal@4545 3 launch_init()
pascal@4545 4 {
pascal@4545 5 echo -e "\\033[70G[ \\033[1;33mOK\\033[0;39m ]"
pascal@4545 6 exec /sbin/switch_root mnt /sbin/init
pascal@4545 7 }
pascal@4545 8
pascal@4545 9 try_init()
pascal@4545 10 {
pascal@4545 11 if [ ! -d /mnt/etc ] && grep -q cryptoroot= /proc/cmdline; then
pascal@4545 12 modprobe dm-mod
pascal@4545 13 modprobe dm-crypt
pascal@4545 14 modprobe aes-i586
pascal@4545 15 root="$(sed 's/.*cryptoroot=\([^ ]*\).*/\1/' < /proc/cmdline)"
pascal@4545 16 dev=${root#/dev/}
pascal@4545 17 dmlabel=crypto-$dev
pascal@4545 18 if cryptsetup isLuks $root 2> /dev/null; then
pascal@4545 19 cryptsetup luksOpen $root $dmlabel
pascal@4545 20 else
pascal@4545 21 read -s -t 60 -p "Pass phrase : " passphrase
pascal@4545 22 key=$(echo $passphrase | hashalot -x -n 32 sha512)
pascal@4545 23 blocks=$(cat $(find /sys/block | grep /$dev/size))
pascal@4545 24 echo 0 $blocks crypt aes-plain $key 0 $root 0 | \
pascal@4545 25 dmsetup create $dmlabel
pascal@4545 26 fi
pascal@4545 27 mount /dev/mapper/$dmlabel /mnt
pascal@4545 28 fi
pascal@4545 29 if [ -d /mnt/etc ]; then
pascal@4545 30 umount /sys
pascal@4545 31 [ -n "$1" ] && for i in $@ ; do
pascal@4545 32 cp -a $i /mnt$i
pascal@4545 33 done
pascal@4545 34 umount /proc
pascal@4545 35 launch_init
pascal@4545 36 fi
pascal@4545 37 echo -e "\\033[70G[ \\033[1;31mFailed\\033[0;39m ]"
pascal@4545 38 }
pascal@4545 39
pascal@4469 40 mount_mapper()
pascal@4469 41 {
pascal@4469 42 mount $root /mnt
pascal@4545 43 try_init /dev/mapper $@
pascal@4469 44 }
pascal@4469 45
pascal@4469 46 mount -t proc proc /proc
pascal@4469 47 mount -t sysfs sysfs /sys
pascal@4469 48 if grep -q dmraid= /proc/cmdline; then
pascal@4469 49 root="$(sed 's/.*dmraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
pascal@4469 50 echo -n "Switching / to dmraid $root..."
pascal@4469 51 case "$(dmraid -s | grep ^type | awk '{ print $3 }')" in
pascal@4469 52 mirror) modprobe dm-mirror;;
pascal@4469 53 raid[456]*) modprobe raid456;;
pascal@4469 54 esac
pascal@4469 55 name=$(dmraid -s | grep ^name | awk '{ print $3 }')
pascal@4469 56 case "$root" in
pascal@4469 57 /dev/*);;
pascal@4469 58 p*) root=/dev/mapper/${name}$root ;;
pascal@4469 59 *) root=/dev/mapper/${name}p$root ;;
pascal@4469 60 esac
pascal@4469 61 dmraid -ay
pascal@4469 62 mount_mapper
pascal@4545 63 fi
pascal@4545 64 if grep -q softraid= /proc/cmdline; then
pascal@4469 65 root="$(sed 's/.*softraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
pascal@4469 66 echo -n "Switching / to softraid $root..."
pascal@4469 67 mdadm --examine --scan --config=partitions > /etc/mdadm.conf
pascal@4469 68 grep level=raid /etc/mdadm.conf | while read line; do
pascal@4469 69 case "$line" in
pascal@4469 70 *=raid1\ *) modprobe dm-mirror ;;
pascal@4469 71 *=raid[456]\ *) modprobe raid456 ;;
pascal@4469 72 esac
pascal@4469 73 done
pascal@4469 74 mdadm --assemble --scan
pascal@4469 75 mount_mapper /etc/mdadm.conf
pascal@4469 76 fi
pascal@4545 77 if grep -q loopfs= /proc/cmdline; then
pascal@4545 78 loopfs="$(sed 's/.*loopfs=\([^ ]*\).*/\1/' < /proc/cmdline)"
pascal@4545 79 dev=${loopfs%,*}
pascal@4545 80 loopfs=${loopfs#*,}
pascal@4545 81 echo -n "Switching / to loop $loopfs on $dev..."
pascal@4545 82 if ! mount $dev /mnt; then
pascal@4545 83 if echo $dev | grep -q "/dev/sd"; then
pascal@4545 84 delay=`cat /sys/module/usb_storage/parameters/delay_use`
pascal@4545 85 delay=$((1+$delay))
pascal@4545 86 echo -n "sleep for $delay seconds..."
pascal@4545 87 sleep $delay
pascal@4545 88 fi
pascal@4545 89 mount $dev /mnt || loopfs=""
pascal@4545 90 fi
pascal@4545 91 if [ -n "$loopfs" ]; then
pascal@4545 92 losetup /dev/loop0 /mnt/$loopfs
pascal@4545 93 mount /dev/loop0 /mnt 2> /dev/null
pascal@4545 94 fi
pascal@4545 95 try_init
pascal@4545 96 fi
pascal@4545 97 grep -q cryptoroot= /proc/cmdline && try_init
pascal@4469 98 umount /sys
pascal@2397 99 echo -n "Switching / to tmpfs..."
pascal@1704 100 size="$(grep rootfssize= < /proc/cmdline | \
pascal@1704 101 sed 's/.*rootfssize=\([0-9]*[kmg%]\).*/-o size=\1/')"
pascal@1772 102 free=$(busybox free | busybox awk '/Mem:/ { print int(($4*100)/$3) }')
pascal@1704 103 umount /proc
pascal@1775 104 [ -n "$size" ] || size="-o size=90%"
pascal@1772 105 if [ $free -lt 100 ] || ! mount -t tmpfs $size tmpfs /mnt; then
pascal@1772 106 echo -e "\\033[70G[ \\033[1;33mSkipped\\033[0;39m]"
pascal@1772 107 exec /sbin/init
pascal@1772 108 fi
pascal@1704 109 for i in $(ls -a /); do
pascal@1704 110 case "$i" in
pascal@1705 111 .|..) ;;
pascal@1704 112 mnt) mkdir /mnt/mnt;;
pascal@1704 113 *) if ! cp -a /$i /mnt 2> /dev/null; then
pascal@1704 114 echo -e "\\033[70G[ \\033[1;31mFailed\\033[0;39m ]"
pascal@1704 115 umount /mnt
pascal@1704 116 exec /sbin/init
pascal@1704 117 fi;;
pascal@1704 118 esac
pascal@1704 119 done
pascal@4545 120 launch_init