1

I just installed FreeBSD 11 on a Raspberry Pi 1 Model B, the raw image is less than 1GB after installing some packages and customising the setup I would like to create an image the one I could share and be installed no matter the size of the SD cards (>= 4GB)

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.

Therefore I would like to know what could be the best way to create a custom img of only the data and not the full size of the SD card.

any ideas?

nbari
  • 113
  • 3

3 Answers3

2

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.

goldilocks
  • 60,325
  • 17
  • 117
  • 234
0

Goldilocks has given an answer which should let you get a result.

You should paste your partition layout (output of fdisk) for a specific answer.

This is not how images are normally made, but you could make a fdisk script to create a new minimal image by copying from existing. Unfortunately fdisk is not particularly easy to script, but if you want to go this way I could post a sample I have used for manipulating images (I would have to hunt for it).

Milliways
  • 62,573
  • 32
  • 113
  • 225
0

Just two things to add here: first, the FreeBSD command is "gpart show" or "gpart list", not fdisk. Second - resizing is not an issue, just set growfs_resize="YES" in /etc/rc.conf and it will resize automatically on reboot. Note that you can only expand the filesystem, not shrink it down.