2016-05-16 20:02:51 -04:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package overlay2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-16 20:02:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Register("docker-mountfrom", mountFromMain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatal(err error) {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mountOptions struct {
|
|
|
|
Device string
|
|
|
|
Target string
|
|
|
|
Type string
|
|
|
|
Label string
|
|
|
|
Flag uint32
|
|
|
|
}
|
|
|
|
|
2016-08-22 14:43:10 -04:00
|
|
|
func mountFrom(dir, device, target, mType string, flags uintptr, label string) error {
|
2016-05-16 20:02:51 -04:00
|
|
|
options := &mountOptions{
|
|
|
|
Device: device,
|
|
|
|
Target: target,
|
|
|
|
Type: mType,
|
2016-08-22 14:43:10 -04:00
|
|
|
Flag: uint32(flags),
|
2016-05-16 20:02:51 -04:00
|
|
|
Label: label,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := reexec.Command("docker-mountfrom", dir)
|
2016-06-28 16:23:00 -04:00
|
|
|
w, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("mountfrom error on pipe creation: %v", err)
|
|
|
|
}
|
2016-05-16 20:02:51 -04:00
|
|
|
|
|
|
|
output := bytes.NewBuffer(nil)
|
|
|
|
cmd.Stdout = output
|
|
|
|
cmd.Stderr = output
|
|
|
|
if err := cmd.Start(); err != nil {
|
2017-09-18 21:25:39 -04:00
|
|
|
w.Close()
|
2016-05-16 20:02:51 -04:00
|
|
|
return fmt.Errorf("mountfrom error on re-exec cmd: %v", err)
|
|
|
|
}
|
|
|
|
//write the options to the pipe for the untar exec to read
|
|
|
|
if err := json.NewEncoder(w).Encode(options); err != nil {
|
2017-09-18 21:25:39 -04:00
|
|
|
w.Close()
|
2016-05-16 20:02:51 -04:00
|
|
|
return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
2017-09-18 21:25:39 -04:00
|
|
|
return fmt.Errorf("mountfrom re-exec error: %v: output: %v", err, output)
|
2016-05-16 20:02:51 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mountfromMain is the entry-point for docker-mountfrom on re-exec.
|
|
|
|
func mountFromMain() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
var options *mountOptions
|
|
|
|
|
|
|
|
if err := json.NewDecoder(os.Stdin).Decode(&options); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chdir(flag.Arg(0)); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Mount(options.Device, options.Target, options.Type, uintptr(options.Flag), options.Label); err != nil {
|
2016-05-16 20:02:51 -04:00
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|