User avatar
radilly
Posts: 2
Joined: Sat Oct 20, 2018 6:35 pm
Location: Pittsburgh, PA USA

Re: How can I prevent root to expand at first boot?

Sun Oct 21, 2018 8:06 pm

scruss wrote:
Sun Oct 21, 2018 5:07 pm
if you leave off count, dd will read all the blocks it can. This will typically be the full size of the filesystem.
Here the terminology, perhaps, trips me up a little. In the fdisk output above, it shows that the "disk" is (nominally) 4 GB - an intentionally small SD card. But only ~ 1.3 GB is allocated to partitions / filesystems (whichever is the correct term). That's the only part I believe I need to image to get the smallest snapshot; 1.3 GB. The structure may be easier to see with lsblk

Code: Select all

$ lsblk /dev/sdc
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdc      8:32   1  3.8G  0 disk
+-sdc1   8:33   1 41.7M  0 part
+-sdc2   8:34   1  1.2G  0 part
So you are right, when I imaged sdc, without count I got a 4 GB image file - the whole thing. When I did sdc1 and sdc2 without count I got 2 separate images totaling about 1.3 GB. But 2 image files are messier to work with than 1, so I think I just want the first part of the disk that has been allocated to partitions / filesystems. Restoring a 1.3 GB image is likely going to be considerably faster than 4 GB irrespective of the blocksize. At least that is my thinking.

Technically I think there are some unalloacted sectors within that 1.3 GB, but it's a rather small percentage. I believe dd'ing the first 1,304,671,232 bytes in this example is enough to copy what Etcher created to another SD card cleanly. Of course, as I think you plan, I want to do some initial configuration and may need to expand sdc2 a little to get the starting point I want work from.

When I searched for information on the use of dd for this purpose I either got very specific examples, or seemingly complicated discussions. I'm hoping this approach is a balance between simplicity and enough generalization to work for many common cases, i.e. the combination of
  • fdisk -l
  • dd (with bs and count numbers copied from the fdisk output)
I guess I could test it, but as @jahboater suggested, a smaller blocksize might keep one from having to try the dd multiple times. And using bs=512 (the default I think) means one can just copy the bs and count values from the fdisk output - without risk of making a math error! ;)

Writing this out helped me clarify my thinking - and hopefully may help others who want to image a (partially allocated) SD card. (Assuming the resize2fs_once process is bypassed as above.)

Cheers, and Thanks!

User avatar
scruss
Posts: 1821
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: How can I prevent root to expand at first boot?

Mon Oct 22, 2018 4:15 pm

radilly wrote:
Sun Oct 21, 2018 8:06 pm
… Restoring a 1.3 GB image is likely going to be considerably faster than 4 GB irrespective of the blocksize. At least that is my thinking.
You're right. It will be. If you want the speed benefit of the 1M block size while ensuring all the filesystem is copied, you could script something like this:

Code: Select all

sudo fdisk -l /dev/sdc | awk '/^Units/ {u=$(NF-1);} END{print int(1 + ($3 * u)/(1024*1024));}'
That gives you the count= value in MB, suitably rounded up.

For me, though, even on my smallest Raspberry Pi setups, this is kind of a moot point. I can't buy 4 GB cards any more (and don't think I ever saw a Class 10 one). USB 3 card readers are fast and cheap.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Raspbian”