...

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

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

     1  //go:build windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/Microsoft/hcsshim"
    11  	"github.com/Microsoft/hcsshim/internal/appargs"
    12  	"github.com/urfave/cli"
    13  )
    14  
    15  var mountCommand = cli.Command{
    16  	Name:      "mount",
    17  	Usage:     "activates a scratch, optionally mounted to provided target",
    18  	ArgsUsage: "<scratch path> [target path]",
    19  	Before:    appargs.Validate(appargs.NonEmptyString, appargs.Optional(appargs.String)),
    20  	Flags: []cli.Flag{
    21  		cli.StringSliceFlag{
    22  			Name:  "layer, l",
    23  			Usage: "paths to the parent layers for this layer",
    24  		},
    25  	},
    26  	Action: func(context *cli.Context) (err error) {
    27  		path, err := filepath.Abs(context.Args().Get(0))
    28  		if err != nil {
    29  			return err
    30  		}
    31  
    32  		targetPath, err := filepath.Abs(context.Args().Get(1))
    33  		if err != nil {
    34  			return err
    35  		}
    36  
    37  		layers, err := normalizeLayers(context.StringSlice("layer"), true)
    38  		if err != nil {
    39  			return err
    40  		}
    41  
    42  		err = hcsshim.ActivateLayer(driverInfo, path)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		defer func() {
    47  			if err != nil {
    48  				_ = hcsshim.DeactivateLayer(driverInfo, path)
    49  			}
    50  		}()
    51  
    52  		err = hcsshim.PrepareLayer(driverInfo, path, layers)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		defer func() {
    57  			if err != nil {
    58  				_ = hcsshim.UnprepareLayer(driverInfo, path)
    59  			}
    60  		}()
    61  
    62  		mountPath, err := hcsshim.GetLayerMountPath(driverInfo, path)
    63  		if err != nil {
    64  			return err
    65  		}
    66  
    67  		if context.NArg() == 2 {
    68  			if err = setVolumeMountPoint(targetPath, mountPath); err != nil {
    69  				return err
    70  			}
    71  			_, err = fmt.Println(targetPath)
    72  			return err
    73  		}
    74  
    75  		_, err = fmt.Println(mountPath)
    76  		return err
    77  	},
    78  }
    79  
    80  var unmountCommand = cli.Command{
    81  	Name:      "unmount",
    82  	Usage:     "deactivates a scratch, optionally unmounting",
    83  	ArgsUsage: "<scratch path> [mounted path]",
    84  	Before:    appargs.Validate(appargs.NonEmptyString, appargs.Optional(appargs.String)),
    85  	Action: func(context *cli.Context) (err error) {
    86  		path, err := filepath.Abs(context.Args().Get(0))
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		mountedPath, err := filepath.Abs(context.Args().Get(1))
    92  		if err != nil {
    93  			return err
    94  		}
    95  
    96  		if context.NArg() == 2 {
    97  			if err = deleteVolumeMountPoint(mountedPath); err != nil {
    98  				return err
    99  			}
   100  		}
   101  
   102  		err = hcsshim.UnprepareLayer(driverInfo, path)
   103  		if err != nil {
   104  			fmt.Fprintln(os.Stderr, err)
   105  		}
   106  		err = hcsshim.DeactivateLayer(driverInfo, path)
   107  		if err != nil {
   108  			return err
   109  		}
   110  		return nil
   111  	},
   112  }
   113  

View as plain text