2012-01-20 04:17:10 +00:00
|
|
|
require 'log4r'
|
|
|
|
|
2012-05-23 22:57:43 +00:00
|
|
|
require "vagrant"
|
|
|
|
|
2012-04-19 04:03:03 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
2012-06-26 23:28:49 +00:00
|
|
|
class Guest < Vagrant.plugin("1", :guest)
|
2012-04-19 04:03:03 +00:00
|
|
|
class LinuxError < Vagrant::Errors::VagrantError
|
|
|
|
error_namespace("vagrant.guest.linux")
|
|
|
|
end
|
|
|
|
|
2012-01-20 04:17:10 +00:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
|
|
|
|
@logger = Log4r::Logger.new("vagrant::guest::linux")
|
|
|
|
end
|
|
|
|
|
2011-01-09 21:19:50 +00:00
|
|
|
def distro_dispatch
|
2012-08-13 01:54:52 +00:00
|
|
|
@vm.communicate.tap do |comm|
|
|
|
|
if comm.test("cat /etc/debian_version") == 0
|
|
|
|
return :debian if comm.test("cat /proc/version | grep 'Debian'") == 0
|
|
|
|
return :ubuntu if comm.test("cat /proc/version | grep 'Ubuntu'") == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return :gentoo if comm.test("cat /etc/gentoo-release") == 0
|
|
|
|
return :fedora if comm.test("grep 'Fedora release 16' /etc/redhat-release") == 0
|
|
|
|
return :redhat if comm.test("cat /etc/redhat-release") == 0
|
|
|
|
return :suse if comm.test("cat /etc/SuSE-release") == 0
|
|
|
|
return :arch if comm.test("cat /etc/arch-release") == 0
|
2010-04-25 23:33:33 +00:00
|
|
|
end
|
2011-01-09 21:19:50 +00:00
|
|
|
|
|
|
|
# Can't detect the distro, assume vanilla linux
|
|
|
|
nil
|
2010-04-25 23:33:33 +00:00
|
|
|
end
|
|
|
|
|
2010-04-25 23:27:03 +00:00
|
|
|
def halt
|
2012-08-13 01:54:52 +00:00
|
|
|
@vm.communicate.sudo("shutdown -h now")
|
2010-04-25 23:27:03 +00:00
|
|
|
|
|
|
|
# Wait until the VM's state is actually powered off. If this doesn't
|
|
|
|
# occur within a reasonable amount of time (15 seconds by default),
|
|
|
|
# then simply return and allow Vagrant to kill the machine.
|
|
|
|
count = 0
|
2012-01-20 04:17:10 +00:00
|
|
|
while @vm.state != :poweroff
|
2010-04-25 23:27:03 +00:00
|
|
|
count += 1
|
|
|
|
|
2012-01-20 04:17:10 +00:00
|
|
|
return if count >= @vm.config.linux.halt_timeout
|
|
|
|
sleep @vm.config.linux.halt_check_interval
|
2010-04-25 23:27:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-12 07:14:04 +00:00
|
|
|
def mount_shared_folder(name, guestpath, options)
|
2012-01-25 05:37:02 +00:00
|
|
|
real_guestpath = expanded_guest_path(guestpath)
|
2012-01-20 04:17:10 +00:00
|
|
|
@logger.debug("Shell expanded guest path: #{real_guestpath}")
|
2012-01-20 04:14:05 +00:00
|
|
|
|
2012-08-13 02:03:31 +00:00
|
|
|
@vm.communicate.sudo("mkdir -p #{real_guestpath}")
|
2012-01-20 04:14:05 +00:00
|
|
|
mount_folder(name, real_guestpath, options)
|
2012-08-13 02:03:31 +00:00
|
|
|
@vm.communicate.sudo("chown `id -u #{options[:owner]}`:`id -g #{options[:group]}` #{real_guestpath}")
|
2010-05-18 08:24:59 +00:00
|
|
|
end
|
|
|
|
|
2010-07-14 05:04:56 +00:00
|
|
|
def mount_nfs(ip, folders)
|
|
|
|
# TODO: Maybe check for nfs support on the guest, since its often
|
|
|
|
# not installed by default
|
|
|
|
folders.each do |name, opts|
|
2012-01-25 05:37:02 +00:00
|
|
|
# Expand the guestpath, so we can handle things like "~/vagrant"
|
|
|
|
real_guestpath = expanded_guest_path(opts[:guestpath])
|
|
|
|
|
|
|
|
# Do the actual creating and mounting
|
2012-08-13 02:03:31 +00:00
|
|
|
@vm.communicate.sudo("mkdir -p #{real_guestpath}")
|
|
|
|
@vm.communicate.sudo("mount -o vers=#{opts[:nfs_version]} #{ip}:'#{opts[:hostpath]}' #{real_guestpath}",
|
2012-01-07 04:03:56 +00:00
|
|
|
:error_class => LinuxError,
|
|
|
|
:error_key => :mount_nfs_fail)
|
2010-06-03 18:09:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-25 05:37:02 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Determine the real guest path. Since we use a `sudo` shell everywhere
|
|
|
|
# else, things like '~' don't expand properly in shared folders. We have
|
|
|
|
# to `echo` here to get that path.
|
|
|
|
#
|
|
|
|
# @param [String] guestpath The unexpanded guest path.
|
|
|
|
# @return [String] The expanded guestpath
|
|
|
|
def expanded_guest_path(guestpath)
|
|
|
|
real_guestpath = nil
|
2012-08-13 02:03:31 +00:00
|
|
|
@vm.communicate.execute("printf #{guestpath}") do |type, data|
|
2012-01-25 05:37:02 +00:00
|
|
|
if type == :stdout
|
|
|
|
real_guestpath ||= ""
|
|
|
|
real_guestpath += data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !real_guestpath
|
|
|
|
# Really strange error case if this happens. Let's throw an error,
|
|
|
|
# tell the user to check the echo output.
|
|
|
|
raise LinuxError, :_key => :guestpath_expand_fail
|
|
|
|
end
|
|
|
|
|
|
|
|
# Chomp the string so that any trailing newlines are killed
|
|
|
|
return real_guestpath.chomp
|
|
|
|
end
|
|
|
|
|
2012-01-12 07:14:04 +00:00
|
|
|
def mount_folder(name, guestpath, options)
|
2010-04-25 08:46:51 +00:00
|
|
|
# Determine the permission string to attach to the mount command
|
2012-01-12 07:23:05 +00:00
|
|
|
mount_options = "-o uid=`id -u #{options[:owner]}`,gid=`id -g #{options[:group]}`"
|
|
|
|
mount_options += ",#{options[:extra]}" if options[:extra]
|
2010-04-25 08:46:51 +00:00
|
|
|
|
|
|
|
attempts = 0
|
|
|
|
while true
|
2012-01-07 02:31:32 +00:00
|
|
|
success = true
|
2012-08-13 02:03:31 +00:00
|
|
|
@vm.communicate.sudo("mount -t vboxsf #{mount_options} #{name} #{guestpath}") do |type, data|
|
2012-01-07 02:31:32 +00:00
|
|
|
success = false if type == :stderr && data =~ /No such device/i
|
2010-04-25 08:46:51 +00:00
|
|
|
end
|
|
|
|
|
2012-01-07 02:31:32 +00:00
|
|
|
break if success
|
2010-04-25 08:46:51 +00:00
|
|
|
|
|
|
|
attempts += 1
|
2010-12-22 04:04:21 +00:00
|
|
|
raise LinuxError, :mount_fail if attempts >= 10
|
2012-01-12 07:14:04 +00:00
|
|
|
sleep 5
|
2010-04-25 08:46:51 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-01 17:00:49 +00:00
|
|
|
end
|
2010-04-25 08:46:51 +00:00
|
|
|
end
|
2010-05-18 08:24:59 +00:00
|
|
|
end
|