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