Skip to content

Commit ab6296b

Browse files
authored
Removed dirty OS check. And support Legacy WSL2 distributions (#15)
* Removed dirty OS check. And support Legacy WSL2 distributions * finishing v0.5.0
1 parent 7cdc6ef commit ab6296b

13 files changed

Lines changed: 1199 additions & 72 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.5.0] - 2025-12-01
11+
1012
### Fixed
1113
- Distribution names now persist across Vagrant commands (fixes #8)
1214
- `vagrant ssh` and other commands now remember auto-generated distribution names
@@ -23,6 +25,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2325
### Added
2426
- Silent distribution startup for SSH commands to prevent output pollution
2527
- `EnsureRunning` action for transparent VM wake-up on SSH access
28+
- Export existing WSL2 distributions to cache for modern distributions (supporting `--name` flag)
29+
- Removed dirty OS check before vagrant up
30+
- Automatic handling for legacy distributions (Ubuntu 20.04/22.04, OracleLinux, etc.) via install -> terminate -> export flow
31+
- Future-proof blocklist approach: only 7 legacy distributions listed, new distributions automatically get modern treatment
32+
- User's original distributions are preserved (not unregistered) when exporting to cache
2633

2734
## [0.4.0] - 2025-11-24
2835

@@ -216,7 +223,8 @@ end
216223
- Some SUSE Enterprise distributions have guest detection issues
217224
- AlmaLinux-10 and archlinux have provisioning limitations
218225

219-
[unreleased]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.4.0...HEAD
226+
[unreleased]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.5.0...HEAD
227+
[0.5.0]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.4.0...v0.5.0
220228
[0.4.0]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.3.0...v0.4.0
221229
[0.3.0]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.2.0...v0.3.0
222230
[0.2.0]: https://github.com/LeeShan87/vagrant-wsl2-provider/compare/v0.1.0...v0.2.0

Rakefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,14 @@ end
9292
desc "Run all distributions compatibility test - full (Pester, all distros)"
9393
task :test_all_distributions_full => :ensure_pester do
9494
sh "powershell -File test/integration/Invoke-PesterTests.ps1 -TestFile AllDistributions -Full"
95-
end
95+
end
96+
97+
desc "Run export existing distribution integration test"
98+
task :test_export_existing => :ensure_pester do
99+
sh "powershell -File test/integration/Invoke-PesterTests.ps1 -TestFile ExportExisting"
100+
end
101+
102+
desc "Run legacy distribution integration test (slow - only with -Full)"
103+
task :test_legacy_distro_full => :ensure_pester do
104+
sh "powershell -File test/integration/Invoke-PesterTests.ps1 -TestFile LegacyDistro -Full"
105+
end

docs/custom-base-images.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# Custom Base Images
2+
3+
This guide explains how to create custom base images for the Vagrant WSL2 provider.
4+
5+
## Overview
6+
7+
The Vagrant WSL2 provider uses a caching system to speed up VM creation. When you first run `vagrant up`, the provider:
8+
9+
1. Downloads/installs a clean WSL2 distribution (e.g., Ubuntu-24.04)
10+
2. Exports it to a cache directory: `~/.vagrant.d/wsl2-cache/`
11+
3. Creates project-specific VMs from this cached image
12+
13+
This ensures every `vagrant up` starts with a clean, reproducible environment.
14+
15+
## Why Create Custom Base Images?
16+
17+
You might want to create a custom base image if you:
18+
19+
- Need pre-installed packages in all your VMs (e.g., Docker, specific tools)
20+
- Want to apply custom configurations before VM creation
21+
- Need a specific distribution version not available via `wsl --install`
22+
- Want to optimize VM startup time by pre-installing common dependencies
23+
24+
## Creating a Custom Base Image
25+
26+
### Method 1: Manual Creation (Recommended)
27+
28+
**Step 1: Create and customize a WSL distribution**
29+
30+
```powershell
31+
# Install a fresh distribution
32+
wsl --install Ubuntu-24.04 --no-launch
33+
34+
# Launch it and install your custom packages
35+
wsl -d Ubuntu-24.04
36+
37+
# Inside the distribution:
38+
sudo apt update
39+
sudo apt install -y build-essential git curl
40+
# ... install whatever you need ...
41+
42+
# Exit the distribution
43+
exit
44+
```
45+
46+
**Step 2: Export to cache directory**
47+
48+
```powershell
49+
# Create cache directory if it doesn't exist
50+
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.vagrant.d\wsl2-cache"
51+
52+
# Export your customized distribution
53+
wsl --export Ubuntu-24.04 "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar"
54+
```
55+
56+
**Step 3: Clean up the original (optional)**
57+
58+
```powershell
59+
# Remove the original distribution to save space
60+
wsl --unregister Ubuntu-24.04
61+
```
62+
63+
**Step 4: Use in Vagrantfile**
64+
65+
```ruby
66+
Vagrant.configure("2") do |config|
67+
config.vm.box = "Ubuntu-24.04"
68+
69+
config.vm.provider "wsl2" do |wsl|
70+
# Vagrant will use your custom cached image!
71+
end
72+
end
73+
```
74+
75+
### Method 2: Using an Existing Distribution
76+
77+
If you already have a WSL distribution that you've customized and want to use as a base:
78+
79+
```powershell
80+
# Export your existing distribution to the cache
81+
wsl --export MyCustomDistro "$env:USERPROFILE\.vagrant.d\wsl2-cache\MyCustomDistro-vagrant-base.tar"
82+
```
83+
84+
Then in your Vagrantfile:
85+
86+
```ruby
87+
Vagrant.configure("2") do |config|
88+
config.vm.box = "MyCustomDistro"
89+
90+
config.vm.provider "wsl2" do |wsl|
91+
# Will use your custom cached image
92+
end
93+
end
94+
```
95+
96+
## Important Considerations
97+
98+
### Cache Naming Convention
99+
100+
The cache file **must** follow this naming pattern:
101+
102+
```
103+
<distribution-name>-vagrant-base.tar
104+
```
105+
106+
For example:
107+
- `Ubuntu-24.04-vagrant-base.tar` for box name `Ubuntu-24.04`
108+
- `Debian-vagrant-base.tar` for box name `Debian`
109+
- `MyCustomDistro-vagrant-base.tar` for box name `MyCustomDistro`
110+
111+
### Keeping Images Clean
112+
113+
**Best Practice**: Don't include user-specific data or temporary files in your base image.
114+
115+
**What to avoid**:
116+
- User home directory modifications
117+
- Running services or daemons
118+
- Temporary files in `/tmp`
119+
- User-specific SSH keys or credentials
120+
- Project-specific code or data
121+
122+
**What's okay**:
123+
- System-wide package installations
124+
- System configuration files
125+
- Pre-compiled tools or binaries
126+
- System-wide environment variables
127+
128+
### Automatic Backup for Legacy Distributions
129+
130+
If you have a legacy distribution (e.g., Ubuntu-20.04) already installed locally, the provider will automatically:
131+
132+
1. Backup your existing distribution
133+
2. Install a fresh clean version
134+
3. Export it to cache
135+
4. Restore your original distribution
136+
137+
This ensures you don't lose your existing work while still getting a clean cache.
138+
139+
## Cache Location
140+
141+
All cached base images are stored in:
142+
143+
```
144+
Windows: C:\Users\<username>\.vagrant.d\wsl2-cache\
145+
```
146+
147+
You can manually manage these files:
148+
149+
```powershell
150+
# List cached images
151+
Get-ChildItem "$env:USERPROFILE\.vagrant.d\wsl2-cache"
152+
153+
# Remove a specific cache
154+
Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar"
155+
156+
# Remove all caches
157+
Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\*"
158+
```
159+
160+
## Updating Cache
161+
162+
To update a cached image with new packages or configurations:
163+
164+
1. Delete the existing cache file
165+
2. Run `vagrant up` - the provider will recreate it
166+
167+
OR manually:
168+
169+
```powershell
170+
# Delete old cache
171+
Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar"
172+
173+
# Create new custom image using Method 1 above
174+
# ...
175+
```
176+
177+
## Examples
178+
179+
### Example 1: Docker Pre-installed
180+
181+
```powershell
182+
# Install Ubuntu
183+
wsl --install Ubuntu-24.04 --no-launch
184+
wsl -d Ubuntu-24.04
185+
186+
# Inside Ubuntu, install Docker
187+
curl -fsSL https://get.docker.com -o get-docker.sh
188+
sudo sh get-docker.sh
189+
exit
190+
191+
# Export to cache
192+
wsl --export Ubuntu-24.04 "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar"
193+
194+
# Clean up
195+
wsl --unregister Ubuntu-24.04
196+
```
197+
198+
### Example 2: Development Tools Pre-installed
199+
200+
```powershell
201+
wsl --install Debian --no-launch
202+
wsl -d Debian
203+
204+
# Install development tools
205+
sudo apt update
206+
sudo apt install -y build-essential cmake ninja-build git
207+
exit
208+
209+
# Export to cache
210+
wsl --export Debian "$env:USERPROFILE\.vagrant.d\wsl2-cache\Debian-vagrant-base.tar"
211+
wsl --unregister Debian
212+
```
213+
214+
## Troubleshooting
215+
216+
### Cache Not Being Used
217+
218+
If Vagrant isn't using your custom cache:
219+
220+
1. **Check the filename**: Must be `<box-name>-vagrant-base.tar`
221+
2. **Check the location**: Must be in `~/.vagrant.d/wsl2-cache/`
222+
3. **Check the box name**: In Vagrantfile, `config.vm.box` must match the cache filename
223+
224+
### Large Cache Files
225+
226+
If your cache files are very large:
227+
228+
- Avoid including package caches: `sudo apt clean`
229+
- Remove temporary files before export
230+
- Consider using VHDX compression (future feature)
231+
232+
### Permission Issues
233+
234+
If you get permission errors when exporting:
235+
236+
```powershell
237+
# Run PowerShell as Administrator
238+
wsl --export DistroName "path\to\cache.tar"
239+
```
240+
241+
## See Also
242+
243+
- [WSL2 Command Cheat Sheet](wsl2-command-cheat-sheet.md)
244+
- [Vagrant Documentation](https://www.vagrantup.com/docs)
245+
- [WSL Documentation](https://learn.microsoft.com/en-us/windows/wsl/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Export Existing Distribution Example
2+
#
3+
# This example demonstrates exporting an existing WSL2 distribution to cache
4+
# when it supports the --name flag
5+
#
6+
# Prerequisites:
7+
# - Ubuntu-24.04 must be installed locally: wsl --install Ubuntu-24.04
8+
#
9+
# What happens:
10+
# 1. Vagrant detects Ubuntu-24.04 is installed locally
11+
# 2. Vagrant checks that Ubuntu-24.04 supports --name flag
12+
# 3. Vagrant exports Ubuntu-24.04 to cache
13+
# 4. Vagrant creates a new VM from the cached tar
14+
# 5. Original Ubuntu-24.04 installation remains untouched
15+
16+
Vagrant.configure("2") do |config|
17+
config.vm.box = "Ubuntu-24.04"
18+
19+
config.vm.provider "wsl2" do |wsl|
20+
# Provider will automatically export existing Ubuntu-24.04 to cache
21+
end
22+
end

examples/legacy-distro/Vagrantfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Legacy Distribution Example
2+
#
3+
# This example demonstrates installing legacy WSL2 distributions
4+
# that don't support the --no-launch flag (Ubuntu-20.04, Ubuntu-22.04, OracleLinux, etc.)
5+
#
6+
# What happens:
7+
# 1. Vagrant installs the distribution interactively (launches automatically)
8+
# 2. Vagrant terminates it before user setup completes
9+
# 3. Vagrant exports it to cache
10+
# 4. Vagrant unregisters the base distribution
11+
# 5. Vagrant creates project VM from cached tar
12+
13+
Vagrant.configure("2") do |config|
14+
# Use a legacy distribution that doesn't support --no-launch
15+
config.vm.box = "Ubuntu-20.04"
16+
17+
config.vm.provider "wsl2" do |wsl|
18+
# Legacy distributions are automatically detected and handled differently
19+
end
20+
end

0 commit comments

Comments
 (0)