-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisk_wipe.sh
executable file
·181 lines (170 loc) · 3.33 KB
/
disk_wipe.sh
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
179
180
181
#!/bin/sh
#
# SPDX-License-Identifier: CDDL-1.0
#
# {{{ CDDL HEADER
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# }}}
#
# Copyright 2024 Peter Tribble
#
#
# wipe a disk
#
# On solarish systems, format/analyze/purge met old DOD requirements for
# overwriting a disk. Truly secure data removal requires degaussing or
# physical destruction, but if any form of overwrite or disk wipe is
# good enough, this script will do that for you
#
#
# arguments
# -A - wipe all drives
# -B - fdisk the drives, so we overwrite the whole drive; by default
# we just purge the Solaris partition
# -D - switch defect lists (not valid for all drives)
# * - a list of drives to wipe
#
#
# this script has minimal requirements so that it can run under mvi
# ksh [as /bin/sh] format devfsadm fdisk cat rm
#
WIPEALL=""
BFLAG=""
DEFECT=""
bail() {
echo "ERROR: $1"
exit 1
}
while getopts "ABD" opt; do
case $opt in
A)
WIPEALL="y"
;;
B)
BFLAG="-B"
;;
D)
DEFECT="y"
;;
esac
done
shift $((OPTIND - 1))
#
# any remaining arguments must be disk names
# must give a list or -A
#
if [[ -n "$WIPEALL" && $# -gt 0 ]]; then
bail "Cannot specify -A with disk names"
fi
if [[ -z "$WIPEALL" && $# -le 0 ]]; then
bail "Must specify -A or provide a list of disks"
fi
#
# construct drive list
# we need the name of the *drive*, not slice or partition
# try and remove all removable devices from the list
#
DLIST=""
if [[ -n "$WIPEALL" ]]; then
cd /dev/dsk || bail "cannot cd"
for drive in c*s2
do
if [ ! -h "/dev/removable-media/dsk/$drive" ]; then
drive=${drive/s2/}
DLIST="${DLIST} ${drive}"
fi
done
if [[ -z "$DLIST" ]]; then
bail "No drives detected"
else
echo "Drive list is"
echo "$DLIST"
fi
else
cd /dev/dsk || bail "cannot cd"
for drive in "$@"
do
#
# there's a complex dance here to normalize the drive name
#
drive=${drive/\/dev\/dsk\//}
if [ -h "${drive}s2" ]; then
drive="${drive}s2"
fi
if [ -h "/dev/dsk/${drive}" ]; then
drivel=${#drive}
drivel=$((drivel-1))
drivel=$((drivel-1))
drive=${drive:0:$drivel}
drive="${drive}s2"
if [ -h "/dev/removable-media/dsk/$drive" ]; then
echo "ERROR: $drive is removable, skipping"
else
drive=${drive/s2/}
DLIST="${DLIST} ${drive}"
fi
else
echo "ERROR: $drive is invalid, skipping"
fi
done
if [[ -z "$DLIST" ]]; then
bail "No valid drives detected"
else
echo "Drive list is"
echo "$DLIST"
fi
fi
FFILE=/tmp/disk_wipe.$$.input
/bin/rm -f $FFILE
if [[ -n $DEFECT ]]; then
cat > $FFILE <<EOF
defect
primary
quit
analyze
purge
quit
defect
both
quit
analyze
purge
quit
backup
quit
EOF
else
cat > $FFILE <<EOF
analyze
purge
quit
EOF
fi
#
# this is where you do the work
#
if [[ -n $BFLAG ]]; then
for disk in $DLIST
do
fdisk -B /dev/rdsk/"${disk}"p0
done
fi
#
for disk in $DLIST
do
format -f $FFILE -l /tmp/disk_wipe."${disk}".log "$disk" > /dev/null 2>&1 &
echo "log output is in /tmp/disk_wipe.${disk}.log"
done
#
# clean up
#
/bin/rm -f $FFILE