From b0b0545400fd2de1a944c9bf4f7368b9f115c6fb Mon Sep 17 00:00:00 2001 From: Mo Morsi Date: Thu, 18 Jul 2013 09:46:09 -0400 Subject: [PATCH] flush out lvm support --- lib/linux_admin.rb | 3 ++ lib/linux_admin/logical_volume.rb | 49 +++++++++++++++++++++++- lib/linux_admin/physical_volume.rb | 56 ++++++++++++++++++++++++++++ lib/linux_admin/volume_group.rb | 50 +++++++++++++++++++++++++ spec/logical_volume_spec.rb | 60 ++++++++++++++++++++++++++++++ spec/physical_volume_spec.rb | 53 ++++++++++++++++++++++++++ spec/volume_group_spec.rb | 38 +++++++++++++++++++ 7 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 lib/linux_admin/physical_volume.rb create mode 100644 lib/linux_admin/volume_group.rb create mode 100644 spec/logical_volume_spec.rb create mode 100644 spec/physical_volume_spec.rb create mode 100644 spec/volume_group_spec.rb diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 04a4dd3..3552d40 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -14,6 +14,9 @@ require 'linux_admin/distro' require 'linux_admin/system' require 'linux_admin/fstab' +require 'linux_admin/logical_volume' +require 'linux_admin/physical_volume' +require 'linux_admin/volume_group' class LinuxAdmin extend Common diff --git a/lib/linux_admin/logical_volume.rb b/lib/linux_admin/logical_volume.rb index 9e76aca..0fd76e0 100644 --- a/lib/linux_admin/logical_volume.rb +++ b/lib/linux_admin/logical_volume.rb @@ -5,7 +5,54 @@ class LinuxAdmin class LogicalVolume < LinuxAdmin - def initialize + # logical volume name + attr_accessor :name + + # volume group name + attr_accessor :volume_group + + # logical volume size in sectors + attr_accessor :sectors + + # other fields available: + # logical volume access + # logical volume status + # internal logical volume number + # open count of logical volume + # current logical extents associated to logical volume + # allocated logical extents of logical volume + # allocation policy of logical volume + # read ahead sectors of logical volume + # major device number of logical volume + # minor device number of logical volume + + def initialize(args = {}) + @name = args[:name] + @volume_group = args[:volume_group] + @sectors = args[:sectors] + end + + def self.scan + @lvs ||= begin + vgs = VolumeGroup.scan + lvs = [] + + out = run(cmd(:lvdisplay), + :return_output => true, + :params => { '-c' => nil}) + + out.each_line do |line| + fields = line.split(':') + vgname = fields[1] + vg = vgs.find { |vg| vg.name == vgname } + + lvs << LogicalVolume.new(:name => fields[0], + :volume_group => vg, + :sectors => fields[6].to_i) + end + + lvs + end end end end diff --git a/lib/linux_admin/physical_volume.rb b/lib/linux_admin/physical_volume.rb new file mode 100644 index 0000000..5dc5369 --- /dev/null +++ b/lib/linux_admin/physical_volume.rb @@ -0,0 +1,56 @@ +# LinuxAdmin Physical Volume Representation +# +# Copyright (C) 2013 Red Hat Inc. +# Licensed under the MIT License + +class LinuxAdmin + class PhysicalVolume < LinuxAdmin + # physical volume device name + attr_accessor :device_name + + # volume group name + attr_accessor :volume_group + + # physical volume size in kilobytes + attr_accessor :size + + # other fields available + # internal physical volume number (obsolete) + # physical volume status + # physical volume (not) allocatable + # current number of logical volumes on this physical volume + # physical extent size in kilobytes + # total number of physical extents + # free number of physical extents + # allocated number of physical extents + + def initialize(args = {}) + @device_name = args[:device_name] + @volume_group = args[:volume_group] + @size = args[:size] + end + + def self.scan + @pvs ||= begin + vgs = VolumeGroup.scan + pvs = [] + + out = run(cmd(:pvdisplay), + :return_output => true, + :params => { '-c' => nil}) + + out.each_line do |line| + fields = line.split(':') + vgname = fields[1] + vg = vgs.find { |vg| vg.name == vgname} + + pvs << PhysicalVolume.new(:device_name => fields[0], + :volume_group => vg, + :size => fields[2].to_i) + end + + pvs + end + end + end +end diff --git a/lib/linux_admin/volume_group.rb b/lib/linux_admin/volume_group.rb new file mode 100644 index 0000000..48391c8 --- /dev/null +++ b/lib/linux_admin/volume_group.rb @@ -0,0 +1,50 @@ +# LinuxAdmin Volume Group Representation +# +# Copyright (C) 2013 Red Hat Inc. +# Licensed under the MIT License + +class LinuxAdmin + class VolumeGroup < LinuxAdmin + # volume group name + attr_accessor :name + + # other fields available + # volume group access + # volume group status + # internal volume group number + # maximum number of logical volumes + # current number of logical volumes + # open count of all logical volumes in this volume group + # maximum logical volume size + # maximum number of physical volumes + # current number of physical volumes + # actual number of physical volumes + # size of volume group in kilobytes + # physical extent size + # total number of physical extents for this volume group + # allocated number of physical extents for this volume group + # free number of physical extents for this volume group + # uuid of volume group + + def initialize(args = {}) + @name = args[:name] + end + + def self.scan + @vgs ||= begin + vgs = [] + + out = run(cmd(:vgdisplay), + :return_output => true, + :params => { '-c' => nil}) + + out.each_line do |line| + fields = line.split(':') + vgs << VolumeGroup.new(:name => fields[0]) + end + + vgs + end + end + end +end diff --git a/spec/logical_volume_spec.rb b/spec/logical_volume_spec.rb new file mode 100644 index 0000000..ceb8e0f --- /dev/null +++ b/spec/logical_volume_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe LinuxAdmin::LogicalVolume do + before(:each) do + LinuxAdmin::Distro.stub(:local). + and_return(LinuxAdmin::Distros::Test.new) + + @logical_volumes = < true, + :params => { '-c' => nil}). + and_return(@logical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) # stub out call to vgdisplay + LinuxAdmin::LogicalVolume.scan + end + + it "returns local logical volumes" do + LinuxAdmin::LogicalVolume.should_receive(:run).and_return(@logical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) + lvs = LinuxAdmin::LogicalVolume.scan + + lvs[0].should be_an_instance_of(LinuxAdmin::LogicalVolume) + lvs[0].name.should == '/dev/vg_foobar/lv_swap' + lvs[0].sectors.should == 4128768 + + lvs[1].should be_an_instance_of(LinuxAdmin::LogicalVolume) + lvs[1].name.should == '/dev/vg_foobar/lv_root' + lvs[1].sectors.should == 19988480 + end + + it "resolves volume group references" do + LinuxAdmin::LogicalVolume.should_receive(:run).and_return(@logical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) + lvs = LinuxAdmin::LogicalVolume.scan + lvs[0].volume_group.should be_an_instance_of(LinuxAdmin::VolumeGroup) + lvs[0].volume_group.name.should == 'vg_foobar' + lvs[1].volume_group.should be_an_instance_of(LinuxAdmin::VolumeGroup) + lvs[1].volume_group.name.should == 'vg_foobar' + end + end +end diff --git a/spec/physical_volume_spec.rb b/spec/physical_volume_spec.rb new file mode 100644 index 0000000..05d6bab --- /dev/null +++ b/spec/physical_volume_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe LinuxAdmin::PhysicalVolume do + before(:each) do + LinuxAdmin::Distro.stub(:local). + and_return(LinuxAdmin::Distros::Test.new) + + @physical_volumes = < true, + :params => { '-c' => nil}). + and_return(@physical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) # stub out call to vgdisplay + LinuxAdmin::PhysicalVolume.scan + end + + it "returns local physical volumes" do + LinuxAdmin::PhysicalVolume.should_receive(:run).and_return(@physical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) + pvs = LinuxAdmin::PhysicalVolume.scan + + pvs[0].should be_an_instance_of(LinuxAdmin::PhysicalVolume) + pvs[0].device_name.should == '/dev/vda2' + pvs[0].size.should == 24139776 + end + + it "resolves volume group references" do + LinuxAdmin::PhysicalVolume.should_receive(:run).and_return(@physical_volumes) + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) + pvs = LinuxAdmin::PhysicalVolume.scan + pvs[0].volume_group.should be_an_instance_of(LinuxAdmin::VolumeGroup) + pvs[0].volume_group.name.should == 'vg_foobar' + end + end +end diff --git a/spec/volume_group_spec.rb b/spec/volume_group_spec.rb new file mode 100644 index 0000000..dce8f06 --- /dev/null +++ b/spec/volume_group_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe LinuxAdmin::VolumeGroup do + before(:each) do + LinuxAdmin::Distro.stub(:local). + and_return(LinuxAdmin::Distros::Test.new) + + @groups = < true, + :params => { '-c' => nil}). + and_return(@groups) + LinuxAdmin::VolumeGroup.scan + end + + it "returns local volume groups" do + LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) + vgs = LinuxAdmin::VolumeGroup.scan + + vgs[0].should be_an_instance_of(LinuxAdmin::VolumeGroup) + vgs[0].name.should == 'vg_foobar' + end + end +end