Browse Source

Merge pull request #4096 from alexlarsson/fix-devmapper-race-2

Avoid extra mount/unmount during build
Michael Crosby 11 years ago
parent
commit
3f1e6b6368
1 changed files with 22 additions and 9 deletions
  1. 22 9
      buildfile.go

+ 22 - 9
buildfile.go

@@ -180,11 +180,20 @@ func (b *buildFile) CmdRun(args string) error {
 		return nil
 		return nil
 	}
 	}
 
 
-	cid, err := b.run()
+	c, err := b.create()
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	if err := b.commit(cid, cmd, "run"); err != nil {
+	// Ensure that we keep the container mounted until the commit
+	// to avoid unmounting and then mounting directly again
+	c.Mount()
+	defer c.Unmount()
+
+	err = b.run(c)
+	if err != nil {
+		return err
+	}
+	if err := b.commit(c.ID, cmd, "run"); err != nil {
 		return err
 		return err
 	}
 	}
 
 
@@ -555,16 +564,16 @@ func (sf *StderrFormater) Write(buf []byte) (int, error) {
 	return len(buf), err
 	return len(buf), err
 }
 }
 
 
-func (b *buildFile) run() (string, error) {
+func (b *buildFile) create() (*Container, error) {
 	if b.image == "" {
 	if b.image == "" {
-		return "", fmt.Errorf("Please provide a source image with `from` prior to run")
+		return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
 	}
 	}
 	b.config.Image = b.image
 	b.config.Image = b.image
 
 
 	// Create the container and start it
 	// Create the container and start it
 	c, _, err := b.runtime.Create(b.config, "")
 	c, _, err := b.runtime.Create(b.config, "")
 	if err != nil {
 	if err != nil {
-		return "", err
+		return nil, err
 	}
 	}
 	b.tmpContainers[c.ID] = struct{}{}
 	b.tmpContainers[c.ID] = struct{}{}
 	fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(c.ID))
 	fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(c.ID))
@@ -573,6 +582,10 @@ func (b *buildFile) run() (string, error) {
 	c.Path = b.config.Cmd[0]
 	c.Path = b.config.Cmd[0]
 	c.Args = b.config.Cmd[1:]
 	c.Args = b.config.Cmd[1:]
 
 
+	return c, nil
+}
+
+func (b *buildFile) run(c *Container) error {
 	var errCh chan error
 	var errCh chan error
 
 
 	if b.verbose {
 	if b.verbose {
@@ -583,12 +596,12 @@ func (b *buildFile) run() (string, error) {
 
 
 	//start the container
 	//start the container
 	if err := c.Start(); err != nil {
 	if err := c.Start(); err != nil {
-		return "", err
+		return err
 	}
 	}
 
 
 	if errCh != nil {
 	if errCh != nil {
 		if err := <-errCh; err != nil {
 		if err := <-errCh; err != nil {
-			return "", err
+			return err
 		}
 		}
 	}
 	}
 
 
@@ -598,10 +611,10 @@ func (b *buildFile) run() (string, error) {
 			Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.config.Cmd, ret),
 			Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.config.Cmd, ret),
 			Code:    ret,
 			Code:    ret,
 		}
 		}
-		return "", err
+		return err
 	}
 	}
 
 
-	return c.ID, nil
+	return nil
 }
 }
 
 
 // Commit the container <id> with the autorun command <autoCmd>
 // Commit the container <id> with the autorun command <autoCmd>