Skip to content

Instantly share code, notes, and snippets.

@ThePlenkov
Last active November 27, 2024 11:53
Show Gist options
  • Save ThePlenkov/e1a19ee8fe0d89552281e20dfba09c6a to your computer and use it in GitHub Desktop.
Save ThePlenkov/e1a19ee8fe0d89552281e20dfba09c6a to your computer and use it in GitHub Desktop.
Organize your workspace by mounting external VHDX drive to WSL

Why?

I am a permanent Windows user and I do not choose Mac on purpose, because of some specfic vendor software I'm used to work with all my career.

At same time I'm a software engineer and I'm used to manage all my projects in Linux only. Luckily Windows delivers WSL, and especially with WSL2 life became much simpler.

When you work with large amount of projects ususally you organize kind of workspace, a folder containing all the projects. At least I do like this.

However next question is - where do we create such a workspace. Initally i tried to work in a workspace stored on windows drive C: opened via /mnt/c folder in wsl. It didn't took me much time to realise this is a bad idea. Ther reason for this is that Windows using NTFS volume format and Linux doesn't work well with it. At same time Linux works just perfectly with ext4 format.

Alternatively, if I create workpsace folder right in WSL and clone all my projects there - I have another problem.

  • I cannot share this folder among distros and I'm used to have several like Ubuntu and debian for different purposes.
  • it's hard to backup such a folder and you have a risk of loosing data. That's how I can to the idea - what if I create an external drive apart of wsl in ext4 format and mount it to wsl? I was very pleased to find that is possible in WSL2: https://learn.microsoft.com/en-us/windows/wsl/wsl2-mount-disk

How?

  • create a new vhdx file. Keep it not formatted
  • for the best experience I advise to format drive as ext4. You can do this from WSL itself once the drive is mount
  • mount the drive using a command like wsl --mount --vhd "path_to_your_drive\ws.vhdx" --partition 1 --name workspace
  • feel free to choose any name. It will be available in wsl via path like /mnt/wsl/workspace
  • Create partition and format drive to ext4 using a tool like fdisk right in WSL

Automatic mount on startup

It's very annoying to mount the drive manually to WSL but it's very simple to avoid.

  • create a simple script like this and save it as powershell command in a file like mount.ps1 next to you vhdx file
# Mount the VHD
wsl --mount --vhd "$PSScriptRoot\ws.vhdx" --partition 1 --name workspace
  • Press Win+R and run shell:startup

image

  • or simply naviage to C:\Users\{your_user}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. The command above just opens up the explorer in this folder.
  • create a shortcut in this folder for a previosly created file mount.ps1

That's it. On system restart a system will execute this scipt and you will have a drive always mount to WSL

Resize volume

If you created a virtual drive with a fixed maximum volume size, it may become full. Here are few steps to perform:

  • first unmount drive from wsl if it's mount wsl --unmount
  • You can try to use Resize-VHD ps command, but on my machine I unistalled Hyper-V intentionally caused it caused problems, so this script is not available
  • Alternative way is to use diskpart
diskpart>
select vdisk file="C:\Path\To\Your\VHDX.vhdx"
attach vdisk
expand vdisk maximum=<new size in MB>  // for example 20000 for 20GB
detach vdsik
  • mount wsl back as it was described above

Please notice that by default you only expand disk, but not the partition inside. You can see this for example using lsblk command in WSL:

sdf      8:80   0  29.3G  0 disk
└─sdf1   8:81   0    20G  0 part /mnt/wsl/workspace

You can see that sdf disk is 29.3G but only 20G is available for the partition sdf1.

Luckily it's very simple to change right from shell. There are multiple ways to do this, but my best choice is growpart command.

All what you need to do is:

sudo growpart /dev/sdf 1

where sdf - is the name of your disk, you should use the right letter, and 1 is the number of partition inside this disk ( sdf ).

Unblock reserved free space

Sometimes you don't want to extend the disk space anymore but you want to clean it a bit. However even if you delete large files or forlders you may notice that space is still taken. You can use this command to solve this problem:

sudo tune2fs -m 1 /dev/sdd1

Just make sure you use a right disk name!

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