Custom WSL Image

Sunday, March 8, 2020

Windows Subsystem for Linux (also known as WSL) is a system where you can run a Linux distribution inside Windows. This is great in that you can run any command line application in Windows. Version 1 of WSL did this using a translation layer between the Linux system and the Windows side. Version 2, which is part of version 2004 of Windows 10, uses an actual Linux kernel to do the work. That’s right, for the first time Microsoft is shipping a Linux kernel. Amazing times. Installing WSL 2 is not the focus of this blog post. You can learn how to install it using these fine docs .

Docker is a tool that allows you to run software inside virtualized containers. You can import base images from DockerHub and enhance these base images with your software. Then you can save your image and deploy it. It’s as close as you can get to saying, “It works on my machine, so let’s just deploy my machine!” The edge release of Docker supports WSL2 as it integrates well with it. Get started with Docker here on their web site . I won’t cover installing Docker, but you’ll need it to follow along with these directions.

Once WSL2 is installed you need a Linux distribution. You can install a distribution from the Microsoft Windows Store by searching for Linux. At the time of writing there are 5 distributions available: Ubuntu, openSUSE, Kali, Debian, and Alpine. I’ve used Ubuntu for a long time, and it’s always been my go-to Linux distribution on WSL. In the physical world I switch between Ubuntu and Arch Linux. Arch Linux is a very customizable distribution. Installing it on a machine takes a good half hour of installing just the pieces that you need. This makes it lightweight and fast.

One more piece that you should have is the new Windows Terminal application. It is currently in preview, but it’s fairly stable and I’ve been using it since version 0.1. You can get this from the Windows Store .

I got to wondering the other day: How can I install Arch in WSL? I was surprised to find out just how easy it is. It’s a simple matter of creating a Docker image, exporting it to a TAR file, and then import that TAR file into WSL. Three steps, that I turned into 15 for this blog post. Here are the steps you need to do to get Arch Linux as an option in WSL.

  1. Run the command docker pull archlinux
  2. Run the command docker create -i archlinux bash
  3. Run the command docker export {hash} > archlinux.tar
  4. GZip the tar file created in step 3.
  5. Make a directory somewhere to store the vhdx file that will be created. I did this in c:\tools\wsl\
  6. In this directory create a directory for Arch Linux mkdir archlinux
  7. Run the command wsl --import Arch ./archlinux archlinux.tar.gz
  8. Make sure it was created using the command wsl --list --verbose
  9. Start Windows Terminal and click the down arrow to select which terminal you want to run. Arch should appear towards the bottom of that list. Launch it. You will be logged in as Root.
  10. Create an admin user with the command useradd -m -G wheel {username}
  11. Change the password with the command passwd {username}
  12. Install sudo, vim, and git pacman -Syu sudo vim git
  13. Update sudo with the command EDITOR=vim visudo and allow the wheel group access to sudo.
  14. Update the Windows Terminal settings to something like this:
    {
        "guid": "{keep the GUID already created}",
        "hidden": false,
        "name": "Arch Linux",
        "source": "Windows.Terminal.Wsl",
        "commandline": "wsl.exe -u {username} -d archlinux",
	    "startingDirectory": "//wsl$/archlinux/home/{username}/",
	    "icon": "C:\\tools\\terminal\\archlinux_logo.png",
	    "useAcrylic": true,
	    "acrylicOpacity": 0.85
    }
  1. Close the open terminal window and launch it again after saving the terminal settings.

The icon is optional, but I like to have it in my settings to give it a nice flair. Also the opacity is optional, but again I think it gives a nice flair.

Windows Terminal running Arch Linux

WindowsWSLLinux

This work is licensed under CC BY-NC-SA 4.0

Intel NUC 10

Cloning a Drive