Giter VIP home page Giter VIP logo

wslcompact's Introduction

WSLCOMPACT

Release Version Github All Releases

Safely compacts the size of the ever-growing WSL vhdx images.

(Do you like this utility? give it a ⭐ and share it)



FEATURES

The Windows Subsystem for Linux (WSL) uses VHDX image files to store the ext4 filesystem, but it lacks an effective way to shrink the image when the files are removed. This utility doesn't require elevated credentials,compacting the VHDX virtual drives of the WSL2 distros, and achieving the minimum possible size. By default it will perform in info mode, no action on images, providing the following information for all the distros installed:

  • Distro's name.
  • image file location.
  • Current size of the image file.
  • Estimated compacted size.
  • Estimated processing time.

If no distro is specified, it will target all the installed images sequentially. It operates in safe mode during the compact process, preventing any unwanted side effect in case of failure. This is a typical use case: Compacting Ubuntu image with confirmation:

PS> wslcompact -c Ubuntu
 WSL compact, v5.0 2023.02.02 (Groundhog edition)
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\Oki\WSL\Ubuntu\ext4.vhdx
 Current size:   12864 MB
 Estimated size: 7700 ± 188 MB
 The estimated process time using an SSD is about 2 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
The operation completed successfully.
 New Image compacted from 12864 MB to 7728 MB
 Do you want to apply changes and use the new image (y/N): y
 Image replaced for distro: Ubuntu

INSTALLATION

Before installing wslcompact, ensure your WSL installation is up to date. You can do that by typing wsl --update in PowerShell. WslCompact requires at least WSL version 1.0.0.

There are two ways to install WslCompact, choose your favorite:

OPTION 1: As a PowerShell module

It requires a special setting to run a remote script. If you have set it in the past, then you don't need it anymore. If you are not sure, in PowerShell just type:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

To install or update the utility, close all your PowerShell instances, open a fresh one and type:

iwr -useb https://raw.githubusercontent.com/okibcn/wslcompact/main/setup | iex

To remove the utility, close all your PowerShell instances, open a fresh one and type:

Remove-Item "$($env:PSModulePath.split(';')[0])/WslCompact" -Recurse -Force

OPTION 2: As a Scoop app

If you use Scoop package manager, then you can add the wslcompact utility directly from its bucket. Type in PowerShell these two lines:

scoop bucket add .oki https://github.com/okibcn/Bucket
scoop install wslcompact

To update the app just type:

scoop update wslcompact

To remove the app just type:

scoop uninstall wslcompact

USAGE

After installation, the usage is straightforward:

  • Calling wslcompact without arguments lists all the WSL images and information. No action on images will be performed.
  • You can select specific distros by passing their names as parameters, for instance wslcompact Ubuntu.
  • When using the -c compact option, wslcompact will modify the images after confirmation.
  • There is a special mode for data partitions. -d allows the compact of data partitions.

the utility ensures a minimal size and you end up with contiguous files for faster access in old HD-based systems. Should you need the list of names of your distros, it is accessible by typing wsl -l.

   Usage: wslcompact [OPTIONS] [DISTROS]

   wslcompact compacts the images of WSL distros by removing unsused space.
   If no option is provided, it will default to info mode, without modifying any image.
   If no distro is provided it will process all the installed images.
   NOTE: WSL will be shutdown for compacting the images.

   Options:
   no opt. Provides name, image file path, current size, and estimated new size information.
       -c   Compacting mode: process the selected distros compacting the images.
       -y   replaces selected images without asking for confirmation.
       -d   Enable the processing of data images. Default is disabled.
       -h   Prints this help

   Examples:
       wslcompact
       wslcompact -c -d
       wslcompact -c -y Ubuntu Kali

if your C: drive doesn't have enough temporal free space, the program won't compact that distro. Just change the TEMP folder before calling the function. So, instead of a simple wslcompact, just do:

$env:TEMP="Z:\your temp\folder"
wslcompact

The new TEMP folder will be active only for that PowerShell terminal session, so no problem at all for the rest of the system and it won't leave garbage.

EXAMPLES

A typical operation would be:

PS> wslcompact
 WSL compact, v5.0 2023.02.02 (Groundhog edition)
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\Oki\WSL\Ubuntu\ext4.vhdx
 Current size:   12864 MB
 Estimated size: 7700 ± 188 MB
 The estimated process time using an SSD is about 2 minutes.

 Distro's name:  Kali
 Image file:     C:\Users\Oki\WSL\Kali\ext4.vhdx
 Current size:   1579 MB
 Estimated size: 723 ± 18 MB
 The estimated process time using an SSD is about 1 minutes.

 Distro's name:  Arch
 Image file:     C:\Users\Oki\WSL\Arch\ext4.vhdx
 Current size:   1075 MB
 Estimated size: 860 ± 21 MB
 The estimated process time using an SSD is about 1 minutes.

Compacting the Ubuntu image with confirmation:

PS> wslcompact -c Ubuntu
 WSL compact, v5.0 2023.02.02 (Groundhog edition)
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\Oki\WSL\Ubuntu\ext4.vhdx
 Current size:   12864 MB
 Estimated size: 7700 ± 188 MB
 The estimated process time using an SSD is about 2 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
The operation completed successfully.
 New Image compacted from 12864 MB to 7728 MB
 Do you want to apply changes and use the new image (y/N): y
 Image replaced for distro: Ubuntu

wslcompact's People

Contributors

felipecrs avatar okibcn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wslcompact's Issues

installation broken: "Internet Explorer engine is not available"

PS C:\> iwr -useb https://raw.githubusercontent.com/okibcn/wslcompact/main/setup | iex
Downloading WslCompact...iwr : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing
parameter and try again.
At line:45 char:10
+ $ZIPURL=(iwr https://api.github.com/repos/okibcn/wslcompact/releases/ ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
    + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:48 char:5
+ iwr $ZIPURL -OutFile "$WCTEMP/wclatest.zip"
+     ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-WebRequest], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Done.
Expand-Archive : The path 'C:\Users\User\AppData\Local\Temp/wslcompacttemp/wclatest.zip' either does not exist or is not a valid file system path.
At line:50 char:1
+ Expand-Archive -LiteralPath "$WCTEMP/wclatest.zip" -DestinationPath " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Users\User...mp/wclatest.zip:String) [Expand-Archive], InvalidOperationException
    + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive

Copy-Item : Cannot find path 'C:\Users\User\AppData\Local\Temp\wslcompacttemp\WslCompact' because it does not exist.
At line:51 char:1
+ Copy-Item "$WCTEMP/WslCompact" "$TARGET" -Force -Recurse | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\User...temp\WslCompact:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

WslCompact Module installed. Type wslcompact -h to know more about it.
PS C:\> wslcompact -h
wslcompact : The term 'wslcompact' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:1 char:1
+ wslcompact -h
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wslcompact:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  2673

File being used by another process error

I'm getting the following error trying to compact my WSL:

C:\Users\darnold> wsl --shutdown
C:\Users\darnold> wslcompact -c Ubuntu
 WslCompact v8.7 2023.03.01
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\darnold\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
 Current size:   42943 MB
 Estimated size: 2609 +/- 64 MB
 The estimated process time using an SSD is about 1 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Move-Item : The process cannot access the file because it is being used by another process.
At C:\Users\darnold\Documents\WindowsPowerShell\Modules\WslCompact\WslCompact.psm1:99 char:25
+ ...             Move-Item "$tmp_folder/ext4.vhdx" "$tmp_folder/$wsl_distr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\darnol...mpact\ext4.vhdx:FileInfo) [Move-Item], IOException
    + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

Get-Item : Cannot find path 'C:\Users\darnold\AppData\Local\Temp\wslcompact\Ubuntu.vhdx' because it does not exist.
At C:\Users\darnold\Documents\WindowsPowerShell\Modules\WslCompact\WslCompact.psm1:101 char:35
+ ...       $size2 = (Get-Item -Path "$tmp_folder/$wsl_distro.vhdx").Length ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\darnol...act\Ubuntu.vhdx:String) [Get-Item], ItemNotFoundExcep
   tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

 New Image compacted from 42943 MB to 0 MB
 Do you want to apply changes and use the new image (y/N): n

C:\Users\darnold>

I tried a reboot, in case something might have been using WSL, but it made no difference.

Any suggestions appreciated!

Cannot index into a null array.

Using WslCompact v8.0 2023.02.13

PS C:\Users\myName> wslcompact
 WslCompact v8.0 2023.02.13
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu-18.04
 Image file:     C:\Users\myName\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
 Current size:   15193 MB
Cannot index into a null array.
At C:\Users\myName\scoop\apps\wslcompact\current\wslcompact.ps1:79 char:17
+ ...             $estimated = ((wsl --system -d "$wsl_distro" -e df /mnt/w ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

 Estimated size: 0 +/- 0 MB
 The estimated process time using an SSD is about 0 minutes.

Weird error message about free space

When attempting to compress an image without enough free space, I get this error message:

The estimated process time using an SSD is about 5 minutes.

WARNING: there isn't enough free space in temp drive C to process Ubuntu-22.04.
There are only 5141 MB available.

Please, change the TEMP folder to a drive with at least 0 MB of free space.

Notice the 0 MB of free space.

Failed to compact WSL Ubuntu for errors

Running the -c command on a basic Ubuntu image gives the following error:

U:\>wslcompact -c Ubuntu
 WslCompact v8.7 2023.03.01
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\QXZ1YFT\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx
 Current size:   169137 MB
<3>WSL (25) ERROR: CreateProcessParseCommon:708: Failed to translate U:\
 Estimated size: 3791 +/- 92 MB
 The estimated process time using an SSD is about 1 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
The operation completed successfully.
 WARNING: wslcompact found errors in the current image. It could be a storage problem,
          a corrupted ext4 filesystem, or any other issue. Image not processed.

And then many errors about not bein able to remove files.
Result is an additional WSL called wslcompact and no shrinkage of the original.

Any idea?

How to clear the swap.vhdx at first?

Hey,

I am using WSL2 Ubuntu and I know that there is an issue with wsl in general capturing memory and not releasing it back. Thats why I wanted to use wslcompact but before that it says that I should clear my temp folder at first. The swap.vhdx file takes almost 50gb and I can not clear it. Does someone know how I can do this?

Thanks for your help

WSL distro corrupted

Hi,
I've successfully shrinked my WSL image but when I enter Ubuntu it seems corrupted: all files in /etc/ are binaries and therefore the OS can't use them. How can I fix this or do I have to reinstall all?
Thanks

Failed to attach disk 'C:\Users\bijay\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx' to WSL2: The APPX file can not be accessed because it is not encrypted as expected. Error code: Wsl/Service/CreateInstance/MountVhd/0x80070199

Tried the PowerShell method everything worked fine, The virtual disk shrunk from 48GB to 28GB, but when tried to launch wsl it didn't work and the following error showed up

Failed to attach disk 'C:\Users\bijay\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx' to WSL2: The APPX file can not be accessed because it is not encrypted as expected.
Error code: Wsl/Service/CreateInstance/MountVhd/0x80070199

Is there a way to fix this or all my data is gone??

Compact failed

Problem with the final copy commands:

Move-Item: An object at the specified path C:\Users\RD2A7~1.FIN does not exist.
Get-Item: Cannot find path 'C:\Users\RD2A7~1.FIN\AppData\Local\Temp\wslcompact\Ubuntu.vhdx' because it does not exist.

Full output:

wslcompact -c 
 WslCompact v8.7 2023.03.01
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\Users\Robula\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
 Current size:   69524 MB
 Estimated size: 43648 +/- 1065 MB
 The estimated process time using an SSD is about 11 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
The operation completed successfully.
Move-Item: An object at the specified path C:\Users\RD2A7~1.FIN does not exist.
Get-Item: Cannot find path 'C:\Users\RD2A7~1.FIN\AppData\Local\Temp\wslcompact\Ubuntu.vhdx' because it does not exist.
 New Image compacted from 69524 MB to 0 MB
 Do you want to apply changes and use the new image (y/N): n

Here is my $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.23430
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Applet not found after installing it successfuly

I ran this to install wslcompact from a PowerShell prompt:

PS C:\Users\warpd> iwr -useb https://raw.githubusercontent.com/okibcn/wslcompact/main/setup | iex
Downloading WslCompact...Done.
WslCompact Module installed. Type wslcompact -h to know more about it.

But when attempting to run it, I get this error:

wslcompact : La commande «wslcompact» a été trouvée dans le module «WslCompact», mais ce dernier n’a pas pu être chargé. Pour
plus d’informations, exécutez la commande «Import-Module WslCompact».
Au caractère Ligne:1 : 1

  • wslcompact
  •   + CategoryInfo          : ObjectNotFound: (wslcompact:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
    

Cannot create a file when that file already exists

The operation completed successfully.
New Image compacted from 14140 MB to 10394 MB
Do you want to apply changes and use the new image (y/N): y
Move-Item: C:\Users\veron\scoop\apps\wslcompact\current\wslcompact.ps1:104
Line |
104 | … Move-Item "$tmp_folder/$wsl_distro.vhdx" "$wsl_path/ext4. …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot create a file when that file already exists.
Image replaced for distro: Ubuntu

Compacting NixOS failed with unspecified error

To get the script working with NixOS I needed to change the path to df to /run/current-system/sw/bin/df.

Then after multiple errors the script failed with a very generic error message and immediately deleted the temporary vhdx so I couldn't inspect that.

PS C:\Users\user> WslCompact -c NixOS
 WslCompact v8.5 2023.02.16
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  NixOS
 Image file:     C:\Users\user\NixOS\ext4.vhdx
 Current size:   152144 MB
 Estimated size: 137549 +/- 3355 MB
 The estimated process time using an SSD is about 34 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
Unspecified error
Error code: Wsl/Service/E_FAIL
 WARNING: wslcompact found errors in the current image. It could be a storage problem,
          a corrupted ext4 filesystem, or any other issue. Image not processed.

wslcompact fails destructively and destroys distro

> $env:TEMP="H:/wslcompact"
> wslcompact -c -d
 WslCompact v7.0 2023.02.08
 (C) 2023 Oscar Lopez
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Distro's name:  Ubuntu
 Image file:     C:\WSL\ext4.vhdx
 Current size:   195125 MB
 Estimated size: 89103 +/- 2173 MB
 The estimated process time using an SSD is about 22 minutes.
 NOTE: You can safely cancel at any time by pressing Ctrl-C
 Import in progress, this may take a few minutes.
The operation completed successfully.
 New Image compacted from 195125 MB to 88055 MB
 Do you want to apply changes and use the new image (y/N): y
Move-Item : The file exists.
At C:\Users\xxx\scoop\apps\wslcompact\current\wslcompact.ps1:104 char:29
+ ...             Move-Item "$tmp_folder/$wsl_distro.vhdx" "$wsl_path/ext4. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\WSL\ext4.vhdx:FileInfo) [Move-Item], IOException
    + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

 Image replaced for distro: Ubuntu

After the failure, wslcompact continues running for other images as if nothing happened. The Ubuntu distro no longer appears in wsl -l and can't be used. The only file remaining in the temp directory is a 12MiB vhdx.

wsl corrupted after compacted

compacting was successful but wsl became corrupted, cant start it anymore
tried to mount manually but every attempt failed. be careful before running this thing.

Unspecified error

It has been working for a loooooot of time. And finished this way:

PS C:\Users\tr> wslcompact
 WSL compact, v2.2023.01.29
 (C) 2023 Oscar Lopez.
 wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

 Processing: Ubuntu-20.04 distro
 Image file: C:\Users\tr\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
 Image size: 61078 MB
 Estimating target size...54015 В± 1317 MB
 Import in progress, this may take a few minutes.
Unspecified error
Error code: Wsl/Service/E_FAIL
Move-Item : Cannot find path 'C:\Users\tr\AppData\Local\Temp\wslcompact\ext4.vhdx' because it does not exist.
At C:\Users\tr\scoop\apps\wslcompact\current\wslcompact.ps1:70 char:17
+                 Move-Item "$tmp_folder/ext4.vhdx" "$wsl_path" -Force
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\tr\App...mpact\ext4.vhdx:String) [Move-Item], ItemNotFoundExce
   ption
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

 Compacted from 61078 MB to 61078 MB

Processing fstab with mount -a failed.

<3>WSL (8) ERROR: CreateProcessEntryCommon:368: getpwuid(0) failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:372: getpwuid(0) failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:574: execvpe df failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:583: Create process not expected to return
Cannot index into a null array.
At C:\Users\tr\scoop\apps\wslcompact\current\wslcompact.ps1:55 char:9
+         $estimated = ((wsl -d "$wsl_distro" -e df /) | select-string  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

 Processing: docker-desktop-data distro
 Image file: C:\Users\tr\AppData\Local\Docker\wsl\data\ext4.vhdx
 Image size: 20451 MB
 Estimating target size...55310906 В± 1349047 MB
 WARNING: there isn't enough free space in temp drive C to process docker-desktop-data.
          There are only 430288 MB available.

 Please, change the TEMP folder to a drive with at least 56659953 MB of free space.
 You cand do it by typing $env:TEMP="Z:/your/new/temp/folder" before using wslcompact.

 Processing: docker-desktop distro
 Image file: C:\Users\tr\AppData\Local\Docker\wsl\distro\ext4.vhdx
 Image size: 125 MB
 Estimating target size...61 В± 1 MB
 Import in progress, this may take a few minutes.
The operation completed successfully.
 Compacted from 125 MB to 118 MB

image

WSL version: 1.1.0.0
Kernel version: 5.15.83.1
WSLg version: 1.0.48
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22623.1245

Cannot index into a null array.

Has anyone encountered this error:

PS C:\Users\rodwjam> wslcompact
WslCompact v7.0 2023.02.08
(C) 2023 Oscar Lopez
wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact

Distro's name: Ubuntu
Image file: C:\Users\canso\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
Current size: 28691 MB
Cannot index into a null array.
At C:\Users\canso\scoop\apps\wslcompact\current\wslcompact.ps1:79 char:17

  • ... $estimated = ((wsl --system -d "$wsl_distro" -e df /mnt/w ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : NullArray

Estimated size: 0 +/- 0 MB
The estimated process time using an SSD is about 0 minutes.

" A parameter cannot be found that matches parameter name 'DestinationPath'." when following install instructions

Per the README:

 $ iwr -useb https://raw.githubusercontent.com/okibcn/wslcompact/main/setup | iex
Downloading WslCompact...Done.
WslCompact Module installed. Type wslcompact -h to know more about it.
Expand-Archive:
Line |
  50 |  … -Archive -LiteralPath "$WCTEMP/wclatest.zip" -DestinationPath "$WCTEM …
     |                                                 ~~~~~~~~~~~~~~~~
     | A parameter cannot be found that matches parameter name 'DestinationPath'.
Copy-Item:
Line |
  51 |  Copy-Item "$WCTEMP/WslCompact" "$TARGET" -Force -Recurse | Out-Null
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path 'C:\Users\mikem\AppData\Local\Temp\wslcompacttemp\WslCompact' because it does not exist.
     

wslcompact doesn't release space, despite available space

While WSL has a lot of available free space, wslcompact doesn't identify it for removal (see below). Changing the temp file as suggested didn't help either. For what's it worth I tried to also compact it with diskpart, but while it said it compacted the disk nothing happened. So perhaps the issue is related to diskpart rather than wslcompact.
image

Installation problem with Powershell 7.3.2

I have just tried to install the utility as a powershell module using Powershell 7.3.2. I correctly set the execution policy as specified in the installation guide. Nevertheless I get the following prompt

Downloading WslCompact...wget:
Line |
45 | $ZIPURL=(wget -q https://api.github.com/repos/okibcn/wslcompact/relea
| ~~~~
| The term 'wget' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
wget:
Line |
48 | wget -q $ZIPURL -O "$WCTEMP/wclatest.zip"
| ~~~~
| The term 'wget' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Done.
Expand-Archive:
Line |
50 | Expand-Archive -LiteralPath "$WCTEMP/wclatest.zip" -DestinationPath " …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The path 'C:\Users\gabriele\AppData\Local\Temp/wslcompacttemp/wclatest.zip' either does not exist or is not a valid file system path.
Copy-Item:
Line |
51 | Copy-Item "$WCTEMP/WslCompact" "$TARGET" -Force -Recurse | Out-Null
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find path 'C:\Users\gabriele\AppData\Local\Temp\wslcompacttemp\WslCompact' because it does not exist.
WslCompact Module installed. Type wslcompact -h to know more about it.

Apparently powershell in my configuration does not recognize wget as a powershell cmdlet and this prevent the module to be installed as it can be seen from the subsequent lines. I am not an expert of powershell, so I have been asking to myself: "Is this a problem with my configuration?". Have you ever faced this issue? Do you know a possible workaround?

Needs 0MB of free space

Used scoop to install instead, running wslcompact gives:

Please, change the TEMP folder to a drive with at least 0 MB of free space.
You cand do it by typing $env:TEMP="Z:/your/new/temp/folder" before using wslcompact.

I hope you don't mind the bug reports. I might do this manually as disk tools with errors are a bit scary. Nothing personal.

Memory usage 8GB+

I think this is related to the approach to pipe the export to import:

wsl --shutdown
cmd /c "wsl --export ""$wsl_distro"" - | wsl --import wslcompact ""$tmp_folder"" -"
wsl --shutdown

I've seen this use over 8GB (roughly size of image) in memory, which if there was not enough free would fail even if there is adequate disk space. Running these two commands sequentially without the pipe avoids the high memory usage.

It might be good to offer an option to opt-in for the alternative without using a pipe?

I could be mistaken, I've not tried to use wslcompact on a much larger VM disk to see if the memory required is expected to scale. Doesn't seem like it's affected by the original size, but the compacted export size might be the memory requirement which is being piped?


I recently also ran wslcompact and it was managing at 98% memory usage (32GB system), although I think this was not failing as some other processes that are memory gluttons like Edge will release memory when the system is under pressure.

I did realize I was not going to have enough disk space and pressed Ctrl +C during the operation, VmmemWSL process was using roughly 10GB of memory (nothing else open due to wsl --shutdown). However I wasn't able to release it via explicit wsl --shutdown command (hangs), and the process was not permitted to terminate via the Task Manager.

I vaguely recall something similar with the memory pipe failure I experienced last month. I didn't abort that operation, but the failure didn't release the memory either IIRC.

EDIT: I roughly documented it in the middle of this comment, closing the powershell terminal app didn't make a difference this time, although the memory usage of VmmemWSL process has been slowly reducing over the past hour from 10GB to 2GB presently (now 1.4GB).

Estimated size larger than current size

Hey,

I wanted to compress my wsl distro, but it shows me that the estimated size is going to be way larger than the current size:

image

Does anyone has an idea what could be the reason?

Thanks for your help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.