首页 > 网站 > 建站经验 > 正文

OpenStack之虚机热迁移的代码详细解析

2019-11-02 16:27:55
字体:
来源:转载
供稿:网友

话说虚机迁移分为冷迁移以及热迁移,所谓热迁移用度娘的话说即是:热迁移(Live Migration,又叫动态迁移、实时迁移),即虚机保存/恢复(Save/Restore):将整个虚拟机的运行状态完整保存下来,同时可以快速的恢复到原有硬件平台甚至是不同硬件平台上。恢复以后,虚机仍旧平滑运行,用户不会察觉到任何差异。OpenStack的虚机迁移是基于Libvirt实现的,下面来看看Openstack虚机热迁移的具体代码实现。

首先,由API入口进入到nova/api/openstack/compute/contrib/admin_actions.py

@wsgi.action('os-migrateLive')  def _migrate_live(self, req, id, body):    """Permit admins to (live) migrate a server to a new host."""    context = req.environ["nova.context"]    authorize(context, 'migrateLive')    try:      block_migration = body["os-migrateLive"]["block_migration"]      disk_over_commit = body["os-migrateLive"]["disk_over_commit"]      host = body["os-migrateLive"]["host"]    except (TypeError, KeyError):      msg = _("host, block_migration and disk_over_commit must "          "be specified for live migration.")      raise exc.HTTPBadRequest(explanation=msg)    try:      block_migration = strutils.bool_from_string(block_migration,                            strict=True)      disk_over_commit = strutils.bool_from_string(disk_over_commit,                             strict=True)    except ValueError as err:      raise exc.HTTPBadRequest(explanation=str(err))    try:      instance = self.compute_api.get(context, id, want_objects=True)      self.compute_api.live_migrate(context, instance, block_migration,                     disk_over_commit, host)    except (exception.ComputeServiceUnavailable,        exception.InvalidHypervisorType,        exception.UnableToMigrateToSelf,        exception.DestinationHypervisorTooOld,        exception.NoValidHost,        exception.InvalidLocalStorage,        exception.InvalidSharedStorage,        exception.MigrationPreCheckError) as ex:      raise exc.HTTPBadRequest(explanation=ex.format_message())    except exception.InstanceNotFound as e:      raise exc.HTTPNotFound(explanation=e.format_message())    except exception.InstanceInvalidState as state_error:      common.raise_http_conflict_for_instance_invalid_state(state_error,          'os-migrateLive')    except Exception:      if host is None:        msg = _("Live migration of instance %s to another host "            "failed") % id      else:        msg = _("Live migration of instance %(id)s to host %(host)s "            "failed") % {'id': id, 'host': host}      LOG.exception(msg)      # Return messages from scheduler      raise exc.HTTPBadRequest(explanation=msg)    return webob.Response(status_int=202)

   这里第一行可以看到是与API文档的第二行照应的:

  

 {  "os-migrateLive": {    "host": "0443e9a1254044d8b99f35eace132080",    "block_migration": false,    "disk_over_commit": false  }}

好了,源码中其实执行迁移工作的就是第26、27行的一条语句:

self.compute_api.live_migrate(context, instance, block_migration,                 disk_over_commit, host)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表