forked from usawa/tomtom-sdcard-mounter
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rlink.sh
executable file
·333 lines (262 loc) · 8.09 KB
/
rlink.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
#
# title : rlink.sh
# description : A shell script to mount TOMTOM.xxx files from a TomTom
# SD card to browse/modify its content.
# author : JV conseil – Internet Consulting
# credits : JV-conseil
# copyright : Copyright (c) 2021, JV conseil – Internet Consulting,
# All rights reserved.
# usage : bash ./rlink.sh
# requirements : VirtualBox, VM VirtualBox Extension Pack and one Linux
# distribution like Ubuntu.
# date : 20210525
# version : 1.0
# bash_version : 5.1.8(1)-release
#
#===============================================================================
__TOMTOM_PATH="./"
__rlink_help() {
cat <<EOF
R-Link Evolution Missing Explorer
---------------------------------
Place this file in the same folder where your TOMTOM.000 ... files are, ideally
on the SD card:
/Volumes/SDCARD
├── TOMTOM.000
├── TOMTOM.001
├── TOMTOM.002
├── TOMTOM.003
└── rlink.sh
options:
h Help.
A TomTom R-Link SD card is made of multiple TOMTOM.xxx (000, 001, ...) files.
These files contain a splitted Lunux filesystem. The scripts will:
- Attach each file to a virtual block device, called a loopback device : /dev/loopX
- Aggregate all these devices as a big "linear" one: /dev/md/tomtom_vfs, seen a a real disk
- Mount this new device to the /mnt/tomtom_vfs directory.
Once done, you can modify the content, to add, for example, POIs.
EOF
}
# Output to stderr
__rlink_echoerr() {
echo "$@" 1>&2
}
# Find first available /dev/loopX
__rlink_loopback_dev() {
typeset -i num
num=0
while [[ ${num} -le 255 ]]; do
if ! losetup /dev/loop${num} >/dev/null 2>&1; then
break
fi
num=$((num + 2))
done
echo "/dev/loop${num}"
}
# Count the number of TOMTOM.xxx files
__rlink_count_tomtom_vfs_files() {
typeset -i count_vfs_files
count_vfs_files=0
while [[ -e ${__TOMTOM_PATH}TOMTOM.$(printf "%03d" ${count_vfs_files}) ]]; do
count_vfs_files+=1
done
echo ${count_vfs_files}
}
# Associate all TOMTOM.xxx files to a loopback device
__rlink_init_loopback_devs() {
typeset -gi COUNT_VFS_FILES
LOOPBACK_DEV_LIST=""
COUNT_VFS_FILES=$(__rlink_count_tomtom_vfs_files ${__TOMTOM_PATH})
echo "${COUNT_VFS_FILES} tomtom files found."
echo "Creating loopback devices."
for ((i = 0; i < COUNT_VFS_FILES; i++)); do
VFS_FILE=${__TOMTOM_PATH}TOMTOM.$(printf "%03d" "$i")
LOOPBACK_DEV=$(__rlink_loopback_dev)
LOOPBACK_DEV_LIST="${LOOPBACK_DEV_LIST} ${LOOPBACK_DEV}"
# associate file to loopback device
if ! sudo losetup "${LOOPBACK_DEV}" "${VFS_FILE}"; then
__rlink_echoerr "Error while creating loopback device ${LOOPBACK_DEV} from ${VFS_FILE}. Cancel."
__rlink_remove_loopback_devs
exit 1
fi
echo "${VFS_FILE} is now associated to ${LOOPBACK_DEV}."
done
}
# Delete all loopback devices
__rlink_remove_loopback_devs() {
echo "Removing all loopback devices."
# shellcheck disable=SC2086
if ! sudo losetup -d $LOOPBACK_DEV_LIST; then
__rlink_echoerr "Error while removing one or multiple loopback devices. Please check with losetp and dmesg."
exit 1
fi
return 0
}
# Build the linear raid device from all loopback devices (TOMTOM.xxx files)
__rlink_build_linear_raid() {
echo "Build linear raid device."
COMMAND="mdadm --build --auto=part --verbose /dev/md/tomtom_vfs --rounding=32 --level=linear -n${COUNT_VFS_FILES} ${LOOPBACK_DEV_LIST}"
# shellcheck disable=SC2086
if ! sudo $COMMAND; then
__rlink_echoerr "Error during raid device creation. Trying to cancel. Check /proc/mdstat, mdadm and dmesg."
__rlink_remove_loopback_devs
exit 1
fi
return 0
}
# Delete the /dev/md/tomtom_vfs raid device
__rlink_delete_linear_raid() {
echo "Stopping linear raid device."
if ! sudo mdadm -S /dev/md/tomtom_vfs; then
__rlink_echoerr "Error during raid device deletion. Please check /proc/mdstat, mdadm and dmesg. Loopback devices won't be removed."
exit 1
fi
return 0
}
# wait for a carriage return
__press_enter_to_continue() {
# shellcheck disable=SC2162
read -p "Please press enter to continue:"
echo "Thank you."
return 0
}
# Mount the TOMTOM.xxx linear raid ex3 filesystem in /mnt/tomtom_vfs
__rlink_mount_vfs() {
# Ensure it's not already mounted
mount | grep "on /mnt/tomtom_vfs " >/dev/null 2>&1
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
echo "Mountpoint already in use. Cancel."
else
echo "Creating mount point and mount tomtom filesystem."
sudo mkdir -p /mnt/tomtom_vfs 2>/dev/null
sudo mount /dev/md/tomtom_vfs /mnt/tomtom_vfs
if [[ $? -eq 0 ]]; then
vfs_user=$(stat -c "%u" /mnt/tomtom_vfs/common)
vfs_group=$(stat -c "%g" /mnt/tomtom_vfs/common)
current_group=$(id -gn)
# bind to
mkdir -p "$HOME/tomtom_vfs"
sudo bindfs -u "$USER" -g "${current_group}" --create-for-user="${vfs_user}" --create-for-group="${vfs_group}" /mnt/tomtom_vfs/ "$HOME/tomtom_vfs"
echo "Filesystem is now available in /mnt/tomtom_vfs as root and $HOME/tomtom_vfs as $USER."
sleep 1
else
__rlink_echoerr "Something is wrong. Corrupted files ? Trying to cancel."
__rlink_umount_vfs
__rlink_delete_linear_raid
__rlink_remove_loopback_devs
exit 1
fi
fi
return 0
}
# unmount /mnt/tomtom_vfs (TOMTOM.xxx linear raid ext3 filesystem)
__rlink_umount_vfs() {
# Ensure it's mounted
mount | grep "on /mnt/tomtom_vfs " >/dev/null 2>&1
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
echo "Nothing to do, /mnt/tomtom_vfs Not mounted."
return 0
fi
# synchronise files
sync
# check if there's something to kill
fuserkill=0
lsof "$HOME/tomtom_vfs" >/dev/null 2>&1
[[ $? -ne 1 ]] && fuserkill=1
lsof /mnt/tomtom_vfs >/dev/null 2>&1
[[ $? -ne 1 ]] && fuserkill=1
if [[ ${fuserkill} -eq 1 ]]; then
# Killing local users
echo "Killing remaining processus using the mountpoints."
sudo fuser -k "$HOME/tomtom_vfs"
# First kill everything that is related to the mountpoint
sudo fuser -k /mnt/tomtom_vfs
echo "Waiting 5 seconds."
sleep 5
fi
# Then, umount
echo "Unmounting tomtom filesystem and remove mount point."
sudo fusermount -u "$HOME/tomtom_vfs"
if ! sudo umount /mnt/tomtom_vfs; then
__rlink_echoerr "Cannot unmount /mnt/tomtom_vfs. Please check dmesg, fuser and dmesg. raid and loopback devices left untouched."
exit 1
fi
sudo rmdir /mnt/tomtom_vfs
sudo rmdir "$HOME/tomtom_vfs"
return 0
}
# If on X, open default file manager
__rlink_open_default_file_manager() {
if xhost >&/dev/null; then
echo "Launching default file manager."
xdg-open "$HOME/tomtom_vfs"
fi
return 0
}
# Check if prerequires are installed
__rlink_check_prerequires() {
ERR=0
PREREQUIRES="mdadm losetup bindfs"
for prerequire in $PREREQUIRES; do
if ! which "$prerequire" >/dev/null 2>&1; then
__rlink_echoerr "$prerequire command is missing."
echo "Please install it (the method depends of your Linux distribution)."
echo "For Ubuntu: apt-get install $prerequire"
ERR=1
fi
done
return $ERR
}
__rlink_search_cards() {
typeset -i COUNT_CARDS
COUNT_CARDS=0
# shellcheck disable=SC2044
for card in $(find "$HOME/tomtom_vfs" -name "*.pna"); do
echo "Found $card card."
COUNT_CARDS+=1
done
echo "Found ${COUNT_CARDS} card(s)."
return 0
}
rlink() {
if ! __rlink_check_prerequires; then
exit 1
fi
echo "Starting."
__rlink_init_loopback_devs
__rlink_build_linear_raid
__rlink_mount_vfs
__rlink_search_cards
__rlink_open_default_file_manager
__press_enter_to_continue
__rlink_umount_vfs
__rlink_delete_linear_raid
__rlink_remove_loopback_devs
echo "End."
return 0
}
# Tree
if [[ -z "$1" ]]; then
__rlink_help
echo
read -p "Do you want to assemble your TOMTOM files? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rlink "$@"
fi
else
while getopts ":h" option; do
case $option in
h) # display Help
__rlink_help
;;
\?) # incorrect option
__rlink_echoerr "Error: Invalid option."
__rlink_help
;;
esac
done
fi