Skip to content

Commit

Permalink
Make all the VirtualBox middleware aware of synced folders
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jan 23, 2013
1 parent 29ca6bc commit 9cb8670
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
17 changes: 7 additions & 10 deletions plugins/kernel_v2/config/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ def merge(other)
def synced_folder(hostpath, guestpath, options=nil)
options ||= {}
options[:id] ||= guestpah
options[:guestpath] = guestpath
options[:hostpath] = hostpath

@synced_folders[options[:id]] = {
:guestpath => guestpath,
:hostpath => hostpath,
:options => options
}
@synced_folders[options[:id]] = options
end

# Define a way to access the machine via a network. This exposes a
Expand Down Expand Up @@ -143,19 +141,18 @@ def validate(machine)
errors << I18n.t("vagrant.config.vm.box_not_found", :name => box) if \
box && !box_url && !machine.box

@synced_folders.each do |id, data|
options = data[:options]
hostpath = Pathname.new(data[:hostpath]).expand_path(machine.env.root_path)
@synced_folders.each do |id, options|
hostpath = Pathname.new(options[:hostpath]).expand_path(machine.env.root_path)

if !hostpath.directory? && !options[:create]
errors << I18n.t("vagrant.config.vm.shared_folder_hostpath_missing",
:path => data[:hostpath])
:path => options[:hostpath])
end

if options[:nfs] && (options[:owner] || options[:group])
# Owner/group don't work with NFS
errors << I18n.t("vagrant.config.vm.shared_folder_nfs_owner_group",
:path => data[:hostpath])
:path => options[:hostpath])
end
end

Expand Down
12 changes: 6 additions & 6 deletions plugins/providers/virtualbox/action/nfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def folders
def extract_folders
# Load the NFS enabled shared folders
@folders = {}
@env[:machine].config.vm.shared_folders.each do |key, opts|
@env[:machine].config.vm.synced_folders.each do |id, opts|
if opts[:nfs]
# Duplicate the options, set the hostpath, and set disabled on the original
# options so the ShareFolders middleware doesn't try to mount it.
Expand All @@ -65,7 +65,7 @@ def extract_folders
folder[:hostpath] = hostpath.to_s

# Assign the folder to our instance variable for later use
@folders[key] = folder
@folders[id] = folder

# Disable the folder so that regular shared folders don't try to
# mount it.
Expand All @@ -78,7 +78,7 @@ def extract_folders
# options on the NFS folders.
def prepare_folders
@folders = @folders.inject({}) do |acc, data|
key, opts = data
id, opts = data
opts[:map_uid] = prepare_permission(:uid, opts)
opts[:map_gid] = prepare_permission(:gid, opts)
opts[:nfs_version] ||= 3
Expand All @@ -89,7 +89,7 @@ def prepare_folders
# the same host path will hash to the same fsid.
opts[:uuid] = Digest::MD5.hexdigest(opts[:hostpath])

acc[key] = opts
acc[id] = opts
acc
end
end
Expand Down Expand Up @@ -166,8 +166,8 @@ def guest_ip

# Checks if there are any NFS enabled shared folders.
def nfs_enabled?
@env[:machine].config.vm.shared_folders.each do |key, opts|
return true if opts[:nfs]
@env[:machine].config.vm.synced_folders.each do |id, options|
return true if options[:nfs]
end

false
Expand Down
30 changes: 14 additions & 16 deletions plugins/providers/virtualbox/action/share_folders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@ def call(env)
# This method returns an actual list of VirtualBox shared
# folders to create and their proper path.
def shared_folders
@env[:machine].config.vm.shared_folders.inject({}) do |acc, data|
key, value = data
{}.tap do |result|
@env[:machine].config.vm.synced_folders.each do |id, data|
next if data[:disabled]

next acc if value[:disabled]

# This to prevent overwriting the actual shared folders data
value = value.dup
acc[key] = value
acc
# This to prevent overwriting the actual shared folders data
result[id] = data.dup
end
end
end

# Prepares the shared folders by verifying they exist and creating them
# if they don't.
def prepare_folders
shared_folders.each do |name, options|
shared_folders.each do |id, options|
hostpath = Pathname.new(options[:hostpath]).expand_path(@env[:root_path])

if !hostpath.directory? && options[:create]
Expand All @@ -61,9 +59,9 @@ def create_metadata
@env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating")

folders = []
shared_folders.each do |name, data|
shared_folders.each do |id, data|
folders << {
:name => name,
:name => id,
:hostpath => File.expand_path(data[:hostpath], @env[:root_path]),
:transient => data[:transient]
}
Expand All @@ -76,7 +74,7 @@ def mount_shared_folders
@env[:ui].info I18n.t("vagrant.actions.vm.share_folders.mounting")

# short guestpaths first, so we don't step on ourselves
folders = shared_folders.sort_by do |name, data|
folders = shared_folders.sort_by do |id, data|
if data[:guestpath]
data[:guestpath].length
else
Expand All @@ -86,11 +84,11 @@ def mount_shared_folders
end

# Go through each folder and mount
folders.each do |name, data|
folders.each do |id, data|
if data[:guestpath]
# Guest path specified, so mount the folder to specified point
@env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
:name => name,
:name => id,
:guest_path => data[:guestpath]))

# Dup the data so we can pass it to the guest API
Expand All @@ -101,11 +99,11 @@ def mount_shared_folders
data[:group] ||= @env[:machine].config.ssh.username

# Mount the actual folder
@env[:machine].guest.mount_shared_folder(name, data[:guestpath], data)
@env[:machine].guest.mount_shared_folder(id, data[:guestpath], data)
else
# If no guest path is specified, then automounting is disabled
@env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",
:name => name))
:name => id))
end
end
end
Expand Down

0 comments on commit 9cb8670

Please sign in to comment.