I am backing up data from an old hard drive one partition at a time to .img files using dd.

sudo dd if=/dev/sdc5 of=/home/flex/data/partition.img bs=1M status=progress

This gives me a 10GB .img file but I know there is only 3GB data on the partition.

The partition is FAT32 and contains a Windows XP OS.

I loop mount the .img file... it appears at /dev/loop7

sudo losetup --find partition.img

Then open in GParted..

sudo gparted /dev/loop7

Now in Gparted when I go to resize the partition it appears to work and gives no errors but I don't think it actually changes anything. The .img file is still 10GB and this command shows that the size of the partition is still 10GB..

sudo parted /dev/loop7 unit s print free

I thought there would unallocated partition space that I could then do...

truncate --size=$[(End Sector+1)*512] partition.img

What am I doing wrong?

I guess my alternative is to TAR the whole partition.img and then extract to a blank .img file but I want to know if this method with GParted is possible because it seems like a convenient way to resize a .img file for storage.




My problem with GParted not re-sizing the partition was because of the way I was extracting each partition individually from my old hard drive each into a separate .img file for each partition.

sudo dd if=/dev/sdc5...

This was giving me a partition.img file that had no partition table or partition either, just the filesystem.

So now I follow these steps instead...

-- Create a blank .img file that is large enough to contain the partition you extract from the old HDD. E.g: 12GB.

sudo dd if=/dev/zero of=Win-XP-New-12G.img bs=1M count=12000

-- Loop mount that file and find out what loop device it got with lsblk (/dev/loop7 in this case)

sudo losetup --find Win-XP-New-12G.img

-- Create a partition table in the 12GB .img file then create a partition then format that partition as fat32 (the partition I was archiving from the old HDD is FAT32)

sudo parted -s /dev/loop7 mklabel msdos
sudo parted -s /dev/loop7 mkpart primary fat32 2048s 100%
sudo mkfs -t vfat /dev/loop7p1

-- With the old HDD connected to my Ubuntu laptop via USB I find the device file of the partition I want using lsblk -- Copy the data from the HDD partition /dev/sdc5 DIRECTLY INTO the PARTITION in the 12GB .img file. e.g: /dev/loop7p1 below

sudo dd if=/dev/sdc5 of=/dev/loop7p1 bs=1M status=progress

-- Now open the device file that points to the 12GB .img file in GParted to re-size the partition to make it smaller. This time the GParted re-size worked and later I truncated the .img file Great help from here.

sudo gparted /dev/loop7

I did not go with the tar or rsync commands because they do not preserve accessed or create timestamps when copying files to a Fat32 filessytem.

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.