22 KiB
title, source
| title | source |
|---|---|
| windows subsystem for linux - How to change the maximum size of the disk image of my WSL2 distro? - Super User | https://superuser.com/questions/1600749/how-to-change-the-maximum-size-of-the-disk-image-of-my-wsl2-distro |
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
-
- Public
- Questions
- Tags
- Users
- Companies
- Unanswered
-
Teams
Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Create a free Team Why Teams?
How to change the maximum size of the disk image of my WSL2 distro?
Asked 2 years, 5 months ago
Modified 5 months ago
Viewed 2k times
4
It seems that WSL stores the filesystem data in a vhdx file. Ubuntu sees this as a 250GB disk, which is a bit much for my purposes. I want to resize this to a 30GB disk or so, to save disk space on the host machine.
I've seen several links that compact the vhdx file and reclaim disk space that way. That's not what I'm looking for. I need a solution that ideally doesn't require manual steps, but at the very least doesn't require me to shutdown WSL
I've seen several links about resizing vhdx files in the context of Hyper-V. Hyper-V tooling doesn't seem to like my WSL image much. Possibly because it doesn't seem to have a partition table?
Background: I'm going to deploy this thing on a buildserver, meaning it has to be as hands-off as possible, and as much uptime as reasonably possible. And I can budget disk usage pretty much in advance. In fact, I need to, otherwise there will be no room for the Windows builds that'll also be running there
15.4k
11 gold badge
3737 silver badges
7070 bronze badges
asked Nov 9, 2020 at 12:43
271
11 gold badge
22 silver badges
88 bronze badges
-
1
I'm pretty sure it's a sparse disk image, ie. unused disk space in a guest doesn't use any disk space on the host.
-
Like I said, I'll deploy to a buildserver, so there'll be a lot of writes followed by deletes. Deleted space is not returned to the host OS (as evidenced by the popularity of links proposing to compact the image)
– Kees-Jan
-
I found these instructions for increasing the disk size, but since this question is about reducing size, I'll not consider it an answer
– Kees-Jan
-
@Kees-Jan Also, reducing the size that way would require a shutdown, which you want to avoid. But interesting that you are still searching for a solution two years later, it sounds like?
1 Answer
Sorted by:
0
I know this is a few years old, so you may not have this particular need any longer. However, in the latest WSL releases, the default maximum size of the dynamic VHDX has been increased to 1TB, so the problem may be even worse.
I need a solution that ideally doesn't require manual steps, but at the very least doesn't require me to shutdown WSL
While this solution has a lot of manual steps and a shutdown, it's one-time configuration work to set things up. For there, you can simply use one wsl --import-in-place command to start up new WSL instances using the smaller drive size on the build server.
Also, don't be too scared by the length of the steps/post below - I'm just very (perhaps overly) detailed.
This particular solution requires Windows 11 or Windows 10 running UBR 2311 or later (see this answer for details on how to update currently).
To summarize what I propose:
- Create a new VHD with your desired maximum size
- Mount it in WSL and create the filesystem.
- Copy over all files from an existing WSL distro to the new VHD.
wsl --importthe VHD as a new distribution.
Full details:
If you are on a Windows 10 xxxxx.2311 or Windows 11 22H2 release:
- Run
wsl --update, which should update your WSL to use the new application package (Store) version of WSL. If you do not have access to the Store from your build server, see the later half of this answer for manual installation steps. - You'll need two distributions installed. The one you want to copy and one for doing the copying (a "temp" distro if needed). If you are working with Ubuntu, for instance, then you can just create a new Ubuntu-22.04 instance from the Store.
- In an admin PowerShell, create a new dynamic VHD using:
new-vhd -Dynamic -SizeBytes 30gb -BlockSizeBytes 1mb -path ./ext4.vhdx
The -BlockSizeBytes matches the existing setting for WSL2 ext4.vhdx files.
- Exit the Admin PowerShell and in a new, regular-user PowerShell, run:
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\ |
ForEach-Object {
(Get-ItemProperty $_.PSPATH) | Select-Object DistributionName,BasePath
}
```
Make note of the path of the original distribution that you want to copy. We'll call this `<srcdrive>`.
- `wsl --shutdown` to make sure no files in the original distribution are in use. Make sure there's no process that might automatically restart that distribution (e.g. Docker Desktop).
- Start the "temp" distribution that you will be using to do the copying with `wsl ~ -d <distroname>`
- `lsblk` and take note of the existing devices listed. Make sure to identify any devices that are the same size as the one you'll be formatting. You don't want to overwrite one that is already in use!
- Exit WSL and return to PowerShell.
```powershell
wsl --mount --vhd --bare \path\to\the\new\ext4.vhdx
wsl --mount --vhd --name srcdrive path\to\<srcdrive.vhdx>
Using the path you determined above for <srcdrive>.
- Start your temp WSL instance again.
lsblk
- Confirm the device name that you want to format. It should be the only 30G device there, hopefully. Regardless, it should be the only 30G device in the list that wasn't there when we checked before the
--mount. We'll call thisnewdev:
sudo mkfs.ext4 /dev/<newdev>
sudo mount -t ext4 /dev/<newdev> /mnt/wsl/newdrive -o X-mount.mkdir
- You should have two mounts:
/mnt/wsl/srcdrive
/mnt/wsl/newdrive
srcdrive should look like the filesystem from your existing distribution that you want to copy. newdrive should currently be empty other than the default lost+found.
- Copy over the filesystem:
sudo cp -axT /mnt/wsl/srcdrive/ /mnt/wsl/newdrive/
- Exit the WSL temp distribution
- Back in PowerShell:
wsl --shutdown
wsl --import-in-place <new_distro_name> \path\to\the\new\ext4.vhdx
- Finally, start the new distro with
wsl ~ -d <new_distro_name>. It will start as the root user. To set your default user, create a/etc/wsl.conffile as noted in this answer.
You can now use the new VHD to seed your build system with multiple (if needed) smaller WSL distributions with just an additional wsl --import-in-place.
answered Nov 22, 2022 at 23:49
15.4k
11 gold badge
3737 silver badges
7070 bronze badges
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged
-
The Overflow Blog
-
Don’t panic! A playbook for managing any production incident
-
Featured on Meta
-
New blog post from our CEO Prashanth: Community is the future of AI
-
We are updating our Code of Conduct and we would like your feedback
Linked
53How to set default user for manually installed WSL distro?
66How do I get back unused disk space from Ubuntu on WSL2?
7Updating WSL 2 without Microsoft Store
1Running WSL that was installed from the Microsoft Store results in "Windows version 10.0.19045.2251 does not support the packaged version of ..."
Related
33How to add second WSL2 Ubuntu distro (fresh install)
2WSL2 network unreachable for any distro
1How to install WSL2 without distro?
4Remove WSL2 files after distro uninstall on Windows 11
Hot Network Questions
- What kind of letters is "many an unexpected letter that WOL had written to himself"?
- Could some sort of genetic engineering allow for "renewable" immortality?
- On a mobile app, can I use IP location services to determine if a user is from EU prior to GDPR consent?
- How to spread a taboo white paper at a large scale in a Renaissance-level, medium magic world?
- How to get the real reasons for an incompatible app/.apk?
- How did the adverbial use of "smack" come into use?
- Is it possible to install a device driver for windows on mac OS?
- How can I convert South and West coordinates to align to X/East and Y/North in QGIS?
- Ubuntu won't accept my choice of password
- Frequency analyzer for loop gain measurement - Why should the injection point have low output impedance and high input impedance?
- Uses for leftover or used handlebar tape?
- Is there an option or hint possible to improve performance of query with multiple values in the "in" clause
- Norms around who contributes to the diversity section in grant proposals?
- Which is brighter, Mars as seen from Earth, or Earth as seen from Mars?
- What should I follow, if two altimeters show different altitudes?
- TikZ: How to fill a part of shape with a color
- How long will it take for a "mutation" to pass to all the human population?
- How can I compare the UV/Vis absorption difference after metal ion chelating of crown ether-containing polymer?
- Why are players required to record the moves in World Championship Classical games?
- Short story about an Earth ambassador who must solve a murder on a planet where all people wear masks and sing
- Difference between \uncover vs \visible vs \only?
- What is the canonical name of the interaction symbol over NPCs heads?
- Is customer entitled to the same car tested in the test drive or in the ad?
- What's the cheapest way to buy out a sibling's share of our parents house if I have no cash and want to pay less than the appraised value?
Super User
Company
- Stack Overflow
- Teams
- Advertising
- Collectives
- Talent
- About
- Press
- Legal
- Privacy Policy
- Terms of Service
- Cookie Settings
- Cookie Policy
Stack Exchange Network
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2023.5.3.43407
