linux使用dd打包boot 、linux、rootfs 到二进制文件,使用win32diskimager一键烧入

运行需要3个参数:
参数1: 目录名(包含boot 、boot.scr、linux)
参数2: 目录 名(包含根文件系统)
参数3: 输出文件名

shell脚本

#!/bin/bash
#!/bin/bash

function echo_log()
{
	echo -e "\033[32m ------------------------------------------ \033[0m"
	echo $1
	echo -e "\033[32m ------------------------------------------\n \033[0m"
}

function echo_error()
{
	echo -e "\033[33m ------------------------------------------ \033[0m"
	echo $1
	echo -e "\033[33m ------------------------------------------\n \033[0m"
}
#卸载p1、p2 (2>&1把错误输出绑定标志输出,都输入到/dev/null)
sudo umount $_PWD/p1 $_PWD/p2 >/dev/null 2>&1      
#set -x  条件不为TRUE则退出程序
set -e

_BOOT="$1"
_ROOTFS="$2"
_DISTRO="$3"
#判断是第三个参数是否存在,不存在则默认赋值为“linux”
if [ -z $_DISTRO ];then _DISTRO=linux;fi
#获取当前文件目录
_PWD=$(pwd)
#_BOOT和_ROOTFS参数字符是否为空,_ROOTFS文件夹是否存在
if [ -n "$_BOOT" -a -n "$_ROOTFS" ]
	#如果boot.scr文件存在且大小不为0
	then if ([ -s $_BOOT/boot.scr -a -s $_BOOT/u-boot-sunxi-with-spl.bin -a -s $_BOOT/zImage ] && \
                 [ -d  $_ROOTFS/lib/modules/4.10.2-licheepi-zero+ ])
		then
		      echo_log "Checking the files ok!"
		else  echo_error "Can not find boot.scr, zImage, uboot or the modules file!" && exit
	     fi
	else echo_error "No boot or rootfs folder!" && exit
fi

echo_log "Calculating the files's total size..."
#计算BOOT目录总大小
_SIZE_BOOT=$(sudo du -s -BM $_BOOT | cut -d 'M' -f 1)
#计算ROOTFS目录总大小
_SIZE_ROOTFS=$(sudo du -s -BM $_ROOTFS | cut -d 'M' -f 1)
#计算镜像文件大小
_IMG_SIZE=$(($_SIZE_BOOT+$_SIZE_ROOTFS+200))
echo_log "Creating dd img file..."
#dd不存在,则创建dd文件
if [ ! -e $_PWD/lichee_zero-${_DISTRO}.dd ]
	then
	    dd if=/dev/zero of=./lichee_zero-${_DISTRO}.dd bs=1M count=$_IMG_SIZE
fi
#创建dd文件是否成功
if [ $? -ne 0 ]
   then echo_error "getting error in creating dd img!"
   exit
fi
#获取loop设备
_LOOP_DEV=$(losetup -f)
if [ -z $_LOOP_DEV ]
    then echo_error "can not find a loop device!"
    exit
fi
#使用 losetup将磁盘镜像文件虚拟成块设备
sudo losetup $_LOOP_DEV ./lichee_zero-${_DISTRO}.dd
if [ $? -ne 0 ]
 	then echo_error "dd img --> $_LOOP_DEV error!"
	sudo losetup -d $_LOOP_DEV >/dev/null 2>&1 && exit
fi

#创建分区
echo_log "creating partitions..."
#blockdev --rereadpt $_LOOP_DEV >/dev/null 2>&1
#sfdisk来进行分区,【1M,16M,c】表示前1M到16M分区为fat格式,【,,L】表示为剩余空间创建分区linux格式
cat <<EOT |sudo  sfdisk $_LOOP_DEV
1M,16M,c
,,L
EOT
sleep 10
#partx -u更新指定分区
sudo partx -u $_LOOP_DEV
#格式化分区,格式化出错则结束脚本
sudo mkfs.vfat ${_LOOP_DEV}p1 ||exit
sudo mkfs.ext4 ${_LOOP_DEV}p2 ||exit
if [ $? -ne 0 ]
	then echo_error "error in creating partitions"
	#卸除设备,结束脚本
	sudo losetup -d $_LOOP_DEV >/dev/null 2>&1 && exit 
	#sudo partprobe $_LOOP_DEV >/dev/null 2>&1 && exit
fi

echo_log "writing u-boot-sunxi-with-spl to $_LOOP_DEV"
#先格式化前面的1k-1M数据(前512bytes包含分区表信息)
sudo dd if=/dev/zero of=$_LOOP_DEV bs=1K seek=1 count=1023
#偏移8k处写入
sudo dd if=$_BOOT/u-boot-sunxi-with-spl.bin of=$_LOOP_DEV bs=1024 seek=8
#失败则卸载退出
if [ $? -ne 0 ]
	then echo_error "writing u-boot error!"
	sudo losetup -d $_LOOP_DEV >/dev/null 2>&1 && exit
	#sudo partprobe $_LOOP_DEV >/dev/null 2>&1 && exit
fi
#sync命令的作用是,将有关文件系统的存储器常驻信息送入物理介质内
sudo sync
#创建p1和p2文件夹
mkdir -p $_PWD/p1 >/dev/null 2>&1
mkdir -p $_PWD/p2 > /dev/null 2>&1
#把前面创建的分区挂载到p1,p2
sudo mount ${_LOOP_DEV}p1 $_PWD/p1
sudo mount ${_LOOP_DEV}p2 $_PWD/p2
echo_log "copy boot and rootfs files..."
#清空p1与p2目录
sudo rm -rf  $_PWD/p1/* && sudo rm -rf  $_PWD/p2/*
#把linux系统相关文件与根文件系统分别拷贝进p1,p2
sudo cp -f -r $_BOOT/* $_PWD/p1 && sudo cp -a  -f  $_ROOTFS/* $_PWD/p2
if [ $? -ne 0 ]
	then echo_error "copy files error! "
	sudo losetup -d $_LOOP_DEV >/dev/null 2>&1
 	sudo umount ${_LOOP_DEV}p1  ${_LOOP_DEV}p2 >/dev/null 2>&1
	exit
fi
echo_log "Done"
#sync
sudo sync
#卸载p1、p2 ,执行losetup从loop设备卸载
sudo umount $_PWD/p1 $_PWD/p2  && sudo losetup -d $_LOOP_DEV
if [ $? -ne 0 ]
	then echo_error "umount or losetup -d error!!"
	exit
fi
#完成
echo_log "The lichee_zero-${_DISTRO}.dd has been created successfully!"
exit


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yfw&武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值