“Can’t have a partition outside the disk!” for vdi created from a truncated image

disk-imagefdiskgpartedpartitionvirtualbox

I have used dd to create a partial/truncated image of a disk which only includes allocated spaces, then converted it to a vdi file for VirtualBox. I have successfully used the vdi image to run the Linux (Ubuntu) which was installed on the disk.

Now I wan't to run GParted to remove partitions which are located before the Linux partition (so that I can create a smaller image). But GParted doesn't work. It shows entire disk as unallocated. How can I fix it?

Additional info:

sudo parted /dev/sda unit s print

Error: Can't have a partition outside the disk!                           

sudo fdisk -l -u /dev/sda

Disk /dev/sda: 97.7 GB, 97729379840 bytes
255 heads, 63 sectors/track, 11881 cylinders, total 190877695 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 identifier: 0x3c36156a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2459647     1228800    7  HPFS/NTFS/exFAT
/dev/sda2         2459648   125339129    61439741    7  HPFS/NTFS/exFAT
/dev/sda3       125339646   190877695    32769025    5  Extended
/dev/sda5       125339648   190877695    32769024   83  Linux

sudo sfdisk -uS -l /dev/sda

Disk /dev/sda: 11881 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sda1   *      2048   2459647    2457600   7  HPFS/NTFS/exFAT
/dev/sda2       2459648 125339129  122879482   7  HPFS/NTFS/exFAT
/dev/sda3     125339646 190877695   65538050   5  Extended
/dev/sda4             0         -          0   0  Empty
/dev/sda5     125339648 190877695   65538048  83  Linux

sudo sfdisk -d /dev/sda

Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=     2048, size=  2457600, Id= 7, bootable
/dev/sda2 : start=  2459648, size=122879482, Id= 7
/dev/sda3 : start=125339646, size= 65538050, Id= 5
/dev/sda4 : start=        0, size=        0, Id= 0
/dev/sda5 : start=125339648, size= 65538048, Id=83

Best Answer

Perhaps the easiest solution would be to increase the size of the image to make gparted happy again. This most probably is a one-off kind of issue, so increasing the image size by a couple of additional megabytes could be enough:

dd if=/dev/zero of=newimage.raw bs=1M count=102400
qemu-img convert -f vdi oldimage.vdi -O raw oldimage.raw
dd if=oldimage.raw of=newimage.raw count=190877695 conv=notrunc
qemu-img convert -f raw newimage.raw -O vdi newimage.vdi

Or simply:

cp oldimage.vdi newimage.vdi
VBoxManage modifyhd /path/to/newimage.vdi --resize 102400

Then you should retry to run gparted on newimage.vdi. If it still complains, you may try to increase the size of the image to the original disk size, provided you have enough space.

Related Question