/usr/share/perl5/Rex/Commands/LVM.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
=head1 NAME
Rex::Commands::LVM - Get LVM Information
=head1 DESCRIPTION
With this module you can get information of your lvm setup.
Version <= 1.0: All these functions will not be reported.
All these functions are not idempotent.
=head1 SYNOPSIS
use Rex::Commands::LVM;
my @physical_devices = pvs;
my @volume_groups = vgs;
my @logical_volumes = lvs;
=head1 EXPORTED FUNCTIONS
=cut
package Rex::Commands::LVM;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
require Rex::Exporter;
use base qw(Rex::Exporter);
use vars qw(@EXPORT);
@EXPORT = qw(pvs vgs lvs pvcreate vgcreate lvcreate vgextend);
use Rex::Commands::Run;
use Rex::Commands::Mkfs;
=head2 pvs
Get Information for all your physical volumes.
use Data::Dumper;
use Rex::Commands::LVM;
task "lvm", sub {
my @physical_volumes = pvs;
for my $physical_volume (@physical_volumes) {
say Dumper($physical_volume);
}
};
=cut
sub pvs {
my @lines = run 'pvdisplay --units b --columns --separator "|" --noheadings';
if ( $? != 0 ) {
die("Error running pvdisplay");
}
my @ret;
for my $line (@lines) {
chomp $line;
$line =~ s/^\s+//g;
my ( $phy_vol, $vol_group, $format, $attr, $psize, $pfree ) =
split( /\|/, $line );
$pfree =~ s/B$//;
$psize =~ s/B$//;
push(
@ret,
{
physical_volume => $phy_vol,
volume_group => $vol_group,
format => $format,
attributes => $attr,
size => $psize,
free => $pfree,
}
);
}
return @ret;
}
=head2 vgs
Get Information for all your volume groups.
use Data::Dumper;
use Rex::Commands::LVM;
task "lvm", sub {
my @volume_groups = vgs;
for my $volume_group (@volume_groups) {
say Dumper($volume_group);
}
};
=cut
sub vgs {
my ($vg) = @_;
my $cmd =
'vgdisplay --units b --columns --separator "|" --noheadings -o "pv_name,vg_name,vg_size,vg_free,vg_attr"';
if ($vg) {
$cmd .= " $vg";
}
my @lines = run $cmd;
if ( $? != 0 ) {
die("Error running vgdisplay");
}
my @ret;
for my $line (@lines) {
chomp $line;
$line =~ s/^\s+//g;
my ( $pv_name, $vg_name, $vg_size, $vg_free, $vg_attr ) =
split( /\|/, $line );
$vg_free =~ s/B$//;
$vg_size =~ s/B$//;
push(
@ret,
{
physical_volume => $pv_name,
volume_group => $vg_name,
size => $vg_size,
free => $vg_free,
attributes => $vg_attr,
}
);
}
return @ret;
}
=head2 lvs
Get Information for all your logical volumes.
use Data::Dumper;
use Rex::Commands::LVM;
task "lvm", sub {
my @logical_volumes = lvs;
for my $logical_volume (@logical_volumes) {
say Dumper($logical_volume);
}
};
=cut
sub lvs {
my ($vg) = @_;
my $cmd =
'lvdisplay --units b --columns --separator "|" -o "lv_name,vg_name,lv_attr,lv_size" --noheading';
if ($vg) {
$cmd .= " " . $vg;
}
my @lines = run $cmd;
if ( $? != 0 ) {
die("Error running lvdisplay");
}
my @ret;
for my $line (@lines) {
chomp $line;
$line =~ s/^\s+//g;
my ( $lv_name, $vg_name, $lv_attr, $lv_size ) = split( /\|/, $line );
$lv_size =~ s/B$//;
push(
@ret,
{
name => $lv_name,
path => "/dev/$vg_name/$lv_name",
attributes => $lv_attr,
size => $lv_size,
}
);
}
return @ret;
}
sub pvcreate {
my ($dev) = @_;
my $s = run "pvcreate $dev";
if ( $? != 0 ) {
die("Error creating pv.\n$s\n");
}
return 1;
}
sub vgcreate {
my ( $vgname, @devices ) = @_;
my $s = run "vgcreate $vgname " . join( " ", @devices );
if ( $? != 0 ) {
die("Error creating vg.\n$s\n");
}
return 1;
}
sub lvcreate {
my ( $lvname, %option ) = @_;
if ( !exists $option{size} || !exists $option{onvg} ) {
die("Missing parameter size or onvg.");
}
unless ( $lvname =~ m/^[a-z0-9\-\._]+$/i ) {
die("Error in lvname. Allowed characters a-z, 0-9 and _-. .");
}
my $size = $option{size};
if ( $size =~ m/^[0-9]+$/ ) { $size .= "M"; }
my $onvg = $option{onvg};
my $s = run "lvcreate -n $lvname -L $size $onvg";
my $lv_path = $option{onvg} . "/" . $lvname;
mkfs "$lv_path", fstype => $option{fstype};
if ( $? != 0 ) {
die("Error creating lv.\n$s\n");
}
return $lv_path;
}
sub vgextend {
my ( $vgname, @devices ) = @_;
my $s = run "vgextend $vgname " . join( " ", @devices );
if ( $? != 0 ) {
die("Error extending vg.\n$s\n");
}
return 1;
}
1;
|