Skip to content

Instantly share code, notes, and snippets.

@GarryLai
Last active March 13, 2024 09:33
Show Gist options
  • Save GarryLai/dac58b18e3860aef20c110058f2f5a3a to your computer and use it in GitHub Desktop.
Save GarryLai/dac58b18e3860aef20c110058f2f5a3a to your computer and use it in GitHub Desktop.
Expand a EXT4 additional VHD in WSL2
@echo off
set VHD_PATH=D:\data.vhdx
set NEW_SIZE_GB=2048
set MOUNT_NAME=data
set WSL_DIST=Ubuntu
set /a NEW_SIZE_MB=%NEW_SIZE_GB%*1024
echo:
echo ========== Expand VHD ==========
echo:
echo select vdisk file="%VHD_PATH%" > vhd_expand_diskpart.txt
echo detail vdisk >> vhd_expand_diskpart.txt
echo expand vdisk maximum=%NEW_SIZE_MB% >> vhd_expand_diskpart.txt
echo detail vdisk >> vhd_expand_diskpart.txt
echo exit >> vhd_expand_diskpart.txt
diskpart /s vhd_expand_diskpart.txt
del /f /q vhd_expand_diskpart.txt
echo:
echo ========== Expand File System ==========
echo:
wsl -d %WSL_DIST% --mount "%VHD_PATH%" --vhd --name %MOUNT_NAME%
echo:
wsl -d %WSL_DIST% -- echo Before: $(df -h ^| grep /mnt/wsl/%MOUNT_NAME%)
echo:
wsl -d %WSL_DIST% -- resize2fs /dev/$(lsblk ^| grep /mnt/wsl/%MOUNT_NAME% ^| awk '{print $1}') %NEW_SIZE_MB%M
echo:
wsl -d %WSL_DIST% -- echo After: $(df -h ^| grep /mnt/wsl/%MOUNT_NAME%)
echo:
wsl -d %WSL_DIST% --unmount %VHD_PATH%
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment