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
30 changes: 27 additions & 3 deletions lib/linux_admin/logical_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,34 @@ def extend_with(vg)
self
end

def self.create(name, vg, size)
private

def self.bytes_to_string(bytes)
if bytes > 1.gigabytes
(bytes / 1.gigabytes).to_s + "G"
elsif bytes > 1.megabytes
(bytes / 1.megabytes).to_s + "M"
elsif bytes > 1.kilobytes
(bytes / 1.kilobytes).to_s + "K"
else
bytes.to_s
end
end

public

def self.create(name, vg, value)
self.scan # initialize local logical volumes
run!(cmd(:lvcreate),
:params => { '-n' => name, nil => vg.name, '-L' => size})
params = { '-n' => name, nil => vg.name}
size = nil
if value <= 100
# size = # TODO size from extents
params.merge!({'-l' => "#{value}%FREE"})
else
size = value
params.merge!({'-L' => bytes_to_string(size)})
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@movitto Does it make sense to simplify this conditional by assuming if the 3rd argument is > 100, the number is a size, otherwise it's a percent?

def self.create(name, vg, value)
  ...
  if value <= 100
    params.merge!({'-l' => "#{value}%FREE"})
  else
    params.merge!({'-L' => bytes_to_string(value)})
  end
  ...
end

run!(cmd(:lvcreate), :params => params)
lv = LogicalVolume.new :name => name,
:volume_group => vg,
:sectors => size
Expand Down
30 changes: 27 additions & 3 deletions spec/logical_volume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,45 @@
:params => { '-n' => 'lv',
nil => 'vg',
'-L' => '256G' })
described_class.create 'lv', @vg, '256G'
described_class.create 'lv', @vg, 256.gigabytes
end

context "size is specified" do
it "passes -L option to 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, 256.gigabytes
end
end

context "extents is specified" do
it "passes -l option to lvcreate" do
described_class.instance_variable_set(:@lvs, [])
described_class.should_receive(:run!).
with(LinuxAdmin.cmd(:lvcreate),
:params => { '-n' => 'lv',
nil => 'vg',
'-l' => '100%FREE' })
described_class.create 'lv', @vg, 100
end
end

it "returns new logical volume" do
LinuxAdmin::VolumeGroup.stub(:run! => double(:output => ""))
described_class.stub(:run! => double(:output => ""))
lv = described_class.create 'lv', @vg, '256G'
lv = described_class.create 'lv', @vg, 256.gigabytes
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! => double(:output => ""))
described_class.stub(:run! => double(:output => ""))
lv = described_class.create 'lv', @vg, '256G'
lv = described_class.create 'lv', @vg, 256.gigabytes
described_class.scan.should include(lv)
end
end
Expand Down