github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/pkg/mount/mount_linux.go (about)

     1  // Copyright 2012-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mount
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  // Mount attaches the fsType file system at path.
    15  //
    16  // dev is the device to mount (this is often the path of a block device, name
    17  // of a file, or a dummy string). data usually contains arguments for the
    18  // specific file system.
    19  func Mount(dev, path, fsType, data string, flags uintptr) error {
    20  	if err := unix.Mount(dev, path, fsType, flags, data); err != nil {
    21  		return fmt.Errorf("mount(%q on %q [type %q flags %x]): %v",
    22  			dev, path, fsType, flags, err)
    23  	}
    24  	return nil
    25  }
    26  
    27  // Unmount detaches any file system mounted at path.
    28  //
    29  // force forces an unmount regardless of currently open or otherwise used files
    30  // within the file system to be unmounted.
    31  //
    32  // lazy disallows future uses of any files below path -- i.e. it hides the file
    33  // system mounted at path, but the file system itself is still active and any
    34  // currently open files can continue to be used. When all references to files
    35  // from this file system are gone, the file system will actually be unmounted.
    36  func Unmount(path string, force, lazy bool) error {
    37  	var flags = unix.UMOUNT_NOFOLLOW
    38  	if len(path) == 0 {
    39  		return errors.New("path cannot be empty")
    40  	}
    41  	if force && lazy {
    42  		return errors.New("force and lazy unmount cannot both be set")
    43  	}
    44  	if force {
    45  		flags |= unix.MNT_FORCE
    46  	}
    47  	if lazy {
    48  		flags |= unix.MNT_DETACH
    49  	}
    50  	if err := unix.Unmount(path, flags); err != nil {
    51  		return fmt.Errorf("umount %q flags %x: %v", path, flags, err)
    52  	}
    53  	return nil
    54  }