Class: VagrantPlugins::QEMU::SyncedFolderVirtioFS

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-qemu/synced_folder_virtiofs.rb

Instance Method Summary collapse

Instance Method Details

#cleanup(machine, opts) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vagrant-qemu/synced_folder_virtiofs.rb', line 118

def cleanup(machine, opts)
  virtiofs_dir = machine.data_dir.join("virtiofs")
  return unless virtiofs_dir.directory?

  Dir.glob(virtiofs_dir.join("*.pid").to_s).each do |pid_file|
    sock_path_file = pid_file.sub(/\.pid$/, ".sock_path")
    sock_file = File.exist?(sock_path_file) ? File.read(sock_path_file).strip : nil
    cleanup_virtiofsd(pid_file, sock_file)
    File.delete(sock_path_file) if File.exist?(sock_path_file)
  end

  FileUtils.rm_rf(virtiofs_dir)
  machine.ui.info("virtiofsd stopped") if machine.ui
end

#disable(machine, folders, opts) ⇒ Object



111
112
113
114
115
116
# File 'lib/vagrant-qemu/synced_folder_virtiofs.rb', line 111

def disable(machine, folders, opts)
  folders.each do |_id, folder_opts|
    guestpath = folder_opts[:guestpath]
    machine.communicate.sudo("umount #{guestpath} 2>/dev/null || true")
  end
end

#enable(machine, folders, opts) ⇒ Object



106
107
108
109
# File 'lib/vagrant-qemu/synced_folder_virtiofs.rb', line 106

def enable(machine, folders, opts)
  # Mounting happens in MountVirtioFS action after boot,
  # since the guest isn't available when SyncedFolders runs.
end

#prepare(machine, folders, opts) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vagrant-qemu/synced_folder_virtiofs.rb', line 21

def prepare(machine, folders, opts)
  @logger = Log4r::Logger.new("vagrant_qemu::synced_folder_virtiofs")

  # Use /tmp for socket files — Unix domain sockets have a 104-byte path limit
  # on macOS, and Vagrant data_dir paths are often too long.
  # PID files and logs stay in the data dir for persistence.
  virtiofs_dir = machine.data_dir.join("virtiofs")
  FileUtils.mkdir_p(virtiofs_dir)
  socket_dir = Dir.tmpdir


  virtiofsd = machine.provider_config.virtiofsd_bin
  memory = machine.provider_config.memory

  mem_path = File.join(Dir.tmpdir, "vagrant-qemu-#{machine.id}-mem")
  extra_args = %W(
    -object memory-backend-file,id=mem,size=#{memory},mem-path=#{mem_path},share=on
    -numa node,memdev=mem
  )

  sorted_folders = folders.sort_by { |_id, opts| opts[:guestpath] }
  sorted_folders.each_with_index do |(_id, folder_opts), i|
    hostpath = File.expand_path(folder_opts[:hostpath])
    tag = "virtiofs#{i}"
    socket_id = "vqemu-#{machine.id}-#{tag}"
    socket_path = File.join(socket_dir, "#{socket_id}.sock")
    pid_file = virtiofs_dir.join("#{tag}.pid").to_s

    # Clean up any stale virtiofsd from a previous run
    cleanup_virtiofsd(pid_file, socket_path)

    # Start virtiofsd
    log_file = virtiofs_dir.join("#{tag}.log").to_s
    host_uid = Process.uid
    host_gid = Process.gid
    virtiofsd_args = [
      virtiofsd,
      "--socket-path=#{socket_path}",
      "--shared-dir=#{hostpath}",
      "--sandbox=none",
      "--inode-file-handles=never",
    ]

    # Add UID/GID mapping if virtiofsd supports it (v1.13+)
    translate_supported = `#{virtiofsd} --help 2>&1`.include?("--translate-uid")
    if translate_supported
      virtiofsd_args += [
        "--translate-uid", "map:#{machine.provider_config.virtiofs_guest_uid}:#{host_uid}:1",
        "--translate-gid", "map:#{machine.provider_config.virtiofs_guest_gid}:#{host_gid}:1",
      ]
    end
    # Append user-supplied extra virtiofsd args from provider config
    extra_vfs = machine.provider_config.extra_virtiofsd_args
    virtiofsd_args += extra_vfs if extra_vfs.is_a?(Array) && !extra_vfs.empty?
    pid = spawn(*virtiofsd_args, [:out, :err] => [log_file, "w"])
    Process.detach(pid)

    # Wait for socket to appear
    30.times do
      break if File.exist?(socket_path)
      sleep 0.1
    end

    unless File.exist?(socket_path)
      Process.kill("TERM", pid) rescue nil
      raise Errors::VirtiofsdStartFailed,
        hostpath: hostpath,
        log_file: log_file
    end

    File.write(pid_file, pid.to_s)
    # Store socket path alongside pid so cleanup can find it
    File.write(virtiofs_dir.join("#{tag}.sock_path").to_s, socket_path)
    machine.ui.info("virtiofsd started (pid #{pid}) sharing #{hostpath}")

    extra_args += %W(
      -chardev socket,id=char_#{tag},path=#{socket_path}
      -device vhost-user-fs-pci,chardev=char_#{tag},tag=#{tag}
    )
  end

  # Inject QEMU args so StartInstance picks them up
  machine.provider_config.extra_qemu_args += extra_args
end

#usable?(machine, raise_error = false) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vagrant-qemu/synced_folder_virtiofs.rb', line 8

def usable?(machine, raise_error = false)
  return false unless machine.provider_name == :qemu

  if machine.provider_config.virtiofsd_bin.nil?
    if raise_error
      raise Errors::VirtiofsdNotFound
    end
    return false
  end

  true
end