Skip to content
Kei Kamikawa edited this page Nov 1, 2022 · 2 revisions

Shared directories allow you expose specific directories in the macOS file system to the guest operating system running in a VM, this allows your user to share files between the host and guest operating system. To enable shared directory device support in Linux, configure the Linux guest kernel to enable the CONFIG_VIRTIO_FS support.

Note

Shared directories in macOS VMs are only available in macOS 13 and later.

Overview

Use vz.VirtioFileSystemDeviceConfiguration to create a Virtio file system device which allows the host to expose directories to a guest using a tag label.

The example below shows the creation of a vz.VirtioFileSystemDeviceConfiguration that shares a single directory that the user can manually mount after creating a mount point in the guest VM:

// Define a path to a specific file, in this case the 
// "Projects" directory in the user's home directory.
var projectPath = filepath.Join(homeDirectory, "Projects")

func createSingleDirectoryShareDeviceConfiguration() (*vz.VirtioFileSystemDeviceConfiguration, error) {
	sharedDirectory, err := vz.NewSharedDirectory(projectPath, false)
	if err != nil {
		return nil, err
	}
	singleDirectoryShare, err := vz.NewSingleDirectoryShare(sharedDirectory)
	if err != nil {
		return nil, err
	}

	// Create the vz.VirtioFileSystemDeviceConfiguration and assign it a unique tag. 
	tag := "Projects"
	sharingConfiguration, err := vz.NewVirtioFileSystemDeviceConfiguration(tag)
	if err != nil {
		return nil, err
	}
	sharingConfiguration.SetDirectoryShare(singleDirectoryShare)

	return sharingConfiguration, nil
}

A vz.VirtioFileSystemDeviceConfiguration can also share multiple directories. The example below demonstrates sharing the ~/Invoices and ~/Projects directories from the user’s home directory to the guest VM:

// Define paths in this case the "Projects" and "Invoices" directories
// in the user's home directory.
var projectPath = filepath.Join(homeDirectory, "Projects")
var invoicesPath = filepath.Join(homeDirectory, "Invoices")

func createMultipleDirectoryShareDeviceConfiguration() (*vz.VirtioFileSystemDeviceConfiguration, error) {
	sharedProjectsDirectory, err := vz.NewSharedDirectory(projectPath, false)
	if err != nil {
		return nil, err
	}

	// It's also possible to share directories that are read-only: 
	sharedInvoicesDirectory, err := vz.NewSharedDirectory(invoicesPath, true)
	if err != nil {
		return nil, err
	}

	// Associate each shared directory with a name, which is how the framework
	// lists them under the mount point.
	directoriesToShare := map[string]*vz.SharedDirectory{
		"My Projects":sharedProjectsDirectory,
		"Invoices": sharedInvoicesDirectory,
	}
	multipleDirectoryShare, err := vz.NewMultipleDirectoryShare(directoriesToShare)
	if err != nil {
		return nil, err
	}

	 // Create the VZVirtioFileSystemDeviceConfiguration and assign it a unique tag. 
	tag := "myfiles"
	sharingConfiguration, err := vz.NewVirtioFileSystemDeviceConfiguration(tag)
	if err != nil {
		return nil, err
	}
	sharingConfiguration.SetDirectoryShare(multipleDirectoryShare)

	return sharingConfiguration, nil
}

Mounting shared directories

Important

The commands required to mount shared directories in a guest VM aren’t commands that your app can execute or that you can script from inside your application to a VM; the user must perform them either interactively or as part of a script while logged in to the guest. You must communicate these requirements to the user of your app.

Mounting a shared directory requires the user to execute a command in a terminal window, the specific mount command depends on the guest VM’s operating system:

  • In macOS guests, use mount_virtiofs tag directory.
  • In Linux guests, use mount -t virtiofs tag directory.

The tag argument is the file system device configuration label — Projects in the single directory case and myfiles in the multiple directory case in these examples — and directory is the mount point in the guest file system to which the framework attaches the shared directories.

Refs

Most of the content on this page has been copied from