func MountSpecialDirs() error {
	dev := filepath.Join(mountAction, "dev")
	if err := syscall.Mount("none", dev, "devtmpfs", syscall.MS_RDONLY, ""); err != nil {
		return fmt.Errorf("Couldn't mount /dev to %v: %v", dev, err)
	}
	proc := filepath.Join(mountAction, "proc")
	if err := syscall.Mount("none", proc, "proc", syscall.MS_RDONLY, ""); err != nil {
		return fmt.Errorf("Couldn't mount /proc to %v: %v", proc, err)
	}
	return nil
}
MountSpecialDirs ensures that /dev /proc exist in the chroot
package main
import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"
	log "github.com/sirupsen/logrus"
)
var Release struct {
	Version string
	Build   string
}
const mountAction = "/mountAction"
func main() {
	fmt.Printf("CEXEC - Chroot Exec\n------------------------\n")
	blockDevice := os.Getenv("BLOCK_DEVICE")
	filesystemType := os.Getenv("FS_TYPE")
	chroot := os.Getenv("CHROOT")
	defaultInterpreter := os.Getenv("DEFAULT_INTERPRETER")
	cmdLine := os.Getenv("CMD_LINE")
	var exitChroot func() error
	if blockDevice == "" {
		log.Fatalf("No Block Device speified with Environment Variable [BLOCK_DEVICE]")
	}
	err := os.Mkdir(mountAction, os.ModeDir)
	if err != nil {
		log.Fatalf("Error creating the action Mountpoint [%s]", mountAction)
	}
	err = syscall.Mount(blockDevice, mountAction, filesystemType, 0, "")
	if err != nil {
		log.Fatalf("Mounting [%s] -> [%s] error [%v]", blockDevice, mountAction, err)
	}
	log.Infof("Mounted [%s] -> [%s]", blockDevice, mountAction)
	if chroot != "" {
		err = MountSpecialDirs()
		if err != nil {
			log.Fatal(err)
		}
		log.Infoln("Changing root before executing command")
		exitChroot, err = Chroot(mountAction)
		if err != nil {
			log.Fatalf("Error changing root to [%s]", mountAction)
		}
	}
	if defaultInterpreter != "" {
		di := strings.Split(defaultInterpreter, " ")
		if len(di) == 0 {
			log.Fatalln("Error parsing [\"DEFAULT_INTERPETER\"] [%s]", defaultInterpreter)
		}
		_, err = os.Stat(di[0])
		if os.IsNotExist(err) {
			log.Fatalf("Unable to find the [\"DEFAULT_INTERPETER\"] [%s], check chroot and interpreter path", defaultInterpreter)
		}
		di = append(di, cmdLine)
		cmd := exec.Command(di[0], di[1:]...)
		cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
		var debugCMD = fmt.Sprintf("%s %v", di[0], di[1:])
		err = cmd.Start()
		if err != nil {
			log.Fatalf("Error starting [%s] [%v]", debugCMD, err)
		}
		err = cmd.Wait()
		if err != nil {
			log.Fatalf("Error running [%s] [%v]", debugCMD, err)
		}
	} else {
		commandLines := strings.Split(cmdLine, ";")
		for x := range commandLines {
			command := strings.Split(commandLines[x], " ")
			cmd := exec.Command(command[0], command[1:]...)
			cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
			var debugCMD = fmt.Sprintf("%s %v", command[0], command[1:])
			err = cmd.Start()
			if err != nil {
				log.Fatalf("Error starting [%s] [%v]", debugCMD, err)
			}
			err = cmd.Wait()
			if err != nil {
				log.Fatalf("Error running [%s] [%v]", debugCMD, err)
			}
		}
	}
	if chroot != "" {
		err = exitChroot()
		if err != nil {
			log.Errorf("Error exiting root from [%s], execution continuing", mountAction)
		}
	}
}
func Chroot(path string) (func() error, error) {
	cwd, err := os.Getwd()
	if err != nil {
		return nil, err
	}
	root, err := os.Open(cwd)
	if err != nil {
		return nil, err
	}
	if err := syscall.Chroot(path); err != nil {
		root.Close()
		return nil, err
	}
	if err := syscall.Chdir("/"); err != nil {
		root.Close()
		return nil, err
	}
	return func() error {
		defer root.Close()
		if err := root.Chdir(); err != nil {
			return err
		}
		return syscall.Chroot(".")
	}, nil
}