2014-10-29 19:06:51 +00:00
|
|
|
package chrootarchive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-12-08 21:14:56 +00:00
|
|
|
"io"
|
2014-11-18 21:33:13 +00:00
|
|
|
"io/ioutil"
|
2014-10-29 19:06:51 +00:00
|
|
|
"os"
|
2014-12-08 21:14:56 +00:00
|
|
|
"path/filepath"
|
2014-10-29 19:06:51 +00:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
func applyLayer() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
flag.Parse()
|
|
|
|
|
2014-12-08 21:14:56 +00:00
|
|
|
if err := chroot(flag.Arg(0)); err != nil {
|
2014-10-29 19:06:51 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-08 21:14:56 +00:00
|
|
|
// We need to be able to set any perms
|
|
|
|
oldmask := syscall.Umask(0)
|
|
|
|
defer syscall.Umask(oldmask)
|
2014-11-18 21:33:13 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("/", "temp-docker-extract")
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
os.Setenv("TMPDIR", tmpDir)
|
2014-12-08 21:14:56 +00:00
|
|
|
err = archive.UnpackLayer("/", os.Stdin)
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
if err != nil {
|
2014-10-29 19:06:51 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-08 23:04:34 +00:00
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
flush(os.Stdin)
|
2014-10-29 19:06:51 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplyLayer(dest string, layer archive.ArchiveReader) error {
|
2014-12-08 21:14:56 +00:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
decompressed, err := archive.DecompressStream(layer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if c, ok := decompressed.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-10-29 19:06:51 +00:00
|
|
|
cmd := reexec.Command("docker-applyLayer", dest)
|
2014-12-08 21:14:56 +00:00
|
|
|
cmd.Stdin = decompressed
|
2014-10-29 19:06:51 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ApplyLayer %s %s", err, out)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|