0

I have a backup of my Raspbian on a .img file. The problem is that even though the file is less than 8GB when I try to flash it on a 16GB microSD card, I get an error that there is not enough space.

This is what I get when I run sudo fdisk -l on my .img file:

Disk Rasp4.img: 14,9 GiB, 15987638272 bytes, 31225856 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: 0xa8c7ffa4

Device          Boot Start      End  Sectors  Size Id Type
Rasp4.img1       8192    93814    85623 41,8M  c W95 FAT32 (LBA)
Rasp4.img2      94208 15564799 15470592  7,4G 83 Linux

When I open the .img file in Disks, I see that there is 8GB of free space. Why is this free space being included in the .img file (and yet is not being shown by fdisk -l as a partition that can be trimmed) and how can I remove it?

2
  • You should specify HOW you "have a backup of my Raspbian on a .img file" – Milliways Mar 13 '18 at 22:23
  • All apologies ;) – goldilocks Mar 17 '18 at 12:46
0

this free space ... is not being shown by fdisk -l

Yes it is. Look at the first line of your fdisk output. That "free space" isn't part of any partition. This is the same as if you had formatted a 16 GB card with one 8 GB partition.

The problem is that even though the file is less than 8GB

Are you sure about that? I suspect it is actually 14.9 GiB, and what you mean is the root filesystem in it is only ~8GB.

If so, try this (you will need 8GB free to do it):

dd if=Rasp4.img of=smaller.img bs=4096 count=1945600

Then run fdisk on smaller.img to check. The math here is 4096 (block size in bytes) * 1945600 (number of blocks) = 7969177600 / 512 (sector size used by fdisk) = 15564800 (one sector beyond the "End" of the 2nd partition).

8
  • I can take a screen shot where Disks literally has a graphic that states I have 8GB of free space on the .img file if you would like... It is, however, a partition that fdisk does not display--and so, I am not sure how to trim it. – user8951490 Mar 13 '18 at 20:59
  • Yes, I believe you. My point is fdisk DOES show the free space. It just does not show it as a partition, because it isn't. "Disks" may indicate the space in a manner orthogonal to the way it shows information about a partition, but it isn't showing you a partition either. To illustrate: If you fire up fdisk on a device or image and delete all the partitions, it will then not show any table -- just the summary up top. Which does say clearly, "14.9 GiB". – goldilocks Mar 13 '18 at 21:20
  • ...I think what you are confused about is the fact that not all the space on a device (or in an image file) is necessarily formatted into partitions. An unformatted 500 GB hard drive has 500 GB of free space and zero partitions (although since they usually come formatted, this is not what you will see with a new drive). Make sense? – goldilocks Mar 13 '18 at 21:21
  • I suggest to use sudo parted Rasp4.img unit s print free. This will also show the free space and clarify the situation. – Ingo Mar 14 '18 at 1:06
  • @goldilocks, that was precisely the misunderstanding. Thank you. Is there anyway I may trim this free space nonetheless? – user8951490 Mar 17 '18 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.