r/kubernetes • • 1d ago

CSI Driver for Hyper-V

Hey!

So, I've been running a K3S cluster on Hyper-V for a while now and have come across a little bit of what I would call - an inability to find a suitable CSI driver for Hyper-V.

There are some available; however from looking into it - they appear to require you to share administrator credentials with Kubernetes along with a network route. Another method I see often quoted is to pass-through a network path to the SAN, share SAN credentials and use the SAN CSI provider - however this kind of breaks the storage network's isolation.

The final method which I personally use is Longhorn - but it's replicated between nodes and obviously takes up multiples of storage requirements - meaning you burn storage at least twice as fast as you need to.

---

To be transparent, I'm building this with Claude completely end to end. I have personal interests and after the day job - I want to make fast progress with this so I can use it - after a working product I'll probably slow down and go through it thoroughly - but if anyone else wants a Hyper-V CSI Driver which passes through the native VHDX file; I welcome any comments on it. I also welcome anyone's testing and feedback - as to be fair, although heavily AI driven in development, I do intend to eventually settle on a stable and trustworthy CSI driver that I can use for my personal endeavours.

Appreciate your reading. If you do happen look at the repo; please look at csi-implementation branch.

https://github.com/ODickins/HyperVolume

Edit: flick to main branch.

3 Upvotes

13 comments sorted by

3

u/addy3006 1d ago

I'd test the ugly case early: a node VM hangs while it still holds the VHDX and the pod gets rescheduled. Does the driver detach it from the old VM by force, or wait for someone to confirm that VM is really off?

1

u/LA33R 1d ago

So on every Hyper-V host, the reconciler runs every 10 seconds (unless you alter this). Each cycle it will list the VM's on it's own host and the current state and disks attached.

For each disk that's attached (that we have in our pool) the reconciler will keep a '.lease' file open next to it. It writes who has it, where it is - and when the lease expires.

I went with leases because - what happens when a Hyper-V node just crashes completely - you need to free it so it can be attached elsewhere.

The leases are renewed every 30 seconds by default - and expire after 120 second - so next to each vhdx file we write "we've got this until now() + 120s"; if the Hyper-V node crashes - our PVC will be unavailable until after that expires.

Now if the VM is "running" and has a disk attached - even if the guest OS is completely hung - it's the agents process (on Hyper-V) that keeps that lease ticking.

Other agents won't touch it until that lease has either been removed or expired.

A case I haven't tested for - but your comment has prompted me now; what happens it VMMS crashes - and we cannot list the VM's.

I think in this case; I'll probably cause the agent to keep renewing the lease - to "stay safe" as it were. Effectively "We cannot confirm it's powered off; so treat it as connected" and keep renewing the lease.

That will keep the PVC bound until the host is rebooted - or VMMS recovers and the VM is confirmed powered off.

I think I'll air on the side of caution there; and wait for an admin to come fix the issue - than attempt automatic recovery blind.

---

After all this though; Hyper-V will observe your standard windows locks. So you can't have a vhdx file attached to two VM's running at the same time - windows will lock the file and Hyper-V will prevent you. So say all the leasing expires; all it should do in reality is change the shape of the errors - and you'll get back an obscure error when attempting to attach it.

--

And what I've just said has prompted another case I'll put in - a saved VM. That will drop the windows lock and permit the file to be attached. I think I'll get the agent to push the lease expiry right out in this instance - effectively "locking off" the pvc completely. Someone saved the VM; I'm not here to decide to pull that VHDX.

1

u/aiadrianchase 1d ago

I’d test two cases with a disposable PVC: a VM that loses contact with the control plane but can still write to the VHDX, and a VM that Hyper-V confirms is powered off. Watch kubectl get volumeattachments -w alongside the CSI controller logs and the disk attachment state in Hyper-V. In the first case, check that the replacement pod can’t mount the disk while the old VM can still write. In the second, verify that the disk detaches from the old VM, attaches once to the new VM, and that the replacement pod can read back a known file. That gives you a clear pass/fail check for both data safety and recovery.

1

u/LA33R 1d ago

So the control plane is not the decider of whether or not the mount the vhdx file; that's the agent - on hyper-v. Final decision is on the lease being held. A control plane can ask for it to be mounted - but the agent may refuse and you'll just get a backoff inside kubernetes and auto-retry.

That being said; I'll add it to my checklist to actually confirm it.

1

u/aiadrianchase 1d ago

Thanks, that clarifies where the decision is made. In the network-partition test, I’d check that the agent keeps rejecting the second attach while the old VM can still write, then confirm that a retry succeeds after the old VM is confirmed off and its lease is released.

1

u/LA33R 1d ago

I have indeed now tested that - I'm now waiting on a few mini PC's. I'm going to spin up a Hyper-V cluster then complete more thorough testing over many physical machines and a "SAN" backed CSV over iSCSI. See how it handles - but I think it's looking pretty promising as a solution IMO; just needs plenty thorough testing and a walk through in code myself rather than just relying on Claude's output.

1

u/aiadrianchase 1d ago

The physical cluster and iSCSI-backed CSV should make for a useful next test. I’d run one pod that appends a sequence number and fsyncs every record, then test a host loss and an iSCSI path loss separately. On recovery, check for a contiguous prefix of the writes the app acknowledged and correlate the last successful write with the lease transition and the next attach. That gives you a data-integrity check alongside the Kubernetes events. For the code walkthrough, I’d start with how the agent handles a mount that succeeds but whose response never reaches the caller, since a retry should be safe in that case.

1

u/LA33R 1d ago

Certainly. I’ll add that as a planned test next. Unfortunately, going to have to wait for hardware and I’m away now for a week. So it’ll be something I pick up in a weeks time. Appreciate the feedback though.

1

u/aiadrianchase 17h ago

Thanks for the update. When the hardware is ready, a run with the host failure and the storage-path failure tested separately would be useful to see. I’d be especially interested in the last acknowledged sequence number before each failure and the first one written after recovery. Enjoy your week away!

1

u/intersectRaven 1d ago

I love the idea actually. Makes it simpler when I already have a set configuration for HyperV.

1

u/LA33R 1d ago

Appreciated. Yeah the current setup for HyperV certainly makes it feel a bit harder than it needs to be. Hopefully this gives an almost one-click method eventually.

3

u/OutcomeMaster6678 1d ago

Hot attaching VHDX files works.