整个流程大体如下:
创建 thinpool
// This is theprogrammatic example of "dmsetup create"
funccreatePool(poolName string, dataFile, metadataFile *os.File, poolBlockSizeuint32) error {
...
params := fmt.Sprintf("%s %s %d 32768 1skip_block_zeroing", metadataFile.Name(), dataFile.Name(), poolBlockSize)
if err := task.AddTarget(0, size/512,"thin-pool", params); err != nil { return fmt.Errorf("Can't addtarget %s", err)
}
...
}
相当于执行下面的操作:
dmsetup create docker-8:1-696417-pool--table ‗0 419430400 thin-pool 7:1 7:0 128 32768 1 skip_block_zeroing‘
# dmsetup table docker-8:1-696417-pool
0 419430400thin-pool 7:1 7:0 128 32768 1 skip_block_zeroing
创建 BaseImage
实际上,thin-provisionedvolume 分两步,首先是发送一个消息给 pool,创建一个 volume。然后激活
volume。只有 activated 的 volume,才能在 dmsetup info 的输出中看到。 (1)Creating a newthinly-provisioned volume
funccreateDevice(poolName string, deviceId *int) error {
…
if err :=task.SetMessage(fmt.Sprintf("create_thin %d", *deviceId)); err != nil{ return fmt.Errorf("Can't set message %s", err)
}
相当于执行下面的操作:
#dmsetup message/dev/mapper/ docker-8:1-696417-pool 0 "create_thin 0"
# hexdump -C /var/lib/docker/devicemapper/metadata/base
00000000
7b
22
64 65 76
69 63
65
5f 69 64 22 3a
30
2c 22
|{"device_id":0,"|
00000010
73
69
7a 65
22
3a 32 31
34 37 34 38 33 36 34 38
|size":2147483648|
00000020
30
2c 22 74 72
61
6e
73
61 63 74 69
6f 6e 5f 69
|0,"transaction_i|
00000030
64
22
3a 31
2c
22
69
6e
69 74 69 61
6c
69
7a 65
|d":1,"initialize|
00000040
64
22
3a 74
72
75 65
7d
|d":true}|
可以看到 base 的 device_id 为 0。
(2)activatedthinly-provisioned volumes
funcactivateDevice(poolName string, name string, deviceId int, size uint64) error {
...
params :=fmt.Sprintf("%s %d", poolName, deviceId)
if err := task.AddTarget(0, size/512,"thin", params); err != nil { return fmt.Errorf("Can't addtarget %s", err)
}
相当于执行下面的操作:
#dmsetup createdocker-8:1-696417-base --table "0 41943040 thin /dev/mapper/docker-8:1-696417-pool 0"
#dmsetup table docker-8:1-696417-base 0 41943040 thin 253:0 0
只有 activated 的 volume,才能在 dmsetup info 的输出中看到。 Devicemapper 的基本操作
Driver的基本操作
///清除 thin pool
func (d *Driver)Cleanup()
///当加载新镜像时,添加一个新 thin volume,id 为 containerid 或 imageid func (d *Driver) Create(id, parent string)
///挂载 thin volume 到/var/lib/docker/devicemapper/mnt/$id 目录下(docker start) func (d *Driver) Get(id, mountLabelstring)
///从/var/lib/docker/devicemapper/mnt/$id目录 umount thinvolume(docker stop) func (d *Driver) Put(id string)
///删除 volume(真正删除)
func (d *Driver)Remove(id string)
Thinpool 的基本操作
///在 thin pool 中创建一个新的 snapshot volume
func (devices*DeviceSet) AddDevice(hash, baseHash string)
///删除 thin volume(释放空间,删除(remove+delete)thin volume) func (devices *DeviceSet) DeleteDevice(hashstring)
/// 将 thin volume 从 /var/lib/docker/devicemapper/mnt/$id umount, deactivate(remove )thinvolume(don't delete)
func (devices*DeviceSet) UnmountDevice(hash string)
///activate thin volume ,然后 mount 到/var/lib/docker/devicemapper/mnt/$idfunc (devices *DeviceSet) MountDevice(hash, path, mountLabel string)
///thin pool 的统计信息(docker info)
func (devices*DeviceSet) Status() *Status
///thin pool 初始化
funcNewDeviceSet(root string, doInit bool, options []string)
Devmapper接口
devmapper/devmapper.go封装了 OS 层的 thin volume 的基本操作。
///dmsetup suspend
funcsuspendDevice(name string)
///dmsetup resume
funcresumeDevice(name string)
///messagecreate_thin
funccreateDevice(poolName string, deviceId *int)
///message delete
funcdeleteDevice(poolName string, deviceId int)
///dmsetup remove
funcremoveDevice(name string)
///dmsetup create
funcactivateDevice(poolName string, name string, deviceId int, size uint64)
///message'create_snap'
funccreateSnapDevice(poolName string, deviceId *int, baseName string, baseDeviceIdint)
三者之间的调用关系如下:
查看 stop 的容器的文件系统
stop 的容器的 thin volume 都处于未激活(deactivate)状态,我们可以将其激活(activate),然后查 看文件系统中的内容。
我们创建一个容器,不启动:
# docker create --name="yy1" centos /bin/bash 93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9
metadata 下面新增两个目录:
# ls metadata/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e993f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9 -init
我们可以查看 thin volume 的信息
#cat metadata/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9{"device_id":5,"size":21474836480,"transaction_id":8,"initialized":false}
# catmetadata/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9 -init{"device_id":4,"size":21474836480,"transaction_id":7,"initialized":false}
我们来尝试手动挂载 thin volume,首先 activate thin volume:
dmsetup
create
93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9 -init
--table "0
41943040 thin 253:0 4"
然后就可以挂载该 thin volume 了:
mount
/dev/mapper/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9-init mnt/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9-init
# lsmnt/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9 -init/ id lost+foundrootfs
deactivate thinvolume
# umountmnt/93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01ad89f8e9-init
# dmsetup remove93f595ea79a0420cc263d054d3e63b5ad1e4cc3da128167984a6ac01
更多精彩内容请关注:http://bbs.superwu.cn
关注超人学院微信二维码:
关注超人学院java免费学习交流群: