2
* ntfslabel - Part of the Linux-NTFS project.
4
* Copyright (c) 2002 Matthew J. Fanto
5
* Copyright (c) 2002-2005 Anton Altaparmakov
6
* Copyright (c) 2002-2003 Richard Russon
7
* Copyright (c) 2012-2014 Jean-Pierre Andre
9
* This utility will display/change the label on an NTFS partition.
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program (in the main directory of the Linux-NTFS
23
* distribution in the file COPYING); if not, write to the Free Software
24
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54
/* #include "version.h" */
58
static const char *EXEC_NAME = "ntfslabel";
60
static struct options {
61
char *device; /* Device/File to work with */
62
char *label; /* Set the label to this */
63
int quiet; /* Less output */
64
int verbose; /* Extra output */
65
int force; /* Override common sense */
66
int new_serial; /* Change the serial number */
67
unsigned long long serial; /* Forced serial number value */
68
int noaction; /* Do not write to disk */
72
* version - Print version information about the program
74
* Print a copyright statement and a brief description of the program.
78
static void version(void)
80
ntfs_log_info("\n%s v%s (libntfs-3g) - Display, or set, the label for an "
81
"NTFS Volume.\n\n", EXEC_NAME, VERSION);
82
ntfs_log_info("Copyright (c)\n");
83
ntfs_log_info(" 2002 Matthew J. Fanto\n");
84
ntfs_log_info(" 2002-2005 Anton Altaparmakov\n");
85
ntfs_log_info(" 2002-2003 Richard Russon\n");
86
ntfs_log_info(" 2012 Jean-Pierre Andre\n");
87
ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
91
* usage - Print a list of the parameters to the program
93
* Print a list of the parameters and options for the program.
97
static void usage(void)
99
ntfs_log_info("\nUsage: %s [options] device [label]\n"
100
" -n, --no-action Do not write to disk\n"
101
" -f, --force Use less caution\n"
102
" --new-serial Set a new serial number\n"
103
" --new-half-serial Set a partial new serial number\n"
104
" -q, --quiet Less output\n"
105
" -v, --verbose More output\n"
106
" -V, --version Display version information\n"
107
" -h, --help Display this help\n\n",
109
ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
113
* parse_options - Read and validate the programs command line
115
* Read the command line, verify the syntax and parse the options.
116
* This function is very long, but quite simple.
119
* 0 Error, one or more problems
121
static int parse_options(int argc, char *argv[])
123
static const char *sopt = "-fh?IinqvV";
124
static const struct option lopt[] = {
125
{ "force", no_argument, NULL, 'f' },
126
{ "help", no_argument, NULL, 'h' },
127
{ "new-serial", optional_argument, NULL, 'I' },
128
{ "new-half-serial", optional_argument, NULL, 'i' },
129
{ "no-action", no_argument, NULL, 'n' },
130
{ "quiet", no_argument, NULL, 'q' },
131
{ "verbose", no_argument, NULL, 'v' },
132
{ "version", no_argument, NULL, 'V' },
133
{ NULL, 0, NULL, 0 },
143
opterr = 0; /* We'll handle the errors, thank you. */
145
while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
147
case 1: /* A non-option argument */
148
if (!err && !opts.device)
149
opts.device = argv[optind-1];
150
else if (!err && !opts.label)
151
opts.label = argv[optind-1];
160
if (strncmp (argv[optind-1], "--log-", 6) == 0) {
161
if (!ntfs_log_parse_option (argv[optind-1]))
167
case 'I' : /* not proposed as a short option letter */
169
opts.serial = strtoull(optarg, &endserial, 16);
171
ntfs_log_error("Bad hexadecimal serial number.\n");
173
opts.new_serial |= 2;
175
case 'i' : /* not proposed as a short option letter */
177
opts.serial = strtoull(optarg, &endserial, 16)
180
ntfs_log_error("Bad hexadecimal serial number.\n");
182
opts.new_serial |= 1;
189
ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
193
ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
199
ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
205
/* Make sure we're in sync with the log levels */
206
levels = ntfs_log_get_levels();
207
if (levels & NTFS_LOG_LEVEL_VERBOSE)
209
if (!(levels & NTFS_LOG_LEVEL_QUIET))
215
if (opts.device == NULL) {
217
ntfs_log_error("You must specify a device.\n");
221
if (opts.quiet && opts.verbose) {
222
ntfs_log_error("You may not use --quiet and --verbose at "
233
return (!err && !help && !ver);
236
static int change_serial(ntfs_volume *vol, u64 sector, le64 serial_number,
237
NTFS_BOOT_SECTOR *bs, NTFS_BOOT_SECTOR *oldbs)
244
if ((ntfs_pread(vol->dev, sector << vol->sector_size_bits,
245
vol->sector_size, bs) == vol->sector_size)) {
248
/* save the real bootsector */
249
memcpy(oldbs, bs, vol->sector_size);
251
/* backup bootsector must be similar */
252
same = !memcmp(oldbs, bs, vol->sector_size);
254
if (opts.new_serial & 2)
255
bs->volume_serial_number = serial_number;
257
mask = const_cpu_to_le64(~0x0ffffffffULL);
258
bs->volume_serial_number
259
= (serial_number & mask)
260
| (bs->volume_serial_number & ~mask);
263
|| (ntfs_pwrite(vol->dev,
264
sector << vol->sector_size_bits,
265
vol->sector_size, bs) == vol->sector_size)) {
269
ntfs_log_info("* Warning : the backup boot sector"
270
" does not match (leaving unchanged)\n");
277
static int set_new_serial(ntfs_volume *vol)
279
NTFS_BOOT_SECTOR *bs; /* full boot sectors */
280
NTFS_BOOT_SECTOR *oldbs; /* full original boot sector */
282
u64 number_of_sectors;
287
bs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size);
288
oldbs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size);
291
serial_number = cpu_to_le64(opts.serial);
293
/* different values for parallel processes */
294
srandom(time((time_t*)NULL) ^ (getpid() << 16));
295
sn = ((u64)random() << 32)
296
| ((u64)random() & 0xffffffff);
297
serial_number = cpu_to_le64(sn);
299
if (!change_serial(vol, 0, serial_number, bs, oldbs)) {
300
number_of_sectors = le64_to_cpu(bs->number_of_sectors);
301
if (!change_serial(vol, number_of_sectors,
302
serial_number, bs, oldbs)) {
303
ntfs_log_info("New serial number : %016llx\n",
304
(long long)le64_to_cpu(
305
bs->volume_serial_number));
313
ntfs_log_info("Error setting a new serial number\n");
317
static int print_serial(ntfs_volume *vol)
319
NTFS_BOOT_SECTOR *bs; /* full boot sectors */
323
bs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size);
325
&& (ntfs_pread(vol->dev, 0,
326
vol->sector_size, bs) == vol->sector_size)) {
327
ntfs_log_info("Serial number : %016llx\n",
328
(long long)le64_to_cpu(bs->volume_serial_number));
333
ntfs_log_info("Error getting the serial number\n");
338
* print_label - display the current label of a mounted ntfs partition.
339
* @dev: device to read the label from
340
* @mnt_flags: mount flags of the device or 0 if not mounted
341
* @mnt_point: mount point of the device or NULL
343
* Print the label of the device @dev.
345
static int print_label(ntfs_volume *vol, unsigned long mnt_flags)
349
if ((mnt_flags & (NTFS_MF_MOUNTED | NTFS_MF_READONLY)) ==
351
ntfs_log_error("%s is mounted read-write, results may be "
352
"unreliable.\n", opts.device);
357
ntfs_log_info("Volume label : %s\n", vol->vol_name);
359
ntfs_log_info("%s\n", vol->vol_name);
364
* change_label - change the current label on a device
365
* @dev: device to change the label on
366
* @mnt_flags: mount flags of the device or 0 if not mounted
367
* @mnt_point: mount point of the device or NULL
368
* @label: the new label
370
* Change the label on the device @dev to @label.
372
static int change_label(ntfs_volume *vol, char *label)
374
ntfschar *new_label = NULL;
378
label_len = ntfs_mbstoucs(label, &new_label);
379
if (label_len == -1) {
380
ntfs_log_perror("Unable to convert label string to Unicode");
383
else if (label_len*sizeof(ntfschar) > 0x100) {
384
ntfs_log_warning("New label is too long. Maximum %u characters "
385
"allowed. Truncating %u excess characters.\n",
386
(unsigned)(0x100 / sizeof(ntfschar)),
387
(unsigned)(label_len -
388
(0x100 / sizeof(ntfschar))));
389
label_len = 0x100 / sizeof(ntfschar);
390
label[label_len] = const_cpu_to_le16(0);
394
result = ntfs_volume_rename(vol, new_label, label_len) ? 1 : 0;
405
* Return: 0 Success, the program worked
406
* 1 Error, something went wrong
408
int main(int argc, char **argv)
410
unsigned long mnt_flags = 0;
414
ntfs_log_set_handler(ntfs_log_handler_outerr);
416
if (!parse_options(argc, argv))
421
if ((opts.label || opts.new_serial)
424
&& !ntfs_check_if_mounted(opts.device, &mnt_flags)
425
&& (mnt_flags & NTFS_MF_MOUNTED)) {
426
ntfs_log_error("Cannot make changes to a mounted device\n");
431
if (!opts.label && !opts.new_serial)
434
vol = utils_mount_volume(opts.device,
435
(opts.noaction ? NTFS_MNT_RDONLY : 0) |
436
(opts.force ? NTFS_MNT_RECOVER : 0));
440
if (opts.new_serial) {
441
result = set_new_serial(vol);
446
result = print_serial(vol);
449
result = change_label(vol, opts.label);
451
result = print_label(vol, mnt_flags);
454
ntfs_umount(vol, FALSE);