首页 > 代码库 > graph driver-device mapper-04libdevmapper基本操作

graph driver-device mapper-04libdevmapper基本操作

//	创建thin pool
//	调用路径:NewDeviceSet->initDevmapper->createPool
1.1 func createPool(poolName string, dataFile, metadataFile *os.File, poolBlockSize uint32) error {
	//通过task封装与libdevmapper的交互
	task, err := createTask(DeviceCreate, poolName)
	if task == nil {
		return err
	}
	//data block device的大小,通过ioctl
	size, err := GetBlockDeviceSize(dataFile)
	if err != nil {
		return fmt.Errorf("Can't get data size %s", err)
	}
	//创建thin pool的参数
	params := fmt.Sprintf("%s %s %d 32768 1 skip_block_zeroing", metadataFile.Name(), dataFile.Name(), poolBlockSize)
	//添加函数参数
	if err := task.AddTarget(0, size/512, "thin-pool", params); err != nil {
		return fmt.Errorf("Can't add target %s", err)
	}
	//创建一个同步量
	var cookie uint = 0
	if err := task.SetCookie(&cookie, 0); err != nil {
		return fmt.Errorf("Can't set cookie %s", err)
	}
	//执行任务
	if err := task.Run(); err != nil {
		return fmt.Errorf("Error running DeviceCreate (createPool) %s", err)
	}
	//等待thin pool创建完成
	UdevWait(cookie)

	return nil
}

//	创建操作,task封装对devicemapper的操作
1.2 func createTask(t TaskType, name string) (*Task, error) {
	//创建tasktype对应的封装函数
	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
}

//	创建一个指定的操作类型
1.3 func TaskCreate(tasktype TaskType) *Task {
	//创建操作
	Ctask := DmTaskCreate(int(tasktype))
	if Ctask == nil {
		return nil
	}
	task := &Task{unmanaged: Ctask}
	runtime.SetFinalizer(task, (*Task).destroy)
	return task
}

//	创建指定的操作
//	DmTaskCreate=dmTaskCreateFct
1.4 func dmTaskCreateFct(taskType int) *CDmTask {
	return (*CDmTask)(C.dm_task_create(C.int(taskType)))
}