I know this is an old question, but I would like to show how to do this process on Mac, because it is not as easy:
fdisk doesn't have the
-l option, and
truncate is not installed by default:
1. Step 1: Install truncate on Mac OS X:
You need MacPorts or Homebrew for this. I use MacPorts. If you don't have one of these, go ahead and install them first. Link to MacPorts
Now, we can install truncate. Open up your Terminal and type:
sudo port install truncate
For Brew installation:
brew install truncate
This should do it.
2. Use Disk Utilities to mount our IMG so that it is visible to the diskutil terminal command.
You will see in a moment why we need this step. Open the Disk Utility app. Click on File (on the top bar) -> Open Disk Image and select your IMG file.
3. Check the partition size of the IMG and where it is mounted.
On a Terminal, type:
And it should show something like this, somewhere in the end:
/dev/disk3 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme +16.0 GB disk3
1: Windows_FAT_32 boot 62.9 MB disk3s1
2: Linux 3.9 GB disk3s2
So, we see that about 12GB are unpartitioned. We need to truncate those.
The reason we need this command is to check out where the disk image is mounted. In my case, it is under: /dev/disk3
4. Find out the actual partition size.
3.9 GB and 62.9 MB are values that won't work with truncate. We need to find the partition sizes in bytes.
5. Run fdisk.
In your Terminal, run this command:
Where X is the number that you found out in the previous step. This should result in something like this:
#: id cyl hd sec - cyl hd sec [ start - size]
1: 0C 0 130 3 - 8 40 32 [ 8192 - 122880] Win95 FAT32L
2: 83 8 40 33 - 478 79 49 [ 131072 - 7553024] Linux files*
3: 00 0 0 0 - 0 0 0 [ 0 - 0] unused
4: 00 0 0 0 - 0 0 0 [ 0 - 0] unused
6. Time to truncate!
On your terminal,
cd to the directory that your image is. Then, write:
truncate FILE SIZE
FILE is your file of course.
SIZE is the size in bytes. What I did, is to add to the size column of the
fdisk command, the start column and multiply by 512. So, in my case SIZE will be: 512 * (7553024 + 131072) = 3934257152, which is approximately 3.9 GB.
You will need the start and size of the last partition that is shown by
fdisk. (Not necessarily the biggest, but the one in the end of the IMG file)
I experimented a bit, and whenever I entered 7553024 * 512 or (7553024
+ 1) * 512 bytes, the IMG file was corrupt. So, just to be sure, do it as I indicate above. It might add more than actually needed, but it is a safe option.
7. (Optional) Easily test if the IMG is not corrupt.
Go once more in the Disk Utility, and try to open the new IMG file as before. If it mounts, you can also see the new (smaller) size. If it doesn't mount, something went wrong.
(Maybe try to increase the size in the
This is not the best test, but it is a sure way to check if the new
IMG is corrupt or not. So, don't really count on it, but it is worth
I hope someone finds this helpful!