Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kusnier committed Nov 3, 2012
0 parents commit fcb0e14
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in vagrant-persistent-storage.gemspec
gemspec
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Vagrant::Persistent-Storage


A Vagrant plugin that creates a persistent storage and attaches it to guest machine.

## Installation

gem 'vagrant-persistent-storage'

And then execute:

$ bundle

Or install it yourself as:

$ gem install vagrant-persistent-storage

## Usage

After installing you can set the location and size of the persistent storage.

The following options will create a persistent storage with 5000 MB:
```ruby
config.persistent_storage.location = "~/development/sourcehdd.vdi"
config.persistent_storage.size = 5000
```

Every `vagrant up` will attach this file as hard disk to the guest machine.
An `vagrant destory` will detach the storage to avoid deletion of the storage by vagrant.
A `vagrant destory` generally destroys all attached drives. See [VBoxMange unregistervm --delete option][vboxmanage_delete].

## TODO

* There's Always Something to Do


[vboxmanage_delete]: http://www.virtualbox.org/manual/ch08.html#vboxmanage-registervm "VBoxManage registervm / unregistervm"
12 changes: 12 additions & 0 deletions lib/vagrant-persistent-storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'vagrant'
require 'vagrant/action/builder'
require 'vagrant-persistent-storage/config'
require 'vagrant-persistent-storage/middleware'
require 'vagrant-persistent-storage/version'

Vagrant.config_keys.register(:persistent_storage) { VagrantPersistentStorage::Config }
Vagrant.actions[:start].insert_after(Vagrant::Action::VM::ShareFolders, VagrantPersistentStorage::AttachPersistentStorage)
Vagrant.actions[:destroy].insert_after(Vagrant::Action::VM::PruneNFSExports, VagrantPersistentStorage::DetachPersistentStorage)

# Add our custom translations to the load path
#I18n.load_path << File.expand_path("../../locales/en.yml", __FILE__)
7 changes: 7 additions & 0 deletions lib/vagrant-persistent-storage/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module VagrantPersistentStorage
class Config < Vagrant::Config::Base
attr_accessor :location
attr_accessor :size
end
end

3 changes: 3 additions & 0 deletions lib/vagrant-persistent-storage/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir[File.dirname(__FILE__) + '/middleware/**/*.rb'].each do |file|
require file
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module VagrantPersistentStorage
class AttachPersistentStorage
def initialize(app, env)
@app = app
@env = env
@vm = env[:vm]
end

def call(env)
options = @vm.config.persistent_storage
if !options.location ^ !options.size
env[:ui].error "Attach Persistent Storage failed. Location and size must be filled out."
else

if !File.exists?(options.location)
@vm.config.vm.customize ["createhd", "--filename", options.location, "--size", options.size]
env[:ui].success "Create Persistent Storage."
end
@vm.config.vm.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", 1, "--type", "hdd", "--medium", options.location]

env[:ui].info "Attach Persistent Storage #{options.location} (Size: #{options.size}MB)"
end

@app.call(env)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module VagrantPersistentStorage
class DetachPersistentStorage
def initialize(app, env)
@app = app
@env = env
@vm = env[:vm]
end

def call(env)
options = @vm.config.persistent_storage
if options.location and read_persistent_storage() == options.location
@vm.driver.execute("storageattach", @vm.uuid, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "none")
env[:ui].info "Detach Persistent Storage #{options.location} (Size: #{options.size}MB)"
end

@app.call(env)
end

def read_persistent_storage
info = @vm.driver.execute("showvminfo", @vm.uuid, "--machinereadable", :retryable => true)
info.split("\n").each do |line|
return $1.to_s if line =~ /^"SATA Controller-1-0"="(.+?)"$/
end

nil
end

end
end
3 changes: 3 additions & 0 deletions lib/vagrant-persistent-storage/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module VagrantPersistentStorage
VERSION = "0.0.1"
end
4 changes: 4 additions & 0 deletions lib/vagrant_init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is automatically loaded by Vagrant to load any
# plugins. This file kicks off this plugin.

require 'vagrant-persistent-storage'
24 changes: 24 additions & 0 deletions vagrant-persistent-storage.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'vagrant-persistent-storage/version'

Gem::Specification.new do |gem|
gem.name = "vagrant-persistent-storage"
gem.version = VagrantPersistentStorage::VERSION
gem.authors = ["Sebastian Kusnier"]
gem.email = ["sebastian@kusnier.net"]
gem.description = "A Vagrant plugin that creates a persistent storage and attaches it to guest machine."
gem.summary = gem.description
gem.homepage = "https://github.com/kusnier/vagrant-persistent-storage"

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency 'vagrant'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
end

0 comments on commit fcb0e14

Please sign in to comment.