source: MondoRescue/branches/3.3/MondoRescue/bin/mr-label@ 3650

Last change on this file since 3650 was 3650, checked in by Bruno Cornec, 7 years ago
  • Fix versions given back for mr-* tools
  • Log pb and mr versions in mindi log
  • mr-getparam now prints a version
  • Property svn:executable set to *
File size: 4.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# $Id$
4# Copyright B. Cornec 2005-2014
5# Provided under the GPL v2
6#
7# Get the complete network configuration of the system
8#
9use strict 'vars';
10use ProjectBuilder::Base;
11use ProjectBuilder::Version;
12use MondoRescue::Version;
13use Data::Dumper;
14use Getopt::Long qw(:config auto_abbrev no_ignore_case);
15use English;
16
17
18=pod
19
20=head1 NAME
21
22mr-label labelling tool
23
24=head1 DESCRIPTION
25
26mr-label labels VFAT devices or files representing VFAT devices
27
28=head1 SYNOPSIS
29
30mr-label [-v][-h][-L LABEL|-U UUID] device|file
31
32=head1 ARGUMENTS
33
34=over 4
35
36=item B<device|file>
37
38This is the VFAT device or file representing a VFAT device to modify with regards to its label
39
40=back
41
42=head1 OPTIONS
43
44=over 4
45
46=item B<-v|--verbose>
47
48Increase verbosity
49
50=item B<-h|--help>
51
52Print a brief help message and exits.
53
54=item B<--man>
55
56Prints the manual page and exits.
57
58=item B<-L LABEL>
59
60This is the new LABEL to put on the VFAT filesystem
61
62=item B<-U UUID>
63
64This is the new UUID to put on the VFAT filesystem
65
66=back
67
68=head1 WEB SITES
69
70The main Web site of the project is available at L<http://www.mondorescue.org>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.
71
72=head1 USER MAILING LIST
73
74For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
75
76=head1 AUTHORS
77
78The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
79
80=head1 COPYRIGHT
81
82MondoRescue is distributed under the GPL v2.0 license or later,
83described in the file C<COPYING> included with the distribution.
84
85=cut
86
87# Global variables
88my ($mrver,$mrrev) = mr_version_init();
89my $appname = "mr-label";
90my %opts; # CLI Options
91
92# Initialize the syntax string
93
94pb_syntax_init("$appname Version $mrver-$mrrev\n");
95
96GetOptions("help|?|h" => \$opts{'h'},
97 "man" => \$opts{'man'},
98 "verbose|v+" => \$opts{'v'},
99 "label|L=s" => \$opts{'L'},
100 "uuid|U=s" => \$opts{'U'},
101) || pb_syntax(-1,0);
102
103if (defined $opts{'h'}) {
104 pb_syntax(0,1);
105}
106if (defined $opts{'man'}) {
107 pb_syntax(0,2);
108}
109if (defined $opts{'v'}) {
110 $pbdebug++;
111}
112pb_log_init($pbdebug, $pbLOG);
113pb_temp_init($pbdebug);
114
115my $label=$opts{'L'};
116my $uuid=$opts{'U'};
117die "Unable no label/uuid defined" if ((not defined $label) && (not defined $uuid));
118
119my $inputf = $ARGV[0];
120die "$0: no input file defined" if (not defined $inputf);
121
122my($inputfh, $buffer, $offset, $length, $hexlabel);
123
124# Evaluate the size of the FAT with blkid -p
125open(FS,"blkid -p $inputf|") || die "$0: cannot blkid -p $inputf: $!";
126my $verfs = <FS>;
127close(FS);
128$verfs =~ s/.*VERSION="FAT([0-9][0-9]).*"/FAT$1/;
129chomp($verfs);
130pb_log(1,"Working on a FS ot type ***$verfs***\n");
131
132open($inputfh, "+< $inputf") || die "$0: cannot open $inputf for reading/writing: $!";
133binmode($inputfh) || die "binmode failed: $!";
134
135# Source https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html
136if (defined $label) {
137 # x first bytes are untouched
138 $offset = 43 if (($verfs =~ /FAT16/) || ($verfs =~ /FAT12/));
139 $offset = 71 if ($verfs =~ /FAT32/);
140 $length = 11;
141} elsif (defined $uuid) {
142 # x first bytes are untouched
143 $offset = 39 if (($verfs =~ /FAT16/) || ($verfs =~ /FAT12/));
144 $offset = 67 if ($verfs =~ /FAT32/);
145 $length = 4;
146} else {
147 die "Nothing to do no label/uuid defined";
148}
149
150if (! seek($inputfh, $offset,0)) {
151 die "Unable to seek $inputf: $!";
152}
153# then read the following bytes
154my $size = read($inputfh, $buffer, $length);
155if ($size != $length) {
156 die "Unable to read existing label/uuid: $!";
157}
158
159if (defined $label) {
160 pb_log(1,"Previous label was ***$buffer***\n");
161} elsif (defined $uuid) {
162 my ($hex) = unpack( 'H*', $buffer );
163 pb_log(1,"Previous hex was ***$hex***\n");
164 my @loc = ($hex =~ m/../g);
165 #print Dumper(@loc);
166 $hex = join('',reverse(@loc));
167 pb_log(1,"Previous uuid was ***$hex***\n");
168} else {
169 die "Nothing to do no label/uuid defined";
170}
171
172if (defined $label) {
173 $hexlabel=sprintf("%-11s", uc($label));
174 pb_log(1,"New label is ***$hexlabel***\n");
175} elsif (defined $uuid) {
176 # Remove separator
177 $uuid =~ s/-//;
178 pb_log(1,"New uuid is ***$uuid***\n");
179 my @loc = ($uuid =~ m/../g);
180 my $hex = join('',reverse(@loc));
181 ($hexlabel) = pack( 'H*', $hex );
182} else {
183 die "Nothing to do no label/uuid defined";
184}
185# Go back
186seek($inputfh, $offset,0);
187print $inputfh $hexlabel;
188close($inputfh) || die "couldn't close $inputf: $!";
Note: See TracBrowser for help on using the repository browser.