2021-01-19

AutoMount VHDx in Azure File Shares for Storage Pool Virtual Disk

Azure Files 就是 Storage Account 的 File Share,直接用 \\UNCPath\ 的方式存取效能不佳 (謎?)
如果在其上建立 VHDx 並在一台 VM 中 Mount 起來當成 File Server 來提供服務效能較佳,若用多個 VHDx 組成 Storage Pool 的話效能更好

Azure Files Standard file shares 單一檔案最大 1 TB,所以可以建立 5 個 1TB 的 Dynamic VHDx 掛接起來,建立 Simple Thin Virtual Disk 來使用,一樣取得 5TB 儲存空間,且具有高存取效率與重複資料刪除 Deduplication (Windows Server Feature),同時還可以 Pay as you go

$StorageArray 定義 Volume Name

$VolumeName 陣列定義 VHDx 成員 的 FullPath

完成以上定義後,Get-Volume 找目前系統中是否已經將 $StorageArray 中的每一個 Volume 都掛上,如果沒有,就檢查是哪一個 VolumeName 還沒掛上,將還沒掛上的 $VolumeName 陣列中的 VHDx 全部掛上。

##### 程式開始 #####

$LogsPath = $(Get-Location).Path + '\Logs\';
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False;

if ((Test-Path $LogsPath) -ne $True) {
New-Item -Path $(Get-Location).Path -Name "Logs" -ItemType "Directory"
};

$ScriptStartTime = (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyy-MM-dd HH:mm:ss');
$DateTimeString = (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyyMMdd HHmmss')
$TranscriptLog = (Get-Item -Path ".\" -Verbose).FullName + "\Logs\" + ("MountACSVHDx " + $DateTimeString + ".log")
start-transcript -path $TranscriptLog

$StorageArray = @();
$StorageArray += 'Storage01';
$StorageArray += 'Storage02';
$StorageArray += 'Storage03';

foreach ($Storage in $StorageArray) {
if ([String]$Storage.contains('Storage01') -eq $True) {
write-host $Storage
if ($Storage -ne $Null) {
Remove-Variable -Name $Storage;
};
New-Variable -Name $Storage -Value @();
(Get-variable -Name $Storage).Value += '\\StorageAccount01.file.core.windows.net\files\VirtualDisk01.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount01.file.core.windows.net\files\VirtualDisk02.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount01.file.core.windows.net\files\VirtualDisk03.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount01.file.core.windows.net\files\VirtualDisk04.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount01.file.core.windows.net\files\VirtualDisk05.vhdx';
(Get-variable -Name $Storage).Value
};

if ([String]$Storage.contains('Storage0') -eq $True) {
write-host $Storage
if ($Storage -ne $Null) {
Remove-Variable -Name $Storage;
};
New-Variable -Name $Storage -Value @();
(Get-variable -Name $Storage).Value += '\\StorageAccount02.file.core.windows.net\files\VirtualDisk01.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount02.file.core.windows.net\files\VirtualDisk02.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount02.file.core.windows.net\files\VirtualDisk03.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount02.file.core.windows.net\files\VirtualDisk04.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount02.file.core.windows.net\files\VirtualDisk05.vhdx';
(Get-variable -Name $Storage).Value
};

if ([String]$Storage.contains('Storage03') -eq $True) {
write-host $Storage
if ($Storage -ne $Null) {
Remove-Variable -Name $Storage;
};
New-Variable -Name $Storage -Value @();
(Get-variable -Name $Storage).Value += '\\StorageAccount03.file.core.windows.net\files\VirtualDisk01.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount03.file.core.windows.net\files\VirtualDisk02.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount03.file.core.windows.net\files\VirtualDisk03.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount03.file.core.windows.net\files\VirtualDisk04.vhdx';
(Get-variable -Name $Storage).Value += '\\StorageAccount03.file.core.windows.net\files\VirtualDisk05.vhdx';
(Get-variable -Name $Storage).Value
};
};

#$PoolDiskArray = (Get-Disk | where {$_.Model -eq "Storage Space"} | select FriendlyName);
$VolumeArray = (Get-Volume | Where-Object {$_.FileSystemLabel -in $StorageArray} | Select FileSystemLabel).FileSystemLabel;

while ($VolumeArray.Length -ne $StorageArray.Length) {
foreach ($Storage in $StorageArray) {
$Hit = 0;
foreach ($Volume in $VolumeArray) {
if ($Volume -eq $Storage) {
$Hit = 1;
break;
};
};

if ($Hit -eq 0) {
foreach ($VHDx in (Get-variable -Name $Storage).Value) {
$TryTimes = 0;
$VHDxExist = (Test-Path -Path $VHDx -PathType Leaf);
while ($VHDxExist -ne $True) {
write-host ($Storage + ' - ' + $VHDx + ': Not Exist, Cannot Mount');
Start-Sleep -Seconds 1;
$VHDxExist = (Test-Path -Path $VHDx -PathType Leaf);
if ($VHDxExist -eq $True) {
break;
};
$TryTimes++;
if ($TryTimes -gt 60) {
write-host ($Storage + ' - ' + $VHDx + ': Not Exist, 60 try times');
break;
};
};

if ($VHDxExist -eq $True) {
write-host ($Storage + ' - ' + $VHDx + ': Exist, Trying to mount');
Mount-VHD -Path $VHDx;
};
};
};
};

$VolumeArray = (Get-Volume | Where-Object {$_.FileSystemLabel -in $StorageArray} | Select FileSystemLabel).FileSystemLabel;
};

Write-Host ("`r`n" + 'Script execution time: ' + [math]::Round((New-TimeSpan -Start $ScriptStartTime -End (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyy-MM-dd HH:mm:ss')).TotalSeconds) + ' Seconds');

stop-transcript

##### 程式結束 #####

沒有留言:

張貼留言