├── .gitignore ├── LICENSE ├── README.md ├── bootstrap-debian-images.PS1 ├── create-machine.PS1 ├── destroy-machine.PS1 ├── files └── bullseye_sources.list ├── personalize.sh ├── upgrade-distro.sh └── upgrade-packages.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | *.tar 3 | *.appx 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Hans Kruse 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automatically generated WSL Debian development environments 2 | 3 | > WARNING! no longer works with more recent WSL. Needs a rewrite! 4 | 5 | This project enables you to quickly create up to date WSL2 Debian instances for testing and 6 | development purposes. For this you need Windows 10 and hardware that is capable of running WSL2. 7 | For some background information I wrote this [blog post](https://hanskruse.eu/post/2020-07-04-throw_away_wsl_environments/) 8 | 9 | To use this, create a directory `c:\wsl` where you have rights to write. 10 | This is used to store your machine's disk images. 11 | 12 | Next clone or get started download this project. 13 | In PowerShell navigate to the project. 14 | 15 | ```cmd 16 | PS C:\Users\Hans Kruse> git clone https://github.com/nicenemo/wsl-debian-boxes 17 | PS C:\Users\Hans Kruse> cd wsl-debian-boxes 18 | PS C:\Users\Hans Kruse\wsl-debian-boxes> 19 | ``` 20 | It needs to run from its directory. I did not make it more robust yet. 21 | 22 | Start [bootstrap-debian-images.PS1](bootstrap-debian-images.PS1) 23 | to create tar balls for Debian: 24 | 25 | * ~~9 Stretch~~ 26 | * ~~10 Buster~~ 27 | * 11 BullsEye 28 | 29 | **Warning!** You should not run the script below if you use the Debian distribution 30 | from the Windows store directly as your personal WSL environment. It will be destroyed! 31 | 32 | ```cmd 33 | PowerShell.exe -ExecutionPolicy Bypass -File .\bootstrap-debian-images.PS1 34 | ``` 35 | When successful: 36 | 37 | * Three tar balls have been created. 38 | * The Debian machine was deleted. 39 | * The downloaded Debian Installation package was deleted. 40 | * The installed Windows store app was uninstalled. 41 | 42 | Once you have created the tar balls, creating new machines is quite fast. 43 | Faster than installing them from the Windows store. It also allows you to 44 | easily create multiple identical machines with the same Linux distribution. 45 | 46 | Machines can be created and personalized with the [create-machine.PS1](create-machine.PS1) script. 47 | This scrip and the related [destroy-machine.PS1](create-machine.PS1) will create virtual machines in c:\wsl. 48 | Change both scripts if you need it to be anywhere else. 49 | 50 | * A user is created with sudo rights without needing a password. 51 | * In the user's home directory a soft link _winhome_ to the windows home directory of the user is created. 52 | * The created machines will start with the created user as default user. 53 | * An updated Ansible is installed in the user's ~/.local/bin. 54 | * ~/.local/bin is added to the user's path. 55 | * No further customization is done yet. 56 | 57 | Three user environment variables need to be set before you can create machines with the provided script. 58 | 59 | * *LINUX_USER* Should be the Linux username of the single user that is created in the machine, e.g. *kruse*. 60 | * *WIN_HOME* Should be a Windows directory as seen from Linux. This will be soft linked as *winhome* in the Linux user's home directory, e.g. */mnt/c/Users/Hans\ Kruse*. Note that spaces need to be escaped! 61 | * *WSL_INSTALL_PATH* The path where your Linux distribution should live, e.g. *C:\WSL* Please do not add an ending backslash 62 | 63 | You can then give a name of the machine to be created and the tar ball to use as command line arguments. 64 | 65 | In PowerShell: 66 | 67 | ```cmd 68 | PowerShell.exe -ExecutionPolicy Bypass -File .\create-machine.PS1 nemolinux debian-bullseye.tar 69 | ``` 70 | Cleaning up machines can be done with the [destroy-machine.PS1](destroy-machine.PS1) script. 71 | 72 | ```cmd 73 | PowerShell.exe -ExecutionPolicy Bypass -File .\destroy-machine.PS1 nemolinux 74 | ``` 75 | 76 | In the _files_ directory, you will find the Debian package sources files that are used. 77 | You can modify them to use a local mirror. That might be faster than pulling them from 78 | the main Debian archives directly. 79 | 80 | ## Beware of smart git on Windows! 81 | 82 | Windows on git by default converts line endings to/from Linux/Windows. 83 | This will go horribly wrong if you edit the bash files from Windows. 84 | Either use git from WSL and edit files from WSL. 85 | You can turn off, line ending conversion on git in Windows. 86 | Bash files with Windows line endings do not run. 87 | This bit me once making small adjustments from Windows in bash files, 88 | while I was editing from WSL most of the day. 89 | 90 | -------------------------------------------------------------------------------- /bootstrap-debian-images.PS1: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env pwsh 2 | # 3 | #Get Standard Debian (Bullseye?) from Windows store and install 4 | # 5 | $url = "https://aka.ms/wsl-debian-gnulinux" 6 | $output = "debian.appx" 7 | echo "Getting Debian WSL image from Microsoft." 8 | Import-Module BitsTransfer 9 | Start-BitsTransfer -Source $url -Destination $output 10 | echo "Installing image." 11 | Add-AppxPackage .\debian.appx 12 | 13 | # Do post install without creating a user. 14 | echo "Performing post install." 15 | debian install --root 16 | 17 | # Create updated Bullseye image. 18 | echo "Set Bullseye package sources." 19 | debian run cp ./files/bullseye_sources.list /etc/apt/sources.list 20 | echo "Perform distribution upgrade from Buster to Bullseye." 21 | debian run ./upgrade-distro.sh 22 | echo "Creating debian-bullseye.tar tarball." 23 | wsl --export Debian debian-bullseye.tar 24 | 25 | # Clean up standard Debian image 26 | wsl --terminate Debian 27 | wsl --unregister Debian 28 | Get-AppxPackage -name TheDebianProject.DebianGNULinux|Remove-AppPackage 29 | rm debian.appx 30 | -------------------------------------------------------------------------------- /create-machine.PS1: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env pwsh 2 | # 3 | # Sets the default user of a distribution. 4 | # 5 | # Usage: 6 | # WSL-SetDefaultUser 7 | # 8 | # Example: 9 | # 10 | # WSL-SetDefaultUser Debian-10 kruse 11 | # 12 | # This standard wsl command does not offer the option to set the default user like distribution specific starter programs do. 13 | # 14 | # Stolen from https://github.com/microsoft/WSL/issues/3974 15 | Function WSL-SetDefaultUser ($distro, $user) { Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq $distro | Set-ItemProperty -Name DefaultUid -Value ((wsl -d $distro -u $user -e id -u) | Out-String); }; 16 | 17 | # 18 | # The linux username you want to use for your machine. 19 | # Set it in your user environment variables. 20 | # 21 | $env:LINUX_USER=[System.Environment]::GetEnvironmentVariable('LINUX_USER','user') 22 | 23 | # 24 | # The location of your Windows home directory as seen from Linux. Note that spaces need to be escaped!. 25 | # E.g.: My home directory on windows is: "C:\Users\Hans Kruse". 26 | # On Linux this WIN_HOME becoes "/mnt/c/Users/Hans\ Kruse" 27 | # We create a win_home soft link in your linux home directory. 28 | # 29 | $env:WIN_HOME=[System.Environment]::GetEnvironmentVariable('WIN_HOME','user') 30 | 31 | 32 | # 33 | # The location where you want to install your Linux distributions. 34 | # E.g.: c:\WSL 35 | # By default Windows will install your linux distributions in your APP_DATA 36 | # folder in your home directory. My Linux distributions tend to grow large. I 37 | # prefer them to be somewhere else, if possible on another disk. 38 | # 39 | $env:WSL_INSTALL_PATH=[System.Environment]::GetEnvironmentVariable('WSL_INSTALL_PATH','user') 40 | 41 | # 42 | # Imports the machine from a tar ball. 43 | # First argument is the name of your box, second the file name of the tar bar in the current directory. 44 | # 45 | wsl --import $args[0] $env:WSL_INSTALL_PATH\$args[0] $args[1] 46 | 47 | # 48 | # Upgrade the created machine first. 49 | # It might have been a while since the tar ball was created. 50 | # 51 | echo "Update packages" 52 | wsl -d $args[0] ./upgrade-packages.sh 53 | 54 | # 55 | # Install python and pip. 56 | # Since further customization is intended to be done with Ansible we need this. 57 | # These are installed from Debian apt-repos. 58 | # A further update is done from within the user account, creating versions specific to the user. 59 | echo "Install python3 and pip" 60 | wsl -d $args[0] sudo apt-get -y install python3 python3-pip python3-apt aptitude 61 | 62 | # 63 | # Create the user with password less sudo right. 64 | # 65 | echo "Create user with password-less sudo rights." 66 | wsl -d $args[0] ./personalize.sh $env:LINUX_USER $env:WIN_HOME 67 | echo "Set default user." 68 | WSL-SetDefaultUser $args[0] $env:LINUX_USER 69 | # 70 | # Install a possible upgraded pip in the users .local/bin 71 | # Then install install ansible. 72 | # 73 | wsl -d $args[0] python3 -m pip install pip --upgrade 74 | wsl -d $args[0] python3 -m pip install ansible 75 | 76 | # 77 | # Remove created function 78 | Remove-Item Function:WSL-SetDefaultUser 79 | -------------------------------------------------------------------------------- /destroy-machine.PS1: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env pwsh 2 | 3 | # 4 | # The location where you want to install your Linux distributions. 5 | # E.g.: c:\WSL 6 | # By default Windows will install your linux distributions in your APP_DATA 7 | # folder in your home directory. My Linux distributions tend to grow large. I 8 | # prefer them to be somewhere else, if possible on another disk. 9 | # 10 | $env:WSL_INSTALL_PATH=[System.Environment]::GetEnvironmentVariable('WSL_INSTAL_PATH','user') 11 | 12 | wsl --terminate $args[0] 13 | wsl --unregister $args[0] 14 | rm -r -fo $env:WSL_INSTALL_PATH\$args[0] 15 | -------------------------------------------------------------------------------- /files/bullseye_sources.list: -------------------------------------------------------------------------------- 1 | deb http://deb.debian.org/debian bullseye main contrib non-free 2 | deb-src http://deb.debian.org/debian bullseye main contrib non-free 3 | 4 | deb http://security.debian.org bullseye-security main contrib non-free 5 | deb-src http://security.debian.org bullseye-security main contrib non-free 6 | 7 | deb http://deb.debian.org/debian bullseye-updates main contrib non-free 8 | deb-src http://deb.debian.org/debian bullseye-updates main contrib non-free 9 | -------------------------------------------------------------------------------- /personalize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | adduser --disabled-password --gecos "" $1 3 | ln -s "$2" /home/$1/winhome 4 | echo 'PATH=~/.local/bin:$PATH:' >> /home/$1/.profile 5 | echo "$1 ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/$1_user" 6 | -------------------------------------------------------------------------------- /upgrade-distro.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | apt-get -y update && \ 3 | apt-get -y upgrade && \ 4 | apt-get -y full-upgrade && \ 5 | apt-get -y autoclean && \ 6 | apt-get -y autoremove 7 | -------------------------------------------------------------------------------- /upgrade-packages.sh: -------------------------------------------------------------------------------- 1 | #/!/bin/sh 2 | apt-get -y update && \ 3 | apt-get -y upgrade && \ 4 | apt-get -y autoclean && \ 5 | apt-get -y autoremove 6 | --------------------------------------------------------------------------------