Class: Bosh::WardenCloud::DiskUtils

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cloud/warden/diskutils.rb

Constant Summary collapse

UMOUNT_GUARD_RETRIES =
60
UMOUNT_GUARD_SLEEP =
3

Constants included from Helpers

Helpers::DEFAULT_SETTINGS_FILE

Instance Method Summary collapse

Methods included from Helpers

#agent_settings_file, #cloud_error, #generate_agent_env, #get_agent_env, #set_agent_env, #sh, #start_agent, #sudo, #uuid, #with_warden

Constructor Details

#initialize(disk_root, stemcell_root, fs_type) ⇒ DiskUtils

Returns a new instance of DiskUtils.



9
10
11
12
13
14
15
16
17
# File 'lib/cloud/warden/diskutils.rb', line 9

def initialize(disk_root, stemcell_root, fs_type)
  @disk_root = disk_root
  @fs_type = fs_type
  @stemcell_root = stemcell_root

  unless Dir.exist?(disk_root)
    FileUtils.mkdir_p(disk_root)
  end
end

Instance Method Details

#create_disk(disk_id, size) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloud/warden/diskutils.rb', line 19

def create_disk(disk_id, size)
  raise ArgumentError, 'disk size <= 0' unless size > 0

  image_file = image_path(disk_id)
  FileUtils.touch(image_file)
  File.truncate(image_file, size << 20) # 1 MB == 1<<20 Byte
  sh "/sbin/mkfs -t #{@fs_type} -F #{image_file} 2>&1"

rescue => e
  if image_file
    FileUtils.rm_f image_file if File.exist?(image_file)
  end
  raise e
end

#delete_disk(disk_id) ⇒ Object



34
35
36
# File 'lib/cloud/warden/diskutils.rb', line 34

def delete_disk(disk_id)
  FileUtils.rm_f image_path(disk_id)
end

#disk_exist?(disk_id) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/cloud/warden/diskutils.rb', line 38

def disk_exist?(disk_id)
  File.exist?(image_path(disk_id))
end

#image_path(disk_id) ⇒ Object



72
73
74
# File 'lib/cloud/warden/diskutils.rb', line 72

def image_path(disk_id)
  File.join(@disk_root, "#{disk_id}.img")
end

#mount_disk(path, disk_id) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cloud/warden/diskutils.rb', line 42

def mount_disk(path, disk_id)
  unless Dir.exist?(path)
    FileUtils.mkdir_p(path)
  end

  disk_img = image_path(disk_id)
  sudo "mount #{disk_img} #{path} -o loop"
end

#stemcell_delete(stemcell_id) ⇒ Object



67
68
69
70
# File 'lib/cloud/warden/diskutils.rb', line 67

def stemcell_delete(stemcell_id)
  stemcell_dir = stemcell_path(stemcell_id)
  sudo "rm -rf #{stemcell_dir}"
end

#stemcell_path(stemcell_id) ⇒ Object



76
77
78
# File 'lib/cloud/warden/diskutils.rb', line 76

def stemcell_path(stemcell_id)
  File.join(@stemcell_root, stemcell_id)
end

#stemcell_unpack(image_path, stemcell_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cloud/warden/diskutils.rb', line 55

def stemcell_unpack(image_path, stemcell_id)
  stemcell_dir = stemcell_path(stemcell_id)
  unless Dir.exist?(stemcell_dir)
    FileUtils.mkdir_p(stemcell_dir)
  end
  raise "#{image_path} not exist for creating stemcell" unless File.exist?(image_path)
  sudo "tar -C #{stemcell_dir} -xzf #{image_path} 2>&1"
rescue => e
  sudo "rm -rf #{stemcell_dir}"
  raise e
end

#umount_disk(path) ⇒ Object



51
52
53
# File 'lib/cloud/warden/diskutils.rb', line 51

def umount_disk(path)
  umount_guard path
end