| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,170 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "flag" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "github.com/dotcloud/docker/graphdriver/devmapper" |
|
| 6 |
+ "os" |
|
| 7 |
+ "path" |
|
| 8 |
+ "sort" |
|
| 9 |
+ "strconv" |
|
| 10 |
+ "strings" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func usage() {
|
|
| 14 |
+ fmt.Fprintf(os.Stderr, "Usage: %s <flags> [status] | [list] | [device id] | [resize new-pool-size] | [snap new-id base-id] | [remove id] | [mount id mountpoint]\n", os.Args[0]) |
|
| 15 |
+ flag.PrintDefaults() |
|
| 16 |
+ os.Exit(1) |
|
| 17 |
+} |
|
| 18 |
+ |
|
| 19 |
+func byteSizeFromString(arg string) (int64, error) {
|
|
| 20 |
+ digits := "" |
|
| 21 |
+ rest := "" |
|
| 22 |
+ last := strings.LastIndexAny(arg, "0123456789") |
|
| 23 |
+ if last >= 0 {
|
|
| 24 |
+ digits = arg[:last+1] |
|
| 25 |
+ rest = arg[last+1:] |
|
| 26 |
+ } |
|
| 27 |
+ |
|
| 28 |
+ val, err := strconv.ParseInt(digits, 10, 64) |
|
| 29 |
+ if err != nil {
|
|
| 30 |
+ return val, err |
|
| 31 |
+ } |
|
| 32 |
+ |
|
| 33 |
+ rest = strings.ToLower(strings.TrimSpace(rest)) |
|
| 34 |
+ |
|
| 35 |
+ var multiplier int64 = 1 |
|
| 36 |
+ switch rest {
|
|
| 37 |
+ case "": |
|
| 38 |
+ multiplier = 1 |
|
| 39 |
+ case "k", "kb": |
|
| 40 |
+ multiplier = 1024 |
|
| 41 |
+ case "m", "mb": |
|
| 42 |
+ multiplier = 1024 * 1024 |
|
| 43 |
+ case "g", "gb": |
|
| 44 |
+ multiplier = 1024 * 1024 * 1024 |
|
| 45 |
+ case "t", "tb": |
|
| 46 |
+ multiplier = 1024 * 1024 * 1024 * 1024 |
|
| 47 |
+ default: |
|
| 48 |
+ return 0, fmt.Errorf("Unknown size unit: %s", rest)
|
|
| 49 |
+ } |
|
| 50 |
+ |
|
| 51 |
+ return val * multiplier, nil |
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+func main() {
|
|
| 55 |
+ root := flag.String("r", "/var/lib/docker", "Docker root dir")
|
|
| 56 |
+ flDebug := flag.Bool("D", false, "Debug mode")
|
|
| 57 |
+ |
|
| 58 |
+ flag.Parse() |
|
| 59 |
+ |
|
| 60 |
+ if *flDebug {
|
|
| 61 |
+ os.Setenv("DEBUG", "1")
|
|
| 62 |
+ } |
|
| 63 |
+ |
|
| 64 |
+ if flag.NArg() < 1 {
|
|
| 65 |
+ usage() |
|
| 66 |
+ } |
|
| 67 |
+ |
|
| 68 |
+ args := flag.Args() |
|
| 69 |
+ |
|
| 70 |
+ home := path.Join(*root, "devicemapper") |
|
| 71 |
+ devices, err := devmapper.NewDeviceSet(home, false) |
|
| 72 |
+ if err != nil {
|
|
| 73 |
+ fmt.Println("Can't initialize device mapper: ", err)
|
|
| 74 |
+ os.Exit(1) |
|
| 75 |
+ } |
|
| 76 |
+ |
|
| 77 |
+ switch args[0] {
|
|
| 78 |
+ case "status": |
|
| 79 |
+ status := devices.Status() |
|
| 80 |
+ fmt.Printf("Pool name: %s\n", status.PoolName)
|
|
| 81 |
+ fmt.Printf("Data Loopback file: %s\n", status.DataLoopback)
|
|
| 82 |
+ fmt.Printf("Metadata Loopback file: %s\n", status.MetadataLoopback)
|
|
| 83 |
+ fmt.Printf("Sector size: %d\n", status.SectorSize)
|
|
| 84 |
+ fmt.Printf("Data use: %d of %d (%.1f %%)\n", status.Data.Used, status.Data.Total, 100.0*float64(status.Data.Used)/float64(status.Data.Total))
|
|
| 85 |
+ fmt.Printf("Metadata use: %d of %d (%.1f %%)\n", status.Metadata.Used, status.Metadata.Total, 100.0*float64(status.Metadata.Used)/float64(status.Metadata.Total))
|
|
| 86 |
+ break |
|
| 87 |
+ case "list": |
|
| 88 |
+ ids := devices.List() |
|
| 89 |
+ sort.Strings(ids) |
|
| 90 |
+ for _, id := range ids {
|
|
| 91 |
+ fmt.Println(id) |
|
| 92 |
+ } |
|
| 93 |
+ break |
|
| 94 |
+ case "device": |
|
| 95 |
+ if flag.NArg() < 2 {
|
|
| 96 |
+ usage() |
|
| 97 |
+ } |
|
| 98 |
+ status, err := devices.GetDeviceStatus(args[1]) |
|
| 99 |
+ if err != nil {
|
|
| 100 |
+ fmt.Println("Can't get device info: ", err)
|
|
| 101 |
+ os.Exit(1) |
|
| 102 |
+ } |
|
| 103 |
+ fmt.Printf("Id: %d\n", status.DeviceId)
|
|
| 104 |
+ fmt.Printf("Size: %d\n", status.Size)
|
|
| 105 |
+ fmt.Printf("Transaction Id: %d\n", status.TransactionId)
|
|
| 106 |
+ fmt.Printf("Size in Sectors: %d\n", status.SizeInSectors)
|
|
| 107 |
+ fmt.Printf("Mapped Sectors: %d\n", status.MappedSectors)
|
|
| 108 |
+ fmt.Printf("Highest Mapped Sector: %d\n", status.HighestMappedSector)
|
|
| 109 |
+ break |
|
| 110 |
+ case "resize": |
|
| 111 |
+ if flag.NArg() < 2 {
|
|
| 112 |
+ usage() |
|
| 113 |
+ } |
|
| 114 |
+ |
|
| 115 |
+ size, err := byteSizeFromString(args[1]) |
|
| 116 |
+ if err != nil {
|
|
| 117 |
+ fmt.Println("Invalid size: ", err)
|
|
| 118 |
+ os.Exit(1) |
|
| 119 |
+ } |
|
| 120 |
+ |
|
| 121 |
+ err = devices.ResizePool(size) |
|
| 122 |
+ if err != nil {
|
|
| 123 |
+ fmt.Println("Error resizeing pool: ", err)
|
|
| 124 |
+ os.Exit(1) |
|
| 125 |
+ } |
|
| 126 |
+ |
|
| 127 |
+ break |
|
| 128 |
+ case "snap": |
|
| 129 |
+ if flag.NArg() < 3 {
|
|
| 130 |
+ usage() |
|
| 131 |
+ } |
|
| 132 |
+ |
|
| 133 |
+ err := devices.AddDevice(args[1], args[2]) |
|
| 134 |
+ if err != nil {
|
|
| 135 |
+ fmt.Println("Can't create snap device: ", err)
|
|
| 136 |
+ os.Exit(1) |
|
| 137 |
+ } |
|
| 138 |
+ break |
|
| 139 |
+ case "remove": |
|
| 140 |
+ if flag.NArg() < 2 {
|
|
| 141 |
+ usage() |
|
| 142 |
+ } |
|
| 143 |
+ |
|
| 144 |
+ err := devices.RemoveDevice(args[1]) |
|
| 145 |
+ if err != nil {
|
|
| 146 |
+ fmt.Println("Can't remove device: ", err)
|
|
| 147 |
+ os.Exit(1) |
|
| 148 |
+ } |
|
| 149 |
+ break |
|
| 150 |
+ case "mount": |
|
| 151 |
+ if flag.NArg() < 3 {
|
|
| 152 |
+ usage() |
|
| 153 |
+ } |
|
| 154 |
+ |
|
| 155 |
+ err := devices.MountDevice(args[1], args[2], false) |
|
| 156 |
+ if err != nil {
|
|
| 157 |
+ fmt.Println("Can't create snap device: ", err)
|
|
| 158 |
+ os.Exit(1) |
|
| 159 |
+ } |
|
| 160 |
+ break |
|
| 161 |
+ default: |
|
| 162 |
+ fmt.Printf("Unknown command %s\n", args[0])
|
|
| 163 |
+ usage() |
|
| 164 |
+ |
|
| 165 |
+ os.Exit(1) |
|
| 166 |
+ } |
|
| 167 |
+ |
|
| 168 |
+ return |
|
| 169 |
+} |
| 0 | 170 |
deleted file mode 100644 |
| ... | ... |
@@ -1,170 +0,0 @@ |
| 1 |
-package main |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "flag" |
|
| 5 |
- "fmt" |
|
| 6 |
- "github.com/dotcloud/docker/graphdriver/devmapper" |
|
| 7 |
- "os" |
|
| 8 |
- "path" |
|
| 9 |
- "sort" |
|
| 10 |
- "strconv" |
|
| 11 |
- "strings" |
|
| 12 |
-) |
|
| 13 |
- |
|
| 14 |
-func usage() {
|
|
| 15 |
- fmt.Fprintf(os.Stderr, "Usage: %s <flags> [status] | [list] | [device id] | [resize new-pool-size] | [snap new-id base-id] | [remove id] | [mount id mountpoint]\n", os.Args[0]) |
|
| 16 |
- flag.PrintDefaults() |
|
| 17 |
- os.Exit(1) |
|
| 18 |
-} |
|
| 19 |
- |
|
| 20 |
-func byteSizeFromString(arg string) (int64, error) {
|
|
| 21 |
- digits := "" |
|
| 22 |
- rest := "" |
|
| 23 |
- last := strings.LastIndexAny(arg, "0123456789") |
|
| 24 |
- if last >= 0 {
|
|
| 25 |
- digits = arg[:last+1] |
|
| 26 |
- rest = arg[last+1:] |
|
| 27 |
- } |
|
| 28 |
- |
|
| 29 |
- val, err := strconv.ParseInt(digits, 10, 64) |
|
| 30 |
- if err != nil {
|
|
| 31 |
- return val, err |
|
| 32 |
- } |
|
| 33 |
- |
|
| 34 |
- rest = strings.ToLower(strings.TrimSpace(rest)) |
|
| 35 |
- |
|
| 36 |
- var multiplier int64 = 1 |
|
| 37 |
- switch rest {
|
|
| 38 |
- case "": |
|
| 39 |
- multiplier = 1 |
|
| 40 |
- case "k", "kb": |
|
| 41 |
- multiplier = 1024 |
|
| 42 |
- case "m", "mb": |
|
| 43 |
- multiplier = 1024 * 1024 |
|
| 44 |
- case "g", "gb": |
|
| 45 |
- multiplier = 1024 * 1024 * 1024 |
|
| 46 |
- case "t", "tb": |
|
| 47 |
- multiplier = 1024 * 1024 * 1024 * 1024 |
|
| 48 |
- default: |
|
| 49 |
- return 0, fmt.Errorf("Unknown size unit: %s", rest)
|
|
| 50 |
- } |
|
| 51 |
- |
|
| 52 |
- return val * multiplier, nil |
|
| 53 |
-} |
|
| 54 |
- |
|
| 55 |
-func main() {
|
|
| 56 |
- root := flag.String("r", "/var/lib/docker", "Docker root dir")
|
|
| 57 |
- flDebug := flag.Bool("D", false, "Debug mode")
|
|
| 58 |
- |
|
| 59 |
- flag.Parse() |
|
| 60 |
- |
|
| 61 |
- if *flDebug {
|
|
| 62 |
- os.Setenv("DEBUG", "1")
|
|
| 63 |
- } |
|
| 64 |
- |
|
| 65 |
- if flag.NArg() < 1 {
|
|
| 66 |
- usage() |
|
| 67 |
- } |
|
| 68 |
- |
|
| 69 |
- args := flag.Args() |
|
| 70 |
- |
|
| 71 |
- home := path.Join(*root, "devicemapper") |
|
| 72 |
- devices, err := devmapper.NewDeviceSet(home, false) |
|
| 73 |
- if err != nil {
|
|
| 74 |
- fmt.Println("Can't initialize device mapper: ", err)
|
|
| 75 |
- os.Exit(1) |
|
| 76 |
- } |
|
| 77 |
- |
|
| 78 |
- switch args[0] {
|
|
| 79 |
- case "status": |
|
| 80 |
- status := devices.Status() |
|
| 81 |
- fmt.Printf("Pool name: %s\n", status.PoolName)
|
|
| 82 |
- fmt.Printf("Data Loopback file: %s\n", status.DataLoopback)
|
|
| 83 |
- fmt.Printf("Metadata Loopback file: %s\n", status.MetadataLoopback)
|
|
| 84 |
- fmt.Printf("Sector size: %d\n", status.SectorSize)
|
|
| 85 |
- fmt.Printf("Data use: %d of %d (%.1f %%)\n", status.Data.Used, status.Data.Total, 100.0*float64(status.Data.Used)/float64(status.Data.Total))
|
|
| 86 |
- fmt.Printf("Metadata use: %d of %d (%.1f %%)\n", status.Metadata.Used, status.Metadata.Total, 100.0*float64(status.Metadata.Used)/float64(status.Metadata.Total))
|
|
| 87 |
- break |
|
| 88 |
- case "list": |
|
| 89 |
- ids := devices.List() |
|
| 90 |
- sort.Strings(ids) |
|
| 91 |
- for _, id := range ids {
|
|
| 92 |
- fmt.Println(id) |
|
| 93 |
- } |
|
| 94 |
- break |
|
| 95 |
- case "device": |
|
| 96 |
- if flag.NArg() < 2 {
|
|
| 97 |
- usage() |
|
| 98 |
- } |
|
| 99 |
- status, err := devices.GetDeviceStatus(args[1]) |
|
| 100 |
- if err != nil {
|
|
| 101 |
- fmt.Println("Can't get device info: ", err)
|
|
| 102 |
- os.Exit(1) |
|
| 103 |
- } |
|
| 104 |
- fmt.Printf("Id: %d\n", status.DeviceId)
|
|
| 105 |
- fmt.Printf("Size: %d\n", status.Size)
|
|
| 106 |
- fmt.Printf("Transaction Id: %d\n", status.TransactionId)
|
|
| 107 |
- fmt.Printf("Size in Sectors: %d\n", status.SizeInSectors)
|
|
| 108 |
- fmt.Printf("Mapped Sectors: %d\n", status.MappedSectors)
|
|
| 109 |
- fmt.Printf("Highest Mapped Sector: %d\n", status.HighestMappedSector)
|
|
| 110 |
- break |
|
| 111 |
- case "resize": |
|
| 112 |
- if flag.NArg() < 2 {
|
|
| 113 |
- usage() |
|
| 114 |
- } |
|
| 115 |
- |
|
| 116 |
- size, err := byteSizeFromString(args[1]) |
|
| 117 |
- if err != nil {
|
|
| 118 |
- fmt.Println("Invalid size: ", err)
|
|
| 119 |
- os.Exit(1) |
|
| 120 |
- } |
|
| 121 |
- |
|
| 122 |
- err = devices.ResizePool(size) |
|
| 123 |
- if err != nil {
|
|
| 124 |
- fmt.Println("Error resizeing pool: ", err)
|
|
| 125 |
- os.Exit(1) |
|
| 126 |
- } |
|
| 127 |
- |
|
| 128 |
- break |
|
| 129 |
- case "snap": |
|
| 130 |
- if flag.NArg() < 3 {
|
|
| 131 |
- usage() |
|
| 132 |
- } |
|
| 133 |
- |
|
| 134 |
- err := devices.AddDevice(args[1], args[2]) |
|
| 135 |
- if err != nil {
|
|
| 136 |
- fmt.Println("Can't create snap device: ", err)
|
|
| 137 |
- os.Exit(1) |
|
| 138 |
- } |
|
| 139 |
- break |
|
| 140 |
- case "remove": |
|
| 141 |
- if flag.NArg() < 2 {
|
|
| 142 |
- usage() |
|
| 143 |
- } |
|
| 144 |
- |
|
| 145 |
- err := devices.RemoveDevice(args[1]) |
|
| 146 |
- if err != nil {
|
|
| 147 |
- fmt.Println("Can't remove device: ", err)
|
|
| 148 |
- os.Exit(1) |
|
| 149 |
- } |
|
| 150 |
- break |
|
| 151 |
- case "mount": |
|
| 152 |
- if flag.NArg() < 3 {
|
|
| 153 |
- usage() |
|
| 154 |
- } |
|
| 155 |
- |
|
| 156 |
- err := devices.MountDevice(args[1], args[2], false) |
|
| 157 |
- if err != nil {
|
|
| 158 |
- fmt.Println("Can't create snap device: ", err)
|
|
| 159 |
- os.Exit(1) |
|
| 160 |
- } |
|
| 161 |
- break |
|
| 162 |
- default: |
|
| 163 |
- fmt.Printf("Unknown command %s\n", args[0])
|
|
| 164 |
- usage() |
|
| 165 |
- |
|
| 166 |
- os.Exit(1) |
|
| 167 |
- } |
|
| 168 |
- |
|
| 169 |
- return |
|
| 170 |
-} |