上文讲到了shell dd命令在bs参数太大的时候会出现异常。
现在讲讲怎么解决。
如果需要dd很大的文件的时候,需要指定很大的bs参数的时候怎么办呢?
答案是分块dd。多进行几次dd。
我写了个脚本sdd.sh,
sdd $file_input $file_output $bs
对应于
dd if=$file_input of=$file_output bs=$bs count=1
其中$bs值可以大于、等于、或小于dd的bs限定大小2147479552 (0x7ffff000)。
上脚本sdd.sh
#!/bin/bash
#
# Copyright 2019 peggy All rights reserved
# Author peggy.o_o@foxmail.com
# Created on 2019-4-25
# Version 1.0
# Title: super dd for <s> too large in 'dd if=<f> of=<o> bs=<s> count=1'
# you may not use this file until keep the declaraction aboving.
# dd if=f of=o bs=s count=1
f=$1
o=$2
s=$3
s_remaining=$s
s_finished=0
if [ $# -gt 3 ] ; then
s_block=$4
else
# 8M, infact 0.5M | 1M | 2M | 4M |8M are ok
s_block=8388608
fi
echo "s_block : $s_block"
ls -al $f
action_times=0
if [ $s_remaining -gt $s_block ] ; then
# $bs太大,需要分块dd
index=0
while [ $s_remaining -gt $s_block ] ;
do
let action_times=action_times+1
echo "++ dd times : $action_times"
time dd if=$f of=$o bs=$s_block count=1 skip=$index seek=$index
let s_remaining=s_remaining-$s_block
let s_finished=s_finished+$s_block
let index=index+1
done
let action_times=action_times+1
echo "++ dd times : $action_times"
time dd if=$f of=$o bs=1 count=$s_remaining skip=$s_finished seek=$s_finished
else
# $bs不大,可以直接dd
echo "bs $s gt 1g $s_block, normal"
let action_times=action_times+1
echo "++ dd times : $action_times"
time dd if=$f of=$o bs=$s count=1
fi