Module: SparseImage

Defined in:
lib/version.rb,
lib/vagrant-sparseimage.rb,
lib/vagrant-sparseimage/command.rb,
lib/vagrant-sparseimage/hdiutil.rb

Defined Under Namespace

Modules: Command, HDIUTIL Classes: Config, Destroy, ImageConfig, Mount, Plugin, Unmount

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.destroy(vm) ⇒ Object



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
# File 'lib/vagrant-sparseimage.rb', line 92

def destroy(vm)
  # Prompt to destroy each configured sparse image
  vm.config.sparseimage.to_hash[:images].each do |opts|
    cancel = false
    full_image_filename = "#{opts.image_folder}/#{opts.volume_name}.#{opts.image_type}".downcase
    # Confirm destruction of the sparse image
    while cancel == false
      choice = vm.ui.ask("Do you want to delete the sparse image at #{full_image_filename}? [y/N] ")
      if choice.upcase == 'Y'
        choice = vm.ui.ask("Confirm the name of the volume to destroy [#{ opts.volume_name}] ")
        if choice == opts.volume_name
          # TODO - Test first whether it's mounted before trying to unmount
          #SparseImage::HDIUTIL::unmount(vm, opts.mounted_name)
          SparseImage::HDIUTIL::destroy(vm, full_image_filename)
          cancel = true
        else
          vm.ui.error("name does not match.")
        end
      else
        cancel = true
        vm.ui.error("Image '#{opts.volume_name}' was not destroyed.")
      end
    end
  end
end

.list(vm) ⇒ Object



34
35
36
37
38
# File 'lib/vagrant-sparseimage.rb', line 34

def list(vm)
  vm.config.sparseimage.to_hash[:images].each do |opts|
    pp opts.to_hash
  end
end

.mount(vm) ⇒ Object



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
# File 'lib/vagrant-sparseimage.rb', line 40

def mount(vm)
  # mount each configured sparse image
  vm.config.sparseimage.to_hash[:images].each do |opts|
    # Derive the full image filename and volume mount path (for the host)
    full_image_filename = "#{opts.image_folder}/#{opts.volume_name}.#{opts.image_type}".downcase

    # Does the image need to be created?
    if File.exists? full_image_filename
      vm.ui.info "Found sparse disk image: #{full_image_filename}"
    else
      # Create the directory if it does not exist
      FileUtils.mkdir_p opts.image_folder if not File.exists? opts.image_folder

      # hdiutil is finnicky with image type
      type = opts.image_type == 'SPARSEIMAGE' ? 'SPARSE' : opts.image_type
      vm.ui.info "Creating #{opts.image_size}MB sparse disk image: #{full_image_filename}"
      # TODO - move this into SparseImage::HDIUTIL::create
      # TODO - raise an exception if create fails
      SparseImage::HDIUTIL::create(vm, type, opts.image_size, opts.image_fs, opts.volume_name, full_image_filename)
      vm.ui.info("Created disk image in the host: #{full_image_filename}")
    end

    # Mount the image in the host
    vm.ui.info("Mounting disk image in the host: #{full_image_filename} at #{opts.mounted_name}")
    SparseImage::HDIUTIL::mount(vm, opts.mounted_name, full_image_filename)

    # Remove nonsense hidden files
    errors = SparseImage::HDIUTIL::remove_OSX_fuzz(vm, opts.mounted_name)
    if errors.length > 0
      errors.each do |error| vm.ui.info(error) end
    end

    if opts.vm_mountpoint
      vm.config.vm.synced_folders[opts.volume_name] = {
        :hostpath => opts.mounted_name,
        :guestpath => opts.vm_mountpoint,
        :disabled => false,
      }
      vm.ui.info("Mounted disk image in the guest: #{full_image_filename} at #{opts.vm_mountpoint}")
    end
  end
end

.run(cmd) ⇒ Object

Run the command, wait for exit and return the Process object.



28
29
30
31
32
# File 'lib/vagrant-sparseimage.rb', line 28

def run(cmd)
  pid = Process.fork { exec(cmd) }
  Process.waitpid(pid)
  return $?
end

.unmount(vm) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/vagrant-sparseimage.rb', line 83

def unmount(vm)
  # Unmount each configured sparse image
  vm.config.sparseimage.to_hash[:images].each do |opts|
    if opts.auto_unmount
      SparseImage::HDIUTIL::unmount(vm, opts.mounted_name)
    end
  end
end