-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk-hotplug.sh
151 lines (137 loc) · 3.64 KB
/
disk-hotplug.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
#!/bin/sh
################### SCSI disk #################################################
#DEVTYPE=partition
#DEVPATH=/devices/platform/ahci.0/ata1/host0/target0:0:0/0:0:0:0/block/sdb/sdb2
#SUBSYSTEM=block
#SEQNUM=800
#UDEVD_EVENT=1
#ACTION=remove
#DEVNAME=/dev/sdb1
#DEVTYPE=partition
#DEVPATH=/devices/platform/ahci.0/ata1/host0/target0:0:0/0:0:0:0/block/sdb/sdb1
#SUBSYSTEM=block
#SEQNUM=801
#UDEVD_EVENT=1
#ACTION=remove
#DEVNAME=/dev/sdb
#DEVTYPE=disk
#DEVPATH=/devices/platform/ahci.0/ata1/host0/target0:0:0/0:0:0:0/block/sdb
#SUBSYSTEM=block
#SEQNUM=803
#UDEVD_EVENT=1
################### USB disk #################################################
#
# Block device event:
#
# ACTION=add
# DEVNAME=/dev/sda
# DEVTYPE=disk
# DEVPATH=/devices/platform/hiusb-ehci.0/usb1/1-2/1-2.1/1-2.1:1.0/host8/target8:0:0/8:0:0:0/block/sda
# SUBSYSTEM=block
# SEQNUM=544
# UDEVD_EVENT=1
#
# ACTION=add
# DEVNAME=/dev/sda1
# DEVTYPE=partition
# DEVPATH=/devices/platform/hiusb-ehci.0/usb1/1-2/1-2.1/1-2.1:1.0/host8/target8:0:0/8:0:0:0/block/sda/sda1
# SUBSYSTEM=block
# SEQNUM=545
# UDEVD_EVENT=1
#
# Use command "hdparm -z /dev/sda" to reread partition table
#
################################################################################
PREFIX=udisk
CONSOLE=/dev/ttyS000
################################################################################
show_env ()
{
local RED="\033[31m"
local NORMAL="\033[00m"
{
echo -e ${RED}"ACTION=$ACTION" ${NORMAL}
echo -e ${RED}"DEVNAME=$DEVNAME" ${NORMAL}
echo -e ${RED}"DEVTYPE=$DEVTYPE" ${NORMAL}
echo -e ${RED}"DEVPATH=$DEVPATH" ${NORMAL}
echo -e ${RED}"SUBSYSTEM=$SUBSYSTEM" ${NORMAL}
echo -e ${RED}"SEQNUM=$SEQNUM" ${NORMAL}
echo -e ${RED}"UDEVD_EVENT=$UDEVD_EVENT" ${NORMAL}
} > ${CONSOLE}
}
################################################################################
# new_name orgin_name
add_disk ()
{
local NEW_NAME=$1
local ORG_NAME=$2
if [ -e ${ORG_NAME} ]; then
cd /dev/; ln -sf ${ORG_NAME} ${NEW_NAME}; cd -
echo "${NEW_NAME} -> ${ORG_NAME}" > ${CONSOLE}
fi
}
################################################################################
# new_name
remove_disk ()
{
local NEW_NAME=$1
if [ -L /dev/${NEW_NAME} ]; then
rm -f /dev/${NEW_NAME}
fi
}
################################################################################
hotplug_usb ()
{
if [ ss"${SUBSYSTEM}" != ss"block" ]; then
return 1
fi
if [ ss"$(echo ${DEVPATH} | grep -r "usb")" = ss"" ]; then
return 1
fi
local TMP=$(echo ${DEVPATH:36} | sed -e 's/\/host.*//')
local NEW_NAME=${PREFIX}$(echo ${TMP##*/} | sed -e 's/[-.:]//g')
local PART_INDEX=`echo $DEVNAME | sed -e 's/\/dev\/sd.//'`
test -z "${PART_INDEX}" || NEW_NAME=${NEW_NAME}p${PART_INDEX}
case "${ACTION}" in
"add" )
add_disk "${NEW_NAME}" "${DEVNAME}"
;;
"remove" )
remove_disk "${NEW_NAME}"
;;
* )
echo "ACTION:${ACTION}" > ${CONSOLE}
;;
esac
return 0
}
hotplug_sata ()
{
if [ ss"${SUBSYSTEM}" != ss"block" ]; then
return 1
fi
if [ ss"$(echo ${DEVPATH} | grep -r "ata")" = ss"" ]; then
return 1
fi
local TMP=$(echo ${TMP} | sed -e 's/\/block.*//g')
TMP=$(echo ${TMP} | sed -e 's/\/devices.*\///g')
local NEW_NAME=${PREFIX}$(echo ${TMP} | sed -e 's/[:]//g')
local PART_INDEX=`echo $DEVNAME | sed -e 's/\/dev\/sd.//'`
test -z "${PART_INDEX}" || NEW_NAME=${NEW_NAME}p${PART_INDEX}
case "${ACTION}" in
"add" )
add_disk "${NEW_NAME}" "${DEVNAME}"
;;
"remove" )
remove_disk "${NEW_NAME}"
;;
* )
echo "ACTION:${ACTION}" > ${CONSOLE}
;;
esac
return 0
}
################################################################################
#show_env
hotplug_usb
hotplug_sata