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"
|
2015-03-27 15:16:25 -04:00
|
|
|
"unsafe"
|
2014-05-16 08:10:02 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2013-10-02 23:18:15 -04:00
|
|
|
)
|
2013-09-04 05:14:31 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// DevmapperLogger defines methods for logging with devicemapper.
|
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 (
|
2015-09-04 17:02:29 -04:00
|
|
|
deviceCreate TaskType = iota
|
|
|
|
deviceReload
|
|
|
|
deviceRemove
|
|
|
|
deviceRemoveAll
|
|
|
|
deviceSuspend
|
|
|
|
deviceResume
|
|
|
|
deviceInfo
|
|
|
|
deviceDeps
|
|
|
|
deviceRename
|
|
|
|
deviceVersion
|
|
|
|
deviceStatus
|
|
|
|
deviceTable
|
|
|
|
deviceWaitevent
|
|
|
|
deviceList
|
|
|
|
deviceClear
|
|
|
|
deviceMknodes
|
|
|
|
deviceListVersions
|
|
|
|
deviceTargetMsg
|
|
|
|
deviceSetGeometry
|
2013-09-04 05:14:31 -04:00
|
|
|
)
|
|
|
|
|
2013-10-15 17:27:47 -04:00
|
|
|
const (
|
2015-09-04 17:02:29 -04:00
|
|
|
addNodeOnResume AddNodeType = iota
|
|
|
|
addNodeOnCreate
|
2013-10-15 17:27:47 -04:00
|
|
|
)
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// List of errors returned when using devicemapper.
|
2013-10-03 21:00:24 -04:00
|
|
|
var (
|
2015-12-14 17:16:34 -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")
|
|
|
|
ErrTaskGetDeps = errors.New("dm_task_get_deps failed")
|
|
|
|
ErrTaskGetInfo = errors.New("dm_task_get_info failed")
|
|
|
|
ErrTaskGetDriverVersion = errors.New("dm_task_get_driver_version failed")
|
|
|
|
ErrTaskDeferredRemove = errors.New("dm_task_deferred_remove failed")
|
|
|
|
ErrTaskSetCookie = errors.New("dm_task_set_cookie failed")
|
|
|
|
ErrNilCookie = errors.New("cookie ptr can't be nil")
|
|
|
|
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("Invalid AddNode type")
|
|
|
|
ErrBusy = errors.New("Device is Busy")
|
|
|
|
ErrDeviceIDExists = errors.New("Device Id Exists")
|
|
|
|
ErrEnxio = errors.New("No such device or address")
|
2015-09-04 17:02:29 -04:00
|
|
|
)
|
2014-04-24 16:17:04 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var (
|
2014-04-24 16:36:45 -04:00
|
|
|
dmSawBusy bool
|
|
|
|
dmSawExist bool
|
2015-04-21 18:14:59 -04:00
|
|
|
dmSawEnxio bool // No Such Device or Address
|
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 (
|
2015-09-04 17:02:29 -04:00
|
|
|
// Task represents a devicemapper task (like lvcreate, etc.) ; a task is needed for each ioctl
|
|
|
|
// command to execute.
|
2013-10-03 21:00:24 -04:00
|
|
|
Task struct {
|
2015-09-04 17:02:29 -04:00
|
|
|
unmanaged *cdmTask
|
2013-10-03 21:00:24 -04:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
// Deps represents dependents (layer) of a device.
|
2014-09-25 10:57:37 -04:00
|
|
|
Deps struct {
|
|
|
|
Count uint32
|
|
|
|
Filler uint32
|
|
|
|
Device []uint64
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
// Info represents information about a device.
|
2013-10-03 21:00:24 -04:00
|
|
|
Info struct {
|
2015-04-21 18:14:59 -04:00
|
|
|
Exists int
|
|
|
|
Suspended int
|
|
|
|
LiveTable int
|
|
|
|
InactiveTable int
|
|
|
|
OpenCount int32
|
|
|
|
EventNr uint32
|
|
|
|
Major uint32
|
|
|
|
Minor uint32
|
|
|
|
ReadOnly int
|
|
|
|
TargetCount int32
|
|
|
|
DeferredRemove int
|
2013-10-03 21:00:24 -04:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
// TaskType represents a type of task
|
|
|
|
TaskType int
|
2015-09-30 04:35:02 -04:00
|
|
|
// AddNodeType represents a type of node to be added
|
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
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// DeviceIDExists returns whether error conveys the information about device Id already
|
2014-12-03 13:06:43 -05:00
|
|
|
// 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.
|
2015-09-04 17:02:29 -04:00
|
|
|
func DeviceIDExists(err error) bool {
|
|
|
|
return fmt.Sprint(err) == fmt.Sprint(ErrDeviceIDExists)
|
2014-12-03 13:06:43 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-12-14 16:07:17 -05:00
|
|
|
return nil, fmt.Errorf("devicemapper: Can't create task of type %d", int(t))
|
2014-11-14 09:33:13 -05:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setName(name); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return nil, fmt.Errorf("devicemapper: Can't set task name %s", name)
|
2014-11-14 09:33:13 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04: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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) setAddNode(addNode AddNodeType) error {
|
|
|
|
if addNode != addNodeOnResume && addNode != addNodeOnCreate {
|
2013-11-13 19:46:10 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) addTarget(start, size uint64, ttype, params string) error {
|
2013-11-13 17:36:31 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) getDeps() (*Deps, error) {
|
2014-09-25 10:57:37 -04:00
|
|
|
var deps *Deps
|
|
|
|
if deps = DmTaskGetDeps(t.unmanaged); deps == nil {
|
|
|
|
return nil, ErrTaskGetDeps
|
|
|
|
}
|
|
|
|
return deps, nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) getInfoWithDeferred() (*Info, error) {
|
2015-04-21 18:14:59 -04:00
|
|
|
info := &Info{}
|
|
|
|
if res := DmTaskGetInfoWithDeferred(t.unmanaged, info); res != 1 {
|
|
|
|
return nil, ErrTaskGetInfo
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) getDriverVersion() (string, error) {
|
2014-03-27 12:45:02 -04:00
|
|
|
res := DmTaskGetDriverVersion(t.unmanaged)
|
|
|
|
if res == "" {
|
|
|
|
return "", ErrTaskGetDriverVersion
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func (t *Task) getNextTarget(next unsafe.Pointer) (nextPtr unsafe.Pointer, start uint64,
|
2013-11-13 17:36:31 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-30 04:35:02 -04:00
|
|
|
// UdevWait waits for any processes that are waiting for udev to complete the specified cookie.
|
2015-04-02 16:47:14 -04:00
|
|
|
func UdevWait(cookie *uint) error {
|
|
|
|
if res := DmUdevWait(*cookie); res != 1 {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: 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
|
|
|
|
}
|
|
|
|
|
2015-09-30 04:35:02 -04:00
|
|
|
// LogInitVerbose is an interface to initialize the verbose logger for the device mapper library.
|
2013-09-04 05:14:31 -04:00
|
|
|
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
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var dmLogger DevmapperLogger
|
2013-10-10 09:24:39 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// LogInit initializes the logger for the device mapper library.
|
2014-11-05 18:10:38 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// SetDevDir sets the dev folder for the device mapper library (usually /dev).
|
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 {
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Debug("devicemapper: Error dm_set_dev_dir")
|
2013-10-03 21:00:24 -04:00
|
|
|
return ErrSetDevDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetLibraryVersion returns the device mapper library version.
|
2013-10-03 21:00:24 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-19 15:10:37 -05:00
|
|
|
// UdevSyncSupported returns whether device-mapper is able to sync with udev
|
|
|
|
//
|
|
|
|
// This is essential otherwise race conditions can arise where both udev and
|
|
|
|
// device-mapper attempt to create and destroy devices.
|
|
|
|
func UdevSyncSupported() bool {
|
|
|
|
return DmUdevGetSyncSupport() != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// UdevSetSyncSupport allows setting whether the udev sync should be enabled.
|
|
|
|
// The return bool indicates the state of whether the sync is enabled.
|
|
|
|
func UdevSetSyncSupport(enable bool) bool {
|
|
|
|
if enable {
|
|
|
|
DmUdevSetSyncSupport(1)
|
|
|
|
} else {
|
|
|
|
DmUdevSetSyncSupport(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UdevSyncSupported()
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:31:54 -05:00
|
|
|
// CookieSupported returns whether the version of device-mapper supports the
|
|
|
|
// use of cookie's in the tasks.
|
|
|
|
// This is largely a lower level call that other functions use.
|
|
|
|
func CookieSupported() bool {
|
|
|
|
return DmCookieSupported() != 0
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// RemoveDevice is a useful helper for cleaning up a device.
|
2013-09-19 14:32:11 -04:00
|
|
|
func RemoveDevice(name string) error {
|
2015-09-04 17:02:29 -04: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
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var cookie uint
|
|
|
|
if err := task.setCookie(&cookie, 0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can not set cookie: %s", err)
|
2013-09-19 14:32:11 -04:00
|
|
|
}
|
2015-04-02 16:47:14 -04: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
|
2015-09-04 17:02:29 -04:00
|
|
|
if err = task.run(); err != nil {
|
2014-11-13 17:20:24 -05:00
|
|
|
if dmSawBusy {
|
|
|
|
return ErrBusy
|
|
|
|
}
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running RemoveDevice %s", err)
|
2014-11-13 17:20:24 -05:00
|
|
|
}
|
|
|
|
|
2013-09-19 14:32:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-10-03 21:00:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// RemoveDeviceDeferred is a useful helper for cleaning up a device, but deferred.
|
2015-04-21 18:14:59 -04:00
|
|
|
func RemoveDeviceDeferred(name string) error {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: RemoveDeviceDeferred START(%s)", name)
|
|
|
|
defer logrus.Debugf("devicemapper: RemoveDeviceDeferred END(%s)", name)
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceRemove, name)
|
2015-04-21 18:14:59 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DmTaskDeferredRemove(task.unmanaged); err != 1 {
|
|
|
|
return ErrTaskDeferredRemove
|
|
|
|
}
|
|
|
|
|
devmapper: prevent libdevmapper from deleting device symlinks in RemoveDeviceDeferred
if there is no cookie set in dm task, or flag DM_UDEV_DISABLE_LIBRARY_FALLBACK
is cleared for a DM_DEV_REMOVE task, libdevmapper will fallback to clean up the
symlink under /dev/mapper by itself, no matter the device removal is executed
immediately or deferred by the kernel.In some cases, the removal is deferred by the
kernel, while the symlink is deleted directly by libdevmapper, when docker tries to
activate the device again, the deferred removal will be canceld, but the symlink will
not show up again, so docker's attempt to mount the device by the symlink will fail,
and it will eventually leads to a `docker start/diff` error.
Fixes #24671
Signed-off-by: Ji.Zhilong <zhilongji@gmail.com>
2016-07-15 10:47:31 -04:00
|
|
|
// set a task cookie and disable library fallback, or else libdevmapper will
|
|
|
|
// disable udev dm rules and delete the symlink under /dev/mapper by itself,
|
|
|
|
// even if the removal is deferred by the kernel.
|
|
|
|
var cookie uint
|
|
|
|
var flags uint16
|
|
|
|
flags = DmUdevDisableLibraryFallback
|
|
|
|
if err := task.setCookie(&cookie, flags); err != nil {
|
|
|
|
return fmt.Errorf("devicemapper: Can not set cookie: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// libdevmapper and udev relies on System V semaphore for synchronization,
|
|
|
|
// semaphores created in `task.setCookie` will be cleaned up in `UdevWait`.
|
|
|
|
// So these two function call must come in pairs, otherwise semaphores will
|
|
|
|
// be leaked, and the limit of number of semaphores defined in `/proc/sys/kernel/sem`
|
|
|
|
// will be reached, which will eventually make all follwing calls to 'task.SetCookie'
|
|
|
|
// fail.
|
|
|
|
// this call will not wait for the deferred removal's final executing, since no
|
|
|
|
// udev event will be generated, and the semaphore's value will not be incremented
|
|
|
|
// by udev, what UdevWait is just cleaning up the semaphore.
|
|
|
|
defer UdevWait(&cookie)
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err = task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running RemoveDeviceDeferred %s", err)
|
2015-04-21 18:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// CancelDeferredRemove cancels a deferred remove for a device.
|
2015-04-21 18:14:59 -04:00
|
|
|
func CancelDeferredRemove(deviceName string) error {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceTargetMsg, deviceName)
|
2015-04-21 18:14:59 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setSector(0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set sector %s", err)
|
2015-04-21 18:14:59 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setMessage(fmt.Sprintf("@cancel_deferred_remove")); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set message %s", err)
|
2015-04-21 18:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dmSawBusy = false
|
|
|
|
dmSawEnxio = false
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-04-21 18:14:59 -04:00
|
|
|
// A device might be being deleted already
|
|
|
|
if dmSawBusy {
|
|
|
|
return ErrBusy
|
|
|
|
} else if dmSawEnxio {
|
|
|
|
return ErrEnxio
|
|
|
|
}
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running CancelDeferredRemove %s", err)
|
2015-04-21 18:14:59 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetBlockDeviceSize returns the size of a block device identified by the specified file.
|
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 {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Errorf("devicemapper: Error getblockdevicesize: %s", err)
|
2013-11-13 19:46:10 -05:00
|
|
|
return 0, ErrGetBlockSize
|
|
|
|
}
|
|
|
|
return uint64(size), nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// BlockDeviceDiscard runs discard for the given path.
|
|
|
|
// This is used as a workaround for the kernel not discarding block so
|
|
|
|
// on the thin pool when we remove a thinp device, so we do it
|
|
|
|
// manually
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// CreatePool is the programmatic example of "dmsetup create".
|
|
|
|
// It creates a device with the specified poolName, data and metadata file and block size.
|
2014-11-05 18:10:38 -05:00
|
|
|
func CreatePool(poolName string, dataFile, metadataFile *os.File, poolBlockSize uint32) error {
|
2015-09-04 17:02:29 -04: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 {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: 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)
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.addTarget(0, size/512, "thin-pool", params); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't add target %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var cookie uint
|
|
|
|
var flags uint16
|
|
|
|
flags = DmUdevDisableSubsystemRulesFlag | DmUdevDisableDiskRulesFlag | DmUdevDisableOtherRulesFlag
|
|
|
|
if err := task.setCookie(&cookie, flags); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2015-04-02 16:47:14 -04:00
|
|
|
defer UdevWait(&cookie)
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceCreate (CreatePool) %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// ReloadPool is the programmatic example of "dmsetup reload".
|
|
|
|
// It reloads the table with the specified poolName, data and metadata file and block size.
|
2014-11-05 18:10:38 -05:00
|
|
|
func ReloadPool(poolName string, dataFile, metadataFile *os.File, poolBlockSize uint32) error {
|
2015-09-04 17:02:29 -04: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 {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: 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)
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.addTarget(0, size/512, "thin-pool", params); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't add target %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceCreate %s", err)
|
2013-11-18 06:12:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetDeps is the programmatic example of "dmsetup deps".
|
|
|
|
// It outputs a list of devices referenced by the live table for the specified device.
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetDeps(name string) (*Deps, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceDeps, name)
|
2014-09-25 10:57:37 -04:00
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2014-09-25 10:57:37 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
return task.getDeps()
|
2014-09-25 10:57:37 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetInfo is the programmatic example of "dmsetup info".
|
|
|
|
// It outputs some brief information about the device.
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetInfo(name string) (*Info, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceInfo, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2013-10-07 08:06:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
return task.getInfo()
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetInfoWithDeferred is the programmatic example of "dmsetup info", but deferred.
|
|
|
|
// It outputs some brief information about the device.
|
2015-04-21 18:14:59 -04:00
|
|
|
func GetInfoWithDeferred(name string) (*Info, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceInfo, name)
|
2015-04-21 18:14:59 -04:00
|
|
|
if task == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-04-21 18:14:59 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
return task.getInfoWithDeferred()
|
2015-04-21 18:14:59 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetDriverVersion is the programmatic example of "dmsetup version".
|
|
|
|
// It outputs version information of the driver.
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetDriverVersion() (string, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task := TaskCreate(deviceVersion)
|
2014-03-27 12:45:02 -04:00
|
|
|
if task == nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return "", fmt.Errorf("devicemapper: Can't create deviceVersion task")
|
2014-03-27 12:45:02 -04:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2014-03-27 12:45:02 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
return task.getDriverVersion()
|
2014-03-27 12:45:02 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetStatus is the programmatic example of "dmsetup status".
|
|
|
|
// It outputs status information for the specified device name.
|
2014-11-05 18:10:38 -05:00
|
|
|
func GetStatus(name string) (uint64, uint64, string, string, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceStatus, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetStatus() Error TaskCreateNamed: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetStatus() Error Run: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
devinfo, err := task.getInfo()
|
2013-10-07 08:06:24 -04:00
|
|
|
if err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetStatus() Error GetInfo: %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if devinfo.Exists == 0 {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetStatus() Non existing device %s", name)
|
|
|
|
return 0, 0, "", "", fmt.Errorf("devicemapper: Non existing device %s", name)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
_, start, length, targetType, params := task.getNextTarget(unsafe.Pointer(nil))
|
2013-11-20 15:49:01 -05:00
|
|
|
return start, length, targetType, params, nil
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// GetTable is the programmatic example for "dmsetup table".
|
|
|
|
// It outputs the current table for the specified device name.
|
2015-07-07 14:13:29 -04:00
|
|
|
func GetTable(name string) (uint64, uint64, string, string, error) {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceTable, name)
|
2015-07-07 14:13:29 -04:00
|
|
|
if task == nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetTable() Error TaskCreateNamed: %s", err)
|
2015-07-07 14:13:29 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetTable() Error Run: %s", err)
|
2015-07-07 14:13:29 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
devinfo, err := task.getInfo()
|
2015-07-07 14:13:29 -04:00
|
|
|
if err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetTable() Error GetInfo: %s", err)
|
2015-07-07 14:13:29 -04:00
|
|
|
return 0, 0, "", "", err
|
|
|
|
}
|
|
|
|
if devinfo.Exists == 0 {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: GetTable() Non existing device %s", name)
|
|
|
|
return 0, 0, "", "", fmt.Errorf("devicemapper: Non existing device %s", name)
|
2015-07-07 14:13:29 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
_, start, length, targetType, params := task.getNextTarget(unsafe.Pointer(nil))
|
2015-07-07 14:13:29 -04:00
|
|
|
return start, length, targetType, params, nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// SetTransactionID sets a transaction id for the specified device name.
|
|
|
|
func SetTransactionID(poolName string, oldID uint64, newID uint64) error {
|
|
|
|
task, err := TaskCreateNamed(deviceTargetMsg, poolName)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setSector(0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set sector %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setMessage(fmt.Sprintf("set_transaction_id %d %d", oldID, newID)); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set message %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running SetTransactionID %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// SuspendDevice is the programmatic example of "dmsetup suspend".
|
|
|
|
// It suspends the specified device.
|
2014-11-05 18:10:38 -05:00
|
|
|
func SuspendDevice(name string) error {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceSuspend, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceSuspend %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// ResumeDevice is the programmatic example of "dmsetup resume".
|
|
|
|
// It un-suspends the specified device.
|
2014-11-05 18:10:38 -05:00
|
|
|
func ResumeDevice(name string) error {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceResume, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var cookie uint
|
|
|
|
if err := task.setCookie(&cookie, 0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2015-04-02 16:47:14 -04:00
|
|
|
defer UdevWait(&cookie)
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceResume %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-30 04:35:02 -04:00
|
|
|
// CreateDevice creates a device with the specified poolName with the specified device id.
|
2015-09-04 17:02:29 -04:00
|
|
|
func CreateDevice(poolName string, deviceID int) error {
|
2015-12-14 16:07:17 -05:00
|
|
|
logrus.Debugf("devicemapper: CreateDevice(poolName=%v, deviceID=%v)", poolName, deviceID)
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceTargetMsg, poolName)
|
2014-12-03 13:06:43 -05:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setSector(0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set sector %s", err)
|
2014-12-03 13:06:43 -05:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setMessage(fmt.Sprintf("create_thin %d", deviceID)); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set message %s", err)
|
2014-12-03 13:06:43 -05:00
|
|
|
}
|
2014-04-24 16:36:45 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
dmSawExist = false // reset before the task is run
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
|
|
|
// Caller wants to know about ErrDeviceIDExists so that it can try with a different device id.
|
2014-12-03 13:06:43 -05:00
|
|
|
if dmSawExist {
|
2015-09-04 17:02:29 -04:00
|
|
|
return ErrDeviceIDExists
|
2014-04-24 16:36:45 -04:00
|
|
|
}
|
2015-03-25 18:34:00 -04:00
|
|
|
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running CreateDevice %s", err)
|
2015-03-25 18:34:00 -04:00
|
|
|
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-30 04:35:02 -04:00
|
|
|
// DeleteDevice deletes a device with the specified poolName with the specified device id.
|
2015-09-04 17:02:29 -04:00
|
|
|
func DeleteDevice(poolName string, deviceID int) error {
|
|
|
|
task, err := TaskCreateNamed(deviceTargetMsg, poolName)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setSector(0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set sector %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setMessage(fmt.Sprintf("delete %d", deviceID)); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set message %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:21 -04:00
|
|
|
dmSawBusy = false
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-10-06 17:37:21 -04:00
|
|
|
if dmSawBusy {
|
|
|
|
return ErrBusy
|
|
|
|
}
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running DeleteDevice %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// ActivateDevice activates the device identified by the specified
|
|
|
|
// poolName, name and deviceID with the specified size.
|
|
|
|
func ActivateDevice(poolName string, name string, deviceID int, size uint64) error {
|
|
|
|
return activateDevice(poolName, name, deviceID, size, "")
|
2015-06-22 20:28:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// ActivateDeviceWithExternal activates the device identified by the specified
|
2015-09-30 04:35:02 -04:00
|
|
|
// poolName, name and deviceID with the specified size.
|
2015-09-04 17:02:29 -04:00
|
|
|
func ActivateDeviceWithExternal(poolName string, name string, deviceID int, size uint64, external string) error {
|
|
|
|
return activateDevice(poolName, name, deviceID, size, external)
|
2015-06-22 20:28:15 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func activateDevice(poolName string, name string, deviceID int, size uint64, external string) error {
|
|
|
|
task, err := TaskCreateNamed(deviceCreate, name)
|
2013-10-07 08:06:24 -04:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-22 20:28:15 -04:00
|
|
|
var params string
|
|
|
|
if len(external) > 0 {
|
2015-09-04 17:02:29 -04:00
|
|
|
params = fmt.Sprintf("%s %d %s", poolName, deviceID, external)
|
2015-06-22 20:28:15 -04:00
|
|
|
} else {
|
2015-09-04 17:02:29 -04:00
|
|
|
params = fmt.Sprintf("%s %d", poolName, deviceID)
|
2015-06-22 20:28:15 -04:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.addTarget(0, size/512, "thin", params); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't add target %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setAddNode(addNodeOnCreate); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't add node %s", err)
|
2013-10-15 17:27:47 -04:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
var cookie uint
|
|
|
|
if err := task.setCookie(&cookie, 0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set cookie %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
2015-04-02 16:47:14 -04:00
|
|
|
defer UdevWait(&cookie)
|
2014-11-14 18:15:56 -05:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceCreate (ActivateDevice) %s", err)
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-09 13:52:25 -04:00
|
|
|
// CreateSnapDeviceRaw creates a snapshot device. Caller needs to suspend and resume the origin device if it is active.
|
|
|
|
func CreateSnapDeviceRaw(poolName string, deviceID int, baseDeviceID int) error {
|
2015-09-04 17:02:29 -04:00
|
|
|
task, err := TaskCreateNamed(deviceTargetMsg, poolName)
|
2014-12-03 13:06:43 -05:00
|
|
|
if task == nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setSector(0); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set sector %s", err)
|
2014-12-03 13:06:43 -05:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.setMessage(fmt.Sprintf("create_snap %d %d", deviceID, baseDeviceID)); err != nil {
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Can't set message %s", err)
|
2014-12-03 13:06:43 -05:00
|
|
|
}
|
2013-10-07 08:06:24 -04:00
|
|
|
|
2014-12-03 13:06:43 -05:00
|
|
|
dmSawExist = false // reset before the task is run
|
2015-09-04 17:02:29 -04:00
|
|
|
if err := task.run(); err != nil {
|
|
|
|
// Caller wants to know about ErrDeviceIDExists so that it can try with a different device id.
|
2014-12-03 13:06:43 -05:00
|
|
|
if dmSawExist {
|
2015-09-04 17:02:29 -04:00
|
|
|
return ErrDeviceIDExists
|
2013-10-07 08:06:24 -04:00
|
|
|
}
|
2015-12-14 16:07:17 -05:00
|
|
|
return fmt.Errorf("devicemapper: Error running deviceCreate (createSnapDevice) %s", err)
|
2016-06-09 13:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSnapDevice creates a snapshot based on the device identified by the baseName and baseDeviceId,
|
|
|
|
func CreateSnapDevice(poolName string, deviceID int, baseName string, baseDeviceID int) error {
|
|
|
|
devinfo, _ := GetInfo(baseName)
|
|
|
|
doSuspend := devinfo != nil && devinfo.Exists != 0
|
2015-03-25 18:34:00 -04:00
|
|
|
|
2016-06-09 13:52:25 -04:00
|
|
|
if doSuspend {
|
|
|
|
if err := SuspendDevice(baseName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := CreateSnapDeviceRaw(poolName, deviceID, baseDeviceID); err != nil {
|
|
|
|
if doSuspend {
|
|
|
|
if err2 := ResumeDevice(baseName); err2 != nil {
|
|
|
|
return fmt.Errorf("CreateSnapDeviceRaw Error: (%v): ResumeDevice Error: (%v)", err, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 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
|
|
|
|
}
|