3

I've got several dozen Raspberry Pis that I use as part of a home lab experiment. I created an installation of Rasbian (the Pi-specific version of Debian) on one Pi, and I want to spread it to the rest of the Pis.

The Pis use MicroSD cards (of which all of the ones I purchased are the same 8GB capacity and the same model number). Previously, I was using dd to create an image of the "gold master" MicroSD card, but that takes a long, long time, as it makes the byte-for-byte image of the 8GB SD card and has to write it all (even though the total used space on the card is maybe 300 megs or less).

There are 3 partitions; a boot partition (with the kernel), a root filesystem partition (with the bulk of the OS), and a third special partition.

Rather than using dd, I was wondering if there was perhaps a way to copy the partition table to a file, write the partition table back to a new SD card, and then use my trusty rsync commands to copy just the data itself back to each partition.

Is this possible? Or am I better off just using parted and try and script that to replicate the partition tables?

Thanks!

asked Nov 12, 2014 at 21:52

5 Answers 5

5

Even tough you explicitly asked about "...copy and write a partition table..." I think you really want:

  • to clone your gold-master image to other brand new, identical, SD-cards:
  • to avoid "dd-ing" the whole 8GB source sd-card, as it contains only 300 MB of data.

In such a case there are alternatives to "dd", that can do exactly what you need: disk-cloning, but considering file-system properties/structure/boundaries.

One of such alternatives is partimage [1]: "...It saves partitions having a supported filesystem on a sector basis to an image file [...] Partimage will only copy data from the used portions of the partition..."

Please note that partimage can also be used in a client-server network environment and that it's included in several live-linux-CD, like SystemRescueCD [2] (so to easier the process of cloning system disks).

Another tool you might want to check is FSArchiver.


[1] http://www.partimage.org/Main_Page

[2] http://www.sysresccd.org/SystemRescueCd_Homepage


P.S.: sorry for not posting other relevant URLs but... this is my first answer and I cannot include more than 2 URLs

answered Nov 13, 2014 at 0:04
3

Sure – you can do that using dd. The MSDOS aka MBR partition table is in the first 512 bytes:

head -c 512 /dev/sdb > mbr.bin
cp mbr.bin /dev/sdc
partprobe
dd if=/dev/sde bs=512 count=1 of=mbr.bin
dd if=mbr.bin of=/dev/sdf
partprobe

However, this will not replicate the actual filesystem structure; you will have to run mkfs on the blank partitions anyway.

You might prefer scripting the sfdisk or parted tools to create partitions of specific sizes instead.

answered Nov 12, 2014 at 21:57
3

You can speed dd up by increasing the block size with the bs flag. I usually use the following: dd if=/dev/source of=/dev/destination bs=8M. In all honesty, dd or parted are your best options, dd being the superior option for quality (in my opinion).

flaviut
2991 gold badge3 silver badges8 bronze badges
answered Nov 12, 2014 at 21:58
3
  • Not an answer to the question, but it should solve the problem Commented Nov 12, 2014 at 23:26
  • Be aware, that for some partition table types you can end up with two devices with the same UUID, which isn't a disaster, but also isn't ideal. I always just copy it by hand. Commented Nov 12, 2014 at 23:35
  • Correct @rrauenza, however changing the UUID is usually simple using blkid, uuidgen and tune2fs. Example here. I'm sure there are cases where this will not work, but I have not encountered them yet. Commented Nov 14, 2014 at 20:01
1

(A duplicate of https://unix.stackexchange.com/questions/12986/how-to-copy-the-partition-layout-of-a-whole-disk-using-standard-tools)

I personally prefer not to use dd to avoid duplicating UUIDs.

Use sfdisk:

 -d, --dump
 Dump the partitions of a device in a format that is usable as input to sfdisk. For example,
 % sfdisk -d /dev/hda > hda.out
 % sfdisk /dev/hda < hda.out
 will correct the bad last extended partition that the OS/2 fdisk creates.

Note this doesn't work with GPT -- but ... you can use sgdisk instead:

sgdisk -R=/dev/sdb /dev/sda # copy the table
sgdisk -G /dev/sdb # randomize the GUIDs
answered Nov 12, 2014 at 23:41
0

(I would have commented on @ray's answer above if I had enough rep)

If you are happy that the data is at the start of the SD card then just use count option of dd.

To add to @ray's example:

dd if=/dev/source of=/dev/destination bs=8M count=38

38 blocks of 8MB each is 304MB. If you are using an intermediate file then this would be 304MB in size and when writing back you can omit the count option.

dd if=/dev/source of=~/intermediate_file bs=8M count=38
dd if=~/intermediate_file of=/dev/destination bs=8M

The intermediate file will have the added advantage that it will be quicker (assuming you save the intermediate file on a HDD or SSD) as there will be no slow reading from the SD card each time you clone.

answered Dec 30, 2015 at 12:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.