The answer to the question will depend on how exactly you copied the partitions.
If you copied the whole drive that the partitions were on (like with dd if=/dev/mmcblk0 of=./sd.img
), then you can just flash the single .img file to the new card and then change the partition and filesystem sizes as you see fit. However, that doesn't sound like the situation you're faced with.
I believe from your post that you did something more along the lines of dd if=/dev/mmcblk0p1 of=./sd_part1.img; dd if=/dev/mmcblk0p2 of=./sd_part2.img
. If that's the case, you have two partition images, not a drive image. The two are somewhat similar, but work differently.
The following is a quick explanation of drive partitioning for those who don't know - if you already know this, skip to the "The Problem" section.
A drive basically acts as a big block of space that your system can access. Technically you can just put a filesystem directly onto a drive (and I have done so in the past), however doing this makes it so that you can only have one filesystem on the drive. This is frequently insufficient for many uses, and so rather than formatting the drive directly, you will usually partition the drive first. Partitions are basically chunks of space that act as their own "mini" drives.
In order for the system to know where a partition stops and where it ends, there is some data stored directly on the drive, outside of all of the partitions, that stores the data about the partitions. This data is called the partition table.
Here's a quick visualization of what that looks like.
(Simplified GPT partition setup illustration)
The Drive (/dev/sda)
|---------------------------------------------|
|---------------|/dev/sda1 |/dev/sda2 |/dev/sda3 |----------------------|
|Partition table|Partition 1|Partition 2|Partition 3|Backup partition table|
The Problem
If you copy the whole entire drive, you capture the partition table and all of the partitions. If you copy each of the individual partitions, you lose the partition table and will have to recreate that from scratch.
So if you took an image of each individual partition, this is where the tricky part comes out - you have to repartition the drive such that you have one partition per .img file you intend to restore. Each partition must be as large or larger than the original partition size so that you can flash each partition image into the newly created partition.
Solution
Note that the following is a general guide - you may have to tweak this to work for your particular setup.
Also, we will be assuming that your SD card is at /dev/mmcblk0 - if it isn't, replace all occurrences of /dev/mmcblk0 in this guide with the actual path to the SD card.
Note that if your SD card is at /dev/sdX (where X is any letter), the partitions will be named things like /dev/sdX1, /dev/sdX2, etc. This is slightly different than how /dev/mmcblkX works, where partitions are named /dev/mmcblkXp1, etc. Take this into account or things may not work right.
Firstly, if there are any partitions already on the new SD card, unmount them (you can find out if you have mounted partitions on the SD card by running lsblk
). Assuming that your SD card is at /dev/mmcblk0, you can do this with the following Bash one-liner: for i in 1 2 3 4 5 6 7 8 9; do sudo umount /dev/mmcblk0p$i; done
If you get any "target is busy" messages when you do this, close any open programs that may be accessing the SD card. It's normal to see a lot of "no mount point specified" messages.
Once all existing partitions are unmounted, it's time to partition the new SD card. I personally prefer using fdisk
for this - if you're already familiar with a different partitioning utility, you can use that instead. run sudo fdisk /dev/mmcblk0
to begin the partitioning process.
Once inside fdisk, you will need to know whether your SD card was originally partitioned using the MBR or GPT partitioning scheme. If you don't know, try to figure out which one to use based on the use you have for the SD card. And if that's not helpful, assume that GPT is the right partitioning scheme to use and hope for the best.
If you are using GPT, type g
and press Enter - this will make a new GPT partition table on the SD card. If using MBR, type o
and press Enter.
Next, make the first of the two partitions. Type n
and press Enter. Accept all of the defaults until you are asked for the "Last sector" - this is where you get your chance to specify the partition size.
Once you're asked for the last sector, determine how large your first partition is - we'll assume that it's 16 GiB. Make your new partition the same size or slightly larger if the filesystem on the first partition can be extended. To make a new 16 GiB partition, type +16G
and press Enter. If you want to make it 17 GiB instead, type +17G
rather than +16G
.
(Note that you should only use a single "G" at the end - if you use "GB", fdisk will make your new partition a bit smaller than you probably expect, due to the difference between straight and binary gigabytes.)
With that done, type n
and press Enter again. This time just accept all of the default settings you are presented with. This will make the last partition fill the entire rest of the SD card.
With that done, type w
and press Enter one last time - this will write your changes to the SD card.
With that done, you should now have /dev/mmcblk0p1 and /dev/mmcblk0p2 partitions. You can now use dd
to flash each individual .img file to the corresponding partition. You can do this with something like sudo dd if=./sd_part1.img of=/dev/mmcblk0p1 bs=4M
for the first partition and sudo dd if=./sd_part2.img of=/dev/mmcblk0p2 bs=4M
for the second partition. (The bs=4M
part causes dd
to write to the target drive in 4 megabyte chunks, which may speed up the write process.)
We're not quite done - the partitions we've made are probably larger than the filesystems that we have now put inside them. The last step will be to resize the filesystems to fill the partitions, allowing you to take advantage of the extra space. How to do this will vary depending on the filesystem type (and may not be possible at all), but for ext4 filesystems, you can do this with sudo resize2fs /dev/mmcblk0pX
, replacing X with the partition number you wish to target.
With all that done, your cloned SD card should be very close to done. If anything you're using depends on the UUIDs of the partitions, you may have to tweak configuration files so that they reference the new partitions rather than the old ones. Once you have that finished, you're done!