Module: VagrantPlugins::ExtendedStorage::ManageStorage

Included in:
Action::ManageAll
Defined in:
lib/vagrant-extended-storage/manage_storage.rb

Instance Method Summary collapse

Instance Method Details

#manage_volumes(m) ⇒ Object



122
123
124
125
126
127
# File 'lib/vagrant-extended-storage/manage_storage.rb', line 122

def manage_volumes(m)
  populate_template(m)
  if m.config.extended_storage.manage?
    run_disk_operations(m)
  end
end

#populate_template(m) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vagrant-extended-storage/manage_storage.rb', line 8

def populate_template(m)
  mnt_name = m.config.extended_storage.mountname
  mnt_point = m.config.extended_storage.mountpoint
  mnt_options = m.config.extended_storage.mountoptions
  vg_name = m.config.extended_storage.volgroupname
  disk_dev = m.config.extended_storage.diskdevice
  fs_type = m.config.extended_storage.filesystem
  manage = m.config.extended_storage.manage
  use_lvm = m.config.extended_storage.use_lvm
  mount = m.config.extended_storage.mount
  format = m.config.extended_storage.format

    ## windows filesystem options
    drive_letter = m.config.extended_storage.drive_letter

    if m.config.vm.communicator == :winrm
      os = "windows"
    else
      os = "linux"
    end

  vg_name = 'vps' unless vg_name != 0
  disk_dev = '/dev/sdb' unless disk_dev != 0
  mnt_name = 'vps' unless mnt_name != 0
  mnt_options = ['defaults'] unless mnt_options != 0
  fs_type = 'ext3' unless fs_type != 0
  if use_lvm
    device = "/dev/#{vg_name}/#{mnt_name}"
  else
    device = "#{disk_dev}1"
  end
    if drive_letter == 0
      drive_letter = ""
    else
      drive_letter = "letter=#{drive_letter}"
    end
    
    if os == "windows"
      ## shell script for windows to create NTFS partition and assign drive letter
      disk_operations_template = ERB.new "      <% if format == true %>\n      foreach ($disk in get-wmiobject Win32_DiskDrive -Filter \"Partitions = 0\"){\n        $disk.DeviceID\n        $disk.Index\n        \"select disk \"+$disk.Index+\"`r clean`r create partition primary`r format fs=ntfs unit=65536 quick`r active`r assign \#{drive_letter}\" | diskpart >> disk_operation_log.txt\n      }\n      <% end %>\n      EOF\n    else\n    ## shell script to format disk, create/manage LVM, mount disk\n  disk_operations_template = ERB.new <<-EOF\n#!/bin/bash\n# fdisk the disk if it's not a block device already:\nre='[0-9][.][0-9.]*[0-9.]*'; [[ $(sfdisk --version) =~ $re ]] && version=\"${BASH_REMATCH}\"\nif ! awk -v ver=\"$version\" 'BEGIN { if (ver < 2.26 ) exit 1; }'; then\n  [ -b \#{disk_dev}1 ] || echo 0,,8e | sfdisk \#{disk_dev}\nelse\n  [ -b \#{disk_dev}1 ] || echo ,,8e | sfdisk \#{disk_dev}\nfi\necho \"fdisk returned:  $?\" >> disk_operation_log.txt\n\n<% if use_lvm == true %>\n# Create the physical volume if it doesn't already exist:\n[[ `pvs \#{disk_dev}1` ]] || pvcreate \#{disk_dev}1\necho \"pvcreate returned:  $?\" >> disk_operation_log.txt\n# Extend the volume group :\nvgextend \#{vg_name} \#{disk_dev}1\necho \"vgextend returned:  $?\" >> disk_operation_log.txt\n# Extend LVM:\nlvextend \#{device} \#{disk_dev}1\necho \"lvextend returned:  $?\" >> disk_operation_log.txt\n# Resize on the fly\nresize2fs \#{device}\necho \"resize2fs \#{device} returned: $?\" >> disk_operation_log.txt\n# reserver 5%\ntune2fs -m 5 /dev/mapper/\#{vg_name}-\#{mnt_name}\n<% end %>\n\nexit $?\n  EOF\n    end\n\n  buffer = disk_operations_template.result(binding)\n    tmp_script = Tempfile.new(\"disk_operations_\#{mnt_name}.sh\")\n\n    if os == 'windows'\n      target_script = \"disk_operations_\#{mnt_name}.ps1\"\n    else\n      target_script = \"/tmp/disk_operations_\#{mnt_name}.sh\"\n    end\n\n  File.open(\"\#{tmp_script.path}\", 'wb') do |f|\n      f.write buffer\n  end\n  m.communicate.upload(tmp_script.path, target_script)\n    unless os == 'windows'\n      m.communicate.sudo(\"chmod 755 \#{target_script}\")\n    end\nend\n"

#run_disk_operations(m) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vagrant-extended-storage/manage_storage.rb', line 108

def run_disk_operations(m)
  return unless m.communicate.ready?
  mnt_name = m.config.extended_storage.mountname
  mnt_name = 'vps' unless mnt_name != 0
    if m.config.vm.communicator == :winrm
      target_script = "disk_operations_#{mnt_name}.ps1"
      m.communicate.sudo("powershell -executionpolicy bypass -file #{target_script}")
    else
      target_script = "/tmp/disk_operations_#{mnt_name}.sh"
      m.communicate.sudo("#{target_script}")
    end

end