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')