Skip to content

Instantly share code, notes, and snippets.

@spajak
Last active October 1, 2023 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spajak/29635eb363b6ec8997240b0b8244d784 to your computer and use it in GitHub Desktop.
Save spajak/29635eb363b6ec8997240b0b8244d784 to your computer and use it in GitHub Desktop.
Creating virtual hard disk (VHD) for WSL2

Creating additional virtual hard disk (VHDX) for WSL2 with ext4 filesystem

Lines starting with # mean the commands have to be executed by root user under Linux shell (WSL distro). All other commands have to be executed under Windows-PowerShell as Administrator.

Make VHDX disk file and mount it under Windows

New-VHD support.vhdx -SizeBytes 25GB -Dynamic -BlockSizeBytes 1MB
Write-Output "\\.\PhysicalDrive$((Mount-VHD -Path support.vhdx -PassThru | Get-Disk).Number)"

Tip: Open "Computer Management" GUI program, then navigate to "Storage/Disk Management" and U can see all the drives attached to Windows. The new virtual disk should also be there.

Note the drive name and mount it for WSL

wsl --mount \\.\PhysicalDriveX --bare

Enter your WSL distribution and note the device name (using one, or better: all the commands)

# lsblk
# dmesg
# fdisk -l

Create a partition on the device, format it with ext4 filesystem and mount it

# fdisk /dev/sdX

Using above tool create GPT partition table and one (or more) partition(s). Write all changes and quit. Then format the partition and mount it somewhere:

# mkfs.ext4 -G 4096 /dev/sdX1
# mkdir ~/support 
# mount /dev/sdX1 ~/support

Done.

To unmount

# unmount ~/support
wsl --unmount \\.\PhysicalDriveX

To detach it completely (eg. if you want to (re)move the file)

Dismount-VHD support.vhdx

Happy WSL!

References & sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment