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

graph driver-device mapper-02driver基本操作

//	清除thin pool
1.1 func (d *Driver) Cleanup() error {
	// 停止thin pool
	err := d.DeviceSet.Shutdown()

	return err
}

//	当加载新镜像时,添加一个新thin device
//	id为containerid或imageid
1.2 func (d *Driver) Create(id, parent string) error {
	if err := d.DeviceSet.AddDevice(id, parent); err != nil {
		return err
	}

	return nil
}

//	删除thin device
1.3 func (d *Driver) Remove(id string) error {
	//检查thin device是否存在
	if !d.DeviceSet.HasDevice(id) {
		return nil
	}

	//通过id从thin pool中删除设备
	if err := d.DeviceSet.DeleteDevice(id); err != nil {
		return err
	}

	//mp为/var/lib/docker/devicemapper/mnt/$id
	mp := path.Join(d.home, "mnt", id)
	//删除目录下所有的文件
	if err := os.RemoveAll(mp); err != nil && !os.IsNotExist(err) {
		return err
	}

	return nil
}

//	挂载thin device到/var/lib/docker/devicemapper/mnt/$id目录下
1.4 func (d *Driver) Get(id, mountLabel string) (string, error) {
	//mp=/var/lib/docker/devicemapper/mnt/$id
	mp := path.Join(d.home, "mnt", id)

	//创建目录/var/lib/docker/devicemapper/mnt/$id
	if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
		return "", err
	}
	//挂载thin device到/var/lib/docker/devicemapper/mnt/$id
	if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
		return "", err
	}

	//创建rootfs到/var/lib/docker/devicemapper/mnt/$id/rootfs
	rootFs := path.Join(mp, "rootfs")
	if err := os.MkdirAll(rootFs, 0755); err != nil && !os.IsExist(err) {
		d.DeviceSet.UnmountDevice(id)
		return "", err
	}

	//创建id
	idFile := path.Join(mp, "id")
	if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
		if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
			d.DeviceSet.UnmountDevice(id)
			return "", err
		}
	}

	//返回/var/lib/docker/devicemapper/mnt/$id/rootfs目录
	return rootFs, nil
}

//	从/var/lib/docker/devicemapper/mnt/$id下解挂设备
1.5 func (d *Driver) Put(id string) {
	if err := d.DeviceSet.UnmountDevice(id); err != nil {
		utils.Errorf("Warning: error unmounting device %s: %s\n", id, err)
	}
}

//	判断$id所对应的设备是否存在
1.6 func (d *Driver) Exists(id string) bool {
	return d.DeviceSet.HasDevice(id)
}