automount

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

automount

Mount an SMB share in the operating system's file manager without any password dialog, and unmount it. A small pure-Go facade (no cgo) over three existing backends:

OS Backend What appears
macOS go-macos/netfs (NetFS, NoUI) a volume under /Volumes, in the Finder sidebar
Linux (GNOME) go-freedesktop/gvfs (answers GVfs's own AskPassword) a gvfs location, in Files
Windows go-mswin/win32 (MSFT_SmbMapping.Create, any TCP port, standard user) a drive letter, in "This PC"

Any other GOOS compiles and answers automount.ErrUnsupported (errors.ErrUnsupported).

u, _ := url.Parse("smb://127.0.0.1:4455/theca")
m, err := automount.Mount(ctx, automount.Share{URL: u, User: "alice", Password: pw}, automount.Options{})
if err != nil { /* errors.Is(err, netfs.ErrAuth) etc. */ }
fmt.Println(m.Path())   // /Volumes/theca, Z:\, /run/user/1000/gvfs/...
defer m.Unmount(ctx)

API

type Share struct {
    URL            *url.URL // smb://host[:port]/share, no user information
    User, Password string
}
type Options struct {
    Drive    string // Windows: "X:"; "" = the highest free letter from Z: down
    ReadOnly bool   // macOS only; elsewhere ErrReadOnlyUnsupported
}
func Mount(ctx context.Context, s Share, o Options) (*Mounted, error)
func (m *Mounted) Path() string
func (m *Mounted) Unmount(ctx context.Context) error // idempotent; a failure may be retried
func (m *Mounted) Reveal() error                      // open Path() in the file manager

Reveal opens Path() in Finder (open -- path), through xdg-open (or gio open) on Linux, in Explorer on Windows. It starts the helper without a shell and without waiting for the window; the path is one argv element, so spaces and quotes are inert and a leading - cannot become an option. A missing helper is an error (ErrNotMounted after Unmount).

The volume name is not an option: macOS names the volume after the share in the URL (smb://host/theca gives /Volumes/theca, and theca-1 when that name is taken), so a caller chooses it with the share name.

(The type is Mounted because Go has one namespace for Mount the function and the type.) Mount refuses, before touching the OS: a nil or non-smb URL, a missing host or share, a share with a sub-path, a bad port (ErrBadURL); user information in the URL (ErrCredentialsInURL); a bad or used Windows drive (ErrBadDrive, ErrDriveInUse, ErrNoFreeDrive); ReadOnly where it cannot be honoured (ErrReadOnlyUnsupported). Loopback hosts (127.0.0.0/8, ::1, localhost) set NetFS's AllowLoopback. On Windows a bare user name is prefixed with .\ (a local account); a name with \ or @ is left alone.

The no-dialog guarantee and secrets

  • macOS: NetFS is told NoUI; credentials are CFStrings, not part of the URL.
  • Linux: the daemon's AskPassword call is answered in-process; a second request (wrong credentials) aborts with gvfs.ErrAuthFailed.
  • Windows: the CIM method takes the credentials as arguments; no net use process is started.

The password never goes into an argument vector, the environment, a URL, an error message or a log. Backend errors are wrapped with %w, so errors.Is reaches netfs.ErrAuth, gvfs.ErrAuthFailed and errors.As reaches *win32.SMBPortError.

Measured, and where

  • macOS 27.2.1 (arm64): measured, see automount-live-darwin-2026-11-16.txt. The SMB server of go-filesystems/smb runs in the test process on 127.0.0.1:<random port>; the facade mounts it, a file is read and written through Path(), Unmount removes the volume. A wrong password fails with netfs.ErrAuth. Run it with AUTOMOUNT_REAL=1 gotest -count=1 -run TestLiveMount . (the same test runs on all three platforms).
  • Linux (Ubuntu 24.04, gvfs 1.54.4): measured in a headless session (dbus-run-session with a private XDG_RUNTIME_DIR, gvfsd, gvfsd-smb and gvfsd-fuse D-Bus-activated), see automount-live-linux-2026-11-16.txt: the same sequence as on macOS, wrong password reaching gvfs.ErrAuthFailed. Not measured: a real GNOME desktop session. The run found a defect in go-freedesktop/gvfs v0.1.0 (/ returned as the path when gvfs-fuse is not serving the session), fixed in v0.1.1. When gvfs-fuse is not serving the session, Path() is the smb:// URI, which GIO can open and os calls cannot.
  • Windows 11 25H2 (arm64, QEMU): measured as a standard user and as an administrator. As the standard user std (Users only, Medium Mandatory Level, not elevated), see automount-live-windows-std-2026-11-17.txt, and as an administrator, automount-live-windows-2026-11-16.txt: wrong password reaching *win32.SMBPortError, map Z:, read and write through it, unmount, Z: gone. Not measured: the letter's appearance in Explorer's "This PC": an ssh logon has its own logon session, so the check is through os calls in the same session.

The package is held to 100 % statement coverage on macOS, Linux and Windows in CI; every OS call sits behind a package-level variable.

License

BSD 3-Clause.

Documentation

Overview

Package automount mounts an SMB share in the operating system's file manager without any password dialog, and unmounts it.

The credentials are handed to the platform's own mount service as values: NetFS with NoUI on macOS (github.com/go-macos/netfs), GVfs answering its own AskPassword call on Linux/GNOME (github.com/go-freedesktop/gvfs), and MSFT_SmbMapping.Create on Windows (github.com/go-mswin/win32). The password is never placed in an argument vector, the environment, a URL, an error message or a log. Every other GOOS answers ErrUnsupported.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadURL: the URL is not smb://host[:port]/share.
	ErrBadURL = errors.New("automount: URL must be smb://host[:port]/share")
	// ErrCredentialsInURL: user information in the URL; pass Share.User and
	// Share.Password instead, so that a secret is never part of a URL.
	ErrCredentialsInURL = errors.New("automount: credentials must be passed in Share, not in the URL")
	// ErrBadDrive: Options.Drive is not a drive letter and a colon ("X:").
	ErrBadDrive = errors.New("automount: Drive must be a letter and a colon, like \"X:\"")
	// ErrDriveInUse: Options.Drive is already a logical drive.
	ErrDriveInUse = errors.New("automount: drive letter already in use")
	// ErrNoFreeDrive: every drive letter from D: to Z: is taken.
	ErrNoFreeDrive = errors.New("automount: no free drive letter")
	// ErrReadOnlyUnsupported: Options.ReadOnly on a platform whose backend has
	// no read-only mount.
	ErrReadOnlyUnsupported = errors.New("automount: read-only mounts are not supported on this platform")
)
View Source
var ErrNotMounted = errors.New("automount: the share is no longer mounted")

ErrNotMounted: Reveal on a share that was already unmounted.

View Source
var ErrUnsupported = errors.ErrUnsupported

ErrUnsupported is returned by Mount on a GOOS without a backend.

Functions

This section is empty.

Types

type Mounted

type Mounted struct {
	// contains filtered or unexported fields
}

Mounted is one mounted share. (The type cannot be called Mount: Go has one namespace for the function and the type.)

func Mount

func Mount(ctx context.Context, s Share, o Options) (*Mounted, error)

Mount mounts the share in the user's file manager: a volume under /Volumes on macOS, a gvfs location on Linux, a drive letter on Windows. No dialog appears; when the credentials are wrong the error says so (errors.Is reaches the backend's sentinel: netfs.ErrAuth, gvfs.ErrAuthFailed, or a *win32.SMBPortError).

func (*Mounted) Path

func (m *Mounted) Path() string

Path is where the share is reachable: a directory on macOS and Linux, a drive root such as `Z:\` on Windows. On Linux, when gvfs-fuse is not serving the session, it is the smb:// URI, which GIO opens and os calls do not.

func (*Mounted) Reveal added in v0.2.0

func (m *Mounted) Reveal() error

Reveal opens the mount's Path in the operating system's file manager: Finder on macOS (open(1)), the default handler through xdg-open (or gio open) on Linux, Explorer on Windows. It returns once the helper is started, without waiting for the window. A missing helper is an error, never a panic.

The path is passed as one argument of an argument vector, never through a shell, so spaces and quotes are inert; a path that could be read as an option is made unambiguous first.

func (*Mounted) Unmount

func (m *Mounted) Unmount(ctx context.Context) error

Unmount unmounts the share. It is safe to call twice; the second call does nothing. A failure (a busy volume, say) leaves the mount in place and may be retried.

type Options

type Options struct {
	// Drive is the Windows drive letter ("X:"). Empty means the highest free
	// letter from Z: down. It is ignored off Windows.
	Drive string
	// ReadOnly mounts read-only. Only macOS supports it; elsewhere Mount fails
	// with ErrReadOnlyUnsupported rather than mounting read-write silently.
	ReadOnly bool
}

Options tunes a mount.

type Share

type Share struct {
	// URL is smb://host[:port]/share. It carries no user information.
	URL *url.URL
	// User and Password are the account. They are passed to the platform's
	// mount service as values.
	User, Password string
}

Share names an SMB share and the account that opens it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址