WARNING!! You may lose all of your data stored in the partition if you don't follow these steps correctly. Backup important data and use at your own risk. I strongly recommend you to start with an empty partition, or a loopback device backed by an empty image file.
This script will allow you to pass existing NTFS partition to windows running on KVM, which is also readable by bare-metal windows.
You can't simply pass existing NTFS partition to the VM since guest OS see the partion as a whole disk device with a partiton table.
The trick used here is sandwiching the partition with 2 loopback devices using software RAID (MD) device built with mdadm --build --level=linear
. First device stores main partition table in the beginning of disk, and second one stores backup table in the end of disk.
This idea is originally posted by a redditor ws-ilazki, I just converted it to the script: https://www.reddit.com/r/VFIO/comments/j443ad/pass_through_a_partition/g7hn38z/
Trim from guest OS seems to be not possible. If you want to run trim, just stop the VM and run from host.
At least on my environment, "${md_device_name}"-gpt-a.img
has to be 1MB for the partition to be read from correct offset, probably because the partion alignment is 1MB.
"${md_device_name}"-gpt-a.img
can be smaller but should be at least 512KB, the chunk size used in mdadm --build
.
Using a image size smaller than this seems to cause the software RAID device to skip loopback devices without warning, which in turn corrupts your data in the partition.
Steps to create new MD device (Replace <MD_DEVICE_NAME> and <PARTITION_UUID> with the value you want):
- If you haven't done so, turn off fast startup in both bare-metal windows and VM windows.
- Run
./init-software-raid.sh <MD_DEVICE_NAME>
to create empty disk images which will be used for loopback devices. - Add
start-software-raid.sh
and relevant configuration to your NixOS configuration to automatically build MD deivce at startup. - Open gparted
- Create a gpt partition table in the MD device.
- Create an unformatted partition that spans whole MD disk. The free space preceeding must be 0, the size must match with original partition, and flags must be
msftdata
. Don't choose NTFS here, that will re-format your partition. - Apply all operations in gparted. If you did everything right NTFS partition shows up after applying.
- You can optionally run
ntfsfix -n
to test that the partition is not corrupted. - Edit your VM configuration to add new MD device to the VM.