#!/bin/sh
#-----------------------------------------------------------
# 指定目标目标存储器
#destination="/dev/mmcblk1"
destination="/dev/sdb"
#-----------------------------------------------------------
# 清空目标存储器 (1M * 16 = 16MB),其实可以省去
dd if=/dev/zero of=${destination} bs=1M count=16
#-----------------------------------------------------------
# 第1步:存储器分区,一共7个分区(重要,实际上并不需要这么多分区)
# 参数格式:起始位置,分区容量,分区类型
# 
# 起始位置:如果未指定,则为第1个可用的柱面
# 分区容量:单位由--unit参数定义
# 分区类型:L:Linux,S: Swap, E:扩展分区
#
#,,E 表示剩余的磁盘全部划为扩展分区
#,,L 将剩余的扩展分区全部划成逻辑分区
#
#--DOS DOS兼容性
#--force 强制执行
#--unit 指定单位:必须是S,C,B,M的一种

LC_ALL=C sfdisk --force --DOS --sectors 63 --heads 255 --unit M "${destination}" <<-__EOF__
    ,32,0xe,*
    ,16,0xe
    ,256,L
    ,,E
    ,256,L
    ,256,L
    ,256,L
    ,,L
__EOF__
sync
#-----------------------------------------------------------
# 格式化各分区
umount ${destination}*

mkfs.vfat -n boot    ${destination}1
mkfs.vfat -n env     ${destination}2
mkfs.ext4 -L rootfs1 ${destination}3
mkfs.ext4 -L rootfs2 ${destination}5
mkfs.ext4 -L app1    ${destination}6
mkfs.ext4 -L update  ${destination}7
mkfs.ext4 -L data    ${destination}8
sync
#-----------------------------------------------------------

#!/bin/sh
destination=sdc

echo "Copy Files to TF Card."
#
#-------------------------------------------------
# 创建挂载磁盘用的目录(如果存在的话,由跳过)
if [ ! -d /mnt/uboot ]; then
    mkdir /mnt/uboot
fi

if [ ! -d /mnt/env ]; then
    mkdir /mnt/env
fi

if [ ! -d /mnt/opt ]; then
    mkdir /mnt/opt
fi

if [ $? -gt 0 ]; then
    exit
fi

#-------------------------------------------------
# 挂载磁盘分区
umount /dev/${destination}* > /dev/null 2>&1
mount  /dev/${destination}1 /mnt/uboot > /dev/null 2>&1
mount  /dev/${destination}2 /mnt/env > /dev/null 2>&1
mount  /dev/${destination}6 /mnt/opt > /dev/null 2>&1
#
if [ $? -gt 0 ]; then
    exit
fi
echo 'mount device done'
#-------------------------------------------------
echo "======== 1. Create Uboot =================="
# 解压文件至uboot分区
tar zxvf uboot.tgz -C /mnt/uboot/
echo $?
sync
#-------------------------------------------------
# 删除环境变量
rm -rf /mnt/env/*
sync
#-------------------------------------------------
echo "======== 2. Create sys-ext-lib ============"
# 复制系统库
rm -rf /mnt/opt/*
tar zxvf sys-ext-lib.tgz -C /mnt/opt/
sync
#-------------------------------------------------
# 复制安装包至指定位置,用于进入系统后进行安装操作
echo "======== 3. Copy Kernel lib and App ======="
if [ ! -d /mnt/opt/all ]; then
    mkdir /mnt/opt/all   
fi
rm -rf /mnt/opt/all/*

cp  uboot.tgz sys-ext-lib.tgz /mnt/opt/all/

if [ -e app.tgz ]; then
    cp app.tgz /mnt/opt/all/
fi

sync
#-------------------------------------------------
umount /dev/${destination}* > /dev/null 2>&1

echo "======== 4. Finished ======================"