今回解説するのは、Hyper-Vの目玉機能の一つ、Live Migrationです。通常のLive Migrationであれば、Hyepr-Vマネージャーから簡単に操作できますが、ここではもう少し踏み込んで、プログラムマチックにLive Migrationの実行方法をご紹介いたします。簡単におさらいとして、WidnwosServer2012R2のLive Migrationの実行方法からご説明いたします。
Live Migrationの実行方法
Hyper-VマネージャーからLive Migrationを実行する仮想マシンを選択して、ウィザードを起動します。
Live Migrationにはいくつか種類があり、仮想マシンを移動する場合と、仮想マシンのディスクのみを移動する方法があります。仮想マシンを移動する場合は、ディスクごと移動することもできますし、Hyper-Vをクラスタ構成して、共有ストレージに仮想マシンのディスクを配置している場合などは、仮想マシンの構成情報のみ移動することが可能です。
共通手順
開始する前に移動先の種類を選択移動先の指定
仮想マシンの移動
移動オプションの選択詳細オプションの選択
仮想マシンのディスクのみ移動
移動オプションの選択詳細オプションの選択
仮想マシン構成情報+ディスクの移動
詳細オプションの選択移動する項目の選択接続されている仮想ハードディスク
仮想マシン構成情報のみ移動
詳細オプションの選択移動する項目の選択接続されている仮想ハードディスク現在の構成チェックポイントスマートペイジング
仮想マシン構成情報のみ移動 ※共有ストレージに仮想ディスクがある場合
移動オプションの選択
ウィザードに従って実行すると、簡単にLive Migrationが実行できます。
さて、以上がHyper-Vマネージャーを使って、自分自身のサーバーから他のサーバーに移動する方法です。
では、外部からサーバー間のLive Migrationを実行するにはどのような手段があるでしょうか。第3のサーバーのHyper-Vマネージャーに対象となるサーバー2つを登録して実行する方法があります。しかし、そのままLive Migrationを実行すると「0x8009030E」というアクセス権のエラーが出現してしまいます。
PowerShellやWMI経由で実行する場合も同様のエラーが発生します。プログラムから実行した場合はこのようなエラーが起こります。
「'VM001' の仮想マシン移行操作が失敗しました。
仮想マシン管理サービスは、ホストとの仮想マシンの移行のための接続を確立できませんでした: セキュリティ パッケージで利用できる資格情報がありません (0x8009030E)。
ソース ホストで接続を認証できませんでした: 適切な資格情報がありません。」
この場合、回避するために、ドメインに参加した状態でKerberos認証 の委任設定を行う必要があります。詳細な設定方法はこちらです。
Kerberos認証 の委任設定方法
まず、ドメインコントローラーの「Active Directory ユーザーとコンピューター」から、Live Migrationを実行するサーバー2台を選択します。
サーバー名を右クリックして「プロパティ」を開きます。「委任」タブを選択します。「指定されたサービスへの委任でのみこのコンピューターを信頼する」を選択し、「Kerberos のみを使う」を選択します。
この状態で「追加」ボタンをクリックし、「サービスの追加」から「ユーザーまたはコンピューター」を選択します。「ユーザーまたはコンピューター の選択」ダイアログにて、「オブジェクトの種類」から「コンピューター」を選択します。そしてLive Migrationを実行する相手方の対象となるサーバー名を入力します。
すると「利用可能なサービス」に一覧が表示されますので、その中から「cifs」と「Microsoft Virtual System Migration Service」の2つのサービスをCtrlキーを押しながら選択して、「OK」ボタンをクリックして追加します。
サーバーのプロパティダイアログに戻り「OK」ボタンをクリックして完了します。
同様に、もう一方のサーバーのプロパティダイアログからも、「cifs」と「Microsoft Virtual System Migration Service」の2つのサービスを追加しておきます。Live Migrationを実行しようとしている2つのサーバーが、お互いに相手となるサーバーを「cifs」と「Microsoft Virtual System Migration Service」サービスに関してKerberosを使用して信頼して委任するという設定です。
この設定の後、あらためてLive Migrationを実行してみると、無事完了することができました。
また、この委任の設定ですが、委任設定を行っても、同様のアクセス権エラーが発生した場合は、委任設定の反映に時間がかかる場合があるようです。その場合、しばらく時間を置き、再度Live Migrationを実行する事をおすすめいたします。
GMO最新ネット業界レポートらしく、WMIを使ったLive Migrationのサンプルコードもご紹介いたします。
Imports System.Management
Module GMOReport
Sub Main()
Dim strUser As String = ""
Dim strPass As String = ""
Dim objManagementScope As ManagementScope = ConnectManagementScope("win2012.local", strUser, strPass)
objManagementScope.Connect()
Dim strVMName As String = "VM001"
Call MigratVM(objManagementScope, strVMName, "Dist_WinSvr2012R2", "D:\Hyper-V\VM01")
End Sub
Function ConnectManagementScope(ByVal strServer As String, ByVal strAccount As String, ByVal strPassword As String) As ManagementScope
Dim objConnectionOptions As New ConnectionOptions()
objConnectionOptions.Impersonation = ImpersonationLevel.Impersonate 'WMI への接続に使用される偽装レベルを設定
objConnectionOptions.EnablePrivileges = True 'WNI経由の操作のためにユーザー特権を有効にする
objConnectionOptions.Username = strAccount
objConnectionOptions.Password = strPassword
Dim objManagementScope As New ManagementScope("\\" + strServer + "\root\virtualization\v2", objConnectionOptions)
objManagementScope.Connect()
Return (objManagementScope)
End Function
Function MigratVM(ByVal objManagementScope As ManagementScope, strVMName As String, strDestinationHost As String, ByVal strRootPath As String) As Boolean
Dim objComputerSystem As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery( "SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'" )).Get
objComputerSystem = objManagementObject
Next
Dim objVirtualSystemsettingData As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In objComputerSystem.GetRelated("Msvm_VirtualSystemSettingData")
If String .Compare(objManagementObject( "ElementName").ToString, strVMName, True ) = 0 Then
objVirtualSystemsettingData = objManagementObject
If Not strRootPath = "" Then
objVirtualSystemsettingData("ConfigurationDataRoot") = strRootPath
objVirtualSystemsettingData("SnapshotDataRoot") = strRootPath
objVirtualSystemsettingData("SwapFileDataRoot") = strRootPath
End If
End If
Next
Dim objVirtualSystemMigrationSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_VirtualSystemMigrationSettingData"), Nothing)
Dim objVirtualSystemMigrationSettingDataInstance As ManagementObject = objEthernetPortAllocationSettingData.CreateInstance
objVirtualSystemMigrationSettingDataInstance("MigrationType") = 32771 'VirtualSystem:32768 Storage:32769 Staged:32770 VirtualSystemAndStorage:32771
objVirtualSystemMigrationSettingDataInstance("TransportType") = 5 'TransportType.Tcp
objVirtualSystemMigrationSettingDataInstance("EnableCompression") = True
Dim i As Integer = 0
Dim objHardDisk As ManagementObject = Nothing
Dim strStorageAllocationSettingData() As String
For Each objManagementObject As ManagementObject = In objVirtualSystemsettingData.GetRelated("Msvm_StorageAllocationSettingData")
If objManagementObject("ResourceSubType") = "Microsoft:Hyper-V:Virtual Hard Disk" Then
objHardDisk = objManagementObject
If Not strRootPath = "" Then
Dim strVhdPath As String = objHardDisk("HostResource")(0)
objHardDisk("HostResource") = New String() {strRootPath & "\" & System.IO.Path.GetFileName(strVhdPath)}
End If
ReDim Preserve strStorageAllocationSettingData(i)
strStorageAllocationSettingData(i) = objHardDisk.GetText(TextFormat.CimDtd20)
i += 1
End If
Next
For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemMigrationService")).Get
Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("MigrateVirtualSystemToHost")
objParams("ComputerSystem") = objComputerSystem.Path.Path
objParams("DestinationHost") = strDestinationHost
objParams("MigrationSettingData") = objVirtualSystemMigrationSettingDataInstance.GetText(TextFormat.CimDtd20)
objParams("NewSystemSettingData") = objVirtualSystemsettingData.GetText(TextFormat.CimDtd20)
objParams("NewResourceSettingData") = strStorageAllocationSettingData
Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("MigrateVirtualSystemToHost", objParams, Nothing)
Return JobComplete(objManagementBaseObject, objManagementScope)
Next
End Function
Function JobComplete(ByVal objManagementBaseObject As ManagementBaseObject, ByVal objManagementScope As ManagementScope) As Boolean
'JobState New = 2 Starting = 3 Running = 4 Suspended = 5 ShuttingDown = 6 Completed = 7 Terminated = 8 Killed = 9 Exception = 10 Service = 11
If objManagementBaseObject("ReturnValue") <> 0 Then
Dim strJobPath As String= objManagementBaseObject("Job")
Dim objJob As New ManagementObject(objManagementScope, New ManagementPath(strJobPath), Nothing)
objJob.Get())
Do While objJob("JobState") = 3 Or objJob("JobState") = 4
System.Threading.Thread.Sleep(1000)
objJob.Get()
Loop
If objJob("JobState") <> 7 Then
Console.WriteLine("ErrorCode="& objJob("ErrorCode") & " JobState=" & objJob("JobState"))
Return False
Else
Return True
End If
Else
Return True
End If
End Function
End Module
今回はPowerShellのコードもご紹介いたします。
自分自身から他のサーバーへのLive Migrationは、
Move-VM -Name "VM001" -DestinationHost "Dist_WinSvr2012R2" -IncludeStorage -DestinationStoragePath "D:\Hyper-V\VM001"
(参考)外部からサーバー間のLive Migrationを実行するには
$AdminPassword = ConvertTo-SecureString "password" - AsPlainText - Force$RremotingCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "DOMAIN\Administrator", $AdminPassword$Session = New-PSSession -Authentication Credssp -Credential $remotingCreds -ComputerName " WinSvr2012R2"Invoke-Command -Session $Session -ScriptBlock {Move-VM -Name "VM001" -DestinationHost "Dist_ WinSvr2012R2" -IncludeStorage -DestinationStoragePath "D:\hyper-V\VM001"}
以上、今回は委任設定が必要なLive Migrationをご紹介いたしました。
といったサンプルコードです。
サンプルコードをこちらからダウンロードいただけます。 → SampleCode.zip(27KB)
*本文中に記載されている会社名および商品名・サービス名は、各社の商標 または登録商標です。