Class: Bosh::Agent::DiskUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/disk_util.rb

Constant Summary collapse

GUARD_RETRIES =
600
GUARD_SLEEP =
1

Class Method Summary collapse

Class Method Details

.base_dirObject



8
9
10
# File 'lib/bosh_agent/disk_util.rb', line 8

def base_dir
  Bosh::Agent::Config.base_dir
end

.ensure_no_partition?(disk, partition) ⇒ Boolean

Pay a penalty on this check the first time a persistent disk is added to a system

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bosh_agent/disk_util.rb', line 43

def ensure_no_partition?(disk, partition)
  check_count = 2
  check_count.times do
    if sfdisk_lookup_partition(disk, partition).empty?
      # keep on trying
    else
      if File.blockdev?(partition)
        return false # break early if partition is there
      end
    end
    sleep 1
  end

  # Double check that the /dev entry is there
  if File.blockdev?(partition)
    return false
  else
    return true
  end
end

.get_usageObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bosh_agent/disk_util.rb', line 68

def get_usage
  usage = {
    system: fs_usage_safe('/')
  }
  ephemeral_percent = fs_usage_safe(File.join(base_dir, 'data'))
  usage[:ephemeral] = ephemeral_percent if ephemeral_percent
  persistent_percent = fs_usage_safe(File.join(base_dir, 'store'))
  usage[:persistent] = persistent_percent if persistent_percent

  usage
end

.loggerObject



4
5
6
# File 'lib/bosh_agent/disk_util.rb', line 4

def logger
  Bosh::Agent::Config.logger
end

.mount_entry(partition) ⇒ Object



12
13
14
# File 'lib/bosh_agent/disk_util.rb', line 12

def mount_entry(partition)
  File.read('/proc/mounts').lines.select { |l| l.match(/#{partition}/) }.first
end

.sfdisk_lookup_partition(disk, partition) ⇒ Object



64
65
66
# File 'lib/bosh_agent/disk_util.rb', line 64

def sfdisk_lookup_partition(disk, partition)
  `sfdisk -Llq #{disk}`.lines.select { |l| l.match(%q{/\A#{partition}.*83.*Linux}) }
end

.umount_guard(mountpoint) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bosh_agent/disk_util.rb', line 19

def umount_guard(mountpoint)
  umount_attempts = GUARD_RETRIES

  loop do
    umount_output = `umount #{mountpoint} 2>&1`

    if $?.exitstatus == 0
      break
    elsif umount_attempts != 0 && umount_output =~ /device is busy/
      #when umount2 syscall fails and errno == EBUSY, umount.c outputs:
      # "umount: %s: device is busy.\n"
      sleep GUARD_SLEEP
      umount_attempts -= 1
    else
      raise Bosh::Agent::MessageHandlerError,
            "Failed to umount #{mountpoint}: #{umount_output}"
    end
  end

  attempts = GUARD_RETRIES - umount_attempts
  logger.info("umount_guard #{mountpoint} succeeded (#{attempts})")
end