]> git.sur5r.net Git - bacula/rescue/blob - rescue/linux/cdrom/yaird-0.0.5/perl/ActiveBlockDev.pm
102f2c9b9052105a985e40154a709ecf7566b86b
[bacula/rescue] / rescue / linux / cdrom / yaird-0.0.5 / perl / ActiveBlockDev.pm
1 #!perl -w
2 #
3 # ActiveBlockDev -- a single blockdev, as found in /sys
4 #   Copyright (C) 2005  Erik van Konijnenburg
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software
18 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 #
20 # The blockdev can be a whole device or a partition.
21 # descriptors contain:
22 # - name, path as found in /sys (sda, sda/sda1)
23 # - devno, as found in /sys (8:1)
24 # - parent, undef for whole device, maj:minor of parent otherwise
25 # - hw, path to underlying hardware device,
26 #               eg an ide controller somewhere on a PCI bus.
27 #               this is a path relative to /sys/devices,
28 #               and can be undef (eg for ram disk)
29 # - special, a block special file giving access to the device
30 #               in the currently running kernel, or undef.
31 # - yspecial, a block special file giving access to the device
32 #               in the generated initrd image.  This often needs
33 #               a mknod on the generated image.  It's not convenient
34 #               to use the previous special, since that may be in
35 #               a subdirectory unavailable on the initrd image.
36 #               It's also not exactly /dev/$name, since LVM logical
37 #               volumes follow another convention.
38 # - partitions, list of partitions contained in the device.
39 #               these partitions are also ActiveBlockDevs.
40 #
41 # NOTE: the partition list relies on ActiveBlockDevTab making
42 # a complete scan of all block devices, each partition registering
43 # itself as partition with the parent device.
44 #
45 use strict;
46 use warnings;
47 use BlockSpecialFileTab;
48 use LvmTab;
49 package ActiveBlockDev;
50 use base 'Obj';
51
52
53 sub fill {
54         my $self = shift;
55         $self->SUPER::fill();
56         $self->takeArgs ('name', 'devno', 'parent', 'hw');
57         $self->{partitions} = [];
58         if (defined ($self->parent)) {
59                 push @{$self->parent->partitions}, $self;
60         }
61
62         my $bfs = BlockSpecialFileTab::findByDevno($self->devno);
63         if (defined ($bfs)) {
64                 $self->{special} = $bfs->path;
65         }
66         else {
67                 $self->{special} = undef;
68         }
69 }
70
71 sub name        { return $_[0]->{name}; }
72 sub devno       { return $_[0]->{devno}; }
73 sub parent      { return $_[0]->{parent}; }
74 sub hw          { return $_[0]->{hw}; }
75 sub special     { return $_[0]->{special}; }
76 sub partitions  { return $_[0]->{partitions}; }
77
78 sub string {
79         my $self = shift;
80         my $name = $self->name;
81         my $devno = $self->devno;
82         my $parent = (defined($self->parent) ? $self->parent->name : "--");
83         my $hw = ($self->hw or "--");
84         my $special = ($self->special or "--");
85         my $yspecial = ($self->yspecial or "--");
86         return "$name($devno) in $parent at $hw via $special becomes $yspecial";
87 }
88
89 sub yspecial {
90         my $self = shift;
91         my $name = $self->name;
92         my $devno = $self->devno;
93         my $yspecial = "/dev/$name";
94         my $lv = LvmTab::findLVByDevno ($devno);
95         if (defined ($lv)) {
96                 $yspecial = $lv->lvnam;
97         }
98         return $yspecial;
99 }
100
101 sub hasPartitions {
102         my $self = shift;
103         return ($#{$self->partitions} >= 0);
104 }
105
106 1;