I installed Raspbian to a 16 GB card and expanded the filesystem. When I made a
dd backup of the card, the .img file output was ~16 GB. Most of it is unused space in the ext4 partition—I'm only using like 2.5 GB in that partition. (There are two partitions—the first is FAT for boot and the second is ext4 for rootfs.) I'd like to shrink the backup.img file which resides on an Ubuntu 16.04 Sever installation (no GUI) so that I can restore the image to a card of smaller size (say 8GB for example).
So far, I have mounted the ext4 partition to
/dev/loop0 by using the offset value provided to me by
fdisk -l backup.img. Then I used
e2fsck -f /dev/loop0 and then
resize2fs -M /dev/loop0 which appeared to shrink the ext4 fs... am I on the right track? I feel like
parted might be next, but I have no experience with it.
How do I accomplish this using only cli tools?
Here is the output from running
fdisk -l backup.img:
Disk backup.img: 14.9 GiB, 15931539456 bytes, 31116288 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x79d38e92 Device Boot Start End Sectors Size Id Type backup.img1 * 8192 124927 116736 57M e W95 FAT16 (LBA) backup.img2 124928 31116287 30991360 14.8G 83 Linux