-
Notifications
You must be signed in to change notification settings - Fork 2
/
remove_disk_device.sh
69 lines (59 loc) · 1.47 KB
/
remove_disk_device.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
#!/bin/bash
function help() {
cat<<EOF
DISK DEVICE REMOVAL TOOL
This tool removes the specified disk device from the host. It can be used when a disk in
a jbod-configured host is bad and pulled from service but doesn't need to be replaced
right away. For safety, the device must be unmounted first, and should be commented out
in /etc/fstab to prevent it from remounting on reboot.
Specify a single device to be removed as the only argument, e.g.
sudo remove_disk_device.sh sdd
EOF
}
function check_root() {
if [ "$(id -u)" -ne "0" ]; then
echo "This script must be run as root."
exit 1
fi
}
function get_lsscsi() {
echo
lsscsi
echo
}
function check_dev_exists_and_unmounted() {
if [[ -b /dev/$1 ]]; then
if [[ $(grep $1 /proc/mounts) ]]; then
echo "$1 is still mounted. Unmount it first."
exit 1
elif [[ $(grep $1 /proc/mdstat) ]]; then
echo "$1 is used in a raid volume. Exiting."
exit 1
fi
else
echo "$1 is not a block device."
exit 1
fi
}
function remove_bypath_links() {
for link in /dev/disk/by-path/*; do
if [[ $(readlink -f $link) =~ "$1" ]]; then
echo "Removing $(ls -l $link | awk '{print $9,$10,$11}')"
rm $link
fi
done
}
function delete_block_device() {
echo "Deleting $1 block device"
echo 1 >> /sys/block/$1/device/delete
}
check_root
if [[ $# == 1 ]]; then
check_dev_exists_and_unmounted $1
get_lsscsi
remove_bypath_links $1
delete_block_device $1
get_lsscsi
else
help
fi