話說虛機遷移分為冷遷移以及熱遷移,所謂熱遷移用度娘的話說即是:熱遷移(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)
新聞熱點
疑難解答