zoukankan      html  css  js  c++  java
  • ext4.vhdx 如何减少其占用空间 Shrink your WSL2 Virtual Disks and Docker Images and Reclaim Disk Space(转发)

    原文:Shrink your WSL2 Virtual Disks and Docker Images and Reclaim Disk Space

    Docker Desktop for Windows uses WSL to manage all your images and container files and keeps them in a private virtual hard drive (VHDX) called ext4.vhdx.

    It's usually in C:\Users\YOURNAME\AppData\Local\Docker\wsl\data and you can often reclaim some of the space if you've cleaned up (pruned your images, etc) with Optimize-Vhd under an administrator PowerShell shell/prompt.

    You'll need to stop Docker Desktop by right clicking on its tray icon and choosing Quit Docker Desktop. Once it's stopped, you'll want to stop all running WSL2 instances with wsl --shutdown

    Mine was 47gigs as I use Docker A LOT so when I optimize it from admin PowerShell from the wsl\data folder

    optimize-vhd -Path .\ext4.vhdx -Mode full

    ...it is now 2 gigs smaller. That's nice, but it's not a massive improvement. I can run docker images and see that many are out of date or old. If I'm not using Kubernetes I can turn it off and delete those containers as well from the Docker settings UI.

    I'll run docker system prune -a to AGRESSIVELY tidy up. Read about these commands before your try yourself. -a means all unused images, not just dangling ones. Don't delete anything you love or care about. If you're worried, docker system is safer without the -a.

    Now my Docker WSL 2 VHD is 15 gigs smaller! Learn more about WSL, Windows 11, and WSLg on my latest YouTube!

    NOTE: You can now get WSL from the Windows Store! Go get it here and then run "wsl --install" at your command line.

    If you want, you can also go find your Ubuntu and other WSL disks and Compact them as well. I only think about this once or twice a year, so don't consider this a major cleanup thing unless you're really tight on space.

    Ubuntu WSL disks will be in folders with names like

    C:\Users\scott\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState

    or

    C:\Users\scott\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState

    But you will want to look around for yours. Again, back things up and make sure WSL is shutdown first!

    Enjoy! REMEMBER - Be sure to back things up before you run commands as admin from some random person's blog. Have a plan.

    PS C:\Users> Optimize-VHD -Path D:\Users\****\AppData\Local\Docker\wsl\data\ext4.vhdx -Mode Full
    Optimize-VHD : 你没有完成此任务所需的权限。请与计算机“WW-BJ-PANXI”授权策略的管理员联系。
    所在位置 行:1 字符: 1
    + Optimize-VHD -Path D:\Users\panxi\AppData\Local\Docker\wsl\data\ext4. ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Optimize-VHD], VirtualizationException
        + FullyQualifiedErrorId : Unspecified,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd
    

      

    以管理员身份运行 power shell

    PS C:\WINDOWS\system32> Optimize-VHD -Path D:\Users\*****\AppData\Local\Docker\wsl\data\ext4.vhdx -Mode Full
    Optimize-VHD : 无法压缩虚拟磁盘。
    系统无法压缩“D:\Users\*****\AppData\Local\Docker\wsl\data\ext4.vhdx”。
    无法压缩虚拟磁盘。
    系统无法压缩“D:\Users\*****\AppData\Local\Docker\wsl\data\ext4.vhdx”: 另一个程序正在使用此文件,进程无法访问。 (0x800
    70020)。
    所在位置 行:1 字符: 1
    + Optimize-VHD -Path D:\Users\*****\AppData\Local\Docker\wsl\data\ext4. ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceBusy: (:) [Optimize-VHD], VirtualizationException
        + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd
    

      

    停止 docker

    PS C:\WINDOWS\system32> Optimize-VHD -Path D:\Users\panxi\AppData\Local\Docker\wsl\data\ext4.vhdx -Mode Full
    PS C:\WINDOWS\system32>
    

      

    压缩磁盘完成

    PS C:\WINDOWS\system32> docker system prune -a
    WARNING! This will remove:
      - all stopped containers
      - all networks not used by at least one container
      - all images without at least one container associated to them
      - all build cache
    
    Are you sure you want to continue? [y/N] y
    error during connect: In the default daemon configuration on Windows, the docker client must be run with elevated privileges to connect.: Post "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/prune": open //./pipe/docker_engine: The system cannot find the file specified.
    

      

    重启docker 

    PS C:\WINDOWS\system32> docker system prune -a
    WARNING! This will remove:
      - all stopped containers
      - all networks not used by at least one container
      - all images without at least one container associated to them
      - all build cache
    
    Are you sure you want to continue? [y/N] y
    Total reclaimed space: 0B
    PS C:\WINDOWS\system32>
    

      

    WSL 2 should automatically release disk space back to the host OS #4699

    Is your feature request related to a problem? Please describe.
    I occasionally have workloads that consume large amounts of disk space for temporary use in /tmp. For example, transcoding large video files or processing large datasets exported from a database. Since WSL2 stores its filesystem on a .vhdx, that file grows when the dataset is processed and never releases that space. Also, it doesn't appear to re-use released disk space in the .vhdx when files are deleted... it seems to prefer to grow the file rather than re-use existing empty space. (I haven't explicitly tested that theory, but my workload deletes temporary files as they are used and I never had enough of them existing simultaneously to reach 250GB, but the size of the .vhdx eventually expanded to that size.

    When I'm done with that workload, I have to:

    wsl --shutdown
    optimize-vhd -Path .\ext4.vhdx -Mode full
    

    This is annoying since it basically means that my system backups include a huge .vhdx file that is mostly empty. Also, until about 2 weeks ago, it was consuming the entire remainder of my system disk. I had only 50GB free, which was enough to handle the workload, but since it continued to grow even after files were removed, that didn't matter. (I figured it was time to upgrade the disk size anyway)

    Describe the solution you'd like
    Automatic compaction of the .vhdx, or a way to do so while WSL2 is still running so that I can schedule it for frequent cleanups.

    Describe alternatives you've considered

      • Going back to WSL1 since it uses the host filesystem and is faster accessing that than WSL2. However, this alternative makes docker less useful. Docker on WSL2 is actually almost perfect; not so much on WSL1.
      • Exporting/Importing WSL 2 to move it to another disk. Not even sure if this is possible, but if it is,
        it would probably be fine for my desktop, but not really an option for my laptop. Not really worth the time either since that entire process would probably take about 30 minutes, but compacting is usually under 3.

    +1 for being able to move the .vhdx to a different partition, either via config file or the "Move" feature in the Control Panel of Windows (currently getting a message that it's disabled)

    For Windows 10 Home (alternative Optimize-VHD cmdlet):

    wsl --shutdown
    diskpart
    # open window Diskpart
    select vdisk file="C:\WSL-Distros\…\ext4.vhdx"
    attach vdisk readonly
    compact vdisk
    detach vdisk
    exit

    Thanks to @davidwin for the tip #4699 (comment).

    @nickjj You need to be in the directory where the distro's vhdx resides. In my case with Ubuntu20.04 I had to

    wsl.exe --shutdown
    cd C:\Users\onoma\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\
    optimize-vhd -Path .\ext4.vhdx -Mode full

    My contribution, bringing together knowledge from above. It still requires a little manual work to get the DistroFolder variable set for the distro you want to target (you could have more than one, find them with wsl --list)

    Cheers!

    ### Optimize (shrink) WSL 2 .vhdx
    ## Must be run in PowerShell as Administrator user
    # DistroFolder found at: $env:LOCALAPPDATA\Packages\
    # Examples:
    #   CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
    #   CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc
    
    cd $env:LOCALAPPDATA\Packages\REPLACE_ME_WITH_TARGET_DISTRO_FOLDERNAME\LocalState\
    wsl --shutdown
    optimize-vhd -Path .\ext4.vhdx -Mode full
    #Run `wsl` or your favorite terminal to resume use

    BELOW UNTESTED!!
    More info at https://docs.microsoft.com/en-us/powershell/module/hyper-v/resize-vhd?view=win10-ps

    ### Resize WSL 2 .vhdx
    ## WARNING!! UNTESTED!! Read and make note of the MaxSizeGB variable and functionality before running
    ## Must be run in PowerShell as Administrator user
    # DistroFolder found at: $env:LOCALAPPDATA\Packages\
    # Examples:
    #   CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
    #   CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc
    
    Set-Variable -Name "MaxSizeGB" -Value "32GB"
    cd $env:LOCALAPPDATA\Packages\REPLACE_ME_WITH_TARGET_DISTRO_FOLDERNAME\LocalState\
    wsl --shutdown
    resize-vhd -Path .\ext4.vhdx -SizeBytes $MaxSize
    #Run `wsl` or your favorite terminal to resume use

    @eromoe optimize-vhd is only available in Windows 10 Pro with Hyper-v feature installed. Otherwise you will need to use the compact option in Diskpart.

    The second alternative mentioned is proving the fastest and most reliable for me, Exporting/Importing WSL 2 to move it to another disk. The other thing I have tried is converting between Fixed and Dynamic volumes in combination with this. My use is for docker's WSL2 integration, and the need to export and restore large data volumes (>250GB). This is similar to what most of the respondents are referring to, yet they do not always say whether they are using WSL1 vs. WSL2. WSL1 sets a fixed disk size as specified in your Docker settings. WSL2 uses windows default setting of 256GB max volume size. I could not bash into or ssh into the docker-data distro shown or access its ext4.vhdx volume through various methods. What would be really nice is if WSL would adopt a docker overlay2 driver or some other more friendly HyperV linux filesystem, faster than vhdx on ntfs, with an API to control allocation. Even better would be some type of cloud filesystem integration that could rival local dev speeds.

    Here are my notes in gist

     

    I just symlinked ~\AppData\Local\Docker\wsl\data to a folder on a different partition/drive, and moved the VHDX. While the problem with the VHDX growing is still there, at least it won't eat all the space on my primary drive. I wrote up a blog post about it, and here's the script I used (use at your own risk):

    $ErrorActionPreference = "Stop"
    
    $newLocation = "E:\VMs\WSL2\"
    
    cd "~\AppData\Local\Docker\wsl\data"
    wsl --shutdown
    Optimize-VHD .\ext4.vhdx -Mode Full
    mkdir $newLocation -Force
    mv ext4.vhdx $newLocation
    cd ..
    rm "data"
    New-Item -ItemType SymbolicLink -Path "data" -Target $newLocation





  • 相关阅读:
    JAVA8学习——Stream底层的实现三(学习过程)
    JAVA8学习——Stream底层的实现二(学习过程)
    JAVA8学习——Stream底层的实现一(学习过程)
    2020年的第一天-我的IDEA出现This license ... has been cancelled
    Java Applet与Java Application的区别
    Spring AOP 详解
    Spring中的代理(proxy)模式
    hibernate中查询方式(二):常用查询
    hibernate中查询方式(一):
    Spring(二)DI( Dependency Injection依赖注入)
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/15740057.html
Copyright © 2011-2022 走看看