[3486] | 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 | #
|
---|
| 9 | use strict 'vars';
|
---|
| 10 | use ProjectBuilder::Base;
|
---|
| 11 | use ProjectBuilder::Version;
|
---|
[3650] | 12 | use MondoRescue::Version;
|
---|
[3486] | 13 | use Data::Dumper;
|
---|
| 14 | use Getopt::Long qw(:config auto_abbrev no_ignore_case);
|
---|
| 15 | use English;
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | =pod
|
---|
| 19 |
|
---|
| 20 | =head1 NAME
|
---|
| 21 |
|
---|
| 22 | mr-label labelling tool
|
---|
| 23 |
|
---|
| 24 | =head1 DESCRIPTION
|
---|
| 25 |
|
---|
| 26 | mr-label labels VFAT devices or files representing VFAT devices
|
---|
| 27 |
|
---|
| 28 | =head1 SYNOPSIS
|
---|
| 29 |
|
---|
[3488] | 30 | mr-label [-v][-h][-L LABEL|-U UUID] device|file
|
---|
[3486] | 31 |
|
---|
| 32 | =head1 ARGUMENTS
|
---|
| 33 |
|
---|
| 34 | =over 4
|
---|
| 35 |
|
---|
| 36 | =item B<device|file>
|
---|
| 37 |
|
---|
| 38 | This 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 |
|
---|
| 48 | Increase verbosity
|
---|
| 49 |
|
---|
| 50 | =item B<-h|--help>
|
---|
| 51 |
|
---|
| 52 | Print a brief help message and exits.
|
---|
| 53 |
|
---|
| 54 | =item B<--man>
|
---|
| 55 |
|
---|
| 56 | Prints the manual page and exits.
|
---|
| 57 |
|
---|
[3488] | 58 | =item B<-L LABEL>
|
---|
[3486] | 59 |
|
---|
[3488] | 60 | This is the new LABEL to put on the VFAT filesystem
|
---|
[3486] | 61 |
|
---|
[3488] | 62 | =item B<-U UUID>
|
---|
| 63 |
|
---|
| 64 | This is the new UUID to put on the VFAT filesystem
|
---|
| 65 |
|
---|
[3486] | 66 | =back
|
---|
| 67 |
|
---|
| 68 | =head1 WEB SITES
|
---|
| 69 |
|
---|
| 70 | The 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 |
|
---|
| 74 | For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>
|
---|
| 75 |
|
---|
| 76 | =head1 AUTHORS
|
---|
| 77 |
|
---|
| 78 | The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
| 79 |
|
---|
| 80 | =head1 COPYRIGHT
|
---|
| 81 |
|
---|
| 82 | MondoRescue is distributed under the GPL v2.0 license or later,
|
---|
| 83 | described in the file C<COPYING> included with the distribution.
|
---|
| 84 |
|
---|
| 85 | =cut
|
---|
| 86 |
|
---|
| 87 | # Global variables
|
---|
[3650] | 88 | my ($mrver,$mrrev) = mr_version_init();
|
---|
[3486] | 89 | my $appname = "mr-label";
|
---|
| 90 | my %opts; # CLI Options
|
---|
| 91 |
|
---|
| 92 | # Initialize the syntax string
|
---|
| 93 |
|
---|
| 94 | pb_syntax_init("$appname Version $mrver-$mrrev\n");
|
---|
| 95 |
|
---|
| 96 | GetOptions("help|?|h" => \$opts{'h'},
|
---|
| 97 | "man" => \$opts{'man'},
|
---|
| 98 | "verbose|v+" => \$opts{'v'},
|
---|
[3488] | 99 | "label|L=s" => \$opts{'L'},
|
---|
| 100 | "uuid|U=s" => \$opts{'U'},
|
---|
[3486] | 101 | ) || pb_syntax(-1,0);
|
---|
| 102 |
|
---|
| 103 | if (defined $opts{'h'}) {
|
---|
| 104 | pb_syntax(0,1);
|
---|
| 105 | }
|
---|
| 106 | if (defined $opts{'man'}) {
|
---|
| 107 | pb_syntax(0,2);
|
---|
| 108 | }
|
---|
| 109 | if (defined $opts{'v'}) {
|
---|
| 110 | $pbdebug++;
|
---|
| 111 | }
|
---|
| 112 | pb_log_init($pbdebug, $pbLOG);
|
---|
| 113 | pb_temp_init($pbdebug);
|
---|
| 114 |
|
---|
[3488] | 115 | my $label=$opts{'L'};
|
---|
| 116 | my $uuid=$opts{'U'};
|
---|
| 117 | die "Unable no label/uuid defined" if ((not defined $label) && (not defined $uuid));
|
---|
[3486] | 118 |
|
---|
[3501] | 119 | my $inputf = $ARGV[0];
|
---|
[3486] | 120 | die "$0: no input file defined" if (not defined $inputf);
|
---|
| 121 |
|
---|
[3501] | 122 | my($inputfh, $buffer, $offset, $length, $hexlabel);
|
---|
| 123 |
|
---|
[3550] | 124 | # Evaluate the size of the FAT with blkid -p
|
---|
| 125 | open(FS,"blkid -p $inputf|") || die "$0: cannot blkid -p $inputf: $!";
|
---|
| 126 | my $verfs = <FS>;
|
---|
| 127 | close(FS);
|
---|
| 128 | $verfs =~ s/.*VERSION="FAT([0-9][0-9]).*"/FAT$1/;
|
---|
| 129 | chomp($verfs);
|
---|
| 130 | pb_log(1,"Working on a FS ot type ***$verfs***\n");
|
---|
| 131 |
|
---|
[3486] | 132 | open($inputfh, "+< $inputf") || die "$0: cannot open $inputf for reading/writing: $!";
|
---|
| 133 | binmode($inputfh) || die "binmode failed: $!";
|
---|
| 134 |
|
---|
[3550] | 135 | # Source https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html
|
---|
[3488] | 136 | if (defined $label) {
|
---|
[3550] | 137 | # x first bytes are untouched
|
---|
| 138 | $offset = 43 if (($verfs =~ /FAT16/) || ($verfs =~ /FAT12/));
|
---|
| 139 | $offset = 71 if ($verfs =~ /FAT32/);
|
---|
[3488] | 140 | $length = 11;
|
---|
| 141 | } elsif (defined $uuid) {
|
---|
[3550] | 142 | # x first bytes are untouched
|
---|
| 143 | $offset = 39 if (($verfs =~ /FAT16/) || ($verfs =~ /FAT12/));
|
---|
| 144 | $offset = 67 if ($verfs =~ /FAT32/);
|
---|
[3488] | 145 | $length = 4;
|
---|
| 146 | } else {
|
---|
| 147 | die "Nothing to do no label/uuid defined";
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | if (! seek($inputfh, $offset,0)) {
|
---|
[3486] | 151 | die "Unable to seek $inputf: $!";
|
---|
| 152 | }
|
---|
[3488] | 153 | # then read the following bytes
|
---|
| 154 | my $size = read($inputfh, $buffer, $length);
|
---|
| 155 | if ($size != $length) {
|
---|
| 156 | die "Unable to read existing label/uuid: $!";
|
---|
| 157 | }
|
---|
[3486] | 158 |
|
---|
[3488] | 159 | if (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);
|
---|
[3522] | 165 | #print Dumper(@loc);
|
---|
[3488] | 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";
|
---|
[3486] | 170 | }
|
---|
[3488] | 171 |
|
---|
| 172 | if (defined $label) {
|
---|
| 173 | $hexlabel=sprintf("%-11s", uc($label));
|
---|
| 174 | pb_log(1,"New label is ***$hexlabel***\n");
|
---|
| 175 | } elsif (defined $uuid) {
|
---|
[3501] | 176 | # Remove separator
|
---|
| 177 | $uuid =~ s/-//;
|
---|
[3488] | 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 | }
|
---|
[3486] | 185 | # Go back
|
---|
[3488] | 186 | seek($inputfh, $offset,0);
|
---|
[3486] | 187 | print $inputfh $hexlabel;
|
---|
| 188 | close($inputfh) || die "couldn't close $inputf: $!";
|
---|