microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.34k stars 814 forks source link

df command is dangerous for scripting - it gives incorrect results if you want to check for free space. #11956

Open Jim-JMCD opened 1 month ago

Jim-JMCD commented 1 month ago

Discussed in https://github.com/microsoft/WSL/discussions/11953

Originally posted by **Jim-JMCD** August 24, 2024 In scripts, if you going to check for free space, before writing, you can't use the standard **df $PWD** or **df $write_target** as a check. According to the sequence below in WSL2, a script checking for free space will fail. The script write until its filled up all the free space on the laptop/PC C:\ drive. This laptop only had WSL in its C:\ location. If its a company server it could be a potential big problem for sysadmins. ``` $ df -h $PWD Filesystem Size Used Avail Use% Mounted on /dev/sdc 1007G 5.3G 951G 1% / $ df -h /mnt/c Filesystem Size Used Avail Use% Mounted on C:\ 931G 815G 116G 88% /mnt/c $uname -a Linux fred 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux ``` - The first **df** tells me I have 951 GB of free space to play with in my WSL2 home directory. - The second **df** tells me I have 116 GB of free space where WSL2 and everything else lives on the C:\ drive - The reality is WSL has only 116 GB cause its VHD will dynamically grow until used up everything on the C:\ drive This was reported earlier but the folks didn't realize the consequences https://github.com/microsoft/WSL/discussions/9757 Having WSL on a non-OS drive [D:\] is interesting the drive size (1007G) is the same as previous but doesn't resemble any drive on both installations. ``` $ df -h $PWD Filesystem Size Used Avail Use% Mounted on /dev/sdc 1007G 2.5G 954G 1% / $ df -h /mnt/d Filesystem Size Used Avail Use% Mounted on D:\ 1.9T 1.6T 280G 85% /mnt/d $ df -h /mnt/c Filesystem Size Used Avail Use% Mounted on C:\ 112G 54G 59G 48% /mnt/c $ echo $PWD /home/jjm bob@HP01:~$ df -h | grep /mnt/ | grep -v none C:\ 112G 54G 59G 48% /mnt/c D:\ 1.9T 1.6T 280G 85% /mnt/d G:\ 1.9T 1.7T 178G 91% /mnt/g S:\ 932G 700G 232G 76% /mnt/s T:\ 932G 900G 33G 97% /mnt/t $ uname -a Linux HP01 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux ```
github-actions[bot] commented 1 month ago

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs Download and execute [collect-wsl-logs.ps1](https://github.com/Microsoft/WSL/blob/master/diagnostics/collect-wsl-logs.ps1) in an **administrative powershell prompt**: ``` Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1 Set-ExecutionPolicy Bypass -Scope Process -Force .\collect-wsl-logs.ps1 ``` The script will output the path of the log file once done. If this is a networking issue, please use [collect-networking-logs.ps1](https://github.com/Microsoft/WSL/blob/master/diagnostics/collect-networking-logs.ps1), following the instructions [here](https://github.com/microsoft/WSL/blob/master/CONTRIBUTING.md#collect-wsl-logs-for-networking-issues) Once completed please upload the output files to this Github issue. [Click here for more info on logging](https://github.com/microsoft/WSL/blob/master/CONTRIBUTING.md#8-collect-wsl-logs-recommended-method) If you choose to email these logs instead of attaching to the bug, please send them to wsl-gh-logs@microsoft.com with the number of the github issue in the subject, and in the message a link to your comment in the github issue and reply with '/emailed-logs'.
Jim-JMCD commented 1 month ago

WslLogs-2024-08-25_06-44-02.zip

github-actions[bot] commented 1 month ago

The log file doesn't contain any WSL traces. Please make sure that you reproduced the issue while the log collection was running.

Diagnostic information ``` Detected appx version: 2.2.4.0 Found no WSL traces in the logs ```
Jim-JMCD commented 1 month ago

WslLogs-2024-08-28_09-44-59.zip

Alternatively you can check it for yourself by running the commands in the order of the original post. Its simple stuff.

github-actions[bot] commented 1 month ago
Diagnostic information ``` Detected appx version: 2.2.4.0 ```