0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure の仮想マシンを Hyper-V 環境で起動させる

Posted at

Azure の仮想マシンを手元の物理端末の Hyper-V 環境へ持ってきて起動しようという話です。

Azure の仮想マシンの仮想ディスクは、以下の Web サイト記載の方法で Azure Portal からダウンロードできますが、なぜかファイル名が "abcd" という謎のファイルがダウンロードされます。

実態は、仮想ディスクで、Azure 仮想マシンが第1世代であれば、abcd.vhd と名前を変更するだけで問題ありませんが、第2世代だどそのまま起動することができません。

第2世代の Azure 仮想マシンの仮想ディスクファイル "abcd" から Hyper-V 仮想マシンを起動させるまでは、ちょっとやることが多いので、PowerShell で自動化してみました。

やること

  1. abcd ファイルに拡張子 .vhd を付ける
  2. vhd 形式の仮想ディスクを vhdx 形式の仮想ディスクへ変更
  3. 仮想マシン作成、仮想マシンの設定を変更
  4. 仮想マシン起動

PowerShell スクリプト

AzureVM2HyperVVM.ps1
# 1. ファイル名 C:\Path\To\abcd を C:\Path\To\abcd.vhd に名前変更
$downloadFilePath = "C:\Path\To\abcd"
$newFileName = "abcd.vhd"

# ファイルが存在するか確認し、存在する場合は名前変更
if (Test-Path $downloadFilePath) {
    Rename-Item -Path $downloadFilePath -NewName $newFileName
    Write-Host "Renamed file to: $newFileName"
} else {
    Write-Host "File not found: $downloadFilePath"
    exit -1
}

# 2. 仮想ディスク abcd.vhd を abcd.vhdx [可変ディスク]へ変換
$directoryPath = Split-Path -Path $downloadFilePath
$vhdFilePath = "$directoryPath\$newFileName"
$vhdxFilePath = "C:\Path\To\abcd.vhdx"

Convert-VHD -Path $vhdFilePath -DestinationPath $vhdxFilePath -VHDType Dynamic
Write-Host "Converted VHD to VHDX: $vhdxFilePath"

# 3. 第2世代の Hyper-V 仮想マシンを変換した仮想ディスク (.vhdx) を指定して作成
$vmName = "TestVM"
$vmMemory = 8GB
$cpuCount = 4

# 仮想マシンの作成
New-VM -Name $vmName -Generation 2 -MemoryStartupBytes $vmMemory -BootDevice VHD -VHDPath $vhdxFilePath

# CPUコア数設定、静的メモリに設定、自動チェックポイント機能をOFF
Set-VM -VMName $vmName -ProcessorCount $cpuCount -StaticMemory -AutomaticCheckpointsEnabled $false
Write-Host "Created VM: $vmName with $cpuCount CPUs and $vmMemory memory."

# DVDドライブを追加(任意)
Add-VMDvdDrive -VMName $vmName -ControllerNumber 0
Write-Host "Added DVD drive to VM: $vmName"

# セキュアブートを解除(任意)
Set-VMFirmware -VMName $vmName -EnableSecureBoot Off
Write-Host "Disabled Secure Boot for VM: $vmName"

# 仮想マシンのチェックポイントを取得(任意)
$checkpointName = "Init Checkpoint"
Checkpoint-VM -Name $vmName -SnapshotName $checkpointName
Write-Host "Checkpoint '$checkpointName' has been created for VM '$vmName'."

# 4. 仮想マシンを起動する
Start-VM -Name $vmName
Write-Host "Started VM: $vmName"

参考URL

New-VM (Hyper-V) | Microsoft Learn
https://learn.microsoft.com/en-us/powershell/module/hyper-v/new-vm

Set-VM (Hyper-V) | Microsoft Learn
https://learn.microsoft.com/en-us/powershell/module/hyper-v/set-vm

Add-VMDvdDrive (Hyper-V) | Microsoft Learn
https://learn.microsoft.com/en-us/powershell/module/hyper-v/add-vmdvddrive

Set-VMFirmware (Hyper-V) | Microsoft Learn
https://learn.microsoft.com/en-us/powershell/module/hyper-v/set-vmfirmware

Start-VM (Hyper-V) | Microsoft Learn
https://learn.microsoft.com/en-us/powershell/module/hyper-v/start-vm

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?