Class: Lxc::VirtualDevice

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/elecksee/storage/virtual_device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#command, #detect_home, #log, #run_command, #sudo

Constructor Details

#initialize(name, args = {}) ⇒ VirtualDevice

Returns a new instance of VirtualDevice.



15
16
17
18
19
20
21
22
23
# File 'lib/elecksee/storage/virtual_device.rb', line 15

def initialize(name, args={})
  @name = name
  @tmp_dir = args[:tmp_dir] || '/tmp/lxc/ephemerals'
  @size = args[:size] || 2000
  @fs_type = args[:fs_type] || 'ext4'
  @tmp_fs = !!args[:tmp_fs]
  @fs_type = 'tmpfs' if @tmp_fs
  create
end

Instance Attribute Details

#fs_typeObject (readonly)

Returns the value of attribute fs_type.



13
14
15
# File 'lib/elecksee/storage/virtual_device.rb', line 13

def fs_type
  @fs_type
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/elecksee/storage/virtual_device.rb', line 9

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/elecksee/storage/virtual_device.rb', line 11

def size
  @size
end

#tmp_dirObject (readonly)

Returns the value of attribute tmp_dir.



10
11
12
# File 'lib/elecksee/storage/virtual_device.rb', line 10

def tmp_dir
  @tmp_dir
end

#tmp_fsObject (readonly)

Returns the value of attribute tmp_fs.



12
13
14
# File 'lib/elecksee/storage/virtual_device.rb', line 12

def tmp_fs
  @tmp_fs
end

Instance Method Details

#createObject



34
35
36
37
38
39
40
# File 'lib/elecksee/storage/virtual_device.rb', line 34

def create
  make_directories!
  unless(tmp_fs)
    command("dd if=/dev/zero of=#{@device_path} bs=1k seek=#{sive}k count=1 > /dev/null")
    command("echo \"y\" | mkfs -t #{fs_type} #{size} > /dev/null")
  end
end

#destroyObject



60
61
62
63
64
65
# File 'lib/elecksee/storage/virtual_device.rb', line 60

def destroy
  unmount
  File.delete(device_path) if File.file?(device_path)
  FileUtils.rm_rf(device_path) if File.directory?(device_path)
  FileUtils.rmdir(mount_path) if File.directory?(mount_path)
end

#device_pathObject



25
26
27
# File 'lib/elecksee/storage/virtual_device.rb', line 25

def device_path
  tmp_fs ? 'none' : File.join(tmp_dir, 'virt-imgs', name)
end

#mountObject



46
47
48
49
50
51
# File 'lib/elecksee/storage/virtual_device.rb', line 46

def mount
  unless(mounted?)
    command("mount -t #{fs_type}#{mount_options} #{device_path} #{mount_path}", :sudo => true)
    true
  end
end

#mount_pathObject Also known as: target_path



29
30
31
# File 'lib/elecksee/storage/virtual_device.rb', line 29

def mount_path
  File.join(tmp_dir, 'virt-mnts', name)
end

#mounted?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/elecksee/storage/virtual_device.rb', line 42

def mounted?
  command("mount").stdout.include?(mount_path)
end

#unmountObject



53
54
55
56
57
58
# File 'lib/elecksee/storage/virtual_device.rb', line 53

def unmount
  if(mounted?)
    command("umount #{mount_path}", :sudo => true)
    true
  end
end