Exemple #1
0
func (devices *RbdSet) createImage(hash, baseHash string) error {
	var snapshot *rbd.Snapshot

	baseImgName := devices.getRbdImageName(baseHash)
	imgName := devices.getRbdImageName(hash)
	img := rbd.GetImage(devices.ioctx, baseImgName)

	// create snapshot for hash
	snapName := devices.getRbdSnapName(hash)

	if err := img.Open(snapName); err != nil {
		if err != rbd.RbdErrorNotFound {
			log.Errorf("Rbd open image %s with snap %s failed: %v", baseImgName, snapName, err)
			return err
		}

		// open image without snapshot name
		if err = img.Open(); err != nil {
			log.Errorf("Rbd open image %s failed: %v", baseImgName, err)
			return err
		}

		// create snapshot
		if snapshot, err = img.CreateSnapshot(snapName); err != nil {
			log.Errorf("Rbd create snaphost %s failed: %v", snapName, err)
			img.Close()
			return err
		}

	} else {
		snapshot = img.GetSnapshot(snapName)
	}

	// open snaphost success
	defer img.Close()

	// protect snapshot
	if err := snapshot.Protect(); err != nil {
		log.Errorf("Rbd protect snapshot %s failed: %v", snapName, err)
		return err
	}

	// clone image
	_, err := img.Clone(snapName, devices.ioctx, imgName, rbd.RbdFeatureLayering)
	if err != nil {
		log.Errorf("Rbd clone snapshot %s@%s to %s failed: %v", baseImgName, snapName, imgName, err)
		return err
	}

	return nil
}
Exemple #2
0
func (devices *RbdSet) deleteImage(info *DevInfo) error {
	var snapshot *rbd.Snapshot

	// remove image
	imgName := devices.getRbdImageName(info.Hash)
	img := rbd.GetImage(devices.ioctx, imgName)
	if err := img.Remove(); err != nil {
		log.Errorf("Rbd delete image %s failed: %v", imgName, err)
		return err
	}

	// hash's parent
	snapName := devices.getRbdSnapName(info.Hash)
	baseImgName := devices.getRbdImageName(info.BaseHash)
	parentImg := rbd.GetImage(devices.ioctx, baseImgName)
	if err := parentImg.Open(snapName); err != nil {
		log.Errorf("Rbd open image %s with snap %s failed: %v", baseImgName, snapName, err)
		return err
	} else {
		snapshot = parentImg.GetSnapshot(snapName)
	}

	defer parentImg.Close()

	// protect snapshot
	if err := snapshot.Unprotect(); err != nil {
		log.Errorf("Rbd unprotect snapshot %s failed: %v", snapName, err)
		return err
	}

	// remove snapshot
	if err := snapshot.Remove(); err != nil {
		log.Errorf("Rbd remove snapshot %s failed: %v", snapName, err)
	}

	// unregister it
	if err := devices.unRegisterDevice(info); err != nil {
		return err
	}
	return nil
}