Skip to content

Instantly share code, notes, and snippets.

@ddebroy
Created May 21, 2019 18:26
Show Gist options
  • Select an option

  • Save ddebroy/67bcbaeb4e1fe58255cd8a356c561c25 to your computer and use it in GitHub Desktop.

Select an option

Save ddebroy/67bcbaeb4e1fe58255cd8a356c561c25 to your computer and use it in GitHub Desktop.
Flexvolume iSCSI diskpart patch
diff --git a/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/disk_utils.ps1 b/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/disk_utils.ps1
index fcf6da9..c99d470 100644
--- a/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/disk_utils.ps1
+++ b/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/disk_utils.ps1
@@ -8,7 +8,32 @@ Function RegetDisk($disk)
Function GetVolumesForDisk($disk)
{
- return @($disk | get-partition | get-volume)
+ # Write-Host "disk: $disk.Number"
+ $p = get-partition | where DiskNumber -eq $disk.Number
+ # Write-Host $p
+ if ($p -eq $Null) {
+ # Write-Host "here"
+ return
+ }
+ $v = get-volume -UniqueId $p.AccessPaths
+ # Write-Host $v
+ get-volume -UniqueId $p.AccessPaths
+}
+
+Function InitalizeDiskIfNecessaryDiskPart($diskNum)
+{
+ $TempFile = New-TemporaryFile
+ $diskpartscript = @"
+select disk $diskNum
+online disk noerr
+attributes disk clear readonly
+clean
+convert mbr
+"@
+ $diskpartscript | Out-File -FilePath $tempFile.FullName -Encoding ASCII
+ # Write-Host "created: $tempFile.FullName"
+ $command = 'C:\Windows\System32\diskpart.exe /s $tempFile.FullName'
+ Invoke-Expression $command
}
Function InitalizeDiskIfNecessary($disk)
diff --git a/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/iscsi.ps1 b/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/iscsi.ps1
index 5449e0e..d8344d5 100644
--- a/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/iscsi.ps1
+++ b/flexvolume/windows/plugins/microsoft.com~iscsi.cmd/iscsi.ps1
@@ -1,5 +1,3 @@
-$global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
-
$logSource = "KubeISCSI"
$FriendlyDiskName = "KubernetesISCSI"
$exeName = "iscsiHelper.exe"
@@ -183,24 +181,29 @@ Function ConnectIscsi(
return $connection
}
-Function CreateVolumeIfNecessary($disk, $fsType)
+Function ExecDiskPartScript($disk)
{
- Update-Disk -number $disk.number
- EnsureDiskIsReadWriteOnline $disk
- InitalizeDiskIfNecessary $disk
- #fetch newest info
- $disk = RegetDisk $disk
-
+ $TempFile = New-TemporaryFile
+ $diskpartscript = @"
+select disk $disk
+online disk noerr
+attributes disk clear readonly
+clean
+convert mbr
+create partition primary
+format quick fs=ntfs
+"@
+ $diskpartscript | Out-File -FilePath $tempFile.FullName -Encoding ASCII
+ # Write-Host "created: $tempFile.FullName"
+ $command = 'C:\Windows\System32\diskpart.exe /s $tempFile.FullName'
+ $output = Invoke-Expression $command
+}
- $volumes = GetVolumesForDisk $disk
- if(($volumes | ? {$_.FileSystemType -eq $fsType} | Measure-Object).count -eq 0)
- {
- Log "There are no volumes of type $fsType so creating a volume of type $fsType on disk $($disk.number)"
- if($volumes.length -ne 0)
- {
- Log "there are however currently existing volumes on the disk!"
- }
- New-Volume -Disk $disk -FileSystem $fsType -FriendlyName $FriendlyDiskName | Out-Null
+Function CreateVolumeIfNecessary($disk, $fsType)
+{
+ $p = get-partition | where DiskNumber -eq $disk.Number
+ if ($p -eq $Null) {
+ ExecDiskPartScript $disk.Number
}
}
@@ -382,8 +385,9 @@ function mount_command_with_options(
$targetAndPorts += ,($target, $port)
}
$connection = ConnectIscsi -targetAndPorts $targetAndPorts -iqn $iqn -authType $authType -multiPathEnabled $isMultiPath -sessionChapUsername $sessionChapUsername -sessionChapSecret $sessionChapSecret -discoveryChapUsername $discoveryChapUsername -discoveryChapSecret $discoveryChapSecret
-
+ update-hoststoragecache
$diskNumber = GetDiskNumberFromIscsi $connection $lun
+ # Write-Host $diskNumber
if($isReadWrite)
{
$reservation = GetReservations -diskNumber $diskNumber
@@ -412,6 +416,7 @@ function mount_command_with_options(
Log "Prempting format key failed, we must already had it. Creating the volume if necessary"
}
$disk = GetDiskByNumber $diskNumber
+ # Write-Host "disk3: $disk.Number"
CreateVolumeIfNecessary $disk $fsType
RegisterDisk $diskNumber $prWrite
}
@@ -419,6 +424,7 @@ function mount_command_with_options(
{
$reservation = GetReservations -diskNumber $diskNumber
RegisterDisk $diskNumber $prWrite
+ $disk = GetDiskByNumber $diskNumber
if($reservation.key -ne $prWrite)
{
MakeDiskIdUnwrittable $diskNumber
@@ -500,21 +506,9 @@ function mount_command_with_options(
catch {}
}
}
-
- $disk = GetDiskByNumber $diskNumber
- if($isReadWrite)
- {
- SetDiskOffline $disk $false
- SetDiskReadOnly $disk $false
- }
- else
- {
- SetDiskOffline $disk $false
- SetDiskReadOnly $disk $true
- }
-
+ # Write-Host "disk2: $disk.Number"
$volumes = GetVolumesForDisk $disk
- $volume = ($volumes | ? {$_.FileSystemType -eq $fsType})| GetFirst "Could not find volume of type $fsType in volume $volume"
+ $volume = ($volumes | ? {$_.FileSystemType -eq $fsType})| GetFirst "Could not find volume of type $fsType in volumes $volumes"
$remotePath = $volume.path
@@ -548,8 +542,6 @@ function unmount_command($path)
$volume = $_
$volume | Write-VolumeCache | out-null
$disk = $volume | Get-Partition | Get-Disk
- SetDiskReadOnly $disk $true
- SetDiskOffline $disk $true
}
DeleteSymLink $path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment