Module: VagrantPlugins::PersistentStorage::ManageStorage

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

Instance Method Summary collapse

Instance Method Details

#manage_volumes(m) ⇒ Object



89
90
91
92
93
94
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 89

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

#populate_template(m) ⇒ Object



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
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 9

def populate_template(m)
  mnt_name = m.config.persistent_storage.mountname
  mnt_point = m.config.persistent_storage.mountpoint
  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
  mount = m.config.persistent_storage.mount
  format = m.config.persistent_storage.format

  vg_name = 'vps' unless vg_name != 0
  disk_dev = '/dev/sdb' unless disk_dev != 0
  mnt_name = 'vps' unless mnt_name != 0
  fs_type = 'ext3' unless fs_type != 0
  if use_lvm
    device = "/dev/#{vg_name}-vg1/#{mnt_name}"
  else
    device = "#{disk_dev}1"
  end
  
  ## shell script to format disk, create/manage LVM, mount disk
  disk_operations_template = ERB.new <<-EOF
#!/bin/bash
# fdisk the disk if it's not a block device already:
[ -b #{disk_dev}1 ] || echo 0,,8e | sfdisk #{disk_dev}
echo "fdisk returned:  $?" >> disk_operation_log.txt

<% if use_lvm == true %>
# Create the physical volume if it doesn't already exist:
[[ `pvs #{disk_dev}1` ]] || pvcreate #{disk_dev}1
echo "pvcreate returned:  $?" >> disk_operation_log.txt
# Create the volume group if it doesn't already exist:
[[ `vgs #{vg_name}-vg1` ]] || vgcreate #{vg_name}-vg1 #{disk_dev}1
echo "vgcreate returned:  $?" >> disk_operation_log.txt
# Create the logical volume if it doesn't already exist:
[[ `lvs #{vg_name}-vg1 | grep #{mnt_name}` ]] || lvcreate -l 100%FREE -n #{mnt_name} #{vg_name}-vg1
echo "lvcreate returned:  $?" >> disk_operation_log.txt
# Activate the volume group if it's inactive:
[[ `lvs #{vg_name}-vg1 --noheadings --nameprefixes | grep LVM2_LV_ATTR | grep "wi\-a"` ]] || vgchange #{vg_name}-vg1 -a y
echo "vg activation returned:  $?" >> disk_operation_log.txt
<% end %>

<% if format == true  %>
# Create the filesytem if it doesn't already exist
[[ `blkid | grep #{mnt_name} | grep #{fs_type}` ]] || mkfs.#{fs_type} #{device}
echo "#{fs_type} creation return:  $?" >> disk_operation_log.txt
<% if mount == true %>
# Create mountpoint #{mnt_point}
[ -d #{mnt_point} ] || mkdir -p #{mnt_point}
# Update fstab with new mountpoint name
[[ `grep -i #{device} /etc/fstab` ]] || echo #{device} #{mnt_point} #{fs_type} defaults 0 0 >> /etc/fstab
echo "fstab update returned:  $?" >> disk_operation_log.txt
# Finally, mount the partition
[[ `mount | grep #{mnt_point}` ]] || mount #{mnt_point}
echo "#{mnt_point} mounting returned:  $?" >> disk_operation_log.txt
<% end %>
<% end %>
exit $?
  EOF

  buffer = disk_operations_template.result(binding)
  tmp_script = "/tmp/disk_operations_#{mnt_name}.sh"
  target_script = "/tmp/disk_operations_#{mnt_name}.sh"

  File.open("#{tmp_script}", 'wb') do |f|
      f.write buffer
  end
  m.communicate.upload(tmp_script, target_script)
  m.communicate.sudo("chmod 755 #{target_script}")
end

#run_disk_operations(m) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/vagrant-persistent-storage/manage_storage.rb', line 81

def run_disk_operations(m)
  return unless m.communicate.ready?
  mnt_name = m.config.persistent_storage.mountname
  mnt_name = 'vps' unless mnt_name != 0
  target_script = "/tmp/disk_operations_#{mnt_name}.sh"
  m.communicate.sudo("#{target_script}")
end