-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdswap
executable file
·162 lines (135 loc) · 3.04 KB
/
sdswap
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
#!/bin/bash
getdevice() {
#based on https://unix.stackexchange.com/questions/136781/how-to-get-device-filename-from-lsusb-output-or-by-device-path
local idV=${1%:*}
local idP=${1#*:}
for path in $(dirname $(find -L /sys/bus/usb/devices/ -maxdepth 2 -name 'idVendor')); do
if grep -q "$idV" "$path/idVendor" && grep -q "$idP" "$path/idProduct"; then
echo "$path"
fi
done
}
run_root() {
if [ "$(id -u)" != "0" ]; then
sudo "$@"
else
"$@"
fi
}
get_blockdev() {
# This can match twice so add `-m 1` to grep to only match once
local SYSFS_BLOCK_RELATIVE
SYSFS_BLOCK_RELATIVE=$(realpath --relative-base="$1" /sys/block/* | grep -vm 1 "^/") || return $?
basename "$SYSFS_BLOCK_RELATIVE" 2>/dev/null
}
print_help() {
cat << EOF
To switch the SD card to USB run:
$0 1
To switch the SD card to SBC run:
$0 0
To print the block dev run:
$0 p
If you have multiple devices run either of the above.
Then run with the last param the same as above:
$0 \$(Your USB number) 0
NOTE:
When printing the block device the script waits until the device exists
Examples:
# $0 0
Uisng device /sys/bus/usb/devices/3-4
# $0 0
Uisng device /sys/bus/usb/devices/3-4
Already off.
# $0 p
/dev/sda
# $0 0
Multiple devices found! Pass a usb number as param 1...
Usb num: 3-4
Usb num: 3-1
# $0 3-1 0
Uisng device /sys/bus/usb/devices/3-1
EOF
exit 1
}
get_devices() {
local DEVICES
DEVICES=$(getdevice 0bda:0316)
if echo "$DEVICES" | grep -q " "; then
if [ $# -eq 2 ]; then
for dev in $DEVICES; do
if echo "$dev" | grep -q "$1"; then
export DEVICE="$dev"
fi
done
if [ -z "$DEVICE" ]; then
echo "Device not found. Pass a usb number as param 1..."
for dev in $DEVICES; do
echo "Usb num:" "$(basename "$dev")"
done
exit 1
fi
else
echo "Multiple devices found! Pass a usb number as param 1..."
for dev in $DEVICES; do
echo "Usb num:" "$(basename "$dev")"
done
exit 1
fi
else
export DEVICE=$DEVICES
fi
if [ -z "$DEVICES" ]; then
echo "No devices found ;("
exit 1
fi
}
setup_pwrctl() {
echo 0 | run_root tee "$1"/power/autosuspend_delay_ms &>/dev/null
echo auto | run_root tee "$1"/power/control &>/dev/null
}
turn_off() {
echo "$1" | run_root tee /sys/bus/usb/drivers/usb/unbind &>/dev/null || echo "Already off."
}
turn_on() {
echo "$1" | run_root tee /sys/bus/usb/drivers/usb/bind &>/dev/null || echo "Already on"
}
print_info() {
echo "Uisng device" "$1"
local DEVNAME
DEVNAME=$(get_blockdev "$1")
if [[ ${DEVNAME} ]]; then
echo "With devpath of /dev/""$DEVNAME"
fi
}
print_dev() {
local DEVNAME
while true; do
DEVNAME=$(get_blockdev "$1") && break
done
echo /dev/"$DEVNAME"
exit 0
}
main() {
local OPERATION
local USB_NUM
OPERATION=$(eval echo "\${$#}")
case "$OPERATION" in
0|1|p)
get_devices "$@";;
*)
print_help;;
esac
if [ "$OPERATION" == 'p' ]; then
print_dev "$DEVICE"
fi
print_info "$DEVICE"
USB_NUM=$(basename "$DEVICE")
setup_pwrctl "$DEVICE"
if [ "$OPERATION" == '1' ]; then
turn_on "$USB_NUM"
elif [ "$OPERATION" == '0' ]; then
turn_off "$USB_NUM"
fi
}
main "$@"