File: Label.pm

package info (click to toggle)
systemconfigurator 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 724 kB
  • ctags: 313
  • sloc: perl: 7,423; makefile: 48
file content (124 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package Boot::Label;

#   $Id: Label.pm,v 1.8 2003/04/10 21:57:56 pramath Exp $

#   Copyright (c) 2002 International Business Machines

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

#   See the
#   GNU General Public License for more details.
 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#   Sean Dague <sean@dague.net>

use strict;
use Carp;
use Data::Dumper;
use Boot::Devfs;
use Util::Cmd qw(:all);
use Util::Log qw(:all);

# gets the label for a device

sub dev2label {
    my $device = shift;
    if(-b $device) {
        return saferun("e2label $device");
    }
    return "";
}

sub saferun {
    my $cmd = shift;
    my $data = `$cmd 2>/dev/null`;
    verbose("$cmd = $data");
    if($?) {
        debug("WARNING: Error while running $cmd. $!");
        return undef;
    } else {
        chomp $data;
        return $data;
    }
}

sub labels {
    local *IN;
    open(IN,"</proc/partitions") or (carp ("Couldn't open /proc/partitions"), return undef);
    my %labels = ();
    while(<IN>) {
        if(/^\s*$/ or /major/) {
            # then we have the header or a blank line, so skip it
            next;
        }
        my ($junk, $major, $minor, $blocks, $name, $rio, $rmerge, $rsect, $ruse, $wio, $wmerge, $wsect, $wuse, $runn,  $ng, $use, $aveq) = split (/\s+/,$_);
        my $device = "/dev/". devfs_lookup($name);
        # only get labels for partitions
        if($device =~ /\d+$/) {
            verbose("getting label for $device");
            my $label = dev2label($device);
            if($label) {
                verbose("label is $label");
                $labels{$label} = $device;
            }
        }
    }
    close(IN);
    return %labels;
}

sub fstab_struct {
    my $file = shift;
    my $struct;
    my %labels;
    local *IN;
    open(IN,"<$file") or (carp ("Couldn't open $file for reading"), return undef);
    while(<IN>) {
        # get rid of starting spaces
        s/^\s+//;
        
        # next if the line is a comment or space
        next if /^(\#|$)/;
        
        my ($device, $mount, $fstype, $options, $fsck, $order) = split(/\s+/,$_);
        my $label = "";
        if ($device =~ /none/) {
            next;
        }
        
        if($device =~ /LABEL=(.+)/) {
            if(!scalar(keys %labels)) {
                verbose("Loading labels for all devices...");
                %labels = labels();
                verbose("Label structure is as follows: " . Dumper(\%labels));
            }
            $label = $1;
            $device = $labels{$label};
            if(!$device) {
                carp("WARNING: Label $label not found anywhere on the system!");
                verbose("Labels are:" . Dumper(\%labels));
                next;
            }
        }
        
        $struct->{$device} = {
                              mount => $mount,
                              label => $label,
                              type => $fstype,
                             };
    }
    close(IN);
    return $struct;
}

1;