mk-sdcard.sh 1.87 KB
#!/bin/sh
if [ `id -u` -ne 0 ]
then
    echo Run as root
    exit
fi

DEVICE=/dev/mmcblk0

if [ -z "$1" ]
then
    echo "-------------------"
    REMOVABLE_DRIVES=""
    for _device in /sys/block/*/device; do
        if echo $(readlink -f "$_device")|egrep -q "usb"; then
            _disk=$(echo "$_device" | cut -f4 -d/)
            _size=$(echo "scale=2;$(cat /sys/block/sdb/device/block/sdb/size) * 512 / 1024 / 1024 / 1024" | bc -l)
            echo /dev/$_disk: $_size GB
            REMOVABLE_DRIVES="$REMOVABLE_DRIVES /dev/$_disk"
        fi
    done
    echo "-------------------"

    PS3="Select usb disk device: "
    select ITEM in "$REMOVABLE_DRIVES"
    do
        if [ -n "$ITEM" ]
        then
	    echo SELECTED DEVICE: $ITEM
            DEVICE=$ITEM
            break
        fi
    done
else
    DEVICE=$1
fi



TEST_LETTER=`basename $DEVICE | cut -c1-2`
case $TEST_LETTER in
        sd)
                PART1=${DEVICE}1
                PART2=${DEVICE}2
                PART3=${DEVICE}3
                PART4=${DEVICE}4
                PART5=${DEVICE}5
                PART6=${DEVICE}6
                PART7=${DEVICE}7
                ;;
        mm)
                PART1=${DEVICE}p1
                PART2=${DEVICE}p2
                PART3=${DEVICE}p3
                PART4=${DEVICE}p4
                PART5=${DEVICE}p5
                PART6=${DEVICE}p6
                PART7=${DEVICE}p7
                ;;
esac


umount ${PART1}
umount ${PART2}
umount ${PART3}
umount ${PART4}

echo "==== make partitions ==============================="
fdisk -S1 -H1 ${DEVICE} << EOF
o
n
p
1
131072
196607
n
p
2
196608
262143
n
p
3
262144
524287
n
p
524288

t
1
b
t
2
b
a
1
w
EOF

## format partitions
echo "==== format partitions ============================="

mkfs.fat ${PART1} -n BOOT0
mkfs.fat ${PART2} -n BOOT1
mkfs.ext4 -F ${PART3} -L APP
mkfs.ext4 -F ${PART4} -L DATA

fdisk -S1 -H1 -l ${DEVICE}