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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
#
# This file is part of StorageDisplay
#
# This software is copyright (c) 2014-2023 by Vincent Danjean.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
use strict;
use warnings;
package StorageDisplay::Block;
# ABSTRACT: Base package for block devices DAG
our $VERSION = '2.06'; # VERSION
1;
package StorageDisplay::BlockTreeElement;
use Moose;
use namespace::sweep;
use Carp;
use StorageDisplay::Role;
with "StorageDisplay::Role::Iterable"
=> {
iterable => "StorageDisplay::BlockTreeElement",
name => "Plain",
};
sub dname {
my $self;
return $self->name;
}
1;
##################################################################
package StorageDisplay::Block;
use Moose;
use namespace::sweep;
extends 'StorageDisplay::BlockTreeElement';
use Path::Class::Dir;
use Carp;
has 'names' => (
traits => [ 'Hash' ],
is => 'ro',
isa => 'HashRef[Path::Class::Dir]',
required => 1,
handles => {
'addname' => 'set',
'names_str' => 'keys',
}
);
has 'path' => (
is => 'rw',
isa => 'Path::Class::Dir',
required => 1,
);
has 'state' => (
is => 'rw',
isa => 'Str',
default => 'unknown',
lazy => 1,
);
has 'elem' => (
is => 'ro',
isa => 'StorageDisplay::Data::Elem',
writer => '_elem',
required => 0,
);
sub provided {
my $self = shift;
return defined($self->elem);
}
sub providedBy {
my $self = shift;
my $elem = shift;
my $name = shift;
if ($self->provided) {
croak "Duplicate provider for ".$self->name.": ".$self->elem." and ".$elem;
}
#print STDERR "Provider for ".$self->name.": ".$elem."\n";
$self->_elem($elem);
}
sub dname {
my $self = shift;
my $best_name=$self->name;
my $score = 0;
#print STDERR "name for ", $self->name, "\n";
#use Data::Dumper;
#print STDERR Dumper($self->names), "\n";
foreach my $n ($self->names_str) {
my $s=0;
if ($n =~ m,^disk/,) {
$s = 20;
} elsif ($n =~ m,^dm-,) {
$s = 30;
} elsif ($n =~ m,^mapper/,) {
$s = 40;
} elsif ($n =~ m,/,) {
$s = 60;
} else {
$s = 50;
}
if ($s > $score) {
#print STDERR " prefer($s) ", $n, "\n";
$score = $s;
$best_name = $n;
} elsif ($s == $score && $n gt $best_name) {
#print STDERR " prefer($s/alpha) ", $n, "\n";
$best_name = $n;
#} else {
# print STDERR " reject($s) ", $n, "\n";
}
}
return '/dev/'.
$best_name;
}
has 'size' => (
is => 'rw',
isa => 'Int',
lazy => 1,
default => sub {
my $self = shift;
confess "Method 'size' not implemented or set in ".ref($self)."@".$self->name;
#return -1;
},
);
sub blk_info {
my $self=shift;
my $key=shift;
return;
}
sub udev_info {
my $self=shift;
my $key=shift;
return;
}
## function, not method
sub asname {
my $block = shift;
my $blockname;
if (blessed($block) && $block->isa("StorageDisplay::BlockTreeElement")) {
$blockname = $block->name;
} else {
$blockname = $block;
}
return $blockname;
}
1;
##################################################################
package StorageDisplay::Block::NoSystem;
use Moose;
use namespace::sweep;
extends 'StorageDisplay::Block';
has 'id' => (
is => 'ro',
isa => 'Str',
required => 0,
);
has 'dname' => (
is => 'ro',
isa => 'Str',
required => 1,
);
use Carp;
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my %args = (@_);
my $name = $args{'name'} // $args{'id'};
my $dname = $args{'name'} // $args{'id'};
return $class->$orig(
'name' => $name,
'dname' => $dname,
'names' => { $name => Path::Class::Dir->new() },
'path' => Path::Class::Dir->new(),
@_
);
};
##################################################################
package StorageDisplay::Block::System;
use Moose;
use namespace::sweep;
extends 'StorageDisplay::Block';
use Carp;
has '_blk_infos' => (
is => 'ro',
isa => 'HashRef',
traits => [ 'Hash' ],
required => 1,
handles => {
'blk_info' => 'get',
}
);
has '_udev_infos' => (
is => 'ro',
isa => 'HashRef[Str]',
traits => [ 'Hash' ],
required => 1,
handles => {
'udev_info' => 'get',
}
);
sub size {
my $self = shift;
return $self->blk_info('SIZE');
}
sub kname {
my $self = shift;
return $self->blk_info('KNAME');
}
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my $name = shift;
my $st = shift;
my $blk_info = $st->get_info('lsblk', $name);
my %args;
if ( @_ != 0 || ref $name ) {
croak "Invalid call to new";
}
my $f;
my $udev_info = $st->get_info('udev', $name);
foreach my $k (keys %{$udev_info}) {
if ($k eq 'path') {
$args{$k}=Path::Class::Dir->new($udev_info->{$k});
} elsif ($k eq 'names') {
$args{$k} = { map { $_ => Path::Class::Dir->new($_) } @{$udev_info->{$k}} };
} else {
$args{$k}=$udev_info->{$k};
}
}
if (defined($blk_info)) {
my %hash;
%hash = map {
if (defined($blk_info->{$_})) {
uc $_ => $blk_info->{$_}
} else {
()
}
} keys %$blk_info;
$args{'_blk_infos'}=\%hash;
} else {
croak "coucou";
}
return $class->$orig(
%args, @_
);
};
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
StorageDisplay::Block - Base package for block devices DAG
=head1 VERSION
version 2.06
=head1 AUTHOR
Vincent Danjean <Vincent.Danjean@ens-lyon.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014-2023 by Vincent Danjean.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|