-
Notifications
You must be signed in to change notification settings - Fork 2
/
sddd
executable file
·87 lines (75 loc) · 1.87 KB
/
sddd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/sddd
#
usage() {
echo "usage: sddd infile outfile [blocksize]"
echo " synchronicely writes infile to outfile using dd"
}
if [ -z "${1}" -o -z "${2}" ];
then
usage
exit 1
fi
INFILE="$1"
OUTFILE="$2"
BS=${3//[^0-9kKmMgGbB]/}
BS=${BS:-512}
if [ ! -r "${INFILE}" -a "${INFILE}" != "-" ];
then
echo "Can't read from ${INFILE}, bailing out"
exit 1
fi
if [ \( ! -e "${OUTFILE}" -a ! -w "${OUTFILE}" \) -a -z "${OUTFILE##*/dev/*}" ];
then
echo "Can't write to ${OUTFILE}, bailing out"
exit 1
fi
echo "copying from ${INFILE} to ${OUTFILE} with blocksize ${BS}"
# conv=fdatasync or oflag=direct ?
#
# dd or dcfldd ??
DD=$(which dcfldd)
if [ -n "${DD}" ]
then
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
if [ -n "${OFLAG}" ]
then
DCFD=1
else
DCFD=0
fi
fi
DD=$(which dd)
if [ -n "${DD}" ]
then
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
if [ -n "${OFLAG}" ]
then
DDD=1
else
DDD=0
fi
fi
if [ ${DDD:-0} -eq 1 -o ${DCFD:-0} -eq 0 ]
then
# Always prefer native DD
DD=dd
PROGRESS="status=progress"
else
# only when DDD -eq 0 and DCFD -eq 1
PROGRESS="status=on"
fi
# Do we support direct io? we've tested this twice, but this is the final test
OFLAG=$(${DD} --help | egrep -wo '(^\s*)direct' | sed 's/[^a-z]//ig')
# We like to be nice to the IO subsystem:
IFLAG=''; # set us up for slow pipes (like decompression pipelines)
if [ "${INFILE}" = "-" ]
then
unset INFILE
IFLAG="iflag=fullblock"
fi
# conv=fsync to catch truncated writes on errors
ionice -c2 -n7 ${DD} ${INFILE:+if="$INFILE"} bs=${BS} ${IFLAG} conv=noerror,fsync,notrunc ${OFLAG:+oflag=$OFLAG} ${PROGRESS} of="${OUTFILE}"