8

I have taken a complete image of a hard drive using:

dd if=/dev/sda of=/home/user/harddriveimg bs=4M

It would seem to me, that I should be able to re-size the partitions within it after suitably mounting it.

As I am less than familiar with the command line parted, I tried:

gparted /home/user/harddriveimg

While this loaded the partition table, it couldn't find the partitions themselves, e.g. harddriveimg0.

Is it possible to modify an image file like this, without writing it back to some disk, and if so how? I would be perfectly happy with a solution that uses only terminal commands.

Timesquare
  • 183
  • 2
  • 1
    If you want to access individual partitions from your disk image file as block devices (in order to, e.g., mount them), look into [kpartx](http://linux.die.net/man/8/kpartx). – Celada Dec 26 '12 at 21:11
  • @Celada, That looks like a good solution! Is kpartx just a wrapper for partx? – Timesquare Dec 26 '12 at 21:28
  • I believe that `partx` only works with real disks, because it uses the kernel-based partition detection magic whereas `kpartx` uses device-mapper, a generic and more powerful mechanism for creating block devices in the kernel. – Celada Dec 26 '12 at 21:40
  • @Celada, `kpartx` does not use the kernel for partition table detection, but it does inform the kernel about them so it requires a partionable device, such as a loop device. – psusi Dec 26 '12 at 23:38

1 Answers1

3

You need to associate a loopback device with the file:

sudo losetup /dev/loop0 /home/user/harddriveimg

Then run gparted on that.

psusi
  • 17,007
  • 3
  • 40
  • 51
  • This, unfortunately does not work. Gparted is still looking for /dev/loop0p1 for the first partition listed in the partition table and so on. – Timesquare Dec 27 '12 at 15:39
  • @Timesquare, then it appears that your libparted is broken. What version are you using and what distribution? – psusi Dec 27 '12 at 16:31
  • My libparted version is 2.0.0 on Archlinux. I believe the issue is in Gparted's method of picking the block device that is associated with a given partition record. (Gparted is version 0.14.1) – Timesquare Dec 27 '12 at 23:50
  • @Timesquare, no, that is the correct block device for the partition. You need parted 3.0 or later. – psusi Dec 28 '12 at 00:31
  • If I understand what you are saying correctly, the problem lies elsewhere. After using losetup as you suggested, the block devices /dev/loop0p1, /dev/loop0p2... do not exist. – Timesquare Dec 28 '12 at 01:40
  • @Timesquare, you can run `partx` to detect and create the partitions, and a `gparted` will create them when you use it to add partitions if you have a newer version of parted. As a workaround, you can use `partx`, you just have to let gparted stop after creating the partition so you can run `kpartx` before formatting. – psusi Dec 28 '12 at 01:51