This article we discuss a procedure on how to shrink USB image made by dd
command. Here is the example scenario. You have made four partitions with a total disk space of 3GB:
# sfdisk -l -uM ubuntu_USB.img sfdisk: Disk ubuntu_USB.img: cannot get geometry Disk ubuntu_USB.img: 950 cylinders, 255 heads, 63 sectors/track Units: 1MiB = 1024*1024 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End MiB #blocks Id System ubuntu_USB.img1 1 50 50 51200 b W95 FAT32 ubuntu_USB.img2 51 150 100 102400 b W95 FAT32 ubuntu_USB.img3 * 151 2650 2500 2560000 b W95 FAT32 ubuntu_USB.img4 2651 3000 350 358400 b W95 FAT32
This partition table exists on your USB drive and with dd
command you have made a clone copy to a file called usb_dd.img
. However, the output dd
image file has size equal to the size of your USB memory stick rather than to have a size of sum of all partitions. One block is equal to 1MB ( 1024 * 1024 ) bytes, that is, the expected size should be 3000MB. However, instead we get 7.3G. This is because dd
command will clone entire block device by default while disregarding entire partition layout:
# ls -lh ubuntu_USB.img -rw-rw-r--. 1 lubos lubos 7.3G Jan 24 11:37 ubuntu_USB.img
Next, we need to get a total number of sectors in order to remove the excess disk size from this image. To get this information run:
# fdisk -l -u ubuntu_USB.img Disk ubuntu_USB.img: 7818 MB, 7818182656 bytes, 15269888 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 Disk label type: dos Disk identifier: 0x000804a3 Device Boot Start End Blocks Id System ubuntu_USB.img1 2048 104447 51200 b W95 FAT32 ubuntu_USB.img2 104448 309247 102400 b W95 FAT32 ubuntu_USB.img3 * 309248 5429247 2560000 b W95 FAT32 ubuntu_USB.img4 5429248 6146047 358400 b W95 FAT32
The last sector is 6146047. Next ,use dd command to create another image but only up to the last sector:
# dd if=ubuntu_USB.img of=ubuntu_USB.iso count=6146047 ALTERNATIVELY CLONE DIRECTLY FROM USB eg.: # dd if=/dev/sdX of=ubuntu_USB.iso count=6146047
The resulting image will have a correct image size based on the sum of its partition(s).
$ ls -lh ubuntu_USB.iso -rw-r--r--. 1 root root 3.0G Jan 24 11:58 ubuntu_USB.iso