-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacefader
executable file
·178 lines (154 loc) · 5.49 KB
/
spacefader
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
VERSION="1.0.5"
#----------------------
TGTDIR="$1"
USERND="$2"
if [[ "" = "${TGTDIR}" || "-h" = "${TGTDIR}" || "--help" = "${TGTDIR}" ]]; then
cat <<_USAGE
Overwrite all remaining space ${VERSION}
------------------------------------------
Jan 2010, EdenWorX, sed
This program will fill up the target directory with files of 1 MiB
size with zeros (from /dev/zero) until all the remaining space is
filled up. After all space is wasted, the files are deleted and the
space freed again.
Usage: $0 targetdir [random]
Arguments:
targetdir - directory in which files are created
if the directory does not exist it will be created,
and deleted again after the work is done.
Options:
random - fill files from /dev/(u)random instead /dev/zero
_USAGE
exit 0;
fi
## Determine source device
#--------------------------
SRCDEV=/dev/zero
if [[ -n "${USERND}" && "${USERND}" == "random" ]]; then
[ -r /dev/random ] && SRCDEV=/dev/random
[ -r /dev/urandom ] && SRCDEV=/dev/urandom
fi
## Be sure the output directory exists
#--------------------------------------
OWNDIR=0; # If the target directory has to be created, it is set to 1.
if [[ ! -d ${TGTDIR} ]]; then
OWNDIR=1; ## We do it ourselves, we delete afterwards!
mkdir -p "${TGTDIR}"
if [[ ! -d "${TGTDIR}" ]]; then
[ -f "${TGTDIR}" ] && echo "${TGTDIR} is not a directory!"
[ -f "${TGTDIR}" ] || echo "${TGTDIR} can not be created!"
exit 1;
fi
fi ## check target directory
## Calculate space and the approx count of files to be created:
#---------------------------------------------------------------
echo -n "determining initial size..."
SPACELEFT=$(df --sync -B 1 "${TGTDIR}" | grep '/' | tr -s ' ' | cut -d ' ' -f 4)
FILESIZE=$((1024*1024)); # One MiB
FILESLEFT=$((SPACELEFT/FILESIZE))
FILESCURR=0; # We need to count what we are doing.
RECALCCNT=$((FILESLEFT/4)); # Recount free space when this number of files have been created
RECALCCUR=${RECALCCNT}; # The next re-calc mark
DOTPRGCNT=$((RECALCCNT/24)); # So we can show some dots to show progress
DDBSIZE=4096; # The blocksize used by dd (adjust this if you think you can get more speed...)
DDCOUNT=$((FILESIZE/DDBSIZE)); # So DDCOUNT * DDBSIZE = FILESIZE
echo " done - ${SPACELEFT} bytes available"
## Now print status and ask whether we should start:
cat << _ENDSTATUS
$0 will be started with the following parameters:
Target Directory .. : ${TGTDIR}
Source Device ..... : ${SRCDEV}
Files to be created : ${FILESLEFT}
Re-Checking every : ${RECALCCNT} files
_ENDSTATUS
echo -n "Start execution ? (y/N)"
read -r DOACT
case ${DOACT} in
[yYjJ])
DOACT=1
;;
*)
DOACT=0
;;
esac
# If we are not allowed to act, leave peacefully
[ ${DOACT} -eq 1 ] || exit 0
echo -n "working "
#==========================================
## === MAIN LOOP PART ONE: Create files ===
#==========================================
while [[ ${FILESLEFT} -gt 0 ]]; do
FILESCURR=$((FILESCURR+1))
#=========================================
## === Step 1: Check for re-count mark ===
#=========================================
if [[ "$FILESCURR" == "$RECALCCUR" ]]; then
## Hit a re-count mark, re-calculate space:
echo -en "\nrecalculating ... "
SPACELEFT=$(df --sync -B 1 "${TGTDIR}" | grep '/' | tr -s ' ' | cut -d ' ' -f 4)
FILESLEFT=$((SPACELEFT/FILESIZE))
# Let's see whether there are files left:
if [[ ${FILESLEFT} -gt 0 ]]; then
# We have to adjust the recalccounter
[ ${RECALCCNT} -lt ${FILESLEFT} ] || RECALCCNT=${FILESLEFT}
RECALCCUR=$((RECALCCUR+RECALCCNT));
echo "${FILESLEFT} files left, re-checking at ${RECALCCUR}"
echo -n "working ... "
elif [[ ${SPACELEFT} -gt 0 ]]; then
# We have no full files, but some space left:
FILESIZE=${SPACELEFT}
FILESLEFT=1
DDBSIZE=${FILESIZE}
DDCOUNT=1
echo "${FILESIZE} bytes left for last file"
else
# We are done!
FILESLEFT=0
echo "everything filled up!"
fi ## end check free space
fi ## end check for re-count mark
#=========================================
## === Step 2: Check for progress mark ===
#=========================================
if [[ 0 -eq $((FILESCURR%DOTPRGCNT)) ]]; then
echo -n "."
fi
#===================================
## === Step 3: Create dummy file ===
#===================================
if [[ ${FILESLEFT} -gt 0 ]]; then
dd if="${SRCDEV}" bs="${DDBSIZE}" count="${DDCOUNT}" of="${TGTDIR}/dummy_${FILESCURR}.waste" 2>/dev/null
FILESLEFT=$((FILESLEFT-1))
fi
done ## end main loop part one
#================================================
## === MAIN LOOP PART TWO: Swallow last bytes ===
#================================================
echo -en "\nrecalculating ... "
SPACELEFT=$(df --sync -B 1 "${TGTDIR}" | grep '/' | tr -s ' ' | cut -d ' ' -f 4)
if [[ ${SPACELEFT} -gt 0 ]]; then
echo -n "${SPACELEFT} bytes left for last file "
FILESCURR=$((FILESCURR+1))
dd if="${SRCDEV}" bs="${SPACELEFT}" count=1 of="${TGTDIR}/dummy_${FILESCURR}.waste" 2>/dev/null
echo "written"
else
echo "no space left on device"
fi
echo "done"
#============================================
## === MAIN LOOP PART THREE: Delete files ===
#============================================
echo -n "deleting "
DOTPRGCNT=$((FILESCURR/20))
while [[ ${FILESCURR} -gt 0 ]]; do
rm -f "${TGTDIR}/dummy_${FILESCURR}.waste"
FILESCURR=$((FILESCURR-1))
if [[ 0 -eq $((FILESCURR%DOTPRGCNT)) ]]; then
echo -n "."
fi
done
echo "done"
## Last one: Delete the directory if it is ours:
[ ${OWNDIR} -eq 1 ] && rm -rf "${TGTDIR}"
echo "program finished."