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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#!/bin/bash
# This script is meant to support common media managing utilities.
#
# Parameters:
# - list -- list media
# - mount <device> -- mount a device
# - unmount <path> -- unmount given mount point
#
# Supported utilities (only one is used, listed higher to lower priority):
# - udevil: udisks-like command-line tool with no D-Bus or daemon
# requirements: `udevil` and `devmon` commands (both are part of udevil)
# - udisks: first vesion of disk management D-Bus daemon
# requirements: `udisks` and `umount` commands
# - udisks2: second vesion of disk management D-Bus daemon
# requirements: `udisksctl` and either `python2` or `python3` with "dbus"
# module
function usage_error() {
echo "Usage: vifm-media list | mount <device> | unmount <path>"
exit 1
}
function list() {
while read -r dev; do
if [ "${dev:0:10}" = /dev/disk/ ]; then
continue
fi
local out=$(info "$dev")
if grep -qe '^\s*partition table:\s*$' <<< "$out"; then
# can't mount the whole drive
continue
fi
if grep -qe '^\s*removable:\s*0\s*$' <<< "$out" &&
grep -qe '^\s*system internal:\s*1\s*$' <<< "$out"; then
# skip drives which aren't detachable
continue
fi
if grep -qe '^\s*has media:\s*0\s*$' <<< "$out"; then
continue
fi
echo device="$dev"
local label=$(grep -m1 '^\s*label:.*$' <<< "$out" | sed 's/^[^:]*:\s*//')
echo "label=$label"
local paths=$(grep -m1 '^\s*mount paths:.*$' <<< "$out" | sed 's/^[^:]*:\s*//')
IFS=',' paths=( $paths )
for path in "${paths[@]}"; do
echo "mount-point=$path"
done
echo
done <<< "$("$monitor" --enumerate-device-files)"
}
if type -P devmon udevil &>/dev/null; then
monitor=devmon
function info() {
udevil info "$@"
}
function mount() {
devmon --mount "$1"
}
function unmount() {
devmon --unmount "$1"
}
elif type -P udisks umount &>/dev/null; then
monitor=udisks
function info() {
udisks --show-info "$@"
}
function mount() {
udisks --mount "$1"
}
function unmount() {
umount "$1"
}
elif python2 -c 'import dbus' &>/dev/null ||
${python3:=python3} -c 'import dbus' &>/dev/null &&
type -P udisksctl &>/dev/null; then
python=${python3:-python2}
function udisks2() {
$python - "$@" <<"EOF"
import dbus
import sys
def decode(array):
return bytearray(array).replace(b'\x00', b'').decode('utf-8')
def devs():
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object('org.freedesktop.UDisks2',
'/org/freedesktop/UDisks2')
om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
for v in om.GetManagedObjects().values():
drive_info = v.get('org.freedesktop.UDisks2.Block', {})
part_table = v.get('org.freedesktop.UDisks2.PartitionTable')
usage = drive_info.get('IdUsage')
system = drive_info.get('HintSystem')
ro = drive_info.get('ReadOnly')
if usage == "filesystem" and \
not system and \
not ro and \
part_table is None:
device = drive_info.get('Device')
if device is not None:
device = decode(device)
fs = v.get('org.freedesktop.UDisks2.Filesystem', {})
yield (device, drive_info.get('IdLabel'), fs)
def list_usb():
for (device, label, fs) in devs():
print('device=%s' % device)
print('label=%s' % label)
for point in fs.get('MountPoints', {}):
point = decode(point)
print("mount-point=%s" % point)
def path_to_dev(path):
import os
path = os.path.normpath(path)
for (device, label, fs) in devs():
for point in fs.get('MountPoints', {}):
point = os.path.normpath(decode(point))
if point == path:
return device
return None
def mount(device):
bus = dbus.SystemBus()
obj = bus.get_object('org.freedesktop.UDisks2',
'/org/freedesktop/UDisks2/block_devices%s'%device[4:])
print(obj.Mount({}, dbus_interface='org.freedesktop.UDisks2.Filesystem'))
def unmount(path):
device = path_to_dev(path)
if device is None:
sys.exit(1)
bus = dbus.SystemBus()
obj = bus.get_object('org.freedesktop.UDisks2',
'/org/freedesktop/UDisks2/block_devices%s'%device[4:])
obj.Unmount({}, dbus_interface='org.freedesktop.UDisks2.Filesystem')
if len(sys.argv) < 2 or sys.argv[1] == 'list':
list_usb()
elif len(sys.argv) == 3 and sys.argv[1] == 'mount':
mount(sys.argv[2])
elif len(sys.argv) == 3 and sys.argv[1] == 'unmount':
unmount(sys.argv[2])
EOF
}
function list() {
udisks2 list
}
function mount() {
udisks2 mount "$1"
}
function unmount() {
udisks2 unmount "$1"
}
else
echo "Neither of the supported backends were found" \
"(udevil, udisks, udisks2)" >&2
exit 1
fi
case "$1" in
list)
if [ $# -ne 1 ]; then
usage_error
fi
list
;;
mount)
if [ $# -ne 2 ]; then
usage_error
fi
mount "$2"
;;
unmount)
if [ $# -ne 2 ]; then
usage_error
fi
unmount "$2"
;;
*)
usage_error
;;
esac
|