Module: VagrantPlugins::PersistentStorage::ManageStorage

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

Instance Method Summary collapse

Instance Method Details

#get_tmp_dir(m) ⇒ Object



8
9
10
11
12
13
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 8

def get_tmp_dir(m)
  m.communicate.sudo('dirname $(mktemp -u)', :error_check => false) do |type, data|
    tmpdir = data.to_s.strip
      return tmpdir
  end
end

#manage_volumes(m) ⇒ Object



152
153
154
155
156
157
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 152

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

#populate_template(m) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 14

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

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

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

    vg_name = 'vps' unless vg_name != ""
    disk_dev = '/dev/sdb' unless disk_dev != ""
    mnt_name = 'vps' unless mnt_name != ""
    mnt_options = ['defaults'] unless mnt_options.any?
    fs_type = 'ext3' unless fs_type != ""
    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 partition == true %>\n      <% 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      <% end %>\n      EOF\n    else\n    ## shell script to format disk, create/manage LVM, mount disk\n  disk_operations_template = ERB.new <<-EOF\n#!/usr/bin/env bash\nDISK_DEV=\#{disk_dev}\n<% if partition == true %>\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,,\#{part_type_code} | sfdisk $DISK_DEV\nelse\n  [ -b ${DISK_DEV}1 ] || echo ,,\#{part_type_code} | sfdisk $DISK_DEV\nfi\necho \"fdisk returned:  $?\" >> disk_operation_log.txt\nDISK_DEV=${DISK_DEV}1\n<% end %>\n\n<% if use_lvm == true %>\n# Create the physical volume if it doesn't already exist:\n[[ `pvs $DISK_DEV` ]] || pvcreate $DISK_DEV\necho \"pvcreate returned:  $?\" >> disk_operation_log.txt\n# Create the volume group if it doesn't already exist:\n[[ `vgs \#{vg_name}` ]] || vgcreate \#{vg_name} $DISK_DEV\necho \"vgcreate returned:  $?\" >> disk_operation_log.txt\n# Create the logical volume if it doesn't already exist:\n[[ `lvs \#{vg_name} | grep \#{mnt_name}` ]] || lvcreate -l 100%FREE -n \#{mnt_name} \#{vg_name}\necho \"lvcreate returned:  $?\" >> disk_operation_log.txt\n# Activate the volume group if it's inactive:\n[[ `lvs \#{vg_name} --noheadings --nameprefixes | grep LVM2_LV_ATTR | grep \"wi\\-a\"` ]] || vgchange \#{vg_name} -a y\necho \"vg activation returned:  $?\" >> disk_operation_log.txt\n<% end %>\n\n<% if format == true  %>\n# Create the filesytem if it doesn't already exist\nMNT_NAME=\#{mnt_name}\n[[ `blkid -t LABEL=${MNT_NAME:0:16} | grep \#{fs_type}` ]] || mkfs.\#{fs_type} -L \#{mnt_name} \#{device}\necho \"\#{fs_type} creation return:  $?\" >> disk_operation_log.txt\n<% end %>\n<% if mount == true %>\n# Create mountpoint \#{mnt_point}\n[ -d \#{mnt_point} ] || mkdir -p \#{mnt_point}\n# Update fstab with new mountpoint name\n[[ `grep -i \#{device} /etc/fstab` ]] || echo \#{device} \#{mnt_point} \#{fs_type} \#{mnt_options.join(',')} 0 0 >> /etc/fstab\necho \"fstab update returned:  $?\" >> disk_operation_log.txt\n# Finally, mount the partition\n[[ `mount | grep \#{mnt_point}` ]] || mount \#{mnt_point}\necho \"\#{mnt_point} mounting returned:  $?\" >> disk_operation_log.txt\n<% end %>\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      tmpdir = get_tmp_dir(m)\n      target_script = tmpdir + \"/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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 137

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

end