One of my Raspberry Pi's is using a 4GB SDcard for the root/boot filesystem.
As seen when mounted on another system:
Disk /dev/sdc: 3904 MB, 3904897024 bytes 121 heads, 62 sectors/track, 1016 cylinders Units = cylinders of 7502 * 512 = 3841024 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000714e9 Device Boot Start End Blocks Id System /dev/sdc1 2 17 57344 c W95 FAT32 (LBA) /dev/sdc2 17 800 2938880 83 Linux This shows that I'm only (and deliberately) using 3GB of the available 4GB and that the cyclinders in use are aligned to the beginning (if flash memory even works that way).
In the past I have used the cp command to migrate an existing filesystem to a smaller (already formatted) target filesystem but I prefer the simplicity of the dd command.
Q1: Can I safely use dd to clone this disk to another, slightly smaller (44MB smaller to be precise) 4GB SDcard?
If the source disk had a second partition that filled up the entire remaining disk space but had a filesystem that was only partially used, I'm pretty confident that over time I could end up with scattered bits of files even near the end of the partition (potentially overwriting the boundaries of the physical target disk).
Q2a: is there perhaps a move-files-to-beginning-kinda-defrag command that I can run to make sure the end of the partition was unused?
Q2b: if I knew the end of the partition was unused, could I safely truncate the partition with dd to the smaller target disk and correct the misalignment afterwards with a simple fsck?