-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrlink.sh
executable file
·277 lines (237 loc) · 7.15 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
#!/usr/bin/env bash
typeset -i MAX_FILES
MAX_FILES=8
# Output to stderr
function echoerr() {
echo "$@" 1>&2;
}
# Find first available /dev/loopX
function loopback_dev() {
typeset -i num
num=0
while [[ ${num} -le 255 ]]
do
losetup /dev/loop${num} >/dev/null 2>&1
[[ $? -ne 0 ]] && break
num=num+1
done
echo "/dev/loop${num}"
}
# Count the number of TOMTOM.xxx files
function count_tomtom_vfs_files() {
typeset -i count_vfs_files
count_vfs_files=0
TOMTOM_PATH=$1
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
function init_loopback_devs() {
typeset -gi COUNT_VFS_FILES
TOMTOM_PATH=$1
LOOPBACK_DEV_LIST=""
COUNT_VFS_FILES=$(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=$(loopback_dev)
LOOPBACK_DEV_LIST="${LOOPBACK_DEV_LIST} ${LOOPBACK_DEV}"
# associate file to loopback device
sudo losetup ${LOOPBACK_DEV} ${VFS_FILE}
if [[ $? -ne 0 ]]
then
echoerr "Error while creating loopback device ${LOOPBACK_DEV} from ${VFS_FILE}. Cancel."
remove_loopback_devs
exit 1
fi
echo "${VFS_FILE} is now associated to ${LOOPBACK_DEV}."
done
}
# Delete all loopback devices
function remove_loopback_devs() {
echo "Removing all loopback devices."
sudo losetup -d ${LOOPBACK_DEV_LIST}
if [[ $? -ne 0 ]]
then
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)
function 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}"
sudo $COMMAND
if [[ $1 -ne 0 ]]
then
echoerr "Error during raid device creation. Trying to cancel. Check /proc/mdstat, mdadm and dmesg."
remove_loopback_devs
exit 1
fi
return 0
}
# Delete the /dev/md/tomtom_vfs raid device
function delete_linear_raid() {
echo "Stopping linear raid device."
sudo mdadm -S /dev/md/tomtom_vfs
if [[ $1 -ne 0 ]]
then
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
function wait_for_return() {
echo "Please press return to finish :"
read dummy
echo "Thank you."
return 0
}
# Mount the TOMTOM.xxx linear raid ex3 filesystem in /mnt/tomtom_vfs
function mount_vfs() {
# Ensure it's not already mounted
mount | grep "on /mnt/tomtom_vfs " >/dev/null 2>&1
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
echoerr "Something is wrong. Corrupted files ? Trying to cancel."
umount_vfs
delete_linear_raid
remove_loopback_devs
exit 1
fi
fi
return 0
}
# unmount /mnt/tomtom_vfs (TOMTOM.xxx linear raid ext3 filesystem)
function umount_vfs() {
# Ensure it's mounted
mount | grep "on /mnt/tomtom_vfs " >/dev/null 2>&1
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
sudo umount /mnt/tomtom_vfs
if [[ $? -ne 0 ]]
then
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
function open_default_file_manager() {
if xhost >& /dev/null
then
echo "Display exists. Starting default file manager."
xdg-open $HOME/tomtom_vfs
fi
return 0
}
# Check if prerequires are installed
function check_prerequires() {
ERR=0
PREREQUIRES="mdadm losetup bindfs"
for prerequire in $PREREQUIRES
do
which $prerequire >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echoerr "$prerequire command is missing. Please install it (the method depends of your Linux distribution)."
ERR=1
fi
done
return $ERR
}
function search_cards() {
typeset -i COUNT_CARDS
COUNT_CARDS=0
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
}
function main() {
if [[ $# -ne 1 ]]
then
echo "$0 <path_of_TOMTOM.xxx_files>"
return 0
fi
if [[ $1 = "--help" ]]
then
cat<<EOT
$0 <path_of_TOMTOM.xxx_files>
A tomtom r-link sdcard is made of multiple TOMTOM.xxx (000, 001, ...) files.
These files contain a splitted linux 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.
EOT
return 0
fi
check_prerequires
[[ $? -ne 0 ]] && exit 1
echo "Starting."
init_loopback_devs $1
build_linear_raid
mount_vfs
search_cards
open_default_file_manager
wait_for_return
umount_vfs
delete_linear_raid
remove_loopback_devs
echo "End."
return 0
}
main $@