DEV Community

box
box

Posted on

Optimize Huge Virtual Hard Disk.

Environment

Target File Example

C:\Users\{user}\AppData\Local\Docker\wsl\data\ext4.vhdx
Enter fullscreen mode Exit fullscreen mode

Background

  1. Docker Desktop for Windows uses a virtual machine (typically virtualization technologies like WSL or Hyper-V) to run Linux containers on Windows.
    The file system used within this virtual machine is generally in the ext4 format, which is commonly used in Linux.

  2. VHDX is a type of virtual hard disk format developed by Microsoft, used as a disk image for virtual machines in virtualized environments.
    Docker Desktop for Windows uses this format files to provide storage for Linux containers.

  3. ext4.vhdx is a virtual hard disk in VHDX format that hosts the ext4 file system.
    Docker Desktop for Windows uses this file to store data such as containers, images, and volumes that are mounted.
    This file will grow as the disk usage by WSL2 (i.e. Docker Desktop for Windows) increases, but it will not automatically shrink even if the disk usage decreases.

Remove Dangling Docker Objects.

As written above, doing this does not reduce the size of ext4.vhdx.
However, doing this makes optimization more efficient.

We simply open PowerShell and execute following commands.

Optimizing with diskpart

  1. Open PowerShell as admin.
    The reason for admin is that UAC pops up and Command Prompt opens if we start diskpart as a normal user.

  2. Shut down WSL to block access from it to a virtual hard disk.

    wsl --shutdown
    
  3. Enter diskpart.

    diskpart
    
  4. Specify the virtual hard disk to be optimized.

    select vdisk file="C:\Users\{user}\AppData\Local\Docker\wsl\data\ext4.vhdx"
    
  5. Attach the specified virtual hard disk.
    Since there is no need to change that data, we do it as read-only to prevent operational errors.

    attach vdisk readonly
    
  6. Optimize.

    compact vdisk
    
  7. Detach the specified virtual hard disk.

    detach vdisk
    
  8. Exit diskpart.

    exit
    

Optimizing with Hyper-V

* The Hyper-V does not exist on Home Edition.

We need to activate Hyper-V before we optimize.

  1. Open Turn Windows features on or off dialog.
    We just press the Windows key and the R key at the same time, then input optionalfeatures and click OK.
    optionalfeatures

  2. Turn on Hyper-V.
    Turn-on_Hyper-V

Now we can execute the command to optimize it.

  1. Open PowerShell as admin.
    This is because the command can only be executed by admin.

  2. Shut down WSL to block access from it to a virtual hard disk.

    wsl --shutdown
    
  3. Move current dir to the location where ext4.vhdx is located.

    cd C:\Users\{user}\AppData\Local\Docker\wsl\data
    
  4. Optimize.
    Since VHDX is optimized in Quick mode by default, we will specify Full mode explicitly for maximum optimization.

    Optimize-VHD -Path .\ext4.vhdx -Mode Full
    

Top comments (1)

Collapse
 
maestroviktorin profile image
Viktor Starodubtsev

If you encounter some problems with diskpart or don't have Hyper-V on your Windows this can be useful.