package list_modules; use MDK::Common; our @ISA = qw(Exporter); our @EXPORT = qw(load_dependencies dependencies_closure category2modules module2category sub_categories); # the categories have 2 purposes # - choosing modules to include on stage1's (cf update_kernel and mdk-stage1/pci-resource/update-pci-ids.pl) # - performing a load_category or probe_category (modules.pm and many files in perl-install) our %l = ( ################################################################################ network => { main => [ if_(arch() =~ /ppc/, qw(mace bmac gmac)), if_(arch() =~ /^sparc/, qw(myri_sbus sunbmac sunhme sunqe)), if_(arch() !~ /alpha/ && arch() !~ /sparc/, qw(3c501 3c503 3c505 3c507 3c509 3c515), # 3c90x qw(82596 abyss ac3200 acenic aironet4500_card at1700 atp com20020-pci), qw(cs89x0 de600 de620), qw(defxx orinoco_plx), # most unused qw(depca dgrs dmfe e100 e1000 e2100 eepro eepro100 eexpress epic100 eth16i), qw(ewrk3 hamachi hp hp-plus hp100 ibmtr), qw(lance natsemi ne ne2k-pci ni5010 ni52 ni65 olympic pcnet32 plip rcpci), #old_tulip qw(sb1000 sis900 sk98lin smc-ultra smc9194 starfire tlan tmspci tulip via-rhine), #sktr qw(wd winbond-840 yellowfin ns83820), qw(iph5526), #- fibre channel ), qw(3c59x 8139too sundance dl2k), #rtl8139 ], raw => [ qw(8390 mii), qw(ppp_generic ppp_async slhc aironet4500_core), ], pcmcia => [ qw(3c574_cs 3c589_cs airo airo_cs aironet4500_cs axnet_cs fmvj18x_cs), qw(ibmtr_cs netwave_cs nmclan_cs pcnet_cs ray_cs smc91c92_cs wavelan_cs wvlan_cs), qw(xirc2ps_cs xircom_cb xircom_tulip_cb), ], usb => [ qw(pegasus kaweth usbnet catc CDCEther), ], isdn => [ qw(hisax hysdn b1pci t1pci c4), ], }, ################################################################################ disk => { scsi => [ if_(arch() =~ /ppc/, qw(mesh mac53c94)), if_(arch() =~ /^sparc/, qw(qlogicpti)), if_(arch() !~ /alpha/ && arch() !~ /sparc/, qw(3w-xxxx AM53C974 BusLogic NCR53c406a a100u2w advansys aha152x aha1542 aha1740), qw(atp870u dc395x_trm dtc fdomain g_NCR5380 in2000 initio pci2220i psi240i), qw(qla1280 qla2x00 qlogicfas qlogicfc), qw(seagate sim710 sym53c416 t128 tmscsim u14-34f ultrastor wd7000), qw(eata eata_pio eata_dma), ), '53c7,8xx', qw(aic7xxx pci2000 qlogicisp sym53c8xx), # ncr53c8xx ], hardware_raid => [ if_(arch() =~ /^sparc/, qw(pluto)), if_(arch() !~ /alpha/ && arch() !~ /sparc/, qw(DAC960 dpt_i2o megaraid aacraid ataraid cciss cpqarray gdth i2o_block), qw(qla2200 qla2300 cpqfc), qw(ips ppa imm), ), ], pcmcia => [ qw(aha152x_cs fdomain_cs nsp_cs qlogic_cs ide-cs) ], #ide_cs raw => [ qw(scsi_mod sd_mod) ], usb => [ qw(usb-storage) ], cdrom => [ qw(ide-cd sr_mod cdrom) ], }, ################################################################################ bus => { usb => [ qw(usbcore usb-uhci usb-ohci ehci-hcd usbkbd keybdev input) ], pcmcia => [ if_(arch() !~ /^sparc/, qw(pcmcia_core tcic ds i82365 yenta_socket)), # cb_enabler ], #serial_cs #ftl_cs 3c575_cb apa1480_cb epic_cb serial_cb tulip_cb iflash2+_mtd iflash2_mtd #cb_enabler }, fs => { network => [ qw(af_packet nfs lockd sunrpc) ], cdrom => [ qw(isofs) ], loopback => [ qw(isofs loop) ], local => [ if_(arch() =~ /^i.86/, qw(vfat fat)), if_(arch() =~ /^ppc/, qw(hfs)), qw(reiserfs), ], various => [ qw(smbfs romfs jbd xfs) ], }, ################################################################################ multimedia => { sound => [ if_(arch() =~ /ppc/, qw(dmasound_awacs)), if_(arch() !~ /^sparc/, qw(cmpci cs46xx cs4281 es1370 es1371 esssolo1 i810_audio maestro maestro3), qw(nm256_audio pas16 trident via82cxxx_audio sonicvibes emu10k1 ymfpci), qw(rme96xx audigy), qw(snd-ice1712 snd-cmipci snd-ens1371 snd-via8233), qw(snd-es1938 snd-fm801 snd-intel8x0 snd-rme96), qw(snd-cs46xx snd-maestro3 snd-korg1212 snd-ens1370 snd-als4000), qw(snd-trident snd-ymfpci), ), ], tv => [ qw(bttv cpia_usb ibmcam mod_quickcam ov511 ultracam usbvideo cyber2000fb) ], photo => [ qw(dc2xx mdc800) ], radio => [ qw(radio-maxiradio) ], scanner => [ qw(scanner microtek) ], joystick => [ qw(ns558 emu10k1-gp iforce) ], }, various => # just here for classification, unused categories (nor auto-detect, nor load_thiskind) { raid => [ qw(linear raid0 raid1 raid5 lvm-mod md multipath xor), ], mouse => [ qw(busmouse msbusmouse logibusmouse serial qpmouse atixlmouse), ], char => [ qw(amd768_rng applicom n_r3964 nvram pc110pad ppdev), qw(mxser moxa isicom wdt_pci epca synclink istallion sonypi i810-tco sx), #- what are these??? ], other => [ qw(agpgart defxx i810_rng i810fb ide-floppy ide-scsi ide-tape loop lp nbd sg st), qw(parport parport_pc parport_serial), qw(btaudio), #- these need checking qw(pcilynx sktr rrunner gmac meye 3c559 buz paep), ], }, ); my %dependencies; sub load_dependencies { my ($file) = @_; %dependencies = map { my ($f, $deps) = split ':'; $f => [ split ' ', $deps ]; } cat_($file); } sub dependencies_closure { my @l = map { dependencies_closure($_) } @{$dependencies{$_[0]} || []}; (@l, $_[0]); } sub category2modules { map { my ($t1, $t2s) = m|(.*)/(.*)|; map { my $l = $l{$t1}{$_} or die "bad category $t1/$_\n" . backtrace(); @$l; } split('\|', $t2s); } split(' ', $_[0]); } sub module2category { my ($module) = @_; foreach my $t1 (keys %l) { my $h = $l{$t1}; foreach my $t2 (keys %$h) { $module eq $_ and return "$t1/$t2" foreach @{$h->{$t2}}; } } return; } sub sub_categories { my ($t1) = @_; keys %{$l{$t1}}; } 1; : cb814bd9bae19281f8f461318602dd5116f00979 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package raid; # $Id: raid.pm 269279 2010-05-24 14:12:09Z pterjan $

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use common;
use fs::type;
use fs::get;
use run_program;
use devices;
use modules;

sub max_nb() { 31 }

sub check_prog {
    my ($in) = @_;
    $::prefix ? whereis_binary('mdadm') : $in->do_pkgs->ensure_binary_is_installed('mdadm', 'mdadm');
}

sub new {
    my ($raids, %opts) = @_;
    my $md_part = { %opts };
    add2hash_($md_part, { 'chunk-size' => '64', disks => [], 
			  fs_type => defaultFS(),
			  device => first(free_mds($raids)), 
			  notFormatted => 1, level => 1 });
    push @$raids, $md_part;
    foreach (@{$md_part->{disks}}) {
	$_->{raid} = $md_part->{device};
	fs::type::set_pt_type($_, 0xfd);
	delete $_->{mntpoint};
    }
    update($md_part);
    $md_part;
}

sub add {
    my ($md_part, $part) = @_;
    $md_part->{isMounted} and die N("Cannot add a partition to _formatted_ RAID %s", $md_part->{device});
    inactivate_and_dirty($md_part);
    set_isFormatted($part, 0);
    $part->{raid} = $md_part->{device};
    delete $part->{mntpoint};
    push @{$md_part->{disks}}, $part;
    update($md_part);
}

sub delete {
    my ($raids, $md_part) = @_;
    inactivate_and_dirty($md_part);
    delete $_->{raid} foreach @{$md_part->{disks}};
    @$raids = grep { $_ != $md_part } @$raids;
    write_conf($raids) if $::isStandalone;
}

sub change_device {
    my ($md_part, $new_device) = @_;
    if ($new_device ne $md_part->{device}) {
	inactivate_and_dirty($md_part);
	$md_part->{device} = $new_device;
	$_->{raid} = $new_device foreach @{$md_part->{disks}};
    }
}

sub removeDisk {
    my ($raids, $part) = @_;
    my $md_part = fs::get::device2part($part->{raid}, $raids);
    inactivate_and_dirty($md_part);
    fs::type::set_isFormatted($part, 0);
    delete $part->{raid};
    my $disks = $md_part->{disks};
    @$disks = grep { $_ != $part } @$disks;
    if (@$disks) {
	update($md_part);
    } else {
	@$raids = grep { $_ != $md_part } @$raids;
    }
    write_conf($raids) if $::isStandalone;
}

sub updateSize {
    my ($part) = @_;
    local $_ = $part->{level};
    my @l = map { $_->{size} } @{$part->{disks}};

    $part->{size} = do {
	if (/0|linear/) { sum @l }
	elsif (/1/)     { min @l }
	elsif (/4|5/)   { min(@l) * (@l - 1) }
	elsif (/6/)     { min(@l) * (@l - 2) }
	elsif (/10/)	{ min(@l) * (@l / 2) }
    };
}

sub allmodules {
    [ 'raid0', 'raid1', 'raid10', 'raid456' ];
}

sub module {
    my ($part) = @_;
    my $level = $part->{level};
    log::l("level $level");

    if (member($level, 4, 5, 6)) {
	'raid456';
    } elsif (member($level, 10)) {
	'raid10';
    } elsif ($level =~ /^\d+$/) {
	"raid$level";
    } else {
	$level;
    }
}


sub update {
    updateSize($_) foreach @_;
}

sub make {
    my ($raids, $part) = @_;    

    return if is_active($part->{device});

    inactivate_and_dirty($part);

    isRAID($_) and make($raids, $_) foreach @{$part->{disks}};
    eval { modules::load(module($part)) };

    whereis_binary('mdadm') or die 'mdadm not installed';

    my $dev = devices::make($part->{device});
    my $nb = @{$part->{disks}};

    run_program::run_or_die('mdadm', '--create', '--run', $dev, 
			    if_($nb == 1, '--force'),
			    '--chunk=' . $part->{'chunk-size'}, 
			    "--level=$part->{level}", 
			    "--raid-devices=$nb",
			    if_($part->{'metadata'}, "--metadata=$part->{'metadata'}"),
			    map { devices::make($_->{device}) } @{$part->{disks}});

    if (my $raw_part = get_md_info($dev)) {
	$part->{UUID} = $raw_part->{UUID};
    }
    write_conf($raids) if $::isStandalone;
}

sub format_part {
    my ($raids, $part) = @_;
    $part->{isFormatted} and return;

    make($raids, $part);
    fs::format::part_raw($part, undef);
    set_isFormatted($_, 1) foreach @{$part->{disks}};
}

sub verify {
    my ($raids) = @_;
    foreach (@$raids) {
	my $nb = $_->{level} =~ /6|10/ ? 4 : $_->{level} =~ /4|5/ ? 3 : 2;
	@{$_->{disks}} >= $nb or die N("Not enough partitions for RAID level %d\n", $_->{level});
    }
}

sub inactivate_and_dirty {
    my ($part) = @_;
    run_program::run('mdadm', '--stop', devices::make($part->{device}));
    set_isFormatted($part, 0);
}

sub active_mds() {
    map { if_(/^(md\S+)\s*:\s*active/, $1) } cat_("/proc/mdstat");
}
sub inactive_mds() {
    map { if_(/^(md\S+)\s*:\s*inactive/, $1) } cat_("/proc/mdstat");
}

sub free_mds {
    my ($raids) = @_;
    difference2([ map { "md$_" } 0 .. max_nb() ], [ map { $_->{device} } @$raids ]);
}

sub detect_during_install {
    my (@parts) = @_;
    foreach (@{allmodules()}) {
	eval { modules::load($_) };
    }
    detect_during_install_once(@parts);
    detect_during_install_once(@parts) if active_mds(); #- try again to detect RAID 10

    foreach (inactive_mds()) {
	log::l("$_ is an inactive md, we stop it to ensure it doesn't busy devices");
	run_program::run('mdadm', '--stop', devices::make($_));
    }
}

sub detect_during_install_once {
    my (@parts) = @_;
    devices::make("md$_") foreach 0 .. max_nb();
    output('/etc/mdadm.conf', join(' ', 'DEVICE', 
				   (map { "/dev/$_" } active_mds()), 
				   map { devices::make($_->{device}) } @parts), "\n");
    run_program::run('mdadm', '>>', '/etc/mdadm.conf', '--examine', '--scan');
    run_program::run('mdadm', '--assemble', '--scan');    
}

sub get_existing {
    my @parts = @_;
    my $raids = [];
    foreach my $md (active_mds()) {
	my $raw_part = get_md_info(devices::make($md)) or next;

	$raw_part->{level} =~ s/raid//; #- { linear | raid0 | raid1 | raid5 | raid6 } -> { linear | 0 | 1 | 5 | 6 }

	my @mdparts = 
	  map { 
	      if (my $part = fs::get::device2part($_, [ @parts, @$raids ])) {
		  $part;
	      } else {
		  log::l("ERROR: unknown raw raid device $_");
		  ();
	      }
	  } split(',', $raw_part->{devices});

	my $md_part = new($raids,
		device => $md,
		UUID => $raw_part->{UUID},
		level => $raw_part->{level},
		metadata => $raw_part->{metadata},
		disks => \@mdparts);

	my $type = fs::type::type_subpart_from_magic($md_part);
	if ($type) {
	    put_in_hash($md_part, $type);
	} else {
	    fs::type::set_fs_type($md_part, defaultFS());
	}
	my $fs_type = $type && $type->{fs_type};
	fs::type::set_isFormatted($md_part, to_bool($fs_type));

	log::l("RAID: found $md (raid $md_part->{level}) type $fs_type with parts $raw_part->{devices}");
    }
    $raids;
}

sub is_active {
    my ($dev) = @_;
    member($dev, active_mds());
}

sub write_conf {
    my ($raids) = @_;

    @$raids or return;

    my @devices = uniq(map { devices::make($_->{device}) } map { @{$_->{disks}} } @$raids);

    output($::isInstall ? "$::prefix/etc/mdadm.conf" : "/etc/mdadm.conf",
	   join(' ', 'DEVICE', @devices) . "\n",
	   map { "ARRAY " . devices::make($_->{device}) . " UUID=$_->{UUID} auto=yes\n" } @$raids);
}

sub get_md_info {
    my ($dev) = @_;
    my $conf = parse_mdadm_conf(scalar run_program::get_stdout('mdadm', '--detail', '--brief', '-v', $dev));

    @{$conf->{ARRAY}} or return;
    @{$conf->{ARRAY}} == 1 or internal_error("too many answers");
    $conf->{ARRAY}[0];
}

sub parse_mdadm_conf {
    my ($s) = @_;
    my %conf = (DEVICE => [], ARRAY => []);
    $s =~ s!^\s*#.*!!gm; #- remove comments
    $s =~ s!\n(\s)!$1!g; #- join lines starting with a space
    foreach (split("\n", $s)) {
	if (/^DEVICE\s+(.*)/) {
	    push @{$conf{DEVICE}}, split(' ', $1);
	} elsif (my ($md, $md_conf) = /^ARRAY\s+(\S+)\s*(.*)/) {
	    my %md_conf = map { if_(/(.*)=(.*)/, $1 => $2) } split(' ', $md_conf);
	    $md_conf{level} =~ s/^raid//;
	    $md_conf{device} = $md;
	    push @{$conf{ARRAY}}, \%md_conf;
	}
    }
    \%conf;
}

1;