The Ultimate Guide to Cloning Disks on Linux with dd

DD - Disk Cloning
Ever found yourself staring at a failing hard drive, dreaming of moving your entire system to a shiny, new SSD without the pain of a fresh install? Or maybe you just upgraded your machine with a massive new drive and want to migrate everything over, but you’re stuck on how to do it without losing your mind.

You’re in luck! This guide will walk you through the entire process of cloning a disk using the legendary, albeit intimidating, dd command in Linux. We’ll cover the nitty-gritty details, show you exactly how to do it, and, most importantly, tackle the dreaded GPT partition size mismatch that so often trips people up when they're cloning a smaller drive to a larger one. By the time we’re done, you’ll be a disk-cloning wizard, ready to migrate your system with confidence.


First Things First: What You'll Need

Before we dive in, let’s make sure you have everything ready to go. You'll need:

  • Your Linux machine: This tutorial assumes you're running a Debian-based distro like Ubuntu or Kubuntu.
  • The source disk: This is the disk you want to clone from.
  • The destination disk: This is the new, blank disk you are cloning to.
  • A live USB or bootable media: Cloning a disk while it's in use is generally not a good idea. Booting from a live environment ensures that the source disk isn't being actively written to.
  • The gdisk utility: We'll be installing this later, but it's the key to fixing our partition size problem.

The dd Command: A Quick Introduction

So, what exactly is dd? Think of it as the ultimate byte-for-byte copy tool. It's a low-level utility that works directly on raw data, which is why it's so powerful and perfect for disk imaging and cloning. But with great power comes great responsibility—a single typo can lead to catastrophic data loss. Always, always double-check your commands.

The dd command is a Swiss Army knife for data manipulation, with use cases that include:

  • Cloning entire disks or individual partitions.
  • Creating bootable USB drives.
  • Backing up crucial partition tables like the MBR or GPT.
  • Creating raw disk images for forensic analysis or system recovery.

The Step-by-Step Cloning Process

Step 1: Identify Your Drives

This is the most critical step. You absolutely must identify your source and destination drives correctly. Open a terminal and run the lsblk command:

$ lsblk

This command will display a tree-like view of all the block devices on your system. Look for your source and destination disks by their size and mount points. For example, your source might be /dev/sda and your new, larger destination might be /dev/sdb. Write these down and commit them to memory. Don't mix them up!

Step 2: Clone the Disk

Now for the main event. We’re going to use dd to perform a full, sector-by-sector clone from the source to the destination. Make sure you are booted from a live USB so neither disk is in use.

$ sudo dd if=/dev/sda of=/dev/sdb bs=4M status=progress

if=/dev/sda: input file — This is your source drive.

of=/dev/sdb: output file — This is your destination drive.

bs=4M: block size — Setting a larger block size can significantly speed up the cloning process.

status=progress: This gives you a nice visual indicator of how the copy is proceeding.

⚠️ A Word of Warning: This command will completely overwrite all data on /dev/sdb without a second thought. If you get the if and of values swapped, you will destroy all data on your source drive. Seriously, take a deep breath and double-check those device names.

Once the command is complete, you'll have an exact replica of your old drive on the new one. But if the new drive is bigger, you'll notice it still reports the old, smaller size.

If you want to just clone partition you need to use partition name sda1/sdb2 like this

$ sudo dd if=/dev/sda1 of=/dev/sdb2 status=progress

The Bigger Disk Problem: Why Your New Drive is Acting Small

You’ve just cloned a 128 GB drive to a shiny 512 GB SSD. You boot up, and to your dismay, your system thinks the new drive is only 128 GB. What?

The dd command is a blunt instrument; it copies everything, including the partition table from the source drive. The partition table defines the size of the disk and its partitions. When dd copies the table from the 128 GB drive, the new 512 GB disk is told to operate as a 128 GB disk.

This is exactly where the dreaded "GPT PMBR size mismatch" error comes in. When you run a tool like gdisk, it sees the discrepancy between the physical drive size and the size defined in the partition table. Fortunately, this is an easy fix.

Fixing the GPT/PMBR Size Mismatch

Step 1: Install gdisk

Most Linux distributions don't come with gdisk pre-installed. It's a powerful tool for manipulating GPT partitions and is exactly what we need.

$ sudo apt update
$ sudo apt install gdisk

Step 2: Repair the GPT Partition Table

Now, let's unleash the magic of gdisk to fix the partition table.

$ sudo gdisk /dev/sdb

Replace /dev/sdb with the device name of your new disk. gdisk will immediately recognize the mismatch and give you a warning. It will ask you to choose which partition table to use, or if you want to fix it.

Type w and press Enter. This command tells gdisk to write the new, corrected partition table to the disk, which will fix the mismatch. You’ll be prompted to confirm; type y and hit Enter once more.

That’s it! The GPT and PMBR tables now correctly reflect the actual size of your new drive.

Expanding or Using the Extra Space

Your new drive is now correctly sized, but you’ll find that a large portion of it is unallocated. You now have two choices for how to use this space:

Option 1: Expand an Existing Partition

This is perfect if you want one big, seamless root partition. You can use a GUI tool like GParted or KDE Partition Manager for this, which makes resizing a breeze. Simply right-click on the partition you want to expand and resize it to take up the unallocated space.

Option 2: Create a New Partition

Maybe you want to have a separate partition for /storage or anything else on your new drive. This is the perfect opportunity.

  • Use GParted or the command-line tool fdisk or parted to create a new partition in the unallocated space.
  • Format the new partition with a filesystem, e.g., ext4 or NTFS.
  • Mount the new partition where you need it (e.g., /mnt/storage).
  • Optionally, add an entry to your /etc/fstab file to have it mount automatically on boot.

Your Cloning Journey is Complete

You've done it! You've successfully cloned your old drive to a new one using the dd command and you've even managed to fix the pesky GPT mismatch that plagues so many people.

The dd command is a phenomenal tool for disk migration and backups, but it demands respect and careful attention. Always double-check your if and of values, back up your critical data beforehand, and remember to fix the partition table if you're moving to a bigger disk.

Post a Comment

Previous Post Next Post