-
Notifications
You must be signed in to change notification settings - Fork 12
/
dd-status
executable file
·131 lines (112 loc) · 3.72 KB
/
dd-status
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#
# dd-status - a dd wrapper with progress status
#
# Copyright (C) 2013 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. See <http://www.gnu.org/licenses/gpl.html>
myname=${0##*/}
block=1Mi
size=0
autosudo=1
force=0
eta=1
fatal() { [[ "$1" ]] && echo "$myname: error: $@" >&2 ; exit 1; }
message() { printf "%s\n" "$1"; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1; }
invalid() { argerr "invalid option: $1" ; }
missing() { argerr "missing ${2:+$2 }argument${1:+ from $1}." ; }
sizefmt() { [[ "$1" ]] || missing "${3:-SIZE}" "${2:-}";
echo $(numfmt --from=auto -- "$1" || fatal); }
usage() {
cat <<-USAGE
Usage: $myname [options] INPUT OUTPUT
USAGE
if [[ "$1" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
dd wrapper with progress status
Options:
-h|--help - show this page.
-f|--force - force operation even on mounted partitions
-S|--no-sudo - do not use 'sudo' even if OUTPUT is not writable by user
-s|--size SIZE - number of BYTES (*NOT* blocks!) to copy.
0 copies the whole input [Default: $size]
-B|--block-size SIZE - copy SIZE blocks at a time. [Default: $block]
All SIZE units are parsed through 'numfmt' auto mode, so SI suffixes such
as K, M, G are allowed. '1K' means 1000; use 'Ki' (and 'Mi', 'Gi', etc)
for 1024-based units.
Copyright (C) 2013 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
# Option handling
files=()
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (($#)); do
case "$1" in
-f|--force ) force=1;;
-S|--no-sudo) autosudo=0;;
-B|--block-size) shift; block=$1;;
-s|--size ) shift; size=$1 ;;
--block-size=*) block=${1#*=};;
--size=* ) size=${1#*=} ;;
--) shift; break;;
-*) invalid "$1";;
* ) files+=( "$1" );;
esac
shift || break
done
files+=( "$@" )
input=${files[0]}
output=${files[1]}
[[ "$input" ]] || missing "" "INPUT"
[[ "$output" ]] || missing "" "OUTPUT"
sudo=()
if ((autosudo)) && [[ -e "$output" && ! -w "$output" ]]; then sudo=(sudo); fi
size=$( sizefmt "$size" '--size' ) || exit
block=$(sizefmt "$block" '--block-size') || exit
if ! ((force)); then
for file in "$input" "$output"; do
if [[ -b "$file" ]] && grep -q "^$file" /proc/mounts; then
fatal "$file seems to be (or contain) mounted partition(s)." \
"Un-mount it first or use --force."
fi
done
fi
args=(of="$output" bs="$block" oflag=nocache,sync)
if !((eta)); then
args+=(if="$input" status=progress)
if ((size)); then
args+=(iflag=count_bytes count="$size")
fi
cmd=("${sudo[@]}" dd "${args[@]}")
echo "${cmd[@]}" >&2
"${cmd[@]}"
exit
fi
args+=(status=none)
pvargs=(--buffer-size "$block")
if ((size)); then
pvargs+=(--size "$size" --stop-at-size)
fi
pvsudo=()
if ((autosudo)) && [[ -e "$input" && ! -w "$input" ]]; then pvsudo=(sudo); fi
echo "${pvsudo[@]}" pv "${pvargs[@]}" -- "$input" '|' "${sudo[@]}" dd "${args[@]}"
"${pvsudo[@]}" pv "${pvargs[@]}" -- "$input" | "${sudo[@]}" dd "${args[@]}"