How to prepare a Raspberry Pi image file on Windows 10 (WSL2)

14/08/2020 17:38 BST

How to prepare a Raspberry Pi image file on Windows 10 (WSL2)

  • Windows 10 May 2020 Update (build number 10.0.19041) or greater
    • This is required for WSL2
  • Windows Subsystem for Linux 2 (WSL2)
    • version 1 does not support ext4 file system, will not work.

Now we are going to prepare an image file with multiple partitions, let create a 1GB image file using WSL2 terminal.

$ dd if=/dev/zero of=1GB_sd.img bs=1M count=1024

You can create a bigger image file if you like, but it will take longer to create, alternatively, you can expand the partition after you booted up the system. link.

Now you going to need to link the image file to the system, you can do that by using losetup

# losetup -Pf --show 1GB_sd.img

That command should tell where it has been linked, I’m going to assume /dev/loop1, now we are going to prepare the partition inside the image using fdisk.

# fdisk /dev/loop1

At the fdisk prompt, create two new partitions.

  • Type n, then p for primary, 1 for the first partition on the drive, press enter to accept the default first sector, then type +100M for the last sector.
  • Type t, then c to set the first partition to type W95 FAT32 (LBA)
  • Type n, then p for primary, 2 for the second partition on the drive, and then press enter twice to accept the default first and the last sector.
  • Write the partition table and exit by typing w

Then create and mount the FAT file system

# mkfs.vfat /dev/loop1p1
# mkdir boot
# mount /dev/loop1p1

Then create and mount the ext4 file system

# mkfs.ext4 /dev/loop1p2
# mkdir root
# mount /dev/loop1p2

Then download and extract the root filesystem (as root, not via sudo)

# wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
# bsdtar -xpf ArchLinuxARM-rpi-4-latest.tar.gz -C root
# sync

Then move boot files to the first partition

# mv root/boot/* boot

Then umount the two partitions

# umount boot root

Then unlink the image from the system.

# losetup -d /dev/loop1

At this point, you should have the image file ready to write to the SD card, you can use Win32 Disk Imager Once you have done that insert the SD into the Raspberry PI and it should boot up. :)

References

Creating Virtual Disks Using Linux Command Line
Arch Linux ARM Raspberry Pi 4

| |

Privacy Policy | CV