forked from MestreLion/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dd-status
executable file
·110 lines (95 loc) · 3.31 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
#!/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##*/}"
time=30
block=1M
fatal() { [[ "$1" ]] && echo "$myname: error: $1" >&2 ; exit ${2:-1} ; }
message() { printf "%s\n" "$1"; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
integer() { [[ "$1" != *[!0-9]* ]] || argerr "'$1'${2:+ in $2} is not an integer." ; }
invalid() { argerr "invalid option: $1" ; }
missing() { argerr "missing ${2:+$2 }argument${1:+ from $1}." ; }
is_running() { kill -0 $pid 2>/dev/null ; }
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 copy even for mounted partitions
--time TIME - display progress every TIME seconds. [Default: $time]
--block SIZE - copy SIZE blocks at a time. [Default: $block]
Copyright (C) 2012 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 ;;
--time ) shift ; time="$1" ;;
--time=* ) time="${1#*=}" ;;
--block ) shift ; block="$1" ;;
--block=* ) block="${1#*=}" ;;
-- ) shift ; break ;;
-* ) invalid "$1" ;;
* ) files+=( "$1" ) ;;
esac
shift
done
files+=( "$@" )
input=${files[0]}
output=${files[1]}
[[ "$input" ]] || missing "" "INPUT"
[[ "$output" ]] || missing "" "OUTPUT"
[[ "$block" ]] || missing "--block" "SIZE"
integer "$time" "--time"
if ! ((force)); then
for file in "$input" "$output"; do
if [[ -b "$file" ]] && grep -q "^$file " /proc/mounts; then
fatal "$file seems to be a mounted partition. Un-mount it first or use --force."
fi
done
fi
echo "dd if=\"$input\" of=\"$output\" bs=\"$block\""
#set -m # do not supress backgound output
dd if="$input" of="$output" bs="$block" & pid=$!
trap 'pkill -P $$' EXIT # abort dd if script it aborted
sleep 0.2 # wait for any immediate errors (permission denied, file not found, etc)
if is_running $pid; then
echo "dd PID: $pid"
echo "Starting at $(date)"
echo "Reporting every $time seconds"
while is_running $pid; do
sleep "$time"
date
kill -USR1 $pid 2>/dev/null # request dd status report
done
fi