11

I have a disk image, it's a "whole" disk image, e.g., contains multiple partitions, and I want to clone just one of them (not the first one..) onto a partition on an external drive with multiple partitions on it (I'm also not cloning it onto the first partition of the disk...)

FDisk'ing the image gives this:

# fdisk -l 2013-02-09-wheezy-raspbian.img 

Disk 2013-02-09-wheezy-raspbian.img: 1939 MB, 1939865600 bytes
255 heads, 63 sectors/track, 235 cylinders, total 3788800 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: 0x00014d34

                         Device Boot      Start         End      Blocks   Id  System
2013-02-09-wheezy-raspbian.img1            8192      122879       57344    c  W95 FAT32 (LBA)
2013-02-09-wheezy-raspbian.img2          122880     3788799     1832960   83  Linux
#

and the block device looks like this:

# fdisk -l /dev/sdc

Disk /dev/sdc: 8014 MB, 8014266368 bytes
247 heads, 62 sectors/track, 1022 cylinders, total 15652864 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048      131071       64512    e  W95 FAT16 (LBA)
/dev/sdc2          131072    15652863     7760896   83  Linux
#

I want the second partition of the image to replace the second partition of the block device. Don't worry about the trailing corrupted free space, I'll use GParted to clean that up, and I need it for something else anyway.

1
  • specify exactly where you want your partition to be cloned and where your image files are!
    – fromnaboo
    Apr 21, 2013 at 10:15

6 Answers 6

12
# losetup --find --show --partscan --read-only 2013-02-09-wheezy-raspbian.img
/dev/loop7
# dd if=/dev/loop7p2 of=/dev/narnia bs=1M

If --partscan doesn't work, you can also use one of:

# partx -a /dev/loop7
# kpartx /dev/loop7

or similar partition mapping solutions.

You should probably mount it first just to see if it's the right thing or what.

Of course you can also read the fdisk output and give dd the skip=131072 or whatever directly, i.e. make it skip that many blocks of input so it starts reading at where the partition is located; but it's nicer to see actual partitions with a loop device.

8
  • Your losetup command didn't work - it said unrecognized option --find. I removed that, it said it didn't recognize --show. Taking that off caused it to complain about --partscan, at which point I realized that command wasn't going to do anything for me anytime soon. I decided to try the others, but both partx and kpartx return "command not found". Apr 20, 2013 at 22:45
  • I would just run dd if=2013-02-09-wheezy-raspbian.img of=/dev/sdc skip=122880, but that returns dd: ‘2013-02-09-wheezy-raspbian.img’: cannot skip: Invalid argument. Apr 20, 2013 at 22:49
  • 1
    skip depends on bs, so if you used bs=1M (or have an alias that does this), that'd explain why it doesn't work. try with bs=512. Also, why 122880 when fdisk says 131072? Also my losetup is losetup from util-linux 2.22.2. same for partx. kpartx is from multipath-tools (used device mapper). Apr 20, 2013 at 23:49
  • kpartx is for LVM. partprobe will also do the job, from the parted package.
    – psusi
    Apr 21, 2013 at 2:18
  • @frostschutz That was 12280 bytes from the start of the image file. Apr 22, 2013 at 21:30
0

Something like this should work:

dd if=/path/to/2013-02-09-wheezy-raspbian.img2 of=/dev/sdc2 bs=4096 conv=notrunc,noerror

if= sets the image you want to clone

of= sets the target partition where you want to clone the image

4
  • 1
    I got the expected error: dd: opening ‘2013-02-09-wheezy-raspbian.img2’: No such file or directory Apr 20, 2013 at 22:31
  • did your replace /path/to/2013-02-09-wheezy-raspbian.img2 with your real path to the image file? :|
    – fromnaboo
    Apr 21, 2013 at 8:39
  • 1
    ****facepalm**** Yes, I just appended the 2 to the end, like you said to. Apr 22, 2013 at 21:28
  • 1
    this doesn't work, dd doesn't see partitions on an image file like that Sep 5, 2019 at 10:00
0
  1. Mount the image as a loopback device: losetup -fP yourImage.img
  2. You may now access each partition of the image directly. If you must use dd, you can do dd if=/dev/loop0p1 of=image.img, but I think that you would like cat better. In a root shell, simply do cat /dev/loop0p1 > image.img. Both examples assume that you want the first partition, and the loopback device you were assigned was /dev/loop0
  3. Once you are done with the loopback device, you can detach it with losetup -d /dev/loop0. Once again, this assumes that /dev/loop0 is your loopback device.
0

You could do:

sudo partx --add -v 2013-02-09-wheezy-raspbian.img 
# time sudo dd if=/dev/loop0p1 |pv -s 80M |sudo dd of=/dev/sdc1
time sudo dd if=/dev/loop0p2 |pv -s 4G |sudo dd of=/dev/sdc2
sudo partx --delete -v /dev/loop0
0

You can copy just the second partition to a separate image with this

dd if=2013-02-09-wheezy-raspbian.img of=second.img bs=512 skip=122880

and after that you can copy it in place of the partition on the disk

dd if=second.img of=/dev/sdc2 bs=512

or you can even do it in one command

dd if=2013-02-09-wheezy-raspbian.img of=/dev/sdc2 bs=512 skip=122880

Just make sure that you unmount /dev/sdc2 before you write to it.

0

Here is the easiest solution :

dd skip=122880 count=1832960 status=progress if=2013-02-09-wheezy-raspbian.img  of=secondpartition.img

dd skip={Partation Start} count={number of Blocks} status=progress if={Disk image}  of={Partition output file}

as it shows in the output of fdisk, fdisk is using 512 bytes as the default block size:

# fdisk -l 2013-02-09-wheezy-raspbian.img 

Disk 2013-02-09-wheezy-raspbian.img: 1939 MB, 1939865600 bytes
255 heads, 63 sectors/track, 235 cylinders, total 3788800 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: 0x00014d34

and as it says in the dd command help, 512 bytes is the default value for the switches "bs", "cbs", "ibs" and "obs"

      bs=BYTES        read and write up to BYTES bytes at a time (default: 512);
                  overrides ibs and obs
  cbs=BYTES       convert BYTES bytes at a time
  conv=CONVS      convert the file as per the comma separated symbol list
  count=N         copy only N input blocks
  ibs=BYTES       read up to BYTES bytes at a time (default: 512)
  if=FILE         read from FILE instead of stdin
  iflag=FLAGS     read as per the comma separated symbol list
  obs=BYTES       write BYTES bytes at a time (default: 512)
  of=FILE         write to FILE instead of stdout
  oflag=FLAGS     write as per the comma separated symbol list
  seek=N          skip N obs-sized blocks at start of output
  skip=N          skip N ibs-sized blocks at start of input
  status=LEVEL    The LEVEL of information to print to stderr;
                  'none' suppresses everything but error messages,
                  'noxfer' suppresses the final transfer statistics,
                  'progress' shows periodic transfer statistics

to confirm that you can use dd itself to create a one block file like this:

dd if=/dev/urandom of=test count=1

The file size should be 512 bytes.

So all you have to do to create an image of a partition or a group of partitions is to get the number of sectors for the start of the partition or first partition from the output of fdisk and use it as the value of the dd parameter "skip" and then the number of sectors for the End of the partition or last partition and set it as the value for the dd parameter "count".

2
  • Don't I have to care about overlaps and things? I do not understand "count=3788799". Shouldn't it rather be "count=1832960"? This could mean you never tried it IRL ;-) May 20 at 9:43
  • You are right opinion_no9 i just fixed it May 21 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .