class SyncWrap::LVMCache

Provision a cache using a fast drive (like an SSD) over a slow drive (magnetic or network attached) via LVM's cache-pool mechanism and dm-cache.

Host component dependencies: <Distro>

Attributes

cache_size[RW]

The target size of the cache volume as a string. This may either express a percentage (via -l) or a real size (via -L, ex: '30G') Default: '100%FREE'

lv_cache[W]

The name of the cache volume Default: #lv_cache_target + '_cache'

lv_cache_target[RW]

The target volume name in the same volume group to cache. (required)

meta_size[RW]

The target size of the cache metadata volume as a string. The dm-cache guidelines are 1/1000th of the cache volume size and a minimum of 8M (MiB). Default: '8M'

raw_device[RW]

The fast raw device to use as the cache, i.e. '/dev/xvdb' (required)

vg_instance[RW]

A volume group instance number compatible with the naming used by MDRaid. The cache and cache-meta volumes must all be under the same volume group as the #lv_cache_target volume. Default: 0 -> 'vg0'

vgextend_flags[RW]

Array of additional flags to vgextend Default: ['-y']

Public Class Methods

new( opts = {} ) click to toggle source
Calls superclass method SyncWrap::Component.new
# File lib/syncwrap/components/lvm_cache.rb, line 76
def initialize( opts = {} )
  @meta_size = '8M'
  @cache_size = '100%FREE'
  @vg_instance = 0
  @raw_device = nil
  @lv_cache_target = nil
  @lv_cache = nil
  @vgextend_flags = %w[ -y ]
  super

  raise "LVMCache#raw_device not set" unless raw_device
  raise "LVMCache#lv_cache_target not set" unless lv_cache_target
end

Public Instance Methods

install() click to toggle source
# File lib/syncwrap/components/lvm_cache.rb, line 90
def install
  if distro.is_a?( Debian )
    dist_install( "lvm2", "thin-provisioning-tools", minimal: true )
  else
    dist_install( "lvm2", minimal: true )
  end
  sudo_if( "! lvs /dev/#{vg}/#{lv_cache}" ) do
    unmount_device( raw_device )
    sudo <<-SH
      vgextend #{vgextend_flags.join ' '} #{vg} #{raw_device}
      lvcreate -L #{meta_size} -n #{lv_cache_meta} #{vg} #{raw_device}
      lvcreate #{cache_size_flag} -n #{lv_cache} #{vg} #{raw_device}
      lvconvert --yes --type cache-pool --cachemode writethrough \
                --poolmetadata #{vg}/#{lv_cache_meta} #{vg}/#{lv_cache}
      lvconvert --yes --type cache --cachepool #{vg}/#{lv_cache} \
                #{vg}/#{lv_cache_target}
    SH
  end
end

Protected Instance Methods

cache_size_flag() click to toggle source
# File lib/syncwrap/components/lvm_cache.rb, line 112
def cache_size_flag
  if cache_size =~ /%/
    "-l #{cache_size}"
  else
    "-L #{cache_size}"
  end
end
lv_cache() click to toggle source
# File lib/syncwrap/components/lvm_cache.rb, line 62
def lv_cache
  @lv_cache || ( lv_cache_target + '_cache' )
end
lv_cache_meta() click to toggle source
# File lib/syncwrap/components/lvm_cache.rb, line 66
def lv_cache_meta
  lv_cache + '_meta'
end
vg() click to toggle source
# File lib/syncwrap/components/lvm_cache.rb, line 70
def vg
  "vg#{vg_instance}"
end