Not all SD cards are the same.  64 gig on one, is not 64 gig on another.  So here’s how to resize and truncate your images so they will fit.

First, enable loopback.

sudo modprobe loop

Then request a free loopback device.

sudo losetup -f

Create a device.

sudo losetup /dev/loop0 64gb.img

Load the partitions in the kernel

sudo partprobe /dev/loop0

That should get you /dev/loop0p1 and /dev/loop0p2 and maybe more.
You could also mount them to copy or modify files at this point.

Resize the some of the partitions with Gparted as much as you can/want.

sudo gparted /dev/loop0

Apply will take a few minutes.

This wont resize the raw image size.  We still need to truncate.

To truncate the image, first see where the last partition ends.

fdisk -l 64gb.img

Device            Boot  Start     End Sectors  Size Id Type
64gb.img1        8192  137215  129024   63M  c W95 FAT32 (LBA)
64gb.img2      137216 3944447 3807232  1.8G 83 Linux

You should see a start and end as well as the block size (unit sectors).  Block size is typically 512.
The end number means that the partition ends on that block PLUS 1 TIMES 512.

So the equation you need to use is (ENDBLOCK# +1)*512.

run this command to truncate.  Replace the first number with your end block.

truncate --size=$[(3944447+1)*512] 64gb.img

Check and the size should be smaller.

Then, write the image to the SD card with some status.

pv -tpreb 64gb.img | dd of=/dev/sdf bs=4M
Categories: Linux

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *