3

I've got a partition P1 (which contains my Linux OS) on a drive A.

I've just gotten a completely new drive B (that is larger than partition P1 AND the entire drive A).

I'd like to copy across the partition from drive A to drive B, and possible resize it later on.

Can this be done with dd? I could easily create a new parition table on drive B, and just cp the files across - but this seems like it might be slightly slower due to the filesystem overhead.


Output of `parted --list`:

It would be the partition 4 that I want to copy to another drive.

Model: ATA Samsung SSD 850 (scsi)
Disk /dev/sdb: 250GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name                  Flags
 1      17.4kB  1049kB  1031kB               BIOS boot partition   bios_grub
 2      1049kB  1074MB  1073MB  fat32        EFI System            boot, esp
 3      1075MB  183GB   181GB   ext4         Linux filesystem
 4      183GB   250GB   67.5GB  ext4         Basic data partition

Please ignore any reference to the boot drive / functionality (which I'll worry about later on) - keeps this question concise.

2 Answers2

4

Yes ,that's what dd is for. Assuming:

  • sxb is the drive to copy from
  • sxc is the drive to copy to
  • sxb4 is the fourth partition on the second drive that you want to copy from
  • sxc1 is the partition you've created to be of equal size to sxb4

do :

    parted /dev/sxc
    GNU Parted 3.2
    Using /dev/sxc
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mklabel GPT
    Warning: The existing disk label on /dev/sxc will be destroyed and all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes
    (parted) mkpart primary 0.0 67.5GB
    (parted) quit

    dd if=/dev/sxb4 of=/dev/sxc1 bs=16M

As that will duplicate the UUID of the partition, in order to change the UUID of the new partition (make sure the partition is not mounted) run the following:

e2fsck -f /dev/sxc1
tune2fs /dev/sxc1 -U random

if sxbis an old drive and you expect it to have read errors, use ddrescue instead.

Note: as dd is known as disk destroyer, and creating a partition table is dangerous, not putting the exact command in the above as some random idiot on the Internet might see this question and copy-paste the codez without understanding what it does...

Fabby
  • 5,384
  • Good to hear, I guess the only issue is I'm not sure how to create a partition with the exact same size? Is the 'size' the 'difference' between the start/end blocks of the partition? – Chris Stryczynski Nov 21 '18 at 21:03
  • 1
    Yes, correct. And if you edit your question and provide the output of parted --list and leave another comment, I'll add that bit too. 0:-) – Fabby Nov 21 '18 at 21:11
  • 1
    Urgh. Sorry: need it in bytes to be exact... sudo parted /dev/sdb unit B p @ChrisStryczynski – Fabby Nov 21 '18 at 21:41
  • I've just done it! Thanks for pointing me in the right direction though. – Chris Stryczynski Nov 21 '18 at 21:44
  • Just a note that this seems to have duplicated the UUID of the partition - I'm not sure if this is any reason for concern though? – Chris Stryczynski Nov 21 '18 at 21:45
  • Not if you delete the old partition OR change the UUID on the new one: uuidgen and tune2fs for the latter. @ChrisStryczynski – Fabby Nov 21 '18 at 21:50
1

GParted can create and manipulate partitions. If you are using a Linux OS then you can also use GParted to achieve this goal entirely in a graphical user interface. In my case I had a few partitions on one drive and I wanted to back up just some of those partitions to another drive.

I attached drive 1 and drive 2 to my PC running Linux OS. Drive 2 had just one big partition on its whole disk. Using Gparted I resized that partition to free up space, enough to contain all the partitions from drive 1 that I wanted to back up. Then I created an "Extended" partition from the now unallocated space.

Then using GParted I selected drive 1 and clicked on a partition I wanted to copy... Partition Menu > Copy then switched to drive 2 then highlight the extended partition then Partition Menu > Paste. I repeated this for all the partitions I wanted to back up into the "Extended partition" on drive 2.