summaryrefslogtreecommitdiffstats
path: root/perl-install/detect_devices.pm
blob: 8f1fac97d3190bcaac984c8a482a92eaee4ebb81 (plain)
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
150
151
package detect_devices;

use diagnostics;
use strict;

use log;
use common qw(:common :file);
use c;


my $scsiDeviceAvailable;
my $CSADeviceAvailable;

1;

sub get {
    # Detect the default BIOS boot harddrive is kind of tricky. We may have IDE,
    # SCSI and RAID devices on the same machine. From what I see so far, the default
    # BIOS boot harddrive will be
    # 1. The first IDE device if IDE exists. Or 
    # 2. The first SCSI device if SCSI exists. Or
    # 3. The first RAID device if RAID exists.

    map { &{$_->[0]}() ? &{$_->[1]}() : () }
    [ \&hasIDE, \&getIDE ],
    [ \&hasSCSI, \&getSCSI ],
    [ \&hasDAC960, \&getDAC960 ],
    [ \&hasCompaqSmartArray, \&getCompaqSmartArray ];
}
sub hds() { grep { $_->{type} eq 'hd' } get(); }
sub cdroms() { grep { $_->{type} eq 'cdrom' } get(); }

sub hasSCSI() {
    defined $scsiDeviceAvailable and return $scsiDeviceAvailable;
    local *F;
    open F, "/proc/scsi/scsi" or log::l("failed to open /proc/scsi/scsi: $!"), return 0;
    foreach (<F>) {
	/devices: none/ and log::l("no scsi devices are available"), return $scsiDeviceAvailable = 0;
    }
    log::l("scsi devices are available");
    $scsiDeviceAvailable = 1;
}
sub hasIDE() { 1 }
sub hasDAC960() { 1 }

sub hasCompaqSmartArray() {
    defined $CSADeviceAvailable and return $CSADeviceAvailable;
    -r "/proc/array/ida0" or log::l("failed to open /proc/array/ida0: $!"), return $CSADeviceAvailable = 0;
    log::l("Compaq Smart Array controllers available");
    $CSADeviceAvailable = 1;
}

sub getSCSI() {
    my @drives;
    my ($driveNum, $cdromNum, $tapeNum) = qw(0 0 0);
    my $err = sub { chop; log::l("unexpected line in /proc/scsi/scsi: $_"); error() };
    local $_;

    local *F;
    open F, "/proc/scsi/scsi" or return &$err();
    $_ = <F>; /^Attached devices:/ or return &$err();
    while ($_ = <F>) {
	my ($id) = /^Host:.*?Id: (\d+)/ or return &$err();
	$_ = <F>; my ($vendor, $model) = /^\s*Vendor:\s*(.*?)\s+Model:\s*(.*?)\s+Rev:/ or return &$err();
	$_ = <F>; my ($type) = /^\s*Type:\s*(.*)/ or &$err();
	my $device;
	if ($type =~ /Direct-Access/) {
	    $type = 'hd';
	    $device = "sd" . chr($driveNum++ + ord('a'));
	} elsif ($type =~ /Sequential-Access/) {
	    $type = 'tape';
	    $device = "st" . $tapeNum++;
	} elsif ($type =~ /CD-ROM/) {
	    $type = 'cdrom';
	    $device = "scd" . $cdromNum++;
	}
	$device and push @drives, { device => $device, type => $type, info => "$vendor $model", id => $id, bus => 0 };
    }
    @drives;
}

sub getIDE() {
    my @idi;

    -r "/proc/ide" or die "sorry, /proc/ide not available, seems like you have a pre-2.2 kernel\n => not handled yet :(";

    # Great. 2.2 kernel, things are much easier and less error prone. 
    foreach my $d (glob_('/proc/ide/hd*')) {
	my ($t) = chop_(cat_("$d/media"));
	my $type = $ {{disk => 'hd', cdrom => 'cdrom', tape => 'tape', floppy => 'fd'}}{$t} or next;
	my ($info) = chop_(cat_("$d/model")); $info ||= "(none)";

	my $num = ord (($d =~ /(.)$/)[0]) - ord 'a';
	push @idi, { type => $type, device => basename($d), info => $info, bus => $num/2, id => $num%2 };
    }
    @idi;
}


sub getCompaqSmartArray() {
    my @idi;
    my $f;

    for (my $i = 0; -r ($f = "/proc/array/ida$i"); $i++) {
	local *F;
	open F, $f or die;
	local $_ = <F>;
	my ($name) = m|ida/(.*?):| or next;
	push @idi, { device => $name, info => "Compaq RAID logical disk", type => 'hd' };
    }
    @idi;
}

sub getDAC960() {
    my @idi;
    my $file = "/var/log/dmesg";
    -r $file or $file = "/tmp/syslog";

    local *F;
    open F, $file or die "Failed to open $file: $!";

    # We are looking for lines of this format:DAC960#0:
    # /dev/rd/c0d0: RAID-7, Online, 17928192 blocks, Write Thru0123456790123456789012    
    foreach (<F>) {
	my ($devicename, $info) = m|/dev/rd/(.*?): (.*?),| or next;
	push @idi, { info => $info, type => 'hd', devicename => $devicename }; 
	log::l("DAC960: $devicename: $info");
    }
    @idi;
}


sub getNet() {
    # I should probably ask which device to use if multiple ones are available -- oh well :-( 
    foreach (qw(eth0 tr0 plip0 plip1 plip2 fddi0)) {
	hasNetDevice($_) and log::l("$_ is available -- using it for networking"), return $_;
    }
    undef;
}
sub getPlip() {
    foreach (0..2) {
	hasNetDevice("plip$_") and log::l("plip$_ will be used for PLIP"), return "plip$_";
    }
    undef;
}

sub hasNet() { goto &getNet }
sub hasPlip() { goto &getPlip }
sub hasEthernet() { hasNetDevice("eth0"); }
sub hasTokenRing() { hasNetDevice("tr0"); }
sub hasNetDevice($) { c::hasNetDevice($_[0]) }