okibcn/wslcompact

wslcompact fails destructively and destroys distro

Closed this issue · 4 comments

> $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.

The image was compacted from 195 GB to 88 GB. The problem during copy can be related to the reliability of the H: or C: drive. Any lock on the original image would have prevented the overwrite of the image, so the loss of the image is quite intriguing.

That is a simple PS Copy-Item instruction that should not cause any issues. I will include a safety check after copy just to avoid the removal of the temp folder in case of problems during the replacement of the image.

Were you using this or any other distro during the process?

No, the distro was not in use (and could not be in use, since it didn't exist). After running wslcompact and receiving the above error, it doesn't appear in wsl -l and can't be used for commands:

>wsl -d Ubuntu -- pwd
There is no distribution with the supplied name.
Error code: Wsl/Service/WSL_E_DISTRO_NOT_FOUND

The original vhdx still exists with a plausible filesize at C:\WSL\ext4.vhdx so I'm trying to import it into a new distro and recreate the one removed by wslcompact.

I still wouldn't expect an error during a file move to result in blowing away the distro.

If you still have the image, there are two ways to import it again. The fastest and simple one is the import-in-place. Just type:

wsl --import-in-place Ubuntu C:\WSL\ext4.vhdx

Ensure you have the latest WSL.

In version v8.0 I have added a safe exit in case of a failure during the image replacement, leaving the compacted image intact as a backup in the event of a failure while moving the compacted image to the new place.