...

Source file src/github.com/Microsoft/hcsshim/cmd/wclayer/volumemountutils.go

Documentation: github.com/Microsoft/hcsshim/cmd/wclayer

     1  //go:build windows
     2  
     3  package main
     4  
     5  // Simple wrappers around SetVolumeMountPoint and DeleteVolumeMountPoint
     6  
     7  import (
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/pkg/errors"
    12  	"golang.org/x/sys/windows"
    13  )
    14  
    15  // Mount volumePath (in format '\\?\Volume{GUID}' at targetPath.
    16  // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setvolumemountpointw
    17  func setVolumeMountPoint(targetPath string, volumePath string) error {
    18  	if !strings.HasPrefix(volumePath, "\\\\?\\Volume{") {
    19  		return errors.Errorf("unable to mount non-volume path %s", volumePath)
    20  	}
    21  
    22  	// Both must end in a backslash
    23  	slashedTarget := filepath.Clean(targetPath) + string(filepath.Separator)
    24  	slashedVolume := volumePath + string(filepath.Separator)
    25  
    26  	targetP, err := windows.UTF16PtrFromString(slashedTarget)
    27  	if err != nil {
    28  		return errors.Wrapf(err, "unable to utf16-ise %s", slashedTarget)
    29  	}
    30  
    31  	volumeP, err := windows.UTF16PtrFromString(slashedVolume)
    32  	if err != nil {
    33  		return errors.Wrapf(err, "unable to utf16-ise %s", slashedVolume)
    34  	}
    35  
    36  	if err := windows.SetVolumeMountPoint(targetP, volumeP); err != nil {
    37  		return errors.Wrapf(err, "failed calling SetVolumeMount('%s', '%s')", slashedTarget, slashedVolume)
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // Remove the volume mount at targetPath
    44  // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-deletevolumemountpointa
    45  func deleteVolumeMountPoint(targetPath string) error {
    46  	// Must end in a backslash
    47  	slashedTarget := filepath.Clean(targetPath) + string(filepath.Separator)
    48  
    49  	targetP, err := windows.UTF16PtrFromString(slashedTarget)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "unable to utf16-ise %s", slashedTarget)
    52  	}
    53  
    54  	if err := windows.DeleteVolumeMountPoint(targetP); err != nil {
    55  		return errors.Wrapf(err, "failed calling DeleteVolumeMountPoint('%s')", slashedTarget)
    56  	}
    57  
    58  	return nil
    59  }
    60  

View as plain text