When using dd like this:
dd if=/dev/rdiskx of=/path/to/image bs=1m`
if the SD card size is of 16GB, the image size is going to be 16GB.
Right, because you are copying the entire physical device. If the partitions on it do not fill the entire card, you should find out how big they are with, e.g., fdisk
. This is from the util-linux version, but I suspect the freeBSD one is very similar. The card is from a Pi, but it is not in the Pi:
Disk /dev/sdc: 7.4 GiB, 7892631552 bytes, 15415296 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: 0x00092fac
Device Boot Start End Sectors Size Id Type
/dev/sdc1 8192 122879 114688 56M c W95 FAT32 (LBA)
/dev/sdc2 122880 15415295 15292416 7.3G 83 Linux
This is a filled 8 GB card, but for illustration let's pretend it's bigger and I only want to copy the actual partitions. The key here is the final "End" sector, 15415295. The sector size is stated as being 512 bytes but to double check:
7.3 * 1024 * 1024 * 1024 / 15292416 ≌ 512
So the total number of bytes in the image would be
15415296 * 512 = 7892631552
Note I added one to the first number to prevent a potential fencepost error, since it doesn't matter much to have a bit too much, but a bit too little will be a problem. (Also notice in this case the number is the same as the total bytes given on the first line of fdisk output, but if the device had free space on it beyond the partitions, it would not).
With dd
, since you probably want to use large blocks to speed the copy:
7892631552 / (1024 * 1024 * 4) = 1881.75
So with the version of dd
I have here, where M
represents mebibytes (check your man page!), I would use:
dd if=/dev/rdiskx of=/path/to/image bs=4M count=1882
Notice the count
parameter. This is a number of blocks equal to bs
in size (also beware I used 4M, not 1).
If you want to resize a partition, either on the card or in an image (you should be able to mount the partitions in an actual image file on freeBSD, but I do not know how) there are actually two steps: Resizing the partition (e.g., with fdisk
) and resizing the filesystem. Which order you need to do them in depends on whether you are shrinking or growing. Partitions within a device image can be mounted and modified, the image shrunk appropriately or recreated from scratch.
Some tools may do both at once for you.
dd
will give you the image of the correct size. – Dmitry Grigoryev Oct 26 '16 at 15:23