Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit fcb0e14066095f3422fda03a329e76becca62f16 0 parents
@kusnier authored
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in vagrant-persistent-storage.gemspec
+gemspec
37 README.md
@@ -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 lib/vagrant-persistent-storage.rb
@@ -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 lib/vagrant-persistent-storage/config.rb
@@ -0,0 +1,7 @@
+module VagrantPersistentStorage
+ class Config < Vagrant::Config::Base
+ attr_accessor :location
+ attr_accessor :size
+ end
+end
+
3  lib/vagrant-persistent-storage/middleware.rb
@@ -0,0 +1,3 @@
+Dir[File.dirname(__FILE__) + '/middleware/**/*.rb'].each do |file|
+ require file
+end
27 lib/vagrant-persistent-storage/middleware/attachpersistentstorage.rb
@@ -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
29 lib/vagrant-persistent-storage/middleware/detachpersistentstorage.rb
@@ -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  lib/vagrant-persistent-storage/version.rb
@@ -0,0 +1,3 @@
+module VagrantPersistentStorage
+ VERSION = "0.0.1"
+end
4 lib/vagrant_init.rb
@@ -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 vagrant-persistent-storage.gemspec
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.