Skip to content

Instantly share code, notes, and snippets.

@amboxer21
Last active May 31, 2024 00:01
Show Gist options
  • Select an option

  • Save amboxer21/778d1ba7415f314e74ea01f4245deed5 to your computer and use it in GitHub Desktop.

Select an option

Save amboxer21/778d1ba7415f314e74ea01f4245deed5 to your computer and use it in GitHub Desktop.

I am using a micro SD Card and an adapter on Gentoo Linux. The SD Card device name is mmcblk0 with 2 partitions. mmcblk0p1 being the boot partition and mmcblk0p2 being the root partition.

Copy the system image to a blank SD Card

anthony@anthony ~ $ sudo dd if=/home/anthony/Documents/IMGs/rpi4b_system_image.img of=/dev/mmcblk0 status=progress bs=512
3218899456 bytes (3.2 GB, 3.0 GiB) copied, 564 s, 5.7 MB/s 
6291456+0 records in
6291456+0 records out
3221225472 bytes (3.2 GB, 3.0 GiB) copied, 573.351 s, 5.6 MB/s
anthony@anthony ~ $

Creating your own image

Finding our device AND the sector size

Run fdisk -l as sudo to check what peripheral storage devices are connected.

anthony@anthony ~ $ sudo fdisk -l
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 850 
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: 0x633c6165

Device     Boot    Start       End   Sectors   Size Id Type
/dev/sda1        3999744   5009407   1009664   493M 83 Linux
/dev/sda2  *     5011456   7362559   2351104   1.1G 83 Linux
/dev/sda3        7364608  23367679  16003072   7.6G 83 Linux
/dev/sda4       23367680 976771071 953403392 454.6G 83 Linux


Disk /dev/mmcblk0: 29.7 GiB, 31914983424 bytes, 62333952 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: 0x6c586e13

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 62333951 61801472 29.5G 83 Linux
anthony@anthony ~ $

Relevant output:

Disk /dev/mmcblk0: 29.7 GiB, 31914983424 bytes, 62333952 sectors
Units: sectors of 1 * 512 = 512 bytes

More specifically: Units: sectors of 1 * 512 = 512 bytes

As you can see in the output above, our SD Card is using 512 bytes for the sector size. This will be our value for dd's bs argument.

Mount the micro SD Card

anthony@anthony ~ $ sudo mount /dev/mmcblk0p2 /mnt/pi
anthony@anthony ~ $ sudo mount /dev/mmcblk0p1 /mnt/pi/boot
anthony@anthony ~ $

Check the size of USED space on the SDCard

anthony@anthony ~ $ df -maxdepth=1 -h /mnt/pi
Filesystem      Size  Used Avail Use% Mounted on
/dev/mmcblk0p2   29G  2.7G   26G  10% /mnt/pi
anthony@anthony ~ $

Finding our count size variable

Since our combined boot and root partions size is a little under 3Gs, we will round our total size up to 3Gs when calculating dd's count variable. The cound variable is used in conjunction with the bs in order to copy only a specific amount of bytes. For example, if you wanted to copy the first 3Gs of a hard disc then this is what that'll achieve.

anthony@anthony ~ $ python
Python 2.7.16 (default, Oct 18 2019, 17:11:22) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int(round((3*1024*1024*1024)/512))
6291456
>>>

The calculation: int(round((3*1024*1024*1024)/512)) 3 is our total hard disc size, 3Gs. 3 * 1024 * 1024 * 1024 is us breaking 3Gs into bytes. 1024 bytes = 1kb, 1024 kb = 1mb, 1024 mb = 1gb We divide that by our sector size and that gives us our count variable. Count is how many sectors we write. So if you're using a 512b sector size then bs=512 count=1 will write 512b. If you use bs=512 count=2 then that will write 1024b and so on and so on.

Unmounting the SD Card

Once you have your count and byte size variable calcuated, umount your SD Card.

anthony@anthony ~ $ sudo umount -R /mnt/pi
Password: 
anthony@anthony ~ $

Double check the image is unmounted

anthony@anthony ~ $ mount | egrep mmcblk0
anthony@anthony ~ $

Creating an image

anthony@anthony ~ $ sudo dd if=/dev/mmcblk0 of=/home/anthony/Documents/IMGs/rpi4b_system_image.img status=progress bs=512 count=6291456
3211375104 bytes (3.2 GB, 3.0 GiB) copied, 44 s, 73.0 MB/s
6291456+0 records in
6291456+0 records out
3221225472 bytes (3.2 GB, 3.0 GiB) copied, 44.2564 s, 72.8 MB/s
anthony@anthony ~ $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment