2023-08-10 16:53:25 -04:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
|
# SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2016-11-30 21:47:08 -05:00
|
|
|
require_relative "../../../synced_folders/unix_mount_helpers"
|
2016-06-24 21:19:58 -04:00
|
|
|
|
2013-04-04 02:01:43 -04:00
|
|
|
module VagrantPlugins
|
|
|
|
|
module GuestLinux
|
|
|
|
|
module Cap
|
|
|
|
|
class MountVirtualBoxSharedFolder
|
2016-11-30 21:47:08 -05:00
|
|
|
extend SyncedFolder::UnixMountHelpers
|
2016-06-24 21:19:58 -04:00
|
|
|
|
2020-08-25 11:32:18 -04:00
|
|
|
# Mounts and virtualbox folder on linux guest
|
|
|
|
|
#
|
|
|
|
|
# @param [Machine] machine
|
|
|
|
|
# @param [String] name of mount
|
|
|
|
|
# @param [String] path of mount on guest
|
|
|
|
|
# @param [Hash] hash of mount options
|
2013-04-04 02:01:43 -04:00
|
|
|
def self.mount_virtualbox_shared_folder(machine, name, guestpath, options)
|
2016-06-24 21:19:58 -04:00
|
|
|
guest_path = Shellwords.escape(guestpath)
|
2020-08-18 20:00:45 -04:00
|
|
|
mount_type = options[:plugin].capability(:mount_type)
|
2013-04-04 02:01:43 -04:00
|
|
|
|
2016-08-09 17:25:16 -04:00
|
|
|
@@logger.debug("Mounting #{name} (#{options[:hostpath]} to #{guestpath})")
|
2013-09-16 23:51:09 -04:00
|
|
|
|
2020-08-12 17:24:49 -04:00
|
|
|
builtin_mount_type = "-cit #{mount_type}"
|
|
|
|
|
addon_mount_type = "-t #{mount_type}"
|
2016-11-30 21:47:08 -05:00
|
|
|
|
2020-08-18 20:00:45 -04:00
|
|
|
mount_options, mount_uid, mount_gid = options[:plugin].capability(:mount_options, name, guest_path, options)
|
2018-05-07 16:00:41 -04:00
|
|
|
mount_command = "mount #{addon_mount_type} -o #{mount_options} #{name} #{guest_path}"
|
2013-04-04 02:01:43 -04:00
|
|
|
|
|
|
|
|
# Create the guest path if it doesn't exist
|
2016-06-24 21:19:58 -04:00
|
|
|
machine.communicate.sudo("mkdir -p #{guest_path}")
|
2013-04-04 02:01:43 -04:00
|
|
|
|
2016-06-24 21:19:58 -04:00
|
|
|
stderr = ""
|
2018-05-07 16:00:41 -04:00
|
|
|
result = machine.communicate.sudo(mount_command, error_check: false) do |type, data|
|
|
|
|
|
stderr << data if type == :stderr
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if result != 0
|
|
|
|
|
if stderr.include?("-cit")
|
|
|
|
|
@@logger.info("Detected builtin vboxsf module, modifying mount command")
|
|
|
|
|
mount_command.sub!(addon_mount_type, builtin_mount_type)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Attempt to mount the folder. We retry here a few times because
|
|
|
|
|
# it can fail early on.
|
|
|
|
|
stderr = ""
|
|
|
|
|
retryable(on: Vagrant::Errors::VirtualBoxMountFailed, tries: 3, sleep: 5) do
|
|
|
|
|
machine.communicate.sudo(mount_command,
|
|
|
|
|
error_class: Vagrant::Errors::VirtualBoxMountFailed,
|
|
|
|
|
error_key: :virtualbox_mount_failed,
|
|
|
|
|
command: mount_command,
|
|
|
|
|
output: stderr,
|
|
|
|
|
) { |type, data| stderr = data if type == :stderr }
|
|
|
|
|
end
|
2013-04-04 02:01:43 -04:00
|
|
|
end
|
|
|
|
|
|
2014-02-04 10:01:14 -05:00
|
|
|
# Chown the directory to the proper user. We skip this if the
|
|
|
|
|
# mount options contained a readonly flag, because it won't work.
|
2014-02-07 19:04:36 -05:00
|
|
|
if !options[:mount_options] || !options[:mount_options].include?("ro")
|
2016-08-09 17:25:16 -04:00
|
|
|
chown_command = "chown #{mount_uid}:#{mount_gid} #{guest_path}"
|
|
|
|
|
machine.communicate.sudo(chown_command)
|
2014-02-04 10:01:14 -05:00
|
|
|
end
|
2013-11-25 14:31:15 -05:00
|
|
|
|
2016-11-30 21:47:08 -05:00
|
|
|
emit_upstart_notification(machine, guest_path)
|
2013-04-04 02:01:43 -04:00
|
|
|
end
|
2014-04-16 17:59:23 -04:00
|
|
|
|
|
|
|
|
def self.unmount_virtualbox_shared_folder(machine, guestpath, options)
|
2016-06-24 21:19:58 -04:00
|
|
|
guest_path = Shellwords.escape(guestpath)
|
|
|
|
|
|
|
|
|
|
result = machine.communicate.sudo("umount #{guest_path}", error_check: false)
|
2014-04-16 17:59:23 -04:00
|
|
|
if result == 0
|
2016-06-24 21:19:58 -04:00
|
|
|
machine.communicate.sudo("rmdir #{guest_path}", error_check: false)
|
2014-04-16 17:59:23 -04:00
|
|
|
end
|
|
|
|
|
end
|
2013-04-04 02:01:43 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|