github.com/shivanshs9/go-overlay2@v0.0.0-20160814221707-8877415a0206/mount.go (about)

     1  // +build linux
     2  
     3  // Copyright 2016 Dennis Chen <barracks510@gmail.com>
     4  // Copyright 2013-2016 Docker, Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package overlay2
    19  
    20  import (
    21  	"os"
    22  	"runtime"
    23  	"syscall"
    24  )
    25  
    26  func mountFrom(dir, device, target, mType, label string) error {
    27  	runtime.LockOSThread()
    28  
    29  	// We want to store the original directory so we can re-enter after a
    30  	// successful mount. This solves the problem of a process living
    31  	// in the mounted directory when we want to unmount it. We do this
    32  	// without invoking the reexec chain.
    33  	cwd, err := os.Getwd()
    34  	if err != nil {
    35  		return err
    36  	}
    37  	if err := os.Chdir(dir); err != nil {
    38  		return err
    39  	}
    40  	if err := syscall.Mount(device, target, mType, uintptr(0), label); err != nil {
    41  		return err
    42  	}
    43  	if err := os.Chdir(cwd); err != nil {
    44  		return err
    45  	}
    46  
    47  	runtime.UnlockOSThread()
    48  
    49  	return nil
    50  }