forked from Real-Time-Kodi/PiBAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuke.sh
executable file
·129 lines (111 loc) · 2.51 KB
/
nuke.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
#!/bin/bash
# Usage: nuke.sh [device]
# where [device] is the full path to the device to erase. Ex. /dev/sda
export devname=$(basename $1)
export logprefix="[$devname]: "
#Load our settings file
source /boot/PiBAN/PiBAN.conf
ERASE_COMMAND=$(eval echo "$ERASE_COMMAND")
log()
{
echo $logprefix$1 >> /var/log/PiBAN.log
echo $1
}
export -f log #Make logging function available
log "NUKING $1"
#TURN LED to blink
echo heartbeat > /sys/class/leds/led1/trigger
if (( $ENABLE_ATA_SECURE_ERASE ))
then
log "Attemting ATA secure erase."
/opt/PiBAN/secure_erase.sh $1
res=$?
else
#This could use some work.
res=1
fi
if [ $res -eq 0 ]
then
log "ATA Secure erase successful."
fi
if [ $res -eq 1 ]
then
log "ATA Secure erase not supported/enabled. Using fallback."
if (( $DRY_RUN ))
then
log "DRY RUN, NO ERASE HAPPENING."
else
nice $ERASE_COMMAND
fi
fi
if [ $res -eq 2 ]
then
log "Drive is locked. Aborting."
gpio -g write 17 0
return 2
fi
if [[ $CREATE_FILESYSTEM -eq 1 ]] && [[ $DRY_RUN -eq 0 ]]
then
# The following ugly mess creates a new partition table with one partition
# that takes up the whole disk.
log "Creating Partiton table."
echo "o
n
p
w
" | fdisk "$1" > /dev/null
# Make our filesystem
# Check what FS we should use
log "Creating filesystem: FAT32"
mkfs.vfat -v -F 32 "$1"1
# Mount our new filesystem
log "Mounting filesystem"
mntpath=/mnt/$(basename "$1")1
rm -r $mntpath &> /dev/null
mkdir $mntpath
mount -o sync "$1"1 $mntpath
cd $mntpath
# Check if we should run a script and run it here
if [[ -n $RUN_SCRIPT ]]
then
log "Running scipt: $RUN_SCRIPT"
$RUN_SCRIPT $1
fi
# Check if we should copy files and do that here
if [[ -n $COPY_FILES ]]
then
log "Copying Files from $COPY_FILES"
cp -r $COPY_FILES .
fi
if (( $COPY_LOGS ))
then
log "Copying LOGS"
touch Erased_With_PiBAN.txt
echo -e "This drive has been securely erased and repartitioned with PiBAN\n\
https://github.com/Real-Time-Kodi/PiBAN" > Erased_With_PiBAN.txt
#Let's also copy our log file to the device.
cp /tmp/$devname.log .
fi
# Unmount our filesystem
log "Unmounting filesystem"
cd /
umount $mntpath
rm -r $mntpath
else
cd /
if [[ -n $DD ]]
then
log "Copying Image to $1"
dd if=$DD of=$1 bs=1M
log "DD Exited with status: $?"
fi
if [[ -n $RUN_SCRIPT ]]
then
log "Running scipt: $RUN_SCRIPT"
$RUN_SCRIPT $1
fi
fi
sync #SYNC because I don't trust the kernel to do it for me.
#TURN LED to normal
echo input > /sys/class/leds/led1/trigger
log "Drive Completed $1"