一、代码执行前准备
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参数不能太小,否则在创建快照的时候会产生错误。
时间: 2024-10-07 01:46:15