Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion plugins/guests/linux/cap/persist_mount_shared_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: BUSL-1.1

require "vagrant/util"
require "vagrant/util/guest_inspection"

require_relative "../../../synced_folders/unix_mount_helpers"

Expand All @@ -10,6 +11,7 @@ module GuestLinux
module Cap
class PersistMountSharedFolder
extend SyncedFolder::UnixMountHelpers
extend Vagrant::Util::GuestInspection::Linux

@@logger = Log4r::Logger.new("vagrant::guest::linux::persist_mount_shared_folders")

Expand All @@ -22,7 +24,8 @@ class PersistMountSharedFolder
def self.persist_mount_shared_folder(machine, folders)
if folders.nil?
@@logger.info("clearing /etc/fstab")
self.remove_vagrant_managed_fstab(machine)
changed = remove_vagrant_managed_fstab(machine)
systemd_daemon_reload(machine) if changed
return
end

Expand Down Expand Up @@ -56,10 +59,17 @@ def self.persist_mount_shared_folder(machine, folders)
fstab_entry = Vagrant::Util::TemplateRenderer.render('guests/linux/etc_fstab', folders: export_folders)
self.remove_vagrant_managed_fstab(machine)
machine.communicate.sudo("echo '#{fstab_entry}' >> /etc/fstab")
systemd_daemon_reload(machine)
end

private

def self.systemd_daemon_reload(machine)
if systemd?(machine.communicate)
machine.communicate.sudo("systemctl daemon-reload")
end
end

def self.fstab_exists?(machine)
machine.communicate.test("test -f /etc/fstab")
end
Expand All @@ -69,15 +79,20 @@ def self.contains_vagrant_data?(machine)
end

def self.remove_vagrant_managed_fstab(machine)
fstab_is_modified = false

if fstab_exists?(machine)
if contains_vagrant_data?(machine)
machine.communicate.sudo("sed -i '/\#VAGRANT-BEGIN/,/\#VAGRANT-END/d' /etc/fstab")
fstab_is_modified = true
else
@@logger.info("no vagrant data in fstab file, carrying on")
end
else
@@logger.info("no fstab file found, carrying on")
end

fstab_is_modified
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,53 @@
end
end

context "fstab modified with systemd" do
before do
allow(cap).to receive(:systemd?).and_return(true)
end

it "reloads systemd daemon" do
expect(comm).to receive(:sudo).with("systemctl daemon-reload")
cap.persist_mount_shared_folder(machine, folders)
end
end

context "fstab modified without systemd" do
before do
allow(cap).to receive(:systemd?).and_return(false)
allow(cap).to receive(:contains_vagrant_data?).and_return(false)
end

it "does not reload systemd daemon" do
expect(comm).not_to receive(:sudo).with("systemctl daemon-reload")
cap.persist_mount_shared_folder(machine, folders)
end
end

context "fstab not modified with systemd" do
before do
allow(cap).to receive(:systemd?).and_return(true)
allow(cap).to receive(:contains_vagrant_data?).and_return(false)
end

it "does not reload systemd daemon" do
expect(comm).not_to receive(:sudo).with("systemctl daemon-reload")
cap.persist_mount_shared_folder(machine, nil)
end
end

context "fstab not modified without systemd" do
before do
allow(cap).to receive(:systemd?).and_return(false)
allow(cap).to receive(:contains_vagrant_data?).and_return(false)
end

it "does not reload systemd daemon" do
expect(comm).not_to receive(:sudo).with("systemctl daemon-reload")
cap.persist_mount_shared_folder(machine, nil)
end
end

context "smb folder" do
let (:fstab_folders) {
Vagrant::Plugin::V2::SyncedFolder::Collection[
Expand Down