When creating images from existing ISOs you often need to allocate a number of megabytes for the image to at least fit the files That is in the ISO. Predicting the exact size of the image is hard and even for a program. In this case you'll create an image that's larger than actually needed:the image is much larger than the files on the Image is combined.
This post would show how to shrink an existing image to a more optimal size. We'll do the Linux, since all required tools is available there:gparted, Fdisk and truncate.
Requirements
- A Linux PC
- Some knowledge how the terminal works would helps
- The Unoptimal image (Myimage.img in this example)
Creating Loopback Device
GParted is a great application the can handle partition tables and filesystems quite well. In this tutorial we'll use GParted to shrink the filesystem (and its accompaning partition in the partition table).
GParted operates on devices, not simple files like images. This is why we first need to create a device for the image. We do this using the loopback-functionality of Linux.
First we'll enable loopback if it wasn ' t already enabled:
$ sudo modprobe loop
Now we can request a new loopback device:
$ sudo losetup-f
This would return the path to a free loopback device. In this example this is/dev/loop0.
Next we create a device of the image:
$ sudo losetup/dev/loop0 myimage.img
Now we had a device/dev/loop0 that represents myimage.img. We want to access the partitions that is on the image, so we need to ask the kernel to load those too:
$ sudo partprobe/dev/loop0
This should give us the DEVICE/DEV/LOOP0P1, which represents the first partition in Myimage.img. We don't need this device directly, but GParted requires it.
Resize partition using GParted
Next we can load the device using GParted:
$ sudo gparted/dev/loop0
This should show a window similar to the following:
Now notice a few things:
- There is one partition.
- The partition allocates the entire disk/device/image.
- The partition is filled partly.
We want to resize this partition so it's fits it content, but isn't more than than that.
Select the partition and click Resize/move. A window similar to the following would popup:
Drag the right bar to the left as much as possible.
Note that sometimes GParted would need a few MB extra to place some filesystem-related data. You can press the Up-arrow at the New Size-box a few times to doing so. For example, I pressed it ten times (=10MIB) for FAT32 to work. For NTFS "might not need" to "all".
Finally Press Resize/move. You'll return to the GParted window. This time it'll look similar to the following:
Notice that there was a part of the disk unallocated. This part of the disk is not being used by the partition, so we can shave this part off of the image later. GParted is a tool for disks, so it doesn ' t shrink images, only partitions, we had to do the shrinking of the image Oursel Ves.
Press Apply in GParted. It'll now move files and finally shrink the partition, so it can take a minute or both, most of the time it finishes Quic Kly. Afterwards close GParted.
Now we don ' t need the loopback-device anymore, so unload it:
$ sudo losetup-d/dev/loop0
Shaving the image
Now, we have all the important data at the beginning of the image it's time to shave of this unallocated part. We'll first need to know where our partition ends and where the unallocated part begins. We do this using fdisk:
$ fdisk-l myimage.img
Here we'll see a output similar to the following:
Disk myimage.img:6144MB,6144000000bytes12000000sectorsunits= Sectors of1* += +bytessector size (logical/physical): +Bytes/ +Bytesi/o Size (minimum/optimal): +Bytes/ +bytesdisk Identifier:0x000ea37dDevice Boot Start End Blocks Id systemmyimage.img12048 9181183 4589568b W95 FAT32
Note the things in the output:
- The partition ends on block 9181183 (shown under End)
- The Block-size is bytes (shown as sectors of 1 * 512)
We'll use these numbers in the rest of the example. The block-size is often the same, but the ending block (9181183) would differ for you. The numbers mean that the parition ends on byte 9181183*512 of the file. After that byte comes the Unallocated-part. Only the first 9181183*512 bytes would be useful to our image.
Next We shrink the image-file to a size that can just contain the partition. For this we'll use the truncate command (Thanks uggla!). With the truncate command need to supply, the size of the file in bytes. The last block is 9181183 and Block-numbers start at 0. That means we need (9181183+1) *512 bytes. This is important, else the partition would not fit the image. Truncate with the calculations:
$ truncate--size=$[(9181183+1) *+] Myimage.img
Now copy the new image through to your phone, where it should act exactly the same as the old/big image.
Shrinking images on Linux