forked from BMarkley/ParkDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParkDrive.sh
150 lines (132 loc) · 3.29 KB
/
ParkDrive.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
#!/bin/bash
#This script unmounts all partitions, and parks the heads on a drive for hotswapping
#Input the designation of the Drive you want to Park
#Revisions
#2020/11/12 Script can now power down USB harddrives.
#2020/11/27 Removed Sudo from inside script. Sudo should be called outside script.
#2022/11/08 fised error where sdd won't unmount as it sips to sdd1 sdd2 etc
#Functions
HelpFunc(){
# Display Help
echo "ParkDrive unmounts and puts a drive into standby, which should park the heads, and prepare the drive for removal."
echo "Ideal for a Hot Swappable drive setup. Uses umount and hdparm. When run without options it is an interactive text UI."
echo "This Script was designed for my personal use. It is being provided for free, and with no garauntees whatsoever."
echo "Use at your own risk."
echo
echo "Syntax: ParkDrive [-o] [-d] [Drive]"
echo "options:"
echo "h Display this help"
echo "d Preselect drive (ie sda sdb sde etc)"
echo "f Fast Skips Countdown"
echo "Y Skips Confirmation Dialogue. Use at your own risk"
echo "s Suspend Drive (default action)"
echo "r Remove (Sleep) Drive (lower power state but can cause problems for some raid controllers, or make drives vanish)"
echo
exit 0
}
Park(){
#Unmounts all Partitions on a drive and puts the Drive into standby
#input Drive Designation (ie sdd)
umount /dev/${1}*
if [ $? -ne 0 ]; then
for File in /dev/${1}?
do
findmnt ${File}
if [ $? -ne 0 ]; then
echo "${File} is not mounted"
else
echo
echo "ERROR: ${File} is still mounted!!"
echo
exit 11
fi
done
else
echo "${1} unmounted"
fi
echo "wait three seconds"
for Index in {3..1}
do
echo "Parking drive in ${Index}"
sleep 1
done
find /dev/disk/by-id/ -lname "*${1}" | grep "usb" > /dev/null
if [ $? -ne 0 ]; then
hdparm -$Mode /dev/${1}
if [ $? -ne 0 ]; then
echo
echo "ERROR: Can Not Put Drive in Standby"
echo
exit 12
fi
else
udisksctl power-off -b /dev/${1}
if [ $? -ne 0 ]; then
echo
echo "ERROR: Can Not Put Drive in Standby"
echo
exit 13
fi
fi
return 0
}
#Variables
Help=0
Drive=NULL
Fast=0
Confirmation=0
Mode="y"
#Options
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) Help=1 ;;
-d|--Drive) Drive="$2"; shift ;;
-f|--fast) Fast=1 ;;
-Y|--NoWarnings) Confirmation=1 ;;
-s|--NoWarnings) Mode="y" ;;
-r|--NoWarnings) Mode="Y" ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
#Help
if [ $Help == 1 ]; then
HelpFunc
exit 0
fi
#Main
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: Run as Root"
exit 2
fi
if [ $Drive == NULL ]; then
printf "$(lsblk /dev/sd?)\n"
echo "Which drive would you like to remove? (ie sda sdb sdc etc)"
read Drive
fi
test -e /dev/${Drive}
if [ $? -ne 0 ]; then
echo "ERROR: Drive does Not Exist"
exit 3
fi
if [ $Confirmation != 1 ]; then
echo
echo "Are you sure you want to unmount /dev/${Drive}?"
echo "Unmounting system drives could cause system instability. y/N"
read Input
if [ "${Input}" != "y" ] && [ "${Input}" != "Y" ]; then
echo "Exit"
exit 0
fi
fi
if [ $Fast != 1 ]; then
for Index in {5..1}
do
echo "unmounting drive in ${Index}"
sleep 1
done
echo "unmounting drive"
echo
fi
Park ${Drive}
exit 0