Class: Bosh::Stemcell::DiskImage

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/stemcell/disk_image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DiskImage

Returns a new instance of DiskImage.


10
11
12
13
14
15
# File 'lib/bosh/stemcell/disk_image.rb', line 10

def initialize(options)
  @image_file_path   = options.fetch(:image_file_path)
  @image_mount_point = options.fetch(:image_mount_point, Dir.mktmpdir)
  @verbose           = options.fetch(:verbose, false)
  @shell             = Bosh::Core::Shell.new
end

Instance Attribute Details

#image_mount_pointObject (readonly)

Returns the value of attribute image_mount_point


8
9
10
# File 'lib/bosh/stemcell/disk_image.rb', line 8

def image_mount_point
  @image_mount_point
end

Instance Method Details

#mountObject


17
18
19
20
21
22
23
24
25
26
# File 'lib/bosh/stemcell/disk_image.rb', line 17

def mount
  device_path   = stemcell_loopback_device_name
  mount_command = "sudo mount #{device_path} #{image_mount_point}"
  shell.run(mount_command, output_command: verbose)
rescue => e
  raise e unless e.message.include?(mount_command)

  sleep 0.5
  shell.run(mount_command, output_command: verbose)
end

#unmountObject


28
29
30
31
32
# File 'lib/bosh/stemcell/disk_image.rb', line 28

def unmount
  shell.run("sudo umount #{image_mount_point}", output_command: verbose)
ensure
  unmap_image
end

#while_mountedObject


34
35
36
37
38
39
# File 'lib/bosh/stemcell/disk_image.rb', line 34

def while_mounted
  mount
  yield self
ensure
  unmount
end