0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows Hyper-V環境において動的メモリ割り当てに修正する

0
Posted at

本記事でやること

  • Windows Hyper-V 環境上の仮想マシンのメモリ割り当てを「静的」から「動的」へ変更する。

前提条件

  • Windows Server 2025 / Windows Hyper-V
  • 仮想マシンは Windows10

先ずは仮想マシンの一覧を見る

PowerShell
Get-VM
Get-VM | Select-Object Name,State,Uptime

表示する項目を指定しない場合は以下のようなレイアウトで出力される。

Name State CPUUsage(%) MemoryAssigned(M) Uptime Status Version
---- ----- ----------- ----------------- ------ ------ -------

条件指定における対象仮想マシンの表示

仮想マシン名での表示
Get-VM -Name [VirtualMachineName]
停止している仮想マシンの表示
Get-VM | Where-Object { $_.State -eq "Off"}
Uptimeで判断したい場合
# 10日以上
Get-VM | Where-Object { $_.Uptime.Days -ge 10 }
# 10時間以上
Get-VM | Where-Object { $_.Uptime.Hours -ge 10 }
Hyper-V仮想ホスト以外からCLIを実施したい場合
# 記載はループバックアドレスなので自分自身ではある
Get-VM -ComputerName "127.0.0.1"
特定のキーワードを含むものを表示する場合
# 以下は「Nahida」を含む仮想マシンを表示
Get-VM -ComputerName "127.0.0.1" | Where-Object { $_.Name -like "*Nahida*" }

現在の設定を見る

メモリに関するpropertyを表示
Get-VM -Name Jedai-MASTER | Select-Object { 
    $_.Name,
    $_.DynamicMemoryEnabled,
    $_.MemoryStartup,
    $_.MemoryMinimum,
    $_.MemoryMaximum,
    $_.MemoryAssigned
    }

この場合、出力結果が「...」で省略,まとまっている状況だと思われるので、以下のようにする。

メモリに関するpropertyを表示
# 規定値:4の為、無制限にする
$FormatEnumerationLimit = -1
# ここはそのまま
Get-VM -Name Jedai-MASTER | Select-Object { 
    $_.Name,
    $_.DynamicMemoryEnabled,
    $_.MemoryStartup,
    $_.MemoryMinimum,
    $_.MemoryMaximum,
    $_.MemoryAssigned
    }

「静的」か「動的」か、はDynamicMemoryEnabledで決まる。
Falseならば「静的」であり、Trueならば「動的」である。

動的メモリの仮想マシンだけを表示
$FormatEnumerationLimit = -1
Get-VM -ComputerName "127.0.0.1" | 
    Where-Object { $_.DynamicMemoryEnabled -eq $true } |
    Select-Object { 
        $_.Name,
        $_.DynamicMemoryEnabled,
        $_.MemoryStartup,
        $_.MemoryMinimum,
        $_.MemoryMaximum,
        $_.MemoryAssigned
    }

動的割り当てへの変更

先ずは仮想マシンを指定して動的割り当てに変更する。
なお、メモリの変更はその対象マシンを停止してから行うこと。
また、PowerShellの実行者は管理者権限(プロンプトが管理者モード)であること。

あと、変更出来る,or出来ないの指標は以下のとおり。

仮想マシンが起動したまま,and DynamicMemoryEnabled $false
-MaximumBytes 設定不可,動的の有効化が必要
-MinimumBytes 設定不可,動的の有効化が必要
-StartupBytes 設定不可,停止が必要
仮想マシンが起動したまま,and DynamicMemoryEnabled $true
-MaximumBytes 設定可能 :)
-MinimumBytes 設定可能 :)
-StartupBytes 設定不可,停止が必要
# ★注意★ MaximumBytes>StartupBytes>MinimumBytesになるようにする。
# ★注意★ MaximumBytesは増やすことのみ可能
# ★注意★ MinimumBytesは減らすことのみ可能
仮想マシンは停止済み,and DynamicMemoryEnabled $false
-MaximumBytes 設定不可,動的の有効化が必要
-MinimumBytes 設定不可,動的の有効化が必要
-StartupBytes 設定可能 :)
# ★注意★ 静的の場合、StartupBytesはその仮想マシンに割り当てるメモリサイズとなる。
# ※気になる点は、MaximumBytesはそのホストの搭載メモリを示している点。
仮想マシンは停止済み,and DynamicMemoryEnabled $true
-MaximumBytes 設定可能 :)
-MinimumBytes 設定可能 :)
-StartupBytes 設定可能 :)
# ★注意★ MaximumBytes>StartupBytes>MinimumBytesになるようにする。

ということで設定コマンドは以下のとおり。

aaa
# 動的有効化
Set-VMMemory -VMName momonga -DynamicMemoryEnabled $true
# 動的無効化
Set-VMMemory -VMName momonga -DynamicMemoryEnabled $false
# 各設定の変更
Set-VMMemory -VMName momonga -MaximumBytes 10GB
Set-VMMemory -VMName momonga -MinimumBytes 2GB
Set-VMMemory -VMName momonga -StartupBytes 4GB

では仮想マシンの動的メモリを実施

動的無効,and 停止済みの対象を変更
$FormatEnumerationLimit = -1
Get-VM -ComputerName "127.0.0.1" | 
    Where-Object { $_.DynamicMemoryEnabled -eq $false -and $_.State -eq "Off" } |
        ForEach-Object {
            Write-Host -ForegroundColor White -BackgroundColor Black $_.Name
            Get-VMMemory -VMName $_.Name | 
                Select-Object VMName,DynamicMemoryEnabled,Mximum,Startup,Minimum,Buffer | 
                    Format-List *
            Set-VMMemory -VMName $_.Name -DynamicMemoryEnabled $true `
                -MaximumBytes 4GB -StartupBytes 2GB `
                -MinimumBytes 512MB -Buffer 20
            Get-VMMemory -VMName $_.Name | 
                Select-Object VMName,DynamicMemoryEnabled,Mximum,Startup,Minimum,Buffer | 
                    Format-List *
        }

もう一度設定が想定どおりかどうか見る

設定確認
$FormatEnumerationLimit = -1
Get-VM -ComputerName "127.0.0.1" | 
    Where-Object { $_.DynamicMemoryEnabled -eq $true } |
    Select-Object { 
        $_.Name,
        $_.DynamicMemoryEnabled,
        $_.MemoryStartup,
        $_.MemoryMinimum,
        $_.MemoryMaximum,
        $_.MemoryAssigned
    }

起動する

PowerOn仮想マシン
Get-VM | Where-Object {$_.State -eq 'Off'} | Start-VM

一気に立ち上げると負荷がかかるので、ちょっとずつ起動タイミングをずらす。並列は5個。

仮想マシンたたき起こすのだ
# 対象の条件
# ★注意★ Select-Object等で入れ込まないこと
$targetVMs = Get-VM | Where-Object {
    $_.Name -like "*TARGET-MACHINE*" -and 
    $_.State -eq "Off"
}

$batchSize = 5     # 一度に起動する台数
$delaySeconds = 30 # 待機時間(sec)

if ($targetVMs.Count -eq 0) {
    Write-Host "対象となる停止中の仮想マシンはないのだ。あきらめて寝るのだ。" -ForegroundColor Yellow
    exit
}

Write-Host ("合計 {0} 台の仮想マシンを {1} 台ずつ起動するのだ。がんばるのだ。" -f $targetVMs.Count, $batchSize) -ForegroundColor Cyan

# 指定した台数ずつ繰り返し
for ($i = 0; $i -lt $targetVMs.Count; $i += $batchSize) {
    # -FirstでSelectの最大を指定、$iはSelectの開始位置、指定した起動台数分で回転する
    $currentBatch = $targetVMs | Select-Object -Skip $i -First $batchSize
    
    foreach ($vm in $currentBatch) {
        Write-Host ("起動中: {0} ..." -f $vm.Name) -NoNewline
        # -Confirm:$falseは、うるさい黙ってろの意味
        Start-VM -Name $vm.Name -Confirm:$false
        Write-Host " [かんりょ~]" -ForegroundColor Green
    }

    # 次のバッチがある場合のみ待機
    if ($i + $batchSize -lt $targetVMs.Count) {
        Write-Host ("`n{0} 秒、待つのだ。いきなりたくさんは入らないのだ。疲れるのだ。`n" -f $delaySeconds) -ForegroundColor Gray
        Start-Sleep -Seconds $delaySeconds
    }
}

Write-Host "すべて起動したのだ。確認しろなのだ。" -ForegroundColor Cyan
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?