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