I have microSD card with Ubuntu 20.10 for RaspberryPi 4. The microSD card is 32GB in size, however only about 2GB in total is being occupied on it. I would like to clone it onto 16GB microSD card and while I know how to successfully do it using GUI tools, I struggle with CLI.
The steps that I came up with so far are:
- Power Off raspberry Pi
- Remove SD card and insert into a computer running Ubuntu 20 (appears as /dev/sdb)
- Check partitions using
parted -lwhich shows that the partition I want to shrink has number2. - As partitions on this microSD card were automatically mounted on this OS, I unmount them all with
umount /dev/sdb1andumount /dev/sdb2 - I run file system check and let it complete using
e2fsck -f /dev/sdb2 - I shrink filesystem using
resize2fs /dev/sdb2 10G - I shrink the partition using
parted /dev/sdb resizepart 2 10G
At this point I open GParted just to visually verify my work and I am greeted with this error on partition /dev/sdb2 : 
If I now insert this microSD card to my RaspberryPi 4 it fails to boot and gets stuck on this screen: 
I am just playing around here so I'm not afraid to lose any data, I just want to learn how to properly shrink a partition of RaspberryPi microSD using CLI.