Class: Bosh::Agent::Message::MountDisk

Inherits:
Base show all
Defined in:
lib/bosh_agent/message/mount_disk.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#base_dir, #handler_error, #logger, #logs_dir, #settings, #store_migration_target, #store_path

Constructor Details

#initialize(args) ⇒ MountDisk

Returns a new instance of MountDisk.



11
12
13
# File 'lib/bosh_agent/message/mount_disk.rb', line 11

def initialize(args)
  @cid = args.first
end

Class Method Details

.long_running?Boolean

Returns:

  • (Boolean)


98
# File 'lib/bosh_agent/message/mount_disk.rb', line 98

def self.long_running?; true; end

.process(args) ⇒ Object



7
8
9
# File 'lib/bosh_agent/message/mount_disk.rb', line 7

def self.process(args)
  new(args).mount
end

Instance Method Details

#mountObject



15
16
17
18
19
20
21
22
# File 'lib/bosh_agent/message/mount_disk.rb', line 15

def mount
  if Bosh::Agent::Config.configure
    update_settings
    logger.info("MountDisk: #{@cid} - #{settings['disks'].inspect}")

    setup_disk
  end
end

#mount_persistent_disk(partition) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bosh_agent/message/mount_disk.rb', line 81

def mount_persistent_disk(partition)
  store_mountpoint = File.join(base_dir, 'store')

  if Pathname.new(store_mountpoint).mountpoint?
    logger.info("Mounting persistent disk store migration target")
    mountpoint = File.join(base_dir, 'store_migraton_target')
  else
    logger.info("Mounting persistent disk store")
    mountpoint = store_mountpoint
  end

  FileUtils.mkdir_p(mountpoint)
  FileUtils.chmod(0700, mountpoint)

  Mounter.new(logger).mount(partition, mountpoint)
end

#setup_diskObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bosh_agent/message/mount_disk.rb', line 28

def setup_disk
  disk = Bosh::Agent::Config.platform.lookup_disk_by_cid(@cid)
  partition = "#{disk}1"

  logger.info("setup disk settings: #{settings.inspect}")

  read_disk_attempts = 300
  read_disk_attempts.downto(0) do |n|
    begin
      # Parition table is blank
      disk_data = File.read(disk, 512)

      if disk_data == "\x00"*512
        logger.info("Found blank disk #{disk}")
      else
        logger.info("Disk has partition table")
        logger.info(`sfdisk -Llq #{disk} 2> /dev/null`)
      end
      break
    rescue => e
      # Do nothing - we'll retry
      logger.info("Re-trying reading from #{disk}")
    end

    if n == 0
      raise Bosh::Agent::MessageHandlerError, "Unable to read from new disk"
    end
    sleep 1
  end

  if File.blockdev?(disk) && DiskUtil.ensure_no_partition?(disk, partition)
    full_disk = ",,L\n"
    logger.info("Partitioning #{disk}")

    Bosh::Agent::Util.partition_disk(disk, full_disk)

    mke2fs_options = ["-t ext4", "-j"]
    mke2fs_options << "-E lazy_itable_init=1" if Bosh::Agent::Util.lazy_itable_init_enabled?
    `/sbin/mke2fs #{mke2fs_options.join(" ")} #{partition}`
    unless $?.exitstatus == 0
      raise Bosh::Agent::MessageHandlerError, "Failed create file system (#{$?.exitstatus})"
    end
  elsif File.blockdev?(partition)
    logger.info("Found existing partition on #{disk}")
    # Do nothing
  else
    raise Bosh::Agent::MessageHandlerError, "Unable to format #{disk}"
  end

  mount_persistent_disk(partition)
  {}
end

#update_settingsObject



24
25
26
# File 'lib/bosh_agent/message/mount_disk.rb', line 24

def update_settings
  Bosh::Agent::Config.settings = Bosh::Agent::Settings.load
end