Oct 13

Shrink a Raspberry Pi or RetroPie .img On Windows with PiShrink

PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. However, thanks to Windows Subsystem for Linux (WSL), you can now run it on Windows!

Prerequisites

  • Install Linux on Windows with WSL
  • Rasberry Pi / RetroPie backup .img
    • An .img backup of a MicroSD includes all of the unused space and will be the size of the Micro SD Card (ie: a 16GB MicroSD Card will create about a 16GB .img backup). The simplest method for Windows is to use Win32 Disk Manager. Windows cannot read an EXT4, so it should show the Micro SD card as a Boot Drive (with a disk size of about 250MB) and another drive (with no disk size). Make sure you backup the second drive without a size listed in WIn32 Disk Manager, and this will backup the entire Micro SD card.
    • Note: Micro SD cards can vary slightly in size by manufacturer as well as between different models of Micro SD cards. Therefore, there’s no guarantee creating an .img backup of a Micro SD Card will be able to write back to the same size Micro SD card unless it’s the exact same manufacturer/model.
  • PiShrink.sh script
    • PiShrink will shrink an .img backup of Raspberry Pi / Retropie by removing the “unused” space. For example, if you create a backup image a 16GB Micro SD with 7GB of data on it, the original .img will be about 16GB but after running PiShrink, it will be about 7GB. Upon the first boot, the image will resize the max size of the SD card, so if you restore the 7GB image to a 32GB card, it will expand to have a max of 32GB.

Shrink Raspberry Pi / RetroPie .img on Windows with WSL

  1. Move your Raspberry Pi / RetroPie .img to C:\RPI\
    1. Ex: C:\RPI\RetroPie48.img
  2. Download PiShrink.sh and move it to C:\RPI\
  3. Open your WLS Linux Distro (ie: Ubuntu) and run the below commands:
    1. The time to complete will vary based on the size of the image and how much it can shrink. It took about 20min for me to shrink a 120GB image to 22GB.
cd /mnt/c/rpi
sudo ./pishrink.sh retropie48.img
Oct 13

Install Linux on Windows with WSL

Install a Linux distribution using the Windows Subsystem for Linux (WSL), which enables you to use Linux tools on you Windows File System.

Install WSL | Microsoft Learn

Prerequisites

  • Windows 10 version 2004 or higher (Build 19041 or higher)
  • Windows 11

Install WSL Command

From an elevated PowerShell or Command Prompt run the below command and you will need to reboot once complete. The command only works if WSL is not installed (See next section to determine WSL version & installed Linux distros), and by default will install Ubuntu (This can be change by specify -d <distributionname> in the command below).

wsl --install

After you reboot, the Ubuntu install will finish up and prompt you to create a username and password (Does not need to match your Windows username).

List and Install Linux Distributions

See a list of available Linux Distributions available for download through the Microsoft Online Store, run the below from an elevated PowerShell or Windows Command Prompt:

wsl --list --online

See a list of Linux Distributions installed

wsl --list -verbose

Install Linux Distribution

wsl --install -d <Distro>
wsl --install -d Debian

Ways to Run Multiple Linux Distributions with WSL

  1. Install Windows Terminal (Recommended). Open as many tabs or Window panes to quickly switch between multiple Linux Distros or other command lines (PowerShell, Command Prompt, Azure CLI, etc). This can be installed by search Windows Terminal in the Microsoft Store.
  2. Click Windows Start menu and start typing the name of the distro such as Ubuntu
  3. From PowerShell or Windows Command Prompt, you can enter the name of your installed distro such as ubuntu
  4. From PowerShell or Windows Command Prompt, open your default Linux distro by entering wsl.exe
  5. From PowerShell or Windows Command Prompt, you can use the default Linux distro

Access Linux File System from Windows

In the Windows Explorer address bar enter \\wsl$ and you will be able to access a hidden file share for your Linux Distros.

Access Windows File System from Linux

In your Linux Distro, it automatically mounts all of your Windows drives (ex: C:). You can list them by running

ls /mnt