2016-06-07 03:45:21 -04:00
|
|
|
// +build !windows,!solaris
|
2015-10-31 14:32:37 -04:00
|
|
|
|
2013-09-04 05:25:32 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-11-18 05:33:07 -05:00
|
|
|
"flag"
|
2013-09-04 05:25:32 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2013-11-18 05:33:07 -05:00
|
|
|
"path"
|
|
|
|
"sort"
|
2013-11-18 06:12:04 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-11-24 13:49:09 -05:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-11-24 13:49:09 -05:00
|
|
|
"github.com/docker/docker/daemon/graphdriver/devmapper"
|
|
|
|
"github.com/docker/docker/pkg/devicemapper"
|
2013-09-04 05:25:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
2013-11-18 06:12:04 -05:00
|
|
|
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])
|
2013-11-18 05:33:07 -05:00
|
|
|
flag.PrintDefaults()
|
2013-09-04 05:25:32 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-11-18 06:12:04 -05:00
|
|
|
func byteSizeFromString(arg string) (int64, error) {
|
|
|
|
digits := ""
|
|
|
|
rest := ""
|
|
|
|
last := strings.LastIndexAny(arg, "0123456789")
|
|
|
|
if last >= 0 {
|
|
|
|
digits = arg[:last+1]
|
|
|
|
rest = arg[last+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.ParseInt(digits, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rest = strings.ToLower(strings.TrimSpace(rest))
|
|
|
|
|
|
|
|
var multiplier int64 = 1
|
|
|
|
switch rest {
|
|
|
|
case "":
|
|
|
|
multiplier = 1
|
|
|
|
case "k", "kb":
|
|
|
|
multiplier = 1024
|
|
|
|
case "m", "mb":
|
|
|
|
multiplier = 1024 * 1024
|
|
|
|
case "g", "gb":
|
|
|
|
multiplier = 1024 * 1024 * 1024
|
|
|
|
case "t", "tb":
|
|
|
|
multiplier = 1024 * 1024 * 1024 * 1024
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("Unknown size unit: %s", rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return val * multiplier, nil
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:25:32 -04:00
|
|
|
func main() {
|
2013-11-18 05:33:07 -05:00
|
|
|
root := flag.String("r", "/var/lib/docker", "Docker root dir")
|
|
|
|
flDebug := flag.Bool("D", false, "Debug mode")
|
2013-09-04 05:25:32 -04:00
|
|
|
|
2013-11-18 05:33:07 -05:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *flDebug {
|
|
|
|
os.Setenv("DEBUG", "1")
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2013-11-18 05:33:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
2013-09-04 05:25:32 -04:00
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2013-11-18 05:33:07 -05:00
|
|
|
args := flag.Args()
|
|
|
|
|
|
|
|
home := path.Join(*root, "devicemapper")
|
2015-10-28 19:43:53 -04:00
|
|
|
devices, err := devmapper.NewDeviceSet(home, false, nil, nil, nil)
|
2013-11-18 05:33:07 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Can't initialize device mapper: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch args[0] {
|
|
|
|
case "status":
|
|
|
|
status := devices.Status()
|
|
|
|
fmt.Printf("Pool name: %s\n", status.PoolName)
|
|
|
|
fmt.Printf("Data Loopback file: %s\n", status.DataLoopback)
|
|
|
|
fmt.Printf("Metadata Loopback file: %s\n", status.MetadataLoopback)
|
|
|
|
fmt.Printf("Sector size: %d\n", status.SectorSize)
|
|
|
|
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))
|
|
|
|
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))
|
|
|
|
break
|
|
|
|
case "list":
|
|
|
|
ids := devices.List()
|
|
|
|
sort.Strings(ids)
|
|
|
|
for _, id := range ids {
|
|
|
|
fmt.Println(id)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case "device":
|
|
|
|
if flag.NArg() < 2 {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
status, err := devices.GetDeviceStatus(args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Can't get device info: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-10-28 19:43:53 -04:00
|
|
|
fmt.Printf("Id: %d\n", status.DeviceID)
|
2013-11-18 05:33:07 -05:00
|
|
|
fmt.Printf("Size: %d\n", status.Size)
|
2015-10-28 19:43:53 -04:00
|
|
|
fmt.Printf("Transaction Id: %d\n", status.TransactionID)
|
2013-11-18 05:33:07 -05:00
|
|
|
fmt.Printf("Size in Sectors: %d\n", status.SizeInSectors)
|
|
|
|
fmt.Printf("Mapped Sectors: %d\n", status.MappedSectors)
|
|
|
|
fmt.Printf("Highest Mapped Sector: %d\n", status.HighestMappedSector)
|
2013-11-18 06:12:04 -05:00
|
|
|
break
|
|
|
|
case "resize":
|
|
|
|
if flag.NArg() < 2 {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := byteSizeFromString(args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Invalid size: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = devices.ResizePool(size)
|
|
|
|
if err != nil {
|
2015-04-27 16:33:30 -04:00
|
|
|
fmt.Println("Error resizing pool: ", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-11-18 05:33:07 -05:00
|
|
|
break
|
|
|
|
case "snap":
|
|
|
|
if flag.NArg() < 3 {
|
2013-09-04 05:25:32 -04:00
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:42:58 -04:00
|
|
|
err := devices.AddDevice(args[1], args[2], nil)
|
2013-09-04 05:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Can't create snap device: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-11-18 05:33:07 -05:00
|
|
|
break
|
|
|
|
case "remove":
|
|
|
|
if flag.NArg() < 2 {
|
2013-09-04 05:25:32 -04:00
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2014-11-24 13:49:09 -05:00
|
|
|
err := devicemapper.RemoveDevice(args[1])
|
2013-09-04 05:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Can't remove device: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-11-18 05:33:07 -05:00
|
|
|
break
|
|
|
|
case "mount":
|
|
|
|
if flag.NArg() < 3 {
|
2013-09-04 05:25:32 -04:00
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2014-11-24 13:49:09 -05:00
|
|
|
err := devices.MountDevice(args[1], args[2], "")
|
2013-09-04 05:25:32 -04:00
|
|
|
if err != nil {
|
2016-12-13 02:57:00 -05:00
|
|
|
fmt.Println("Can't mount device: ", err)
|
2013-09-04 05:25:32 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-11-18 05:33:07 -05:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Printf("Unknown command %s\n", args[0])
|
|
|
|
usage()
|
2013-09-04 05:25:32 -04:00
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|