-
Notifications
You must be signed in to change notification settings - Fork 12
/
ntfsuuid
executable file
·371 lines (315 loc) · 9.93 KB
/
ntfsuuid
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
#
# ntfsuuid - Tools for manipulating UUIDs in NTFS filesystems
#
# Copyright (C) 2011 Rodrigo Silva - <linux@rodrigosilva.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Huge thanks to all the gurus and friends in irc://irc.freenet.org/#bash
# and the contributors of http://mywiki.wooledge.org/
#
# TODO: Integrate with common/common lib (SELF, _Confirm, _TestRoot)
# TODO: Test avaliability of tools (xxd, blkid, dd)
# TODO: Document error messages
# TODO: Allow volume LABEL instead of /dev/sdxy
# TODO: reconsider getopts http://mywiki.wooledge.org/BashFAQ/035
exec 3> /dev/null # to avoid harcoding /dev/null everywhere. For tools' stderr.
SELF="${0##*/}" # builin $(basename $0)
COMMAND=
VERBOSE=
LOWER=
FORCE=
TEST=
DEBUG=
BINARY=
DEVICE=
NTFSUUID=
usage ()
{
local explicit="$1"
cat <<EOF
Usage: $SELF [OPTIONS] --generate
$SELF [OPTIONS] --read DEVICE
$SELF [OPTIONS] --set DEVICE [--uuid UUID] [--binary]
$SELF [OPTIONS] --copy INPUT_DEVICE OUTPUT_DEVICE
EOF
if [[ -z "$explicit" ]] ; then
cat <<EOF
Try '$SELF --help' for more information.
EOF
else
cat <<EOF
Tools for reading, setting and generating UUIDs suitable for NTFS filesystems (8 bytes)
Options:
-h, --help print this message and exit
-v, --verbose verbose output (displays extra information)
-l, --lower use lower case for displaying UUIDs
-f, --force do not prompt before changes, assumes Yes for all questions
-t, --test simulation test, do not write any data on disk (affects --set and --copy only)
-d, --debug displays debug information on parsed options and arguments
-b, --binary interprets UUID as 8-byte binary data (rather than hexadecimal string)
--binary is only used with --uuid, otherwise it is ignored
-u, --uuid UUID uses the specified UUID. Must be a 16 chars ASCII string, case-insensitive, [0-9,A-F]
if --binary is used, UUID must be 8 bytes long
--uuid is only used with --set, otherwise it is ignored
Commands:
--generate output a random UUID suitable for an NTFS device (8 bytes)
--read DEVICE output the current UUID of the specified NTFS device
--set DEVICE sets the UUID for the specified NTFS device (uses a random one if --uuid is ommited)
--copy DEV_IN DEV_OUT copy (clone) the UUID from an NTFS device to another
Examples:
$SELF --generate
$SELF --read /dev/sdx1
$SELF --set /dev/sdx1
$SELF --set /dev/sdx1 --uuid 2AE2C85D31835048
$SELF --set /dev/sdx1 --uuid rodrigo1 --binary
$SELF --copy /dev/sdx1 /dev/sdx2
Author: Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 <http://www.gnu.org/licenses/>
EOF
fi
}
####################################### Auxiliary Functions
_NTFSUuid()
{
local device="$1"
local lower="$2"
local uuid=$(
sudo xxd -plain -s 72 -len 8 "$device" |
grep -o .. |
tac |
tr -d '\n'
)
if [[ "$lower" ]]; then
echo "${uuid,,}"
else
echo "${uuid^^}"
fi
}
_RandomUuid()
{
local lower=""
if [[ -z "$LOWER" ]] ; then lower="-u" ; fi
NTFSUUID=$(xxd "$lower" -plain -l 8 /dev/urandom)
}
_TestNTFS()
{
local device="$1"
if [[ -n "$device" ]] ; then
if [[ -b "$device" ]] ; then
if sudo blkid -p "$device" | grep -q "TYPE=\"ntfs\"" ; then
return 0
else
echo "error: $device: permission denied or not an NTFS device"
return 13
fi
else
echo "error: $device: not found or not a device"
return 12
fi
else
# Redundant, since DEVICE is mandatory, enforced by getopts
echo "error: this operation requires a device"
return 11
fi
}
_Confirm()
{
local message="$1"
local confirm
if [[ -z "$FORCE" ]] ; then
# can be improved with smarter setting of default value
read -p "$message (y/n, default YES): " confirm
case "x$confirm" in
x[Yy]*|x) return 0;;
*)
if [[ -n "$VERBOSE" ]] ; then
echo "$SELF: $COMMAND: cancelled by user"
fi
return 2
;;
esac
fi
}
_PrintFlags()
{
# Echoing $DEBUG value is redundant, since this will only be printed if = 1
# The missing space before $GETOPT is intentional, as getopt output
# contains a leading space
cat <<EOF
Parsed Options =$GETOPT
Debug = $DEBUG
Verbose = $VERBOSE
Lower case = $LOWER
Non-Interactive = $FORCE
Simulation Test = $TEST
Binary = $BINARY
Command = $COMMAND
Device = $DEVICE
Uuid = $NTFSUUID
EOF
}
####################################### Main Functions
Uuid_Generate()
{
_RandomUuid
# All input data is set, print debug info
if [[ -n "$DEBUG" ]] ; then _PrintFlags ; fi
if [[ -n "$VERBOSE" ]] ; then
echo "Random UUID: $NTFSUUID"
else
echo "$NTFSUUID"
fi
}
Uuid_Read()
{
_TestNTFS "$DEVICE" || return $?
NTFSUUID=$(_NTFSUuid "$DEVICE" "$LOWER")
# All input data is set, print debug info
if [[ -n "$DEBUG" ]] ; then _PrintFlags ; fi
if [[ -n "$VERBOSE" ]] ; then
echo "UUID of $DEVICE is $NTFSUUID"
else
echo "$NTFSUUID"
fi
}
Uuid_Set()
{
local binary_uuid
local result=0
# Handle UUID: Calculate a random one if not given
if [[ -z "$NTFSUUID" ]] ; then
_RandomUuid
else
if [[ -z "$BINARY" ]] ; then
# Hexadecimal String format
if ! [[ "$NTFSUUID" =~ ^[0-9A-Fa-f]{16}$ ]] ; then
echo "error: $NTFSUUID: invalid UUID. Must be 16 hexadecimal characters [0-9,A-F]. Ex: 2AE2C85D31835048"
return 21
fi
else
# Binary format
if [[ ${#NTFSUUID} -ne 8 ]] ; then
echo "error: $NTFSUUID: invalid UUID. Binary format must be 8 bytes long"
return 22
fi
# Save original UUID
binary_uuid="$NTFSUUID"
# Convert to hexadecimal string
NTFSUUID=$(echo "$NTFSUUID" | xxd -len 8 -plain)
fi
fi
# Adjust UUID case, for display messages purposes only
if [[ "$LOWER" ]]; then
NTFSUUID="${NTFSUUID,,}"
else
NTFSUUID="${NTFSUUID^^}"
fi
# Test device
_TestNTFS "$DEVICE" || return $?
# All input data is set, print debug info
if [[ -n "$DEBUG" ]] ; then _PrintFlags ; fi
if [[ -n "$BINARY" ]] ; then echo "Binary UUID = $binary_uuid" ; fi
# Confirm with user
_Confirm "Set $DEVICE UUID to $NTFSUUID?" || return $?
# Set the UUID
if [[ -z "$TEST" ]] ; then
echo "$NTFSUUID" |
grep -o .. |
tac |
xxd -r -plain |
sudo dd of="$DEVICE" bs=8 count=1 seek=9 \
conv=notrunc status=noxfer 2>&3
result=$?
fi
if [[ $result -eq 0 ]] ; then
# Verbose message
if [[ -n "$VERBOSE" ]] ; then echo "UUID of $DEVICE set to $NTFSUUID" ; fi
return 0
else
echo "error: $COMMAND: $result (permission denied?)"
return $result
fi
}
Uuid_Copy()
{
local input="$DEVICE"
local output="$1"
local result=0
local lower
# Check DEVICE_OUTPUT (getopt only handle 1 mandatory argument per option)
if [[ -z "$output" ]] ; then
echo "error: output device not specified"
usage
exit 31
fi
# Test Input and Output devices
_TestNTFS "$input" && _TestNTFS "$output" || return $?
# Read input device's UUID
NTFSUUID=$(_NTFSUuid "$input" "$LOWER")
# All input data is set, print debug info (with additional DEVICE_OUTPUT parameter)
if [[ -n "$DEBUG" ]] ; then _PrintFlags ; echo "Output = $1" ; fi
# Confirm with user
_Confirm "Copy UUID $NTFSUUID from $input to $output ?" || return $?
# Copy the UUID
if [[ -z "$TEST" ]] ; then
sudo dd if="$input" of="$output" bs=8 count=1 seek=9 skip=9 conv=notrunc /
status=noxfer 2>&3
result=$?
fi
if [[ $result -eq 0 ]] ; then
# Verbose message
if [[ -n "$VERBOSE" ]] ; then echo "UUID $NTFSUUID copied from $input to $output" ; fi
return 0
else
echo "error: $COMMAND ended with status $result (permission denied?)"
return $result
fi
}
####################################### Command Line parsing
# Print usage if no parameter passed
if [[ $# -eq 0 ]] ; then
usage
exit 1
fi
GETOPT=$(getopt --alternative --options "vlftbd" --longoptions "verbose,lower,force,test,binary,debug,uuid:,help,generate,read:,set:,copy:" --name "$SELF" -- "$@")
if [[ $? -gt 0 ]] ; then usage ; exit 1 ; fi # error in getopt
eval set -- "$GETOPT"
while true ; do
case "$1" in
-v|--verbose ) shift ; VERBOSE="1" ;; # Set verbose
-l|--lower ) shift ; LOWER="1" ;; # Set lower case
-f|--force ) shift ; FORCE="1" ;; # Set non-interactive
-t|--test ) shift ; TEST="1" ;; # Set simulation test
-b|--binary ) shift ; BINARY="1" ;; # Set binary mode
-d|--debug ) shift ; DEBUG="1" ;; # Set debug mode
--uuid ) shift ; NTFSUUID="$1" ; shift ;; # Set UUID
--help ) shift ; COMMAND="help" ;; # Help requested
--generate ) shift ; COMMAND="generate" ;; # Prints random UUID
--read ) shift ; COMMAND="read" ; DEVICE="$1" ; shift ;; # Reads UUID from dev
--set ) shift ; COMMAND="set" ; DEVICE="$1" ; shift ;; # Sets UUID of dev
--copy ) shift ; COMMAND="copy" ; DEVICE="$1" ; shift ;; # Copy UUID from dev
-- ) shift ; break ;;
* ) break ;;
esac
done
case "$COMMAND" in
help ) usage "HELP" ; exit 0 ;;
generate ) Uuid_Generate ; exit $? ;;
read ) Uuid_Read ; exit $? ;;
set ) Uuid_Set ; exit $? ;;
copy ) Uuid_Copy "$@" ; exit $? ;;
* ) usage ; exit 1 ;;
esac