Add multiple physical disks at a time to Hyper-V VM?

davidv

New Member
Aug 2, 2022
16
1
3
I couldn't find any info on this anywhere, which may suggest it's not possible, but I'd love to find a way to do it.

I have 2 NetApp disk shelves with 48 disks total, and running TrueNAS in a VM. I'm still in the stage of playing around with it and I've already had to add all the physical disks to the VM a few times and it's painful doing it one at a time!

It would be nice if there was a way to just quickly tick all the disks to add, but I'm guessing that's not possible.

The solution would probably be via scripting. It could be achieved by selecting all disks that have a certain string in their model # as all the disks are the same model in each shelf (one has HGST, the other Seagate), or even matching on NetApp. I'm terrible with powershell and command line scripting though.

UPDATE: with some pointing in the right direction from j_h_o and cesmith9999 I cobbled together a little one-liner:

EDIT: Updated to remove curly braces around the $_.Number and to add the ControllerType flag to SCSI (otherwise it fills IDE first, then SCSI)

To add all offline drives that match "NETAPP" to VM TrueNAS on the SCSI controller:
get-disk | where-object FriendlyName -match "NETAPP" | where-object OperationalStatus -eq "Offline" | foreach-object { Add-VMHardDiskDrive -VMName "TrueNAS" -Number $_.Number -Passthru -ControllerType SCSI}

To remove all the Passthru drives on VM TrueNAS from the SCSI controller:
Get-VMHardDiskDrive -VMName "TrueNAS" | Where-Object DiskNumber -gt -1 | foreach-object {Remove-VMHardDiskDrive -VMName $_.VMName -Passthru -ControllerNumber $_.ControllerNumber -ControllerLocation $_.ControllerLocation -ControllerType SCSI}

To those wondering what the "Where-Object DiskNumber -gt -1" is for (where DiskNumber>-1), it's so it only removes the physical disks, not vdhx disks. It's a bit of a hack, but it worked.

NOTE: it can take a while.. you can refresh the settings in Hyper-V manager to confirm it's doing what's it's supposed to.
 
Last edited:

cesmith9999

Well-Known Member
Mar 26, 2013
1,361
459
83
There is, ...

We need a better understanding of what you have and where you want to go.

From your brief description, I am assuming that you want the disks added as passthrough disks to the truenas vm... from a hyper-v host.

Chris
 

davidv

New Member
Aug 2, 2022
16
1
3
From your brief description, I am assuming that you want the disks added as passthrough disks to the truenas vm... from a hyper-v host.
That's correct. I have a Windows Server 2022 host and passing through the disks to the TrueNAS VM by manually adding them one at a time to the VM's iSCSI controller as physical disks in the VM settings.

It's a painful process adding them one at a time for 48 drives.
 
Last edited:

j_h_o

Active Member
Apr 21, 2015
593
144
43
California, US
You can add with Powershell.

Figure out what all the disk numbers are (In Computer Management > Disk Management) and issue a bunch of these commands? Or try Get-Disk without any integer after it to do all drives; I think it'll error on drives that aren't offline anyway, so the host drives shouldn't get passed thru.

Code:
Get-Disk 2 | Add-VMHardDiskDrive -VMName TrueNAS
 
  • Like
Reactions: cesmith9999

cesmith9999

Well-Known Member
Mar 26, 2013
1,361
459
83
Why iscsi?


You can add all of the disks with this command. And the -passthru param.

Chris
 

nabsltd

Active Member
Jan 26, 2022
196
115
43
I have a Windows Server 2022 host and passing through the disks to the TrueNAS VM by manually adding them one at a time to the VM's iSCSI controller as physical disks in the VM settings.
Note that you aren't actually getting close enough to the physical disk to give ZFS the extra information that the TrueNAS community claims is required to avoid data loss. Personally, I don't think this is an issue, but the LUN exported via an iSCSI server does not allow the direct hardware access that it is claimed that ZFS needs.

If you are using the NetApp disk shelves as part of an actual NetApp storage system, it can do the same levels of data protection that TrueNAS can do, so you are better off just setting up the protection at that point and serving out the storage via NFS or SMB instead of adding the layer of virtualized TrueNAS into the mix.

If you are just using the NetApp disk shelves as DAS for your Windows 2022 server, then you shouldn't use iSCSI, since the disks are directly attached to the OS that runs Hyper-V, and can then be directly passed through with all the physical properties to the VM. But, if you are using the shelves as DAS, the TrueNAS community would say you should just run TrueNAS on the bare metal instead of using Windows 2022.
 

davidv

New Member
Aug 2, 2022
16
1
3
Why iscsi?
Oops, didn't mean to say iSCSI. Meant the SCSI controller.

OK I figured it out.

I haven't run the actual Add-VMHardDiskDrive bit yet because the drives are attached now, but I know the rest before it works.
So this should do it (unless I've messed up the last foreach-object bit, which I haven't tested yet):

get-disk | where-object FriendlyName -match "NETAPP" | where-object OperationalStatus -eq "Offline" | foreach-object { Add-VMHardDiskDrive -VMName "TrueNAS" -Number $_.Number -Passthru -ControllerType SCSI}

I've put two where-object selects in there, one for the text match for "NETAPP" and the other to make sure it's offline.

Thanks for the help guys!
 
Last edited:

davidv

New Member
Aug 2, 2022
16
1
3
you shouldn't use iSCSI, since the disks are directly attached to the OS that runs Hyper-V, and can then be directly passed through with all the physical properties to the VM.
Yes, that's what I've been doing, I don't know how that little "i" snuck in before the SCSI.. I meant adding the physical drives to the virtual SCSI controller of the VM to pass them through, not an iSCSI LUN.
 

davidv

New Member
Aug 2, 2022
16
1
3
BTW It's my first time writing my own Powershell script. Haven't needed to go beyond copy and pasting others' code until now.
Must say it's nice being able to script with objects in the command line.
 

davidv

New Member
Aug 2, 2022
16
1
3
the TrueNAS community would say you should just run TrueNAS on the bare metal instead of using Windows 2022.
Yeah I've just been playing around with it for now.. Playing with a bunch of things and haven't committed to one yet, so using Hyper-V for now as it's what I know. I've already experienced one issue with passing through via Hyper-V.. I unplugged one of my shelves to attach a power monitor to it. When it came back up, Windows had changed the disk IDs, so Hyper-V couldn't find the drives to pass through. That's a bit lame that it's not using GUIDs or serial numbers etc to identify disks. This is precisely why I had to remove and reattach all 48 drives back to the VM. TrueNAS doesn't seem fussed by the order of the drives as it's obviously keeping track of serials or a GUID it's written to them, so that was a relief. Not that there was any valuable data in the pool.. just test files.

Goals are 1 - to come up with a longer term NAS solution for the home network as my Synology is down to 3TB free, 2 - improve the home network infrastructure with Unifi (again moving from Synology - RT2600ac / MR2200ac), 4 - step up my game with extra workloads like HomeAssistant, SyncThing etc, from my old Synology with only 4GB RAM, 5 - have a home environment to test all sorts of new toys and deployments (homelab I guess), and 6 - to run basic test setups for stuff I'll be deploying to a DC soon. I've been purely cloud for years and moved away from hardware in favour of AWS and Azure as soon as AWS hit the market, but this current venture I'm working on has an unavoidable hardware element since I'll be setting up fibre links to customers, aggregating in my DC rack where I'll have a cache layer that acts as a storage gateway between my other cloud services and customer on-prem.

I might be deploying a HA Ceph cluster in the cloud soon, so I'm about to start playing with that - using Proxmox as the bare-metal hypervisor as I haven't been able to get it to successfully install under Hyper-V yet. Proxmox wants to commandeer an entire disk though which is a bit annoying.
 
Last edited: