Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/linux_admin/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ def initialize(args = {})
@path = args[:path]
end

def size
@size ||= begin
size = nil
out = run(cmd(:fdisk),
:return_output => true,
:params => {"-l" => nil})
out.each_line { |l|
if l =~ /Disk #{path}: ([0-9\.]*) ([KMG])B.*/
size = case $2
when 'K' then
$1.to_f.kilobytes
when 'M' then
$1.to_f.megabytes
when 'G' then
$1.to_f.gigabytes
end
break
end
}
size
end
end

def partitions
@partitions ||= begin
partitions = []
Expand Down Expand Up @@ -88,5 +111,16 @@ def create_partition(partition_type, size)
partitions << partition
partition
end

def clear!
@partitions = []

# clear partition table
run(cmd(:dd),
:params => { 'if=' => '/dev/zero', 'of=' => @path,
'bs=' => 512, 'count=' => 1})

self
end
end
end
12 changes: 11 additions & 1 deletion lib/linux_admin/distro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ class RedHat < Distro
:mount => '/bin/mount',
:umount => '/bin/umount',
:shutdown => '/sbin/shutdown',
:mke2fs => '/sbin/mke2fs'}
:mke2fs => '/sbin/mke2fs',
:fdisk => '/sbin/fdisk',
:dd => '/bin/dd',
:vgdisplay => '/sbin/vgdisplay',
:pvdisplay => '/sbin/pvdisplay',
:lvdisplay => '/sbin/lvdisplay',
:lvextend => '/sbin/lvextend',
:vgextend => '/sbin/vgextend',
:lvcreate => '/sbin/lvcreate',
:pvcreate => '/sbin/pvcreate',
:vgcreate => '/sbin/vgcreate'}

def initialize
@id = :redhat
Expand Down
17 changes: 17 additions & 0 deletions lib/linux_admin/logical_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ def initialize(args = {})
@sectors = args[:sectors]
end

def extend_with(vg)
run(cmd(:lvextend),
:params => [self.name, vg.name])
self
end

def self.create(name, vg, size)
self.scan # initialize local logical volumes
run(cmd(:lvcreate),
:params => { '-n' => name, nil => vg.name, '-L' => size})
lv = LogicalVolume.new :name => name,
:volume_group => vg,
:sectors => size
@lvs << lv
lv
end

def self.scan
@lvs ||= begin
vgs = VolumeGroup.scan
Expand Down
19 changes: 19 additions & 0 deletions lib/linux_admin/physical_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ def initialize(args = {})
@size = args[:size]
end

def attach_to(vg)
run(cmd(:vgextend),
:params => [vg.name, @device_name])
self.volume_group = vg
self
end

# specify disk or partition instance to create physical volume on
def self.create(device)
self.scan # initialize local physical volumes
run(cmd(:pvcreate),
:params => { nil => device.path})
pv = PhysicalVolume.new(:device_name => device.path,
:volume_group => nil,
:size => device.size)
@pvs << pv
pv
end

def self.scan
@pvs ||= begin
vgs = VolumeGroup.scan
Expand Down
23 changes: 23 additions & 0 deletions lib/linux_admin/volume_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def initialize(args = {})
@name = args[:name]
end

def attach_to(lv)
run(cmd(:lvextend),
:params => [lv.name, self.name])
self
end

def extend_with(pv)
run(cmd(:vgextend),
:params => [@name, pv.device_name])
pv.volume_group = self
self
end

def self.create(name, pv)
self.scan # initialize local volume groups
run(cmd(:vgcreate),
:params => [name, pv.device_name])
vg = VolumeGroup.new :name => name
pv.volume_group = vg
@vgs << vg
vg
end

def self.scan
@vgs ||= begin
vgs = []
Expand Down
58 changes: 58 additions & 0 deletions spec/disk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@
end
end

describe "#size" do
it "uses fdisk" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.should_receive(:run).
with(disk.cmd(:fdisk),
:return_output => true,
:params => {"-l" => nil}).
and_return("")
disk.size
end

it "returns disk size" do
fdisk = <<eos
Disk /dev/hda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3ddb508b

Device Boot Start End Blocks Id System
1 1259MB 81.8GB 80.5GB primary ntfs
2 81.8GB 162GB 80.5GB primary ext4
3 162GB 163GB 1074MB logical linux-swap(v1)
eos

disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.stub(:run).and_return(fdisk)
disk.size.should == 500.1.gigabytes
end
end

describe "#partitions" do
it "uses parted" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
Expand Down Expand Up @@ -111,4 +143,30 @@
}.should change{@disk.partitions.size}.by(1)
end
end

describe "#clear!" do
it "clears partitions" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.stub(:run).and_return("") # stub out call to cmds
disk.partitions << LinuxAdmin::Partition.new
disk.clear!
disk.partitions.should be_empty
end

it "uses dd to clear partition table" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.should_receive(:run).
with(disk.cmd(:dd),
:params => {'if=' => '/dev/zero', 'of=' => '/dev/hda',
'bs=' => 512, 'count=' => 1})
disk.clear!
end

it "returns self" do
disk = LinuxAdmin::Disk.new :path => '/dev/hda'
disk.stub(:run) # stub out call to dd
disk.clear!.should == disk
end
end

end
70 changes: 59 additions & 11 deletions spec/logical_volume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

describe LinuxAdmin::LogicalVolume do
before(:each) do
LinuxAdmin::Distro.stub(:local).
and_return(LinuxAdmin::Distros::Test.new)
LinuxAdmin::Distro.stub(:local => LinuxAdmin::Distros::Test.new)

@logical_volumes = <<eos
/dev/vg_foobar/lv_swap:vg_foobar:3:1:-1:2:4128768:63:-1:0:-1:253:0
Expand All @@ -17,40 +16,89 @@

after(:each) do
# reset local copies of volumes / groups
LinuxAdmin::LogicalVolume.instance_variable_set(:@lvs, nil)
described_class.instance_variable_set(:@lvs, nil)
LinuxAdmin::PhysicalVolume.instance_variable_set(:@pvs, nil)
LinuxAdmin::VolumeGroup.instance_variable_set(:@vgs, nil)
end

describe "#extend_with" do
it "uses lvextend" do
lv = described_class.new :name => 'lv'
vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
lv.should_receive(:run).
with(vg.cmd(:lvextend),
:params => ['lv', 'vg'])
lv.extend_with(vg)
end

it "returns self" do
lv = described_class.new :name => 'lv'
vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
lv.stub(:run)
lv.extend_with(vg).should == lv
end
end

describe "#create" do
before(:each) do
@vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
end

it "uses lvcreate" do
described_class.instance_variable_set(:@lvs, [])
described_class.should_receive(:run).
with(LinuxAdmin.cmd(:lvcreate),
:params => { '-n' => 'lv',
nil => 'vg',
'-L' => '256G' })
described_class.create 'lv', @vg, '256G'
end

it "returns new logical volume" do
LinuxAdmin::VolumeGroup.stub(:run => "")
described_class.stub(:run => "")
lv = described_class.create 'lv', @vg, '256G'
lv.should be_an_instance_of(described_class)
lv.name.should == 'lv'
end

it "adds logical volume to local registry" do
LinuxAdmin::VolumeGroup.stub(:run => "")
described_class.stub(:run => "")
lv = described_class.create 'lv', @vg, '256G'
described_class.scan.should include(lv)
end
end

describe "#scan" do
it "uses lvdisplay" do
LinuxAdmin::LogicalVolume.should_receive(:run).
described_class.should_receive(:run).
with(LinuxAdmin.cmd(:lvdisplay),
:return_output => true,
:params => { '-c' => nil}).
and_return(@logical_volumes)
LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups) # stub out call to vgdisplay
LinuxAdmin::LogicalVolume.scan
described_class.scan
end

it "returns local logical volumes" do
LinuxAdmin::LogicalVolume.should_receive(:run).and_return(@logical_volumes)
described_class.should_receive(:run).and_return(@logical_volumes)
LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
lvs = LinuxAdmin::LogicalVolume.scan
lvs = described_class.scan

lvs[0].should be_an_instance_of(LinuxAdmin::LogicalVolume)
lvs[0].should be_an_instance_of(described_class)
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].should be_an_instance_of(described_class)
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)
described_class.should_receive(:run).and_return(@logical_volumes)
LinuxAdmin::VolumeGroup.should_receive(:run).and_return(@groups)
lvs = LinuxAdmin::LogicalVolume.scan
lvs = described_class.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)
Expand Down
Loading