Browse code

Merge pull request #9267 from crosbymichael/devmapper-mknod

Mknod more loopbacks for devmapper

Vincent Batts authored on 2014/11/22 10:23:43
Showing 2 changed files
... ...
@@ -13,6 +13,9 @@ func init() {
13 13
 	DefaultDataLoopbackSize = 300 * 1024 * 1024
14 14
 	DefaultMetaDataLoopbackSize = 200 * 1024 * 1024
15 15
 	DefaultBaseFsSize = 300 * 1024 * 1024
16
+	if err := graphtest.InitLoopbacks(); err != nil {
17
+		panic(err)
18
+	}
16 19
 }
17 20
 
18 21
 // This avoids creating a new driver for each test if all tests are run
... ...
@@ -1,6 +1,7 @@
1 1
 package graphtest
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"io/ioutil"
5 6
 	"os"
6 7
 	"path"
... ...
@@ -20,6 +21,46 @@ type Driver struct {
20 20
 	refCount int
21 21
 }
22 22
 
23
+// InitLoopbacks ensures that the loopback devices are properly created within
24
+// the system running the device mapper tests.
25
+func InitLoopbacks() error {
26
+	stat_t, err := getBaseLoopStats()
27
+	if err != nil {
28
+		return err
29
+	}
30
+	// create atleast 8 loopback files, ya, that is a good number
31
+	for i := 0; i < 8; i++ {
32
+		loopPath := fmt.Sprintf("/dev/loop%d", i)
33
+		// only create new loopback files if they don't exist
34
+		if _, err := os.Stat(loopPath); err != nil {
35
+			if mkerr := syscall.Mknod(loopPath,
36
+				uint32(stat_t.Mode|syscall.S_IFBLK), int((7<<8)|(i&0xff)|((i&0xfff00)<<12))); mkerr != nil {
37
+				return mkerr
38
+			}
39
+			os.Chown(loopPath, int(stat_t.Uid), int(stat_t.Gid))
40
+		}
41
+	}
42
+	return nil
43
+}
44
+
45
+// getBaseLoopStats inspects /dev/loop0 to collect uid,gid, and mode for the
46
+// loop0 device on the system.  If it does not exist we assume 0,0,0660 for the
47
+// stat data
48
+func getBaseLoopStats() (*syscall.Stat_t, error) {
49
+	loop0, err := os.Stat("/dev/loop0")
50
+	if err != nil {
51
+		if os.IsNotExist(err) {
52
+			return &syscall.Stat_t{
53
+				Uid:  0,
54
+				Gid:  0,
55
+				Mode: 0660,
56
+			}, nil
57
+		}
58
+		return nil, err
59
+	}
60
+	return loop0.Sys().(*syscall.Stat_t), nil
61
+}
62
+
23 63
 func newDriver(t *testing.T, name string) *Driver {
24 64
 	root, err := ioutil.TempDir("/var/tmp", "docker-graphtest-")
25 65
 	if err != nil {