How to create a small SD image and automatically expand the filesystem later?

Hi, recently I installed Manjaro XFCE on my Raspberry and I like it very much. This is my first Linux experience :slight_smile:

After I customized Manjaro to my needs, I’d like to create an image out of the SD card so I can use it anytime later with my changes already included, but…

  1. How to create SD image of the size of real data included (and not the size of the entire SD card capacity) ?

  2. How to make the image’s filesystem self-expandable on first boot so I can write it on any SD card and use its full capacity again?

I tried PiShrink but the filesystem did not expand… GitHub - Drewsif/PiShrink: Make your pi images smaller!

How exactly is the image of the actual Manjaro XFCE (or any other distro) created? And how exactly it resizes the filesystem on its first boot?

Bump. Anyone, please?

I have been doing this manually with Win 32 disk imager (running on Windows) to write an image file to
my large USB file system, the image file is the same size as the SD Card, 64 GB for example.

Then use Pi Power tools (running on Pi) to shrink the image down to less than 10 GB in my case.

The 10 GB image can then be flashed to any other SD Card greater than 10GB.

Then boot the Pi using this other SD card and once up and running use gparted to expand the running FS to fill the SD Card. Some Linux distros do auto resize, e.g. Ubuntu MATE.

I would love to exclude having to run Win 32 disk imager on Windows but all suggested methods that I have tried using dd on linux do not work.

I now have dd working and so can exclude having to use Windows :slight_smile:

find the SD Card device

sudo fdisk -l | grep “sd”

unmount any file systems using the SD Card, e.g. if SD Card = /dev/sdb

sudo umount -v /dev/sdb1
sudo umount -v /dev/sdb2

use dd to read SD Card and write myCard.img to TOSH USB3 drive

sudo dd if=/dev/sdb of=/media/pi/TOSH/myCard.img bs=64K conv=notrunc,noerror

Thanks for the tip, Mike!

I did something similar and at the end I created .xz archive which you can use with Raspberry Pi Imager or Balena Etcher directly without a need to unpack it first.

But still, creating an image out of real files only and including a resize script would be a neat solution.