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-2014 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];
161 case 'I' : /* not proposed as a short option letter */
163 opts
.serial
= strtoull(optarg
, &endserial
, 16);
165 ntfs_log_error("Bad hexadecimal serial number.\n");
167 opts
.new_serial
|= 2;
169 case 'i' : /* not proposed as a short option letter */
171 opts
.serial
= strtoull(optarg
, &endserial
, 16)
174 ntfs_log_error("Bad hexadecimal serial number.\n");
176 opts
.new_serial
|= 1;
183 ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET
);
187 ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE
);
193 if (strncmp (argv
[optind
-1], "--log-", 6) == 0) {
194 if (!ntfs_log_parse_option (argv
[optind
-1]))
200 ntfs_log_error("Unknown option '%s'.\n", argv
[optind
-1]);
206 /* Make sure we're in sync with the log levels */
207 levels
= ntfs_log_get_levels();
208 if (levels
& NTFS_LOG_LEVEL_VERBOSE
)
210 if (!(levels
& NTFS_LOG_LEVEL_QUIET
))
216 if (opts
.device
== NULL
) {
218 ntfs_log_error("You must specify a device.\n");
222 if (opts
.quiet
&& opts
.verbose
) {
223 ntfs_log_error("You may not use --quiet and --verbose at "
234 /* tri-state 0 : done, 1 : error, -1 : proceed */
235 return (err
? 1 : (help
|| ver
? 0 : -1));
238 static int change_serial(ntfs_volume
*vol
, u64 sector
, le64 serial_number
,
239 NTFS_BOOT_SECTOR
*bs
, NTFS_BOOT_SECTOR
*oldbs
)
246 if ((ntfs_pread(vol
->dev
, sector
<< vol
->sector_size_bits
,
247 vol
->sector_size
, bs
) == vol
->sector_size
)) {
250 /* save the real bootsector */
251 memcpy(oldbs
, bs
, vol
->sector_size
);
253 /* backup bootsector must be similar */
254 same
= !memcmp(oldbs
, bs
, vol
->sector_size
);
256 if (opts
.new_serial
& 2)
257 bs
->volume_serial_number
= serial_number
;
259 mask
= const_cpu_to_le64(~0x0ffffffffULL
);
260 bs
->volume_serial_number
261 = (serial_number
& mask
)
262 | (bs
->volume_serial_number
& ~mask
);
265 || (ntfs_pwrite(vol
->dev
,
266 sector
<< vol
->sector_size_bits
,
267 vol
->sector_size
, bs
) == vol
->sector_size
)) {
271 ntfs_log_info("* Warning : the backup boot sector"
272 " does not match (leaving unchanged)\n");
279 static int set_new_serial(ntfs_volume
*vol
)
281 NTFS_BOOT_SECTOR
*bs
; /* full boot sectors */
282 NTFS_BOOT_SECTOR
*oldbs
; /* full original boot sector */
284 u64 number_of_sectors
;
289 bs
= (NTFS_BOOT_SECTOR
*)ntfs_malloc(vol
->sector_size
);
290 oldbs
= (NTFS_BOOT_SECTOR
*)ntfs_malloc(vol
->sector_size
);
293 serial_number
= cpu_to_le64(opts
.serial
);
295 /* different values for parallel processes */
296 srandom(time((time_t*)NULL
) ^ (getpid() << 16));
297 sn
= ((u64
)random() << 32)
298 | ((u64
)random() & 0xffffffff);
299 serial_number
= cpu_to_le64(sn
);
301 if (!change_serial(vol
, 0, serial_number
, bs
, oldbs
)) {
302 number_of_sectors
= le64_to_cpu(bs
->number_of_sectors
);
303 if (!change_serial(vol
, number_of_sectors
,
304 serial_number
, bs
, oldbs
)) {
305 ntfs_log_info("New serial number : %016llx\n",
306 (long long)le64_to_cpu(
307 bs
->volume_serial_number
));
315 ntfs_log_info("Error setting a new serial number\n");
319 static int print_serial(ntfs_volume
*vol
)
321 NTFS_BOOT_SECTOR
*bs
; /* full boot sectors */
325 bs
= (NTFS_BOOT_SECTOR
*)ntfs_malloc(vol
->sector_size
);
327 && (ntfs_pread(vol
->dev
, 0,
328 vol
->sector_size
, bs
) == vol
->sector_size
)) {
329 ntfs_log_info("Serial number : %016llx\n",
330 (long long)le64_to_cpu(bs
->volume_serial_number
));
335 ntfs_log_info("Error getting the serial number\n");
340 * print_label - display the current label of a mounted ntfs partition.
341 * @dev: device to read the label from
342 * @mnt_flags: mount flags of the device or 0 if not mounted
343 * @mnt_point: mount point of the device or NULL
345 * Print the label of the device @dev.
347 static int print_label(ntfs_volume
*vol
, unsigned long mnt_flags
)
351 if ((mnt_flags
& (NTFS_MF_MOUNTED
| NTFS_MF_READONLY
)) ==
353 ntfs_log_error("%s is mounted read-write, results may be "
354 "unreliable.\n", opts
.device
);
359 ntfs_log_info("Volume label : %s\n", vol
->vol_name
);
361 ntfs_log_info("%s\n", vol
->vol_name
);
366 * change_label - change the current label on a device
367 * @dev: device to change the label on
368 * @mnt_flags: mount flags of the device or 0 if not mounted
369 * @mnt_point: mount point of the device or NULL
370 * @label: the new label
372 * Change the label on the device @dev to @label.
374 static int change_label(ntfs_volume
*vol
, char *label
)
376 ntfschar
*new_label
= NULL
;
380 label_len
= ntfs_mbstoucs(label
, &new_label
);
381 if (label_len
== -1) {
382 ntfs_log_perror("Unable to convert label string to Unicode");
385 else if (label_len
*sizeof(ntfschar
) > 0x100) {
386 ntfs_log_warning("New label is too long. Maximum %u characters "
387 "allowed. Truncating %u excess characters.\n",
388 (unsigned)(0x100 / sizeof(ntfschar
)),
389 (unsigned)(label_len
-
390 (0x100 / sizeof(ntfschar
))));
391 label_len
= 0x100 / sizeof(ntfschar
);
392 label
[label_len
] = const_cpu_to_le16(0);
396 result
= ntfs_volume_rename(vol
, new_label
, label_len
) ? 1 : 0;
407 * Return: 0 Success, the program worked
408 * 1 Error, something went wrong
410 int main(int argc
, char **argv
)
412 unsigned long mnt_flags
= 0;
416 ntfs_log_set_handler(ntfs_log_handler_outerr
);
418 result
= parse_options(argc
, argv
);
425 if ((opts
.label
|| opts
.new_serial
)
428 && !ntfs_check_if_mounted(opts
.device
, &mnt_flags
)
429 && (mnt_flags
& NTFS_MF_MOUNTED
)) {
430 ntfs_log_error("Cannot make changes to a mounted device\n");
435 if (!opts
.label
&& !opts
.new_serial
)
438 vol
= utils_mount_volume(opts
.device
,
439 (opts
.noaction
? NTFS_MNT_RDONLY
: 0) |
440 (opts
.force
? NTFS_MNT_RECOVER
: 0));
444 if (opts
.new_serial
) {
445 result
= set_new_serial(vol
);
450 result
= print_serial(vol
);
453 result
= change_label(vol
, opts
.label
);
455 result
= print_label(vol
, mnt_flags
);
458 ntfs_umount(vol
, FALSE
);
460 /* "result" may be a negative reply of a library function */
461 return (result
? 1 : 0);