2013-11-27 22:12:51 -05:00
|
|
|
// +build linux
|
|
|
|
|
2013-09-04 05:14:31 -04:00
|
|
|
package devmapper
|
|
|
|
|
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"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"runtime"
|
|
|
|
)
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2013-10-17 18:04:14 -04:00
|
|
|
type DevmapperLogger interface {
|
2013-10-10 09:24:39 -04:00
|
|
|
log(level int, file string, line int, dmError int, message string)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
ErrTaskGetInfo = errors.New("dm_task_get_info failed")
|
|
|
|
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")
|
|
|
|
ErrRunRemoveDevice = errors.New("running removeDevice failed")
|
|
|
|
ErrInvalidAddNode = errors.New("Invalide AddNoce type")
|
|
|
|
ErrGetLoopbackBackingFile = errors.New("Unable to get loopback backing file")
|
|
|
|
ErrLoopbackSetCapacity = errors.New("Unable set loopback capacity")
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
func getLoopbackBackingFile(file *osFile) (uint64, uint64, error) {
|
2013-11-27 20:44:54 -05:00
|
|
|
loopInfo, err := ioctlLoopGetStatus64(file.Fd())
|
|
|
|
if err != nil {
|
|
|
|
utils.Errorf("Error get loopback backing file: %s\n", 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
|
|
|
}
|
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
func LoopbackSetCapacity(file *osFile) error {
|
2013-11-27 20:44:54 -05:00
|
|
|
if err := ioctlLoopSetCapacity(file.Fd(), 0); err != nil {
|
|
|
|
utils.Errorf("Error loopbackSetCapacity: %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
return ErrLoopbackSetCapacity
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
func FindLoopDeviceFor(file *osFile) *osFile {
|
2013-11-18 06:12:04 -05:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-20 15:49:01 -05:00
|
|
|
targetInode := stat.Sys().(*sysStatT).Ino
|
|
|
|
targetDevice := stat.Sys().(*sysStatT).Dev
|
2013-11-18 06:12:04 -05:00
|
|
|
|
|
|
|
for i := 0; true; i++ {
|
|
|
|
path := fmt.Sprintf("/dev/loop%d", i)
|
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
file, err := osOpenFile(path, osORdWr, 0)
|
2013-11-18 06:12:04 -05:00
|
|
|
if err != nil {
|
2013-11-20 16:05:17 -05:00
|
|
|
if osIsNotExist(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 {
|
2013-10-03 21:00:24 -04:00
|
|
|
utils.Debugf("Failed to wait on udev cookie %d", cookie)
|
|
|
|
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
|
|
|
|
|
|
|
|
func logInit(logger DevmapperLogger) {
|
|
|
|
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 {
|
2013-10-03 21:00:24 -04:00
|
|
|
utils.Debugf("Error dm_set_dev_dir")
|
|
|
|
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 {
|
|
|
|
task := TaskCreate(DeviceRemove)
|
|
|
|
if task == nil {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrCreateRemoveTask
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
2013-10-02 23:18:15 -04:00
|
|
|
if err := task.SetName(name); err != nil {
|
2013-10-03 21:00:24 -04:00
|
|
|
utils.Debugf("Can't set task name %s", name)
|
|
|
|
return err
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
2013-10-02 23:18:15 -04:00
|
|
|
if err := task.Run(); err != nil {
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrRunRemoveDevice
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-10-03 21:00:24 -04:00
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
func GetBlockDeviceSize(file *osFile) (uint64, error) {
|
2013-11-27 20:44:54 -05:00
|
|
|
size, err := ioctlBlkGetSize64(file.Fd())
|
|
|
|
if err != nil {
|
|
|
|
utils.Errorf("Error getblockdevicesize: %s", err)
|
2013-11-13 19:46:10 -05:00
|
|
|
return 0, ErrGetBlockSize
|
|
|
|
}
|
|
|
|
return uint64(size), nil
|
|
|
|
}
|
|
|
|
|
2013-10-16 21:42:05 -04:00
|
|
|
// This is the programmatic example of "dmsetup create"
|
2013-11-20 16:05:17 -05:00
|
|
|
func createPool(poolName string, dataFile, metadataFile *osFile) error {
|
2013-10-07 08:06:24 -04:00
|
|
|
task, err := createTask(DeviceCreate, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := GetBlockDeviceSize(dataFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't get data size")
|
|
|
|
}
|
|
|
|
|
2013-10-17 09:33:00 -04:00
|
|
|
params := metadataFile.Name() + " " + dataFile.Name() + " 128 32768"
|
2013-10-07 08:06:24 -04:00
|
|
|
if err := task.AddTarget(0, size/512, "thin-pool", params); err != nil {
|
|
|
|
return fmt.Errorf("Can't add target")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Can't set cookie")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2013-11-13 19:46:10 -05:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (createPool)")
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
UdevWait(cookie)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-20 16:05:17 -05:00
|
|
|
func reloadPool(poolName string, dataFile, metadataFile *osFile) error {
|
2013-11-18 06:12:04 -05:00
|
|
|
task, err := createTask(DeviceReload, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := GetBlockDeviceSize(dataFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can't get data size")
|
|
|
|
}
|
|
|
|
|
|
|
|
params := metadataFile.Name() + " " + dataFile.Name() + " 128 32768"
|
|
|
|
if err := task.AddTarget(0, size/512, "thin-pool", params); err != nil {
|
|
|
|
return fmt.Errorf("Can't add target")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return fmt.Errorf("Error running DeviceCreate")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-07 08:06:24 -04:00
|
|
|
func createTask(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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInfo(name string) (*Info, error) {
|
|
|
|
task, err := createTask(DeviceInfo, name)
|
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return task.GetInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStatus(name string) (uint64, uint64, string, string, error) {
|
|
|
|
task, err := createTask(DeviceStatus, name)
|
|
|
|
if task == nil {
|
|
|
|
utils.Debugf("getStatus: Error createTask: %s", err)
|
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
utils.Debugf("getStatus: Error Run: %s", err)
|
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
devinfo, err := task.GetInfo()
|
|
|
|
if err != nil {
|
|
|
|
utils.Debugf("getStatus: Error GetInfo: %s", err)
|
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if devinfo.Exists == 0 {
|
|
|
|
utils.Debugf("getStatus: Non existing device %s", name)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func setTransactionId(poolName string, oldId uint64, newId uint64) error {
|
|
|
|
task, err := createTask(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
return fmt.Errorf("Can't set sector")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("set_transaction_id %d %d", oldId, newId)); err != nil {
|
|
|
|
return fmt.Errorf("Can't set message")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return fmt.Errorf("Error running setTransactionId")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func suspendDevice(name string) error {
|
|
|
|
task, err := createTask(DeviceSuspend, name)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := task.Run(); err != nil {
|
2013-11-27 02:16:34 -05:00
|
|
|
return fmt.Errorf("Error running DeviceSuspend: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resumeDevice(name string) error {
|
|
|
|
task, err := createTask(DeviceResume, name)
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Can't set cookie")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2013-11-27 02:16:34 -05:00
|
|
|
return fmt.Errorf("Error running DeviceResume")
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
UdevWait(cookie)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDevice(poolName string, deviceId int) error {
|
2013-10-16 16:45:59 -04:00
|
|
|
utils.Debugf("[devmapper] createDevice(poolName=%v, deviceId=%v)", poolName, deviceId)
|
2013-10-07 08:06:24 -04:00
|
|
|
task, err := createTask(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
return fmt.Errorf("Can't set sector")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("create_thin %d", deviceId)); err != nil {
|
|
|
|
return fmt.Errorf("Can't set message")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return fmt.Errorf("Error running createDevice")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteDevice(poolName string, deviceId int) error {
|
|
|
|
task, err := createTask(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
return fmt.Errorf("Can't set sector")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("delete %d", deviceId)); err != nil {
|
|
|
|
return fmt.Errorf("Can't set message")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
return fmt.Errorf("Error running deleteDevice")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeDevice(name string) error {
|
2013-10-16 19:27:33 -04:00
|
|
|
utils.Debugf("[devmapper] removeDevice START")
|
|
|
|
defer utils.Debugf("[devmapper] removeDevice END")
|
2013-10-07 08:06:24 -04:00
|
|
|
task, err := createTask(DeviceRemove, name)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = task.Run(); err != nil {
|
|
|
|
return fmt.Errorf("Error running removeDevice")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func activateDevice(poolName string, name string, deviceId int, size uint64) error {
|
|
|
|
task, err := createTask(DeviceCreate, name)
|
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
params := fmt.Sprintf("%s %d", poolName, deviceId)
|
|
|
|
if err := task.AddTarget(0, size/512, "thin", params); err != nil {
|
|
|
|
return fmt.Errorf("Can't add target")
|
|
|
|
}
|
2013-10-15 17:27:47 -04:00
|
|
|
if err := task.SetAddNode(AddNodeOnCreate); err != nil {
|
|
|
|
return fmt.Errorf("Can't add node")
|
|
|
|
}
|
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 {
|
|
|
|
return fmt.Errorf("Can't set cookie")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
2013-11-13 19:46:10 -05:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (activateDevice)")
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
UdevWait(cookie)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:07:34 -04:00
|
|
|
func (devices *DeviceSet) createSnapDevice(poolName string, deviceId int, baseName string, baseDeviceId int) error {
|
2013-10-07 08:06:24 -04:00
|
|
|
devinfo, _ := getInfo(baseName)
|
|
|
|
doSuspend := devinfo != nil && devinfo.Exists != 0
|
|
|
|
|
|
|
|
if doSuspend {
|
|
|
|
if err := suspendDevice(baseName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
task, err := createTask(DeviceTargetMsg, poolName)
|
|
|
|
if task == nil {
|
|
|
|
if doSuspend {
|
|
|
|
resumeDevice(baseName)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetSector(0); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
resumeDevice(baseName)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Can't set sector")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.SetMessage(fmt.Sprintf("create_snap %d %d", deviceId, baseDeviceId)); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
resumeDevice(baseName)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Can't set message")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
resumeDevice(baseName)
|
|
|
|
}
|
2013-11-13 19:46:10 -05:00
|
|
|
return fmt.Errorf("Error running DeviceCreate (createSnapDevice)")
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if doSuspend {
|
|
|
|
if err := resumeDevice(baseName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|