question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create NTFS VHD?

See original GitHub issue

In the Readme.md is a nice short example, how to create a FAT VHD. But how do I create an NTFS VHD?

So far I have

    long diskSize = 300 * Sizes.OneMiB;
    var vhdPath = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), "vhd"));
    using (Stream vhdStream = File.Create(vhdPath))
    {
        var disk = DiscUtils.Vhd.Disk.InitializeFixed(vhdStream, Ownership.Dispose, diskSize); // What's the difference between Ownership.Dispose and Ownership.None?
        BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsNtfs);

        using (var ntfs = NtfsFileSystem.Format(disk.Content, "Virtual NTFS drive", disk.Geometry, disk.Partitions[0].FirstSector, diskSize / disk.SectorSize))
        {
            ntfs.CreateDirectory(@"TestDir\CHILD");
            // do other things with the file system…
        }
    }

Before NtfsFileSystem.Format I have a VHD that seems to have a valid MBR and if I mount it, Windows tells me to format that new drive. After NtfsFileSystem.Format the MBR is gone and the Windows Management Tools tell me to initialize the drive with an MBR or GPT first.

Also, I commented it in the code snippet: What’s the difference between Ownership.Dispose and Ownership.None?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
EricZimmermancommented, Feb 20, 2019

this code (slightly changed) works for me fine. if i make a vhdx file i can double click to mount it in windows

       var diskSize = 2000 * 1024 * 1024;
      var asVhd = false;

       using (var fs = new FileStream(_vhfxFileName, FileMode.OpenOrCreate))
        {
            VirtualDisk destDisk = Disk.InitializeDynamic(fs, Ownership.None, diskSize);

            if (asVhd)
            {
                destDisk = DiscUtils.Vhd.Disk.InitializeDynamic(fs, Ownership.None, diskSize);
            }

            BiosPartitionTable.Initialize(destDisk, WellKnownPartitionType.WindowsNtfs);
            var volMgr = new VolumeManager(destDisk);

            var label = $"ZZZZZZ ({dateStamp})";

            using (var destNtfs =
                NtfsFileSystem.Format(volMgr.GetLogicalVolumes()[0], label, new NtfsFormatOptions()))
            {
                destNtfs.NtfsOptions.ShortNameCreation = ShortFileNameOption.Disabled;

				var destDirectory = Path.GetDirectoryName(@"Some file");
				destNtfs.CreateDirectory(destDirectory, nfo);
				
				
				using (Stream source = new FileStream(@"Some file2", FileMode.Open, FileAccess.Read))
                {
                    var destFileName = @"foo";
                    

                    using (Stream dest = destNtfs.OpenFile(destFileName, FileMode.Create,
                        FileAccess.ReadWrite))
                    {
                        
                        source.CopyTo(dest);
                        dest.Flush();
                    }

                  
                }
				
				//do more stuff here

                
            }

            //commit everything to the stream before closing
            fs.Flush();
        }
0reactions
wrcgatorcommented, Aug 11, 2021

I dont think the solution posted is working properly. It returns successfully and mounts, but if you look at it with disk/partition tools it doesnt have the same properties as a volume created with disk manager. For example, what ever is not correct about the drive is preventing Windows from allowing it to be Bitlocker’d. Has anyone else observed the same and have you found a solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create and set up a virtual hard disk on Windows 10
How to create a VHDX or VHD on Windows 10 · Open Start. · Search for Disk Management and click the top result...
Read more >
Create and Set Up New VHD or VHDX File in Windows 10
1 Open Disk Management (diskmgmt.msc). 2 Click/tap on Action in the menu bar, and click/tap on Create VHD. (see screenshot below).
Read more >
How to create and set up new VHD or VHDX File in ...
Press Windows key + R. · Click Action in the menu bar, and click Create VHD. · On the dialog that pops up,...
Read more >
Create vdisk
Creates a new virtual hard disk (VHD) file by using the available disk ... the VHD and format the partition(s) by using FAT,...
Read more >
VHD/VHDX: How to Create a Dynamically Expanding ...
Open Disk Management · Create a Virtual Drive for Windows 10 · Set a name and location for your VDHX File · Create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found