• 基于go-ceph创建CEPH块设备及快照


    一、代码执行前准备

    1、系统中安装了CEPH集群

    2、GOPATH目录下存在src/github.com/noahdesu/go-ceph代码库

    3、在ubuntu 14.04下还需apt-get librados-dev和librbd-dev两个包

    二、代码示例

    package main 
    
    import (
    	"fmt"
    	"github.com/noahdesu/go-ceph/rados"
    	"github.com/noahdesu/go-ceph/rbd"
    )
    
    const (
    	DefaultRadosConfigFile = "/etc/ceph/ceph.conf"
    	DefaultBaseImageSize = 10 * 1024 * 1024 * 1024
    	DefaultPoolName = "rbd"
    )
    
    func main() {
    	// connect to the cluster
    	conn,_ := rados.NewConn()
    	if err := conn.ReadConfigFile(DefaultRadosConfigFile); err != nil {
    		fmt.Printf("Rbd read config failed: %v",err)
    		return
    	}
    	if err := conn.Connect(); err != nil {
    		fmt.Printf("Rbd connect failed: %v",err)
    		return
    	}
    	
    	// connect to the pool
    	ioctx,err := conn.OpenIOContext(DefaultPoolName)
    	if err != nil {
    		fmt.Printf("Rbd open pool failed: %v",err)
    		return
    	}
    
    	// create base image
    	baseImageName := "test"
    	_,err = rbd.Create(ioctx,baseImageName,DefaultBaseImageSize,rbd.RbdFeatureLayering)
    	if err != nil {
    		fmt.Printf("Rbd create image failed: %v",err)
    		return
    	}
    
    	img := rbd.GetImage(ioctx,baseImageName)
    
    	// we should open base image first
    	if err := img.Open(); err != nil {
    		fmt.Printf("Rbd open image  failed: %v",err)
    		return
    	}
    
    	defer img.Close()
    	
    	// create snapshot
    	snapName := "test-snap"
    	snapshot,err := img.CreateSnapshot(snapName)
    	if err != nil {
    		fmt.Printf("Rbd create snapshot failed: %v",err)
    		return
    	}
    
    	// protect snapshot 
    	if err := snapshot.Protect(); err != nil {
    		fmt.Printf("Rbd create snapshot failed: %v",err)
    		return 
    	}
    
    	// make a clone image based on the snap shot
    	cloneImageName := "clone-test"
    	_,err = img.Clone(snapName,ioctx,cloneImageName,rbd.RbdFeatureLayering)
    	if err != nil {
    		fmt.Printf("Rbd clone snapshot failed: %v",err)
    		return 
    	}
    	
    	return
    }
    

      

    三、最终会生成一个名为"test"的基础image,以及一个基于"test"的名为"test-snap"的snapshot,最后还有一个基于快照"test-snap"的clone。

      注:在创建"test"时,size参数不能太小,否则在创建快照的时候会产生错误。

  • 相关阅读:
    函数练习之计算机
    函数练习小程序
    Java—Day5课堂练习
    mysql-用户权限管理
    liunx-tail 实时显示文件内容
    Linux-diff --比较两个文件并输出不同之处
    linux-查找某目录下包含关键字内容的文件
    mysql-允许远程连接
    mysql-基本操作
    liunx-指令
  • 原文地址:https://www.cnblogs.com/YaoDD/p/5278395.html
Copyright © 2020-2023  润新知