summaryrefslogtreecommitdiffstats
path: root/perl-install/fs/dmraid.pm
blob: 54c6532f1bb099071d91dcf95f53956d2c222b31 (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
package fs::dmraid; # $Id$

use diagnostics;
use strict;

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


sub init() {
    whereis_binary('dmraid') or die "dmraid not installed";

    eval { modules::load('dm-mirror') };
    devices::init_device_mapper();
    if ($::isInstall) {
	call_dmraid('-ay');
    }
    1;
}

#- call_dmraid is overloaded when debugging, see the end of this file
sub call_dmraid {
    my ($option, @args) = @_;
    run_program::get_stdout('dmraid', $option, @args);
}

sub check {
    my ($in) = @_;

    $in->do_pkgs->ensure_binary_is_installed('dmraid', 'dmraid') or return;
    init();
    1;
}

sub _raid_devices_raw() {
    map {
	chomp;
	log::l("got: $_");
	my %l; @l{qw(pv format vg level status size)} = split(':');
	if_(defined $l{size}, \%l);
    } call_dmraid('-r', '-c', '-c');
}

sub _raid_devices() {
    my @l = _raid_devices_raw();
    my %vg2pv; push @{$vg2pv{$_->{vg}}}, delete $_->{pv} foreach @l;
    my %vg2status; push @{$vg2status{$_->{vg}}}, delete $_->{status} foreach @l;
    map {
	delete $_->{size}; #- now irrelevant
	$_->{disks} = $vg2pv{$_->{vg}};
	$_->{status} = (every { $_ eq 'ok' } @{$vg2status{$_->{vg}}}) ? 'ok' : join(' ', @{$vg2status{$_->{vg}}});
	$_;
    } uniq_ { $_->{vg} } @l;
}

sub _sets_raw() {
    map {
	chomp;
	log::l("got: $_");
	my %l; @l{qw(name size stride level status subsets devs spares)} = split(':');
	if_(defined $l{spares}, \%l);
    } call_dmraid('-s', '-c', '-c');
}

sub _sets() {
    my @sets = _sets_raw();
    my @raid_devices = _raid_devices();
    foreach (@sets) {
	my $name = $_->{name};
	my @l = grep { begins_with($name, $_->{vg}) } @raid_devices;
	if (@l) {
	    log::l("ERROR: multiple match for set $name: " . join(' ', map { $_->{vg} } @l)) if @l > 1;
	    my ($raid) = @l;
	    add2hash($_, $raid);
	    $_->{status} = $raid->{status} if $_->{status} eq 'ok' && $::isInstall;
	} else {
	    log::l("ERROR: no matching raid devices for set $name");
	}
    }
    @sets;
}

sub vgs() {
    map {
	my $dev = "mapper/$_->{name}";
	my $vg = fs::wild_device::to_subpart("/dev/$dev");
	add2hash($vg, { media_type => 'hd', prefix => $dev, bus => "dmraid_$_->{format}", disks => $_->{disks} });

	#- device should exist, created by dmraid(8) using libdevmapper
	#- if it doesn't, we suppose it's not in use
	if_(-e "/dev/$dev", $vg); 

    } grep { 
	if ($_->{status} eq 'ok') {
	    1;
	} else {
	    call_dmraid('-an', $_->{vg}) if $::isInstall; #- for things like bad_sil below, deactivating half activated dmraid
	    0;
	}
    } _sets();
}

if ($ENV{DRAKX_DEBUG_DMRAID}) {
    eval(<<'EOF');
    my %debug_data = (
    
     isw => {
    
      # dmraid -s ####################
      # *** Group superset isw_ffafgbdhi
      # --> Active Subset
      # name   : isw_ffafgbdhi_toto
      # size   : 234441216
      # stride : 256
      # type   : mirror
      # status : ok
      # subsets: 0
      # devs   : 2
      # spares : 0
    
      '-s' => "isw_ffafgbdhi_toto:234441216:256:mirror:ok:0:2:0\n",
    
      # dmraid -r ####################
      #/dev/sda: isw, "isw_ffafgbdhi", GROUP, ok, 488397166 sectors, data@ 0
      #/dev/sdb: isw, "isw_ffafgbdhi", GROUP, ok, 234441646 sectors, data@ 0
    
      '-r' => "/dev/sda:isw:isw_ffafgbdhi:GROUP:ok:488397166:0\n" .
              "/dev/sdb:isw:isw_ffafgbdhi:GROUP:ok:234441646:0\n",
     },
    
     pdc => {
      # dmraid -s ####################
      # *** Active Set
      # name   : pdc_bcefbiigfg
      # size   : 80043200
      # stride : 128
      # type   : mirror
      # status : ok
      # subsets: 0
      # devs   : 2
      # spares : 0
    
      '-s' => "pdc_bcefbiigfg:80043200:128:mirror:ok:0:2:0\n",
    
      # dmraid -r ####################
      # /dev/sda: pdc, "pdc_bcefbiigfg", mirror, ok, 80043200 sectors, data@ 0
      # /dev/sdb: pdc, "pdc_bcefbiigfg", mirror, ok, 80043200 sectors, data@ 0
    
      '-r' => "/dev/sda:pdc:pdc_bcefbiigfg:mirror:ok:80043200:0\n" .
                "/dev/sdb:pdc:pdc_bcefbiigfg:mirror:ok:80043200:0\n",
     },
    
     bad_sil => {
      '-s' => "sil_aeacdidecbcb:234439600:0:mirror:ok:0:1:0\n",
                # ERROR: sil: only 3/4 metadata areas found on /dev/sdb, electing...

      '-r' => "/dev/sdb:sil:sil_aeacdidecbcb:mirror:broken:234439600:0\n",
                # ERROR: sil: only 3/4 metadata areas found on /dev/sdb, electing...
     },

     weird_nvidia =>  {
      '-s' => <<'EO',
/dev/sda: "sil" and "nvidia" formats discovered (using nvidia)!
/dev/sdb: "sil" and "nvidia" formats discovered (using nvidia)!
nvidia_bcjdbjfa:586114702:128:mirror:ok:0:2:0
EO
       '-r' => <<'EO',
/dev/sda: "sil" and "nvidia" formats discovered (using nvidia)!
/dev/sdb: "sil" and "nvidia" formats discovered (using nvidia)!
/dev/sda:nvidia:nvidia_bcjdbjfa:mirror:ok:586114702:0
/dev/sdb:nvidia:nvidia_bcjdbjfa:mirror:ok:586114702:0
EO
	 # ERROR: multiple match for set nvidia_bcjdbjfa:  nvidia_bcjdbjfa
     },


    );
    
    *call_dmraid = sub {
        my ($option, @args) = @_;
        if (my $s = $debug_data{$ENV{DRAKX_DEBUG_DMRAID}}{$option}) {
            split("\n", $s);
	} else {
            warn "dmraid $option @args\n";
        }
    };
EOF
    $@ and die;
}

1;