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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#!perl -w
#
# ActiveBlockDevTab -- descriptors of all block devices in /sys
# Copyright (C) 2005 Erik van Konijnenburg
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
use strict;
use warnings;
use Base;
use Conf;
use ActiveBlockDev;
package ActiveBlockDevTab;
my $abdTab = undef;
my $abdList = undef;
sub init () {
if (defined ($abdTab)) {
return;
}
$abdTab = {};
my $blockdir;
my $blockPath = Conf::get('sysFs') . "/block";
if (! opendir ($blockdir, "$blockPath")) {
Base::fatal ("can't open directory $blockPath");
}
while (defined (my $entry = readdir ($blockdir))) {
next if $entry eq ".";
next if $entry eq "..";
oneBlockDev($blockPath, $entry);
}
if (! closedir ($blockdir)) {
Base::fatal ("could not read directory $blockPath");
}
$abdList = [values %{$abdTab}];
}
sub oneBlockDev ($$) {
my ($blockPath, $entry) = @_;
my $devno = Base::getStringFile("$blockPath/$entry/dev");
if (exists ($abdTab->{$devno})) {
Base::fatal ("found duplicate devno in $blockPath/$entry");
}
my $hw = readlink("$blockPath/$entry/device");
# failure results in undef, which is exactly how
# we want to represent absence of a hw device.
if (defined ($hw)) {
unless ($hw =~ s!^(\.\./)+devices/!!) {
# imagine localised linux (/sys/geraete ...)
Base::fatal ("bad device link in $blockPath/$entry");
}
}
my $descr = ActiveBlockDev->new (
name => $entry,
devno => $devno,
parent => undef,
hw => $hw,
);
$abdTab->{$devno} = $descr;
#
# Scan partitions.
# Note that these are only visible after someone
# read the a block from the device.
#
my $devdir;
if (! opendir ($devdir, "$blockPath/$entry")) {
Base::fatal ("can't open directory $blockPath/$entry");
}
while (defined (my $partition = readdir ($devdir))) {
next if $partition eq ".";
next if $partition eq "..";
next unless -d "$blockPath/$entry/$partition";
# there can be subdirectories in a blockdev
# other than partitions; eg /sys/block/sda/queue.
# a partition has major:minor, so has a dev file.
next unless -f "$blockPath/$entry/$partition/dev";
onePartition($blockPath, $entry, $partition, $descr);
}
if (! closedir ($devdir)) {
Base::fatal ("could not read directory $blockPath/$entry");
}
}
sub onePartition ($$$$) {
my ($blockPath, $entry, $partition, $parent) = @_;
my $dev = Base::getStringFile("$blockPath/$entry/$partition/dev");
if (exists ($abdTab->{$dev})) {
Base::fatal ("found duplicate devno in $blockPath/$entry/$partition");
}
$abdTab->{$dev} = ActiveBlockDev->new (
name => $partition,
devno => $dev,
parent => $parent,
hw => undef,
);
}
#
# all -- return list of all known active block devices.
#
sub all () {
init;
return $abdList;
}
#
# findByDevno -- given devno in format maj:min,
# return corresponding descriptor or undef.
#
sub findByDevno ($) {
my ($devno) = @_;
init;
return $abdTab->{$devno};
}
#
# findByPath -- given path to block device file in /dev,
# return corresponding descriptor or undef.
#
sub findByPath ($) {
my ($path) = @_;
my $devno = Base::devno ($path);
if (! defined ($devno)) {
return undef;
}
return findByDevno ($devno);
}
1;
|