Image Shrinking for ext4
Original use case: I have a debian sid image from a 256 gb ext4 lvm volume. It contains 18 GB of data. I want it to be 18 GB, and then I want to compress it and store it in archive.
This image will only be used if my debian sid image is borked.
I would like to quickly reproduce the process in the future.
Getting the image
Background: The debian volume does not contain /home or other "user data" directories. It is also intended that flatpaks, docker images, and snaps should not be in the debian volume.
Current method to get the image file
- boot debian recovery usb
- dd the debian lvm volume directly to an image on a data partition
- It will be 256GB since that is the size of the drive. Mostly not used.
Future method to get the image
Test this out when there is a good opportunity. Maybe read up as well, and ideally validate (probably not, since the consequences are just reinstall debian sid)
- Can we remount the filesystem read-only while in the live filesystem? I think so.
- use dd on the filesystem while it is read-only.
Resizing the image
After you have the file, you can boot back into debian.
- e2fsck, resize2fs -M /dev/loop0 - shrink the partition to the minimum possible size within the file (filesize does not change)
- reference these commands where needed
- calculate bytes from the blocksize * blocks, get it right or you lose data...
- this is in the resize2fs data
- truncate -s SIZE filename.img
- reattach to loop0 and e2fsck
- reference these commands where needed
Compressing the image
use zstd -9. 9 is the top standard speed compression level. This compressed a truncated image from 18 GB to 6.4 GB.