openstack cinder snapshot快照源码分析

本文深入分析了OpenStack Cinder快照的源码,包括Cinder的组件架构、命令行工具、快照的创建、显示和删除等功能。重点讨论了在不同存储后端如LVM和Ceph中创建和删除快照的实现细节,涉及RPC调用及操作系统级别的命令执行。
摘要由CSDN通过智能技术生成
cinder snapshot快照源码分析

源码下载地址:
https://releases.openstack.org/index.html


1. 介绍

Cinder是OpenStack Block Storage服务,用于为Nova虚拟机,Ironic裸机主机,容器等提供卷。Cinder的一些目标是:

基于组件的体系结构:快速添加新行为

高度可用:扩展到非常严重的工作负载

容错:隔离的进程可以避免级联故障


2.命令行

命令行查看功能:

[root@cinder ~]# cinder -h | grep snap
    cgsnapshot-create   Creates a cgsnapshot.
    cgsnapshot-delete   Removes one or more cgsnapshots.
    cgsnapshot-list     Lists all cgsnapshots.
    cgsnapshot-show     Shows cgsnapshot details.
                        Creates a consistency group from a cgsnapshot or a
    snapshot-create     Creates a snapshot.
    snapshot-delete     Removes one or more snapshots.
    snapshot-list       Lists all snapshots.
    snapshot-manage     Manage an existing snapshot.
    snapshot-manageable-list
                        Lists all manageable snapshots.
    snapshot-metadata   Sets or deletes snapshot metadata.
    snapshot-metadata-show
                        Shows snapshot metadata.
    snapshot-metadata-update-all
                        Updates snapshot metadata.
    snapshot-rename     Renames a snapshot.
    snapshot-reset-state
                        Explicitly updates the snapshot state.
    snapshot-show       Shows snapshot details.
    snapshot-unmanage   Stop managing a snapshot.
[root@cinder ~]# 
[root@cinder ~]# 

查看命令详情帮助

[root@cinder ~]# cinder help snapshot-create
usage: cinder snapshot-create [--force [<True|False>]] [--name <name>]
                              [--description <description>]
                              [--metadata [<key=value> [<key=value> ...]]]
                              <volume>

Creates a snapshot.

Positional arguments:
  <volume>              Name or ID of volume to snapshot.

Optional arguments:
  --force [<True|False>]
                        Allows or disallows snapshot of a volume when the
                        volume is attached to an instance. If set to True,
                        ignores the current status of the volume when
                        attempting to snapshot it rather than forcing it to be
                        available. Default=False.
  --name <name>         Snapshot name. Default=None.
  --description <description>
                        Snapshot description. Default=None.
  --metadata [<key=value> [<key=value> ...]]
                        Snapshot metadata key and value pairs. Default=None.
[root@cinder ~]# 



3. 源码分析

从最新版本开始查找,目前为V3版本:

cinder snapshot快照功能的API入口在文件cinder/api/v3/snapshots.py

快照功能定义类:

class SnapshotsController(snapshots_v2.SnapshotsController):
    """The Snapshots API controller for the OpenStack API."""

    _view_builder_class = snapshot_views.ViewBuilder

实际使用v2版本, 源码文件在cinder/api/v2/snapshots.py

class SnapshotsController(wsgi.Controller):
    """The Snapshots API controller for the OpenStack API."""

    _view_builder_class = snapshot_views.ViewBuilder
    
    def __init__(self, ext_mgr=None):
        self.volume_api = volume.API()
        self.ext_mgr = ext_mgr
        super(SnapshotsController, self).__init__()

一. show 详情功能:
    def show(self, req, id):
        """Return data about the given snapshot."""
        context = req.environ['cinder.context']
    
        # Not found exception will be handled at the wsgi level
        snapshot = self.volume_api.get_snapshot(context, id)
        req.cache_db_snapshot(snapshot)
    
        return self._view_builder.detail(req, snapshot)

获取信息self.volume_api.get_snapshot(context, id), 具体实现在API中,
定义在文件: cinder/volume/api.py

class API(base.Base):
    """API for interacting with the volume manager."""

		.....
		
	def get_snapshot(self, context, snapshot_id):
	    check_policy(context, 'get_snapshot')
	    snapshot = objects.Snapshot.get_by_id(context, snapshot_id)
	
	    # FIXME(jdg): The objects don't have the db name entries
	    # so build the resource tag manually for now.
	    LOG.info(_LI("Snapshot retrieved successfully."),
	             resource={'type': 'snapshot',
	                       'id': snapshot.id})
	    return snapshot

objects.Snapshot.get_by_id(context, snapshot_id)查询db,获取快照信息

可以看到该功能基本是直接获取db的操作, 比较简单

二. list, update与show差不多, 基本都是操作db信息, 这里不做分析

三. 创建create创建快照功能:
@wsgi.response(202)
    def create(self, req, body):
        """Creates a new snapshot."""
        kwargs = {}
        
#获取并校验参数信息        
        context = req.environ['cinder.context']

        self.assert_valid_body(body, 'snapshot')
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值