2014-08-02 03:46:15 -04:00
|
|
|
// +build linux
|
2013-11-27 22:12:51 -05:00
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
package devicemapper
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2013-10-02 23:18:15 -04:00
|
|
|
import (
|
2013-10-03 21:00:24 -04:00
|
|
|
"errors"
|
2013-10-02 23:18:15 -04:00
|
|
|
"fmt"
|
2014-05-16 08:10:02 -04:00
|
|
|
"os"
|
2013-10-02 23:18:15 -04:00
|
|
|
"runtime"
|
2013-12-17 03:12:44 -05:00
|
|
|
"syscall"
|
2014-05-16 08:10:02 -04:00
|
|
|
|
2014-10-24 13:12:35 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2013-10-02 23:18:15 -04:00
|
|
|
)
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2013-10-17 18:04:14 -04:00
|
|
|
type DevmapperLogger interface {
|
2014-11-05 18:10:38 -05:00
|
|
|
DMLog(level int, file string, line int, dmError int, message string)
|
2013-10-10 09:24:39 -04:00
|
|
|
}
|
|
|
|
|
2013-09-04 05:14:31 -04:00
|
|
|
const (
|
|
|
|
DeviceCreate TaskType = iota
|
|
|
|
DeviceReload
|
|
|
|
DeviceRemove
|
|
|
|
DeviceRemoveAll
|
|
|
|
DeviceSuspend
|
|
|
|
DeviceResume
|
|
|
|
DeviceInfo
|
|
|
|
DeviceDeps
|
|
|
|
DeviceRename
|
|
|
|
DeviceVersion
|
|
|
|
DeviceStatus
|
|
|
|
DeviceTable
|
|
|
|
DeviceWaitevent
|
|
|
|
DeviceList
|
|
|
|
DeviceClear
|
|
|
|
DeviceMknodes
|
|
|
|
DeviceListVersions
|
|
|
|
DeviceTargetMsg
|
|
|
|
DeviceSetGeometry
|
|
|
|
)
|
|
|
|
|
2013-10-15 17:27:47 -04:00
|
|
|
const (
|
|
|
|
AddNodeOnResume AddNodeType = iota
|
|
|
|
AddNodeOnCreate
|
|
|
|
)
|
|
|
|
|
2013-10-03 21:00:24 -04:00
|
|
|
var (
|
2013-11-18 06:12:04 -05:00
|
|
|
ErrTaskRun = errors.New("dm_task_run failed")
|
|
|
|
ErrTaskSetName = errors.New("dm_task_set_name failed")
|
|
|
|
ErrTaskSetMessage = errors.New("dm_task_set_message failed")
|
|
|
|
ErrTaskSetAddNode = errors.New("dm_task_set_add_node failed")
|
|
|
|
ErrTaskSetRo = errors.New("dm_task_set_ro failed")
|
|
|
|
ErrTaskAddTarget = errors.New("dm_task_add_target failed")
|
|
|
|
ErrTaskSetSector = errors.New("dm_task_set_sector failed")
|
2014-09-25 10:57:37 -04:00
|
|
|
ErrTaskGetDeps = errors.New("dm_task_get_deps failed")
|
2013-11-18 06:12:04 -05:00
|
|
|
ErrTaskGetInfo = errors.New("dm_task_get_info failed")
|
2014-03-27 12:45:02 -04:00
|
|
|
ErrTaskGetDriverVersion = errors.New("dm_task_get_driver_version failed")
|
2013-11-18 06:12:04 -05:00
|
|
|
ErrTaskSetCookie = errors.New("dm_task_set_cookie failed")
|
|
|
|
ErrNilCookie = errors.New("cookie ptr can't be nil")
|
|
|
|
ErrAttachLoopbackDevice = errors.New("loopback mounting failed")
|
|
|
|
ErrGetBlockSize = errors.New("Can't get block size")
|
|
|
|
ErrUdevWait = errors.New("wait on udev cookie failed")
|
|
|
|
ErrSetDevDir = errors.New("dm_set_dev_dir failed")
|
|
|
|
ErrGetLibraryVersion = errors.New("dm_get_library_version failed")
|
|
|
|
ErrCreateRemoveTask = errors.New("Can't create task of type DeviceRemove")
|
2014-11-13 17:20:24 -05:00
|
|
|
ErrRunRemoveDevice = errors.New("running RemoveDevice failed")
|
2014-11-14 09:33:13 -05:00
|
|
|
ErrInvalidAddNode = errors.New("Invalid AddNode type")
|
2013-11-18 06:12:04 -05:00
|
|
|
ErrGetLoopbackBackingFile = errors.New("Unable to get loopback backing file")
|
|
|
|
ErrLoopbackSetCapacity = errors.New("Unable set loopback capacity")
|
2014-04-24 16:17:04 -04:00
|
|
|
ErrBusy = errors.New("Device is Busy")
|
2014-12-03 13:06:43 -05:00
|
|
|
ErrDeviceIdExists = errors.New("Device Id Exists")
|
2014-04-24 16:17:04 -04:00
|
|
|
|
2014-04-24 16:36:45 -04:00
|
|
|
dmSawBusy bool
|
|
|
|
dmSawExist bool
|
2013-10-03 21:00:24 -04:00
|
|
|
)
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2013-10-03 21:00:24 -04:00
|
|
|
type (
|
|
|
|
Task struct {
|
2013-11-13 17:36:31 -05:00
|
|
|
unmanaged *CDmTask
|
2013-10-03 21:00:24 -04:00
|
|
|
}
|
2014-09-25 10:57:37 -04:00
|
|
|
Deps struct {
|
|
|
|
Count uint32
|
|
|
|
Filler uint32
|
|
|
|
Device []uint64
|
|
|
|
}
|
2013-10-03 21:00:24 -04:00
|
|
|
Info struct {
|
|
|
|
Exists int
|
|
|
|
Suspended int
|
|
|
|
LiveTable int
|
|
|
|
InactiveTable int
|
|
|
|
OpenCount int32
|
|
|
|
EventNr uint32
|
|
|
|
Major uint32
|
|
|
|
Minor uint32
|
|
|
|
ReadOnly int
|
|
|
|
TargetCount int32
|
|
|
|
}
|
2013-10-17 18:04:14 -04:00
|
|
|
TaskType int
|
2013-10-15 17:27:47 -04:00
|
|
|
AddNodeType int
|
2013-10-03 21:00:24 -04:00
|
|
|
)
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
// Returns whether error conveys the information about device Id already
|
|
|
|
// exist or not. This will be true if device creation or snap creation
|
|
|
|
// operation fails if device or snap device already exists in pool.
|
|
|
|
// Current implementation is little crude as it scans the error string
|
|
|
|
// for exact pattern match. Replacing it with more robust implementation
|
|
|
|
// is desirable.
|
|
|
|
func DeviceIdExists(err error) bool {
|
|
|
|
return fmt.Sprint(err) == fmt.Sprint(ErrDeviceIdExists)
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:14:31 -04:00
|
|
|
func (t *Task) destroy() {
|
|
|
|
if t != nil {
|
2013-11-20 18:12:19 -05:00
|
|
|
DmTaskDestroy(t.unmanaged)
|
2013-09-04 05:14:31 -04:00
|
|
|
runtime.SetFinalizer(t, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 09:33:13 -05:00
|
|
|
// TaskCreateNamed is a convenience function for TaskCreate when a name
|
|
|
|
// will be set on the task as well
|
|
|
|
func TaskCreateNamed(t TaskType, name string) (*Task, error) {
|
|
|
|
task := TaskCreate(t)
|
|
|
|
if task == nil {
|
|
|
|
return nil, fmt.Errorf("Can't create task of type %d", int(t))
|
|
|
|
}
|
|
|
|
if err := task.SetName(name); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can't set task name %s", name)
|
|
|
|
}
|
|
|
|
return task, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskCreate initializes a devicemapper task of tasktype
|
2013-09-04 05:14:31 -04:00
|
|
|
func TaskCreate(tasktype TaskType) *Task {
|
2013-11-13 17:36:31 -05:00
|
|
|
Ctask := DmTaskCreate(int(tasktype))
|
|
|
|
if Ctask == nil {
|
2013-09-04 05:14:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-13 17:36:31 -05:00
|
|
|
task := &Task{unmanaged: Ctask}
|
2013-09-04 05:14:31 -04:00
|
|
|
runtime.SetFinalizer(task, (*Task).destroy)
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) Run() error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskRun(t.unmanaged); res != 1 {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrTaskRun
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) SetName(name string) error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetName(t.unmanaged, name); res != 1 {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrTaskSetName
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) SetMessage(message string) error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetMessage(t.unmanaged, message); res != 1 {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrTaskSetMessage
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Task) SetSector(sector uint64) error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetSector(t.unmanaged, sector); res != 1 {
|
2013-11-13 18:35:52 -05:00
|
|
|
return ErrTaskSetSector
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func (t *Task) SetCookie(cookie *uint, flags uint16) error {
|
2013-11-13 18:35:52 -05:00
|
|
|
if cookie == nil {
|
|
|
|
return ErrNilCookie
|
|
|
|
}
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetCookie(t.unmanaged, cookie, flags); res != 1 {
|
2013-11-13 18:35:52 -05:00
|
|
|
return ErrTaskSetCookie
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func (t *Task) SetAddNode(addNode AddNodeType) error {
|
2013-11-13 19:46:10 -05:00
|
|
|
if addNode != AddNodeOnResume && addNode != AddNodeOnCreate {
|
|
|
|
return ErrInvalidAddNode
|
|
|
|
}
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetAddNode(t.unmanaged, addNode); res != 1 {
|
2013-10-15 17:27:47 -04:00
|
|
|
return ErrTaskSetAddNode
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:14:31 -04:00
|
|
|
func (t *Task) SetRo() error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmTaskSetRo(t.unmanaged); res != 1 {
|
2013-11-13 19:46:10 -05:00
|
|
|
return ErrTaskSetRo
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func (t *Task) AddTarget(start, size uint64, ttype, params string) error {
|
|
|
|
if res := DmTaskAddTarget(t.unmanaged, start, size,
|
|
|
|
ttype, params); res != 1 {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrTaskAddTarget
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 10:57:37 -04:00
|
|
|
func (t *Task) GetDeps() (*Deps, error) {
|
|
|
|
var deps *Deps
|
|
|
|
if deps = DmTaskGetDeps(t.unmanaged); deps == nil {
|
|
|
|
return nil, ErrTaskGetDeps
|
|
|
|
}
|
|
|
|
return deps, nil
|
|
|
|
}
|
|
|
|
|
2013-09-04 05:14:31 -04:00
|
|
|
func (t *Task) GetInfo() (*Info, error) {
|
2013-11-13 17:36:31 -05:00
|
|
|
info := &Info{}
|
|
|
|
if res := DmTaskGetInfo(t.unmanaged, info); res != 1 {
|
2013-11-13 19:46:10 -05:00
|
|
|
return nil, ErrTaskGetInfo
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2014-03-27 12:45:02 -04:00
|
|
|
func (t *Task) GetDriverVersion() (string, error) {
|
|
|
|
res := DmTaskGetDriverVersion(t.unmanaged)
|
|
|
|
if res == "" {
|
|
|
|
return "", ErrTaskGetDriverVersion
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func (t *Task) GetNextTarget(next uintptr) (nextPtr uintptr, start uint64,
|
|
|
|
length uint64, targetType string, params string) {
|
|
|
|
|
|
|
|
return DmGetNextTarget(t.unmanaged, next, &start, &length,
|
|
|
|
&targetType, ¶ms),
|
|
|
|
start, length, targetType, params
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
|
2014-05-16 08:10:02 -04:00
|
|
|
func getLoopbackBackingFile(file *os.File) (uint64, uint64, error) {
|
2013-11-27 20:44:54 -05:00
|
|
|
loopInfo, err := ioctlLoopGetStatus64(file.Fd())
|
|
|
|
if err != nil {
|
2014-08-13 21:36:26 -04:00
|
|
|
log.Errorf("Error get loopback backing file: %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
return 0, 0, ErrGetLoopbackBackingFile
|
|
|
|
}
|
2013-11-27 20:44:54 -05:00
|
|
|
return loopInfo.loDevice, loopInfo.loInode, nil
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 08:10:02 -04:00
|
|
|
func LoopbackSetCapacity(file *os.File) error {
|
2013-11-27 20:44:54 -05:00
|
|
|
if err := ioctlLoopSetCapacity(file.Fd(), 0); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error loopbackSetCapacity: %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
return ErrLoopbackSetCapacity
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-16 08:10:02 -04:00
|
|
|
func FindLoopDeviceFor(file *os.File) *os.File {
|
2013-11-18 06:12:04 -05:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-16 08:10:02 -04:00
|
|
|
targetInode := stat.Sys().(*syscall.Stat_t).Ino
|
|
|
|
targetDevice := stat.Sys().(*syscall.Stat_t).Dev
|
2013-11-18 06:12:04 -05:00
|
|
|
|
|
|
|
for i := 0; true; i++ {
|
|
|
|
path := fmt.Sprintf("/dev/loop%d", i)
|
|
|
|
|
2014-05-16 08:10:02 -04:00
|
|
|
file, err := os.OpenFile(path, os.O_RDWR, 0)
|
2013-11-18 06:12:04 -05:00
|
|
|
if err != nil {
|
2014-05-16 08:10:02 -04:00
|
|
|
if os.IsNotExist(err) {
|
2013-11-18 06:12:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore all errors until the first not-exist
|
|
|
|
// we want to continue looking for the file
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
dev, inode, err := getLoopbackBackingFile(file)
|
2013-11-18 06:12:04 -05:00
|
|
|
if err == nil && dev == targetDevice && inode == targetInode {
|
2013-11-27 02:16:34 -05:00
|
|
|
return file
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func UdevWait(cookie uint) error {
|
|
|
|
if res := DmUdevWait(cookie); res != 1 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Failed to wait on udev cookie %d", cookie)
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrUdevWait
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LogInitVerbose(level int) {
|
2013-11-13 17:36:31 -05:00
|
|
|
DmLogInitVerbose(level)
|
2013-09-04 05:14:31 -04:00
|
|
|
}
|
2013-09-19 14:32:11 -04:00
|
|
|
|
2013-10-10 09:24:39 -04:00
|
|
|
var dmLogger DevmapperLogger = nil
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
// initialize the logger for the device mapper library
|
|
|
|
func LogInit(logger DevmapperLogger) {
|
2013-10-10 09:24:39 -04:00
|
|
|
dmLogger = logger
|
2013-11-13 17:36:31 -05:00
|
|
|
LogWithErrnoInit()
|
2013-10-10 09:24:39 -04:00
|
|
|
}
|
|
|
|
|
2013-10-03 21:00:24 -04:00
|
|
|
func SetDevDir(dir string) error {
|
2013-11-13 17:36:31 -05:00
|
|
|
if res := DmSetDevDir(dir); res != 1 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Error dm_set_dev_dir")
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrSetDevDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLibraryVersion() (string, error) {
|
2013-11-13 17:36:31 -05:00
|
|
|
var version string
|
|
|
|
if res := DmGetLibraryVersion(&version); res != 1 {
|
2013-10-03 21:00:24 -04:00
|
|
|
return "", ErrGetLibraryVersion
|
|
|
|
}
|
2013-11-13 17:36:31 -05:00
|
|
|
return version, nil
|
2013-10-03 21:00:24 -04:00
|
|
|
}
|
|
|
|
|
2013-09-19 14:32:11 -04:00
|
|
|
// Useful helper for cleanup
|
|
|
|
func RemoveDevice(name string) error {
|
2014-11-13 17:20:24 -05:00
|
|
|
log.Debugf("[devmapper] RemoveDevice START")
|
|
|
|
defer log.Debugf("[devmapper] RemoveDevice END")
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceRemove, name)
|
2013-09-19 14:32:11 -04:00
|
|
|
if task == nil {
|
2013-10-03 21:00:24 -04:00
|
|
|
return err
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
2014-11-13 17:20:24 -05:00
|
|
|
|
|
|
|
var cookie uint = 0
|
|
|
|
if err := task.SetCookie(&cookie, 0); err != nil {
|
|
|
|
return fmt.Errorf("Can not set cookie: %s", err)
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
2014-11-14 14:18:35 -05:00
|
|
|
defer UdevWait(cookie)
|
2014-11-13 17:20:24 -05:00
|
|
|
|
2014-11-14 14:18:35 -05:00
|
|
|
dmSawBusy = false // reset before the task is run
|
2014-11-13 17:20:24 -05:00
|
|
|
if err = task.Run(); err != nil {
|
|
|
|
if dmSawBusy {
|
|
|
|
return ErrBusy
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error running RemoveDevice %s", err)
|
|
|
|
}
|
|
|
|
|
2013-09-19 14:32:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-10-03 21:00:24 -04:00
|
|
|
|
2014-05-16 08:10:02 -04:00
|
|
|
func GetBlockDeviceSize(file *os.File) (uint64, error) {
|
2013-11-27 20:44:54 -05:00
|
|
|
size, err := ioctlBlkGetSize64(file.Fd())
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error getblockdevicesize: %s", err)
|
2013-11-13 19:46:10 -05:00
|
|
|
return 0, ErrGetBlockSize
|
|
|
|
}
|
|
|
|
return uint64(size), nil
|
|
|
|
}
|
|
|
|
|
2013-12-17 03:12:44 -05:00
|
|
|
func BlockDeviceDiscard(path string) error {
|
2014-05-16 08:10:02 -04:00
|
|
|
file, err := os.OpenFile(path, os.O_RDWR, 0)
|
2013-12-17 03:12:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
size, err := GetBlockDeviceSize(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioctlBlkDiscard(file.Fd(), 0, size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Without this sometimes the remove of the device that happens after
|
|
|
|
// discard fails with EBUSY.
|
|
|
|
syscall.Sync()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-16 21:42:05 -04:00
|
|
|
// This is the programmatic example of "dmsetup create"
|
2014-11-05 18:10:38 -05:00
|
|
|
func CreatePool(poolName string, dataFile, metadataFile *os.File, poolBlockSize uint32) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceCreate, poolName)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := GetBlockDeviceSize(dataFile)
|
|
|
|
if err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't get data size %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2014-06-20 11:53:18 -04:00
|
|
|
params := fmt.Sprintf("%s %s %d 32768 1 skip_block_zeroing", metadataFile.Name(), dataFile.Name(), poolBlockSize)
|
2013-10-07 08:06:24 -04:00
|
|
|
if err := task.AddTarget(0, size/512, "thin-pool", params); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't add target %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
var cookie uint = 0
|
2014-10-30 20:26:39 -04:00
|
|
|
var flags uint16 = DmUdevDisableSubsystemRulesFlag | DmUdevDisableDiskRulesFlag | DmUdevDisableOtherRulesFlag
|
|
|
|
if err := task.SetCookie(&cookie, flags); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2014-11-14 18:15:56 -05:00
|
|
|
defer UdevWait(cookie)
|
2013-10-07 08:06:24 -04:00
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (CreatePool) %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func ReloadPool(poolName string, dataFile, metadataFile *os.File, poolBlockSize uint32) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceReload, poolName)
|
2013-11-18 06:12:04 -05:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := GetBlockDeviceSize(dataFile)
|
|
|
|
if err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't get data size %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
2014-06-20 11:53:18 -04:00
|
|
|
params := fmt.Sprintf("%s %s %d 32768 1 skip_block_zeroing", metadataFile.Name(), dataFile.Name(), poolBlockSize)
|
2013-11-18 06:12:04 -05:00
|
|
|
if err := task.AddTarget(0, size/512, "thin-pool", params); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't add target %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Error running DeviceCreate %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetDeps(name string) (*Deps, error) {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceDeps, name)
|
2014-09-25 10:57:37 -04:00
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return task.GetDeps()
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetInfo(name string) (*Info, error) {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceInfo, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return task.GetInfo()
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetDriverVersion() (string, error) {
|
2014-03-27 12:45:02 -04:00
|
|
|
task := TaskCreate(DeviceVersion)
|
|
|
|
if task == nil {
|
|
|
|
return "", fmt.Errorf("Can't create DeviceVersion task")
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return task.GetDriverVersion()
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetStatus(name string) (uint64, uint64, string, string, error) {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceStatus, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
2014-11-14 09:33:13 -05:00
|
|
|
log.Debugf("GetStatus: Error TaskCreateNamed: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
log.Debugf("GetStatus: Error Run: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
devinfo, err := task.GetInfo()
|
|
|
|
if err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
log.Debugf("GetStatus: Error GetInfo: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if devinfo.Exists == 0 {
|
2014-11-05 18:10:38 -05:00
|
|
|
log.Debugf("GetStatus: Non existing device %s", name)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", fmt.Errorf("Non existing device %s", name)
|
|
|
|
}
|
|
|
|
|
2013-11-20 15:49:01 -05:00
|
|
|
_, start, length, targetType, params := task.GetNextTarget(0)
|
|
|
|
return start, length, targetType, params, nil
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func SetTransactionId(poolName string, oldId uint64, newId uint64) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set sector %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("set_transaction_id %d %d", oldId, newId)); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set message %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
return fmt.Errorf("Error running SetTransactionId %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func SuspendDevice(name string) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceSuspend, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Error running DeviceSuspend %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func ResumeDevice(name string) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceResume, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
var cookie uint = 0
|
2013-10-07 08:06:24 -04:00
|
|
|
if err := task.SetCookie(&cookie, 0); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2014-11-14 18:15:56 -05:00
|
|
|
defer UdevWait(cookie)
|
2013-10-07 08:06:24 -04:00
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Error running DeviceResume %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
func CreateDevice(poolName string, deviceId int) error {
|
|
|
|
log.Debugf("[devmapper] CreateDevice(poolName=%v, deviceId=%v)", poolName, deviceId)
|
|
|
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
return fmt.Errorf("Can't set sector %s", err)
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
if err := task.SetMessage(fmt.Sprintf("create_thin %d", deviceId)); err != nil {
|
|
|
|
return fmt.Errorf("Can't set message %s", err)
|
|
|
|
}
|
2014-04-24 16:36:45 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
dmSawExist = false // reset before the task is run
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
// Caller wants to know about ErrDeviceIdExists so that it can try with a different device id.
|
|
|
|
if dmSawExist {
|
|
|
|
return ErrDeviceIdExists
|
|
|
|
} else {
|
2014-11-05 18:10:38 -05:00
|
|
|
return fmt.Errorf("Error running CreateDevice %s", err)
|
2014-04-24 16:36:45 -04:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func DeleteDevice(poolName string, deviceId int) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set sector %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("delete %d", deviceId)); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set message %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
return fmt.Errorf("Error running DeleteDevice %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
func ActivateDevice(poolName string, name string, deviceId int, size uint64) error {
|
2014-11-14 09:33:13 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceCreate, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
params := fmt.Sprintf("%s %d", poolName, deviceId)
|
|
|
|
if err := task.AddTarget(0, size/512, "thin", params); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't add target %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2013-10-15 17:27:47 -04:00
|
|
|
if err := task.SetAddNode(AddNodeOnCreate); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't add node %s", err)
|
2013-10-15 17:27:47 -04:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
var cookie uint = 0
|
2013-10-07 08:06:24 -04:00
|
|
|
if err := task.SetCookie(&cookie, 0); err != nil {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2014-11-14 18:15:56 -05:00
|
|
|
defer UdevWait(cookie)
|
|
|
|
|
2013-10-07 08:06:24 -04:00
|
|
|
if err := task.Run(); err != nil {
|
2014-11-05 18:10:38 -05:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (ActivateDevice) %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
func CreateSnapDevice(poolName string, deviceId int, baseName string, baseDeviceId int) error {
|
2014-11-05 18:10:38 -05:00
|
|
|
devinfo, _ := GetInfo(baseName)
|
2013-10-07 08:06:24 -04:00
|
|
|
doSuspend := devinfo != nil && devinfo.Exists != 0
|
|
|
|
|
|
|
|
if doSuspend {
|
2014-11-05 18:10:38 -05:00
|
|
|
if err := SuspendDevice(baseName); err != nil {
|
2013-10-07 08:06:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
if doSuspend {
|
|
|
|
ResumeDevice(baseName)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2014-12-03 13:06:43 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
ResumeDevice(baseName)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2014-12-03 13:06:43 -05:00
|
|
|
return fmt.Errorf("Can't set sector %s", err)
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
if err := task.SetMessage(fmt.Sprintf("create_snap %d %d", deviceId, baseDeviceId)); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
ResumeDevice(baseName)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2014-12-03 13:06:43 -05:00
|
|
|
return fmt.Errorf("Can't set message %s", err)
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
dmSawExist = false // reset before the task is run
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
ResumeDevice(baseName)
|
|
|
|
}
|
|
|
|
// Caller wants to know about ErrDeviceIdExists so that it can try with a different device id.
|
|
|
|
if dmSawExist {
|
|
|
|
return ErrDeviceIdExists
|
|
|
|
} else {
|
2014-05-27 22:03:57 -04:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (createSnapDevice) %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if doSuspend {
|
2014-11-05 18:10:38 -05:00
|
|
|
if err := ResumeDevice(baseName); err != nil {
|
2013-10-07 08:06:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|