Skip to content

Commit

Permalink
Add code 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
xianlubird authored and Xianlu committed Jan 11, 2017
1 parent 285ab7b commit b24cc46
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
106 changes: 88 additions & 18 deletions container/container_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"os"
"os/exec"
"syscall"
"strings"
)

func NewParentProcess(tty bool) (*exec.Cmd, *os.File) {
func NewParentProcess(tty bool, volume string) (*exec.Cmd, *os.File) {
readPipe, writePipe, err := NewPipe()
if err != nil {
log.Errorf("New pipe error %v", err)
Expand All @@ -24,9 +25,9 @@ func NewParentProcess(tty bool) (*exec.Cmd, *os.File) {
cmd.Stderr = os.Stderr
}
cmd.ExtraFiles = []*os.File{readPipe}
mntURL := "/root/mnt/"
rootURL := "/root/"
NewWorkSpace(rootURL, mntURL)
mntURL := "/root/mnt"
rootURL := "/root"
NewWorkSpace(rootURL, mntURL, volume)
cmd.Dir = mntURL
return cmd, writePipe
}
Expand All @@ -40,22 +41,33 @@ func NewPipe() (*os.File, *os.File, error) {
}

//Create a AUFS filesystem as container root workspace
func NewWorkSpace(rootURL string, mntURL string) {
func NewWorkSpace(rootURL string, mntURL string, volume string) {
CreateReadOnlyLayer(rootURL)
CreateWriteLayer(rootURL)
CreateMountPoint(rootURL, mntURL)
if(volume != ""){
volumeURLs := volumeUrlExtract(volume)
length := len(volumeURLs)
if(length == 2 && volumeURLs[0] != "" && volumeURLs[1] !=""){
MountVolume(rootURL, mntURL, volumeURLs)
log.Infof("%q",volumeURLs)
}else{
log.Infof("Volume parameter input is not correct.")
}
}
}


func CreateReadOnlyLayer(rootURL string) {
busyboxURL := rootURL + "busybox/"
busyboxTarURL := rootURL + "busybox.tar"
busyboxURL := rootURL + "/busybox"
busyboxTarURL := rootURL + "/busybox.tar"
exist, err := PathExists(busyboxURL)
if err != nil {
log.Infof("Fail to judge whether dir %s exists. %v", busyboxURL, err)
}
if exist == false {
if err := os.Mkdir(busyboxURL, 0777); err != nil {
log.Errorf("Mkdir dir %s error. %v", busyboxURL, err)
log.Errorf("Mkdir busybox dir %s error. %v", busyboxURL, err)
}
if _, err := exec.Command("tar", "-xvf", busyboxTarURL, "-C", busyboxURL).CombinedOutput(); err != nil {
log.Errorf("Untar dir %s error %v", busyboxURL, err)
Expand All @@ -64,28 +76,58 @@ func CreateReadOnlyLayer(rootURL string) {
}

func CreateWriteLayer(rootURL string) {
writeURL := rootURL + "writeLayer/"
writeURL := rootURL + "/writeLayer"
if err := os.Mkdir(writeURL, 0777); err != nil {
log.Errorf("Mkdir dir %s error. %v", writeURL, err)
log.Infof("Mkdir write layer dir %s error. %v", writeURL, err)
}
}

func MountVolume(rootURL string, mntURL string, volumeURLs []string) {
parentUrl := volumeURLs[0]
if err := os.Mkdir(parentUrl, 0777); err != nil {
log.Infof("Mkdir parent dir %s error. %v", parentUrl, err)
}
containerUrl := volumeURLs[1]
containerVolumeURL := mntURL + containerUrl
if err := os.Mkdir(containerVolumeURL, 0777); err != nil {
log.Infof("Mkdir container dir %s error. %v", containerVolumeURL, err)
}
dirs := "dirs=" + parentUrl
cmd := exec.Command("mount", "-t", "aufs", "-o", dirs, "none", containerVolumeURL)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Errorf("Mount volume failed. %v", err)
}

}

func CreateMountPoint(rootURL string, mntURL string) {
if err := os.Mkdir(mntURL, 0777); err != nil {
log.Errorf("Mkdir dir %s error. %v", mntURL, err)
log.Infof("Mkdir mountpoint dir %s error. %v", mntURL, err)
}
dirs := "dirs=" + rootURL + "writeLayer:" + rootURL + "busybox"
dirs := "dirs=" + rootURL + "/writeLayer:" + rootURL + "/busybox"
cmd := exec.Command("mount", "-t", "aufs", "-o", dirs, "none", mntURL)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Errorf("%v", err)
log.Errorf("Mount mountpoint dir failed. %v", err)
}
}

//Delete the AUFS filesystem while container exit
func DeleteWorkSpace(rootURL string, mntURL string){
DeleteMountPoint(rootURL, mntURL)
func DeleteWorkSpace(rootURL string, mntURL string, volume string){
if(volume != ""){
volumeURLs := volumeUrlExtract(volume)
length := len(volumeURLs)
if(length == 2 && volumeURLs[0] != "" && volumeURLs[1] !=""){
DeleteMountPointWithVolume(rootURL, mntURL, volumeURLs)
}else{
DeleteMountPoint(rootURL, mntURL)
}
}else {
DeleteMountPoint(rootURL, mntURL)
}
DeleteWriteLayer(rootURL)
}

Expand All @@ -97,17 +139,39 @@ func DeleteMountPoint(rootURL string, mntURL string){
log.Errorf("%v",err)
}
if err := os.RemoveAll(mntURL); err != nil {
log.Errorf("Remove dir %s error %v", mntURL, err)
log.Infof("Remove mountpoint dir %s error %v", mntURL, err)
}
}

func DeleteMountPointWithVolume(rootURL string, mntURL string, volumeURLs []string){
containerUrl := mntURL + volumeURLs[1]
cmd := exec.Command("umount", containerUrl)
cmd.Stdout=os.Stdout
cmd.Stderr=os.Stderr
if err := cmd.Run(); err != nil {
log.Errorf("Umount volume failed. %v",err)
}

cmd = exec.Command("umount", mntURL)
cmd.Stdout=os.Stdout
cmd.Stderr=os.Stderr
if err := cmd.Run(); err != nil {
log.Errorf("Umount mountpoint failed. %v",err)
}

if err := os.RemoveAll(mntURL); err != nil {
log.Infof("Remove mountpoint dir %s error %v", mntURL, err)
}
}

func DeleteWriteLayer(rootURL string) {
writeURL := rootURL + "writeLayer/"
writeURL := rootURL + "/writeLayer"
if err := os.RemoveAll(writeURL); err != nil {
log.Errorf("Remove dir %s error %v", writeURL, err)
log.Infof("Remove writeLayer dir %s error %v", writeURL, err)
}
}


func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
Expand All @@ -118,3 +182,9 @@ func PathExists(path string) (bool, error) {
}
return false, err
}

func volumeUrlExtract(volume string)([]string){
var volumeURLs []string
volumeURLs = strings.Split(volume, ":")
return volumeURLs
}
7 changes: 6 additions & 1 deletion main_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var runCommand = cli.Command{
Name: "ti",
Usage: "enable tty",
},
cli.StringFlag{
Name: "v",
Usage: "volume",
},
},
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
Expand All @@ -26,7 +30,8 @@ var runCommand = cli.Command{
cmdArray = append(cmdArray, arg)
}
tty := context.Bool("ti")
Run(tty, cmdArray)
volume := context.String("v")
Run(tty, cmdArray, volume)
return nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)


func Run(tty bool, comArray []string) {
parent, writePipe := container.NewParentProcess(tty)
func Run(tty bool, comArray []string, volume string) {
parent, writePipe := container.NewParentProcess(tty, volume)
if parent == nil {
log.Errorf("New parent process error")
return
Expand All @@ -19,9 +19,9 @@ func Run(tty bool, comArray []string) {
}
sendInitCommand(comArray, writePipe)
parent.Wait()
mntURL := "/root/mnt/"
rootURL := "/root/"
container.DeleteWorkSpace(rootURL, mntURL)
mntURL := "/root/mnt"
rootURL := "/root"
container.DeleteWorkSpace(rootURL, mntURL, volume)
os.Exit(0)
}

Expand Down

0 comments on commit b24cc46

Please sign in to comment.