Class: LVM::LogicalVolume

Inherits:
Object
  • Object
show all
Defined in:
lib/lvm/logical_volume.rb

Overview

This class represents an LVM logical volume, in all its glory. You can perform various operations on it.

Instance Method Summary collapse

Constructor Details

#initialize(path_or_vg_name, lv_name = nil) ⇒ LogicalVolume

Create a new instance of LVM::LogicalVolume.

New instances can be created in one of two ways:

* Pass a single argument, containing any path which LVM can resolve to
  a logical volume.  Typically, this will either be `/dev/<vg>/<lv>`
  or `/dev/mapper/<vg>-<lv>`, but we don't try to parse it ourselves,
  relying instead on `lvs` to do the heavy lifting.

* Pass two arguments, which are the volume group name and logical
  volume name, respectively.

This method will raise ‘RuntimeError` if the path specified can’t be resolved to an LV, or if the specified VG name or LV name don’t resolve to an active logical volume.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lvm/logical_volume.rb', line 22

def initialize(path_or_vg_name, lv_name=nil)
  if lv_name.nil?
    path = path_or_vg_name
    @vg_name, @lv_name = `lvs --noheadings -o vg_name,lv_name #{path} 2>/dev/null`.strip.split(/\s+/, 2)
    if $?.exitstatus != 0
      raise RuntimeError,
            "Failed to interrogate LVM about '#{path}'.  Perhaps you misspelt it?"
    end
  else
    @vg_name = path_or_vg_name
    @lv_name = lv_name
  end

  @vgcfg = LVM::VGConfig.new(@vg_name)
  @lvcfg = @vgcfg.logical_volumes[@lv_name]

  if @lvcfg.nil?
    raise RuntimeError,
          "Logical volume #{@lv_name} does not exist in volume group #{@vg_name}"
  end
end

Instance Method Details

#changesObject

Return an array of ranges, each of which represents an inclusive range of bytes which are different between this logical volume and its origin.

If this LV is not a snapshot, this method returns an empty array.



74
75
76
77
78
79
80
81
82
# File 'lib/lvm/logical_volume.rb', line 74

def changes
  return [] unless snapshot?

  if @lvcfg.thin?
    LVM::ThinSnapshot.new(@vg_name, @lv_name)
  else
    LVM::Snapshot.new(@vg_name, @lv_name)
  end.differences
end

#originObject

Return an LVM::LogicalVolume object which is the origin volume of this one (if this LV is a snapshot), or ‘nil` otherwise.



57
58
59
60
61
62
63
64
65
66
# File 'lib/lvm/logical_volume.rb', line 57

def origin
  return nil unless snapshot?

  if @lvcfg.origin
    LVM::LogicalVolume.new(@vg_name, @lvcfg.origin)
  else
    origin_lv_name = @vgcfg.logical_volumes.values.find { |lv| lv.cow_store == @lv_name }.origin
    LVM::LogicalVolume.new(@vg_name, origin_lv_name)
  end
end

#pathObject

Return a string containing a canonical path to the block device representing this LV.



46
47
48
# File 'lib/lvm/logical_volume.rb', line 46

def path
  "/dev/mapper/#{@vg_name.gsub('-', '--')}-#{@lv_name.gsub('-', '--')}"
end

#snapshot?Boolean

Is this LV a snapshot?

Returns:

  • (Boolean)


51
52
53
# File 'lib/lvm/logical_volume.rb', line 51

def snapshot?
  @lvcfg.snapshot?
end