img为一种镜像文件格式,它包含整个系统的所需要的文件。
#!/bin/bash
_ersze=10
# 进入脚本所在目录
_DIR=$( cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $_DIR
sdcard="bbb.img"
# 设置boot分区大小为40MB,根文件系统为1G
fatsize=40
linuxsize=512
# 创建普通文件,并用0填充
dd if=/dev/zero of=${sdcard}1 bs=1M count=$fatsize > /dev/null 2>&1
dd if=/dev/zero of=${sdcard}2 bs=1M count=$linuxsize > /dev/null 2>&1
_ersz=$(expr $fatsize + $linuxsize + 30)
dd if=/dev/zero of=${sdcard} bs=1M count=$_ersz > /dev/null 2>&1
# 创建msdos分区表
echo ""
echo "Creating new partition table"
echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR"
exit 0
fi
sync
echo "New filesystem created on $sdcard"
sleep 1
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR"
exit 1
fi
sleep 1
echo ""
echo "Partitioning $sdcard..."
# 创建分区
sfat=40960
efat=$(expr $fatsize \* 1024 \* 1024 / 512 + $sfat - 1)
echo " Creating boot & linux partitions"
sext4=$(expr $efat + 1)
if [ $linuxsize == 0 ]; then
eext4=""
else
eext4=$(expr $linuxsize \* 1024 \* 1024 / 512 + $sext4)
fi
echo -e "n\np\n1\n$sfat\n$efat\na\nn\np\n2\n$sext4\n$eext4\nt\n1\nb\nt\n2\n83\nw" | fdisk ${sdcard} > /dev/null 2>&1
sync
sleep 2
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR."
exit 0
fi
# 格式化分区
echo "Formating fat partition ..."
mkfs -t vfat -F 32 -n BOOT ${sdcard}1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating fat partition."
exit 0
fi
vfatuuid=`blkid -s UUID -o value ${sdcard}1`
echo " fat partition formated."
echo "Formating linux partition (ext4), please wait ..."
mkfs -F -t ext4 -L linux ${sdcard}2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating ext4 partition."
exit 1
fi
ext4uuid=`blkid -s UUID -o value ${sdcard}2`
echo " ext4 partition formated."
# 备份镜像的分区信息
dd if=${sdcard} of=${sdcard}u bs=512 count=40960 > /dev/null 2>&1
rm -f ${sdcard}
# 挂载镜像的各个分区
bootdir="boot"
rootfsdir="linux"
if [ ! -d $bootdir ]; then
mkdir $bootdir
fi
if [! -d $rootfsdir ]; then
mkdir $rootfsdir
fi
echo ""
echo "Mounting SD Card partitions..."
if ! mount ${sdcard}1 $bootdir; then
echo "ERROR mounting fat partitions..."
exit 1
fi
echo "FAT partitions mounted to $bootdir"
if ! mount ${sdcard}2 $rootfsdir; then
echo "ERROR mounting linux partitions..."
umount $bootdir
exit 1
fi
echo "linux partition mounted to $rootfsdir"
# 复制镜像到各个分区
#-----------------------------------------------------------------------------------------------
echo ""
echo "Copying file system to image ..."
echo ""
cp u-boot/* $bootdir
sync
#-------------------------------------------------------------------------------
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats rootfs/ $rootfsdir/
#-------------------------------------------------------------------------------
sync
# 取消挂载的分区
if ! umount $bootdir; then
echo "ERROR unmounting fat partition."
fi
rm -rf $bootdir/* > /dev/null 2>&1
rmdir $bootdir > /dev/null 2>&1
if ! umount $rootfsdir; then
echo "ERROR unmounting linux partitions."
fi
rmdir linux > /dev/null 2>&1
# 将各分区合并到最后的镜像
echo "Creating SDCard image..."
dd if=${sdcard}u of=${sdcard} > /dev/null 2>&1
if [ -f ${sdcard}1 ]; then
dd if=${sdcard}1 of=${sdcard} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
fi
if [ -f ${sdcard}2 ]; then
dd if=${sdcard}2 of=${sdcard} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
fi
exit 0