Block level full disk cloning using dd in Linux

article-featured-image

Taking a backup of a system is very sensitive and critical process and sometimes It can be tough to maintain the current file structure in the backup. A little misconfiguration or wrong option can cause some serious issues. This is why this process should always be handled with much care.

In this article, I'll show you how you can take the backup of your entire disk with just one command. I'll be discussing how to use dd command to perform block-level cloning of a disk. Block level cloning is the method of copying data bit by bit. It simply means that It clones everything from the source disk including all unused blocks, used data blocks, boot records, and all partitions and their file-systems.

Full disk cloning using dd

dd stands for disk duplication. It is widely used for various purposes including ISO burning, block-level cloning, blank file creation, and partition formatting. Before we start with the process, I advise you to take a snapshot of your source disk.

For our purpose of copying the entire source disk to another disk, we need to perform a copy action on the root partition. It's neither a good idea nor recommended to do this while the root partition is mounted. We have to boot from a USB drive. Follow Create bootable USB if you don't know how to. I'm using "Ubuntu Desktop" to boot from USB but you can use any Debian distribution. After booting from the USB drive, run this command in the terminal with all these options:

$
sudo dd if=/dev/sda of=/dev/sdb conv=noerror,sync bs=1M status=progress
Let's break down what each option means:
  • sudo dd is no doubt the command name. sudo is used to escalate privileges for the process because we need root privileges to copy the content of the root directory and disk partitions.
  • if=/dev/sda used to specify source disk. Here, if= stands for "input file" and /dev/sda is the location of our source disk. If you are familiar with the Linux architecture, you will know that everything is a file in Linux. You must change it with your source disk path.
  • of=/dev/sdb used to specify the destination disk. Here, of= stands for "output file" and /dev/sdb is the location of destination disk where we want to copy the data of source disk. You must change it with our destination disk path.
  • conv=noerror,sync is specifying set of conversion flags. conv= stands for "conversion" which is used to instruct how data should be processed while copying from the source disk to the destination disk. noerror flag tells dd to continue the process even after an error while reading the block on source disk. It will replace non-readable data blocks with zeros. sync flag make sure that the block size remains the same if the block reading process encounters an error.
  • bs=1M is specifying the size of single batch. bs stands for "block size" and 1M is size of the block which is equivalent to 1 MB. This option instructs dd to read this much size of data blocks from the source disk and write to the destination disk in the same block size. Lower block size means slow progress & higher efficiency with less memory usage and higher block size means fast progress & less efficiency with higher memory usage in the process.
  • status=progress is used to show the status of the process. It will show how much data has been copied from the source disk to destination disk at what rate.

After the cloning process is completed, you'll have your second disk as same as the source disk. You must detach any one disk from the system and keep it as a backup while using the other disk in the system.

Fix duplicate UUID after dd

If you boot the system with both disks attached, you'll most likely encounter duplicate UUID error. By performing full clone using dd, all file systems and partitions UUIDs are also replicated. Use sudo blkid to see all UUIDs in the system. You will notice that the source partition and destination partition UUID are exactly the same. Due to this reason, bootloader got confused in which one to use and results induplicate UUIDs error.

If you are struggling with it, then you can fix it by assigning a new UUID to the appropriate file system and partition. UUID stands for Universal Unique Identifier which is an 128-bit value assigned to an object in Linux. Make sure you are booting from a USB drive. Perform these commands carefully as one mistake might cause some serious issues in the disk. To assign a new UUID, there are different commands for different resources.

Assign new UUID to a GPT partition

Let's say we want to assign a new UUID to /dev/sda1 partition. First use sudo blkid command to check the file-system type of all objects and their respective UUIDs. You will notice that both /dev/sda1 and /dev/sdb1 have same UUID. Some partition does not have a file system and hence only a single UUID.

To change the UUID of such partition, use the below command:
$
sgdisk -u 1:$(uuidgen) /dev/sda

Here sgdisk is command line tool that we are using for assigning new UUID to /dev/sda disk. 1: is used to specify the partition number in the disk, which in our case is 1 (/dev/sda1). uuidgen is another tool that generates a unique value that we will assign as a new UUID to the partition.

After this command, check UUID of /dev/sda1 partition, which will now be different from /dev/sdb1. This tool does not work for swap partitions. It only works for GPT partitions.

Assign new UUID to a file system

Let's say our root file system is on /dev/sda3 partition. After checking file system type using sudo blkid command, we found out that the file system is ext4 type. You may notice that if the partition has a file system, then there are two UUIDs present in the object, one is for the system, and the other PARTUUID is for the partition itself.

To fix the duplicate UUID issue in the partition containing file system, It's best if we change both PARTUUID & UUID. Use the below commands to generate a new value and assign it to the file system and partition:

$
sudo sgdisk -u 3:$(uuidgen) /dev/sda && sudo tune2fs -U $(uuidgen) /dev/sda3

This command will assign new UUIDs and assign them to file system and partition respectively. sgdisk tool is used for partition (it must be a GPT partition to use this command) and tune2fs tool is used for file-system. Keep in mind that tune2fs can also be used to assign new UUID to ext2 and ext3 file-systems also.

Assign new UUID to a swap partition

To assign new UUID to a swap partition, we will use a different command. Let's suppose we have /dev/sda2 swap partition. It's UUID is similar to /dev/sdb2 swap partition. If there is no PARTUUID then use the below command to assign new UUID:

$
sudo mkswap -U $(uuidgen) /dev/sda2
For some unknown reason, this command can also be used to change UUID of LVM2_MEMBER type file-system.

Assign new UUID to MBR partition

If your system is using an MBR partitioning scheme then the method is different from GPT to change the UUID of partition. The UUID value of MBR partition is smaller as compared to GPT partition. Suppose we have /dev/sda4 MBR partition then we have to use sudo fdisk /dev/sda4 command to change the UUID value. After executing the command, follow the below steps:

  • Input p to see partition & disk information. From here, note down the Disk identifier value which will be something like 0x878d5e1f.
  • Input x to use the utility in expert mode.
  • Input i to enter new disk identifier. Type the disk identifier value from the previous step by incrementing the last digit. So in our case, It'll be 0x878d5e1g.
  • Input r to return to the main menu after changing disk identifier value.
  • Input w to write the changes made to the disk and exit.

After this process, use sudo partprobe /dev/sda4 to rescan the partition. Now check the UUID of /dev/sda4 and It'll be different.

Apart from re-assigning new UUIDs to partitions, if you decided to use this as primary disk in the system, then OS may not boot due to /etc/fstab entries identifying partitions by UUIDs. You have to change the UUIDs of listed partitions in the "fstab" file.

Block-level disk cloning
protocolten-admin

Author: Harpreet Singh

Created: Wed 30 Aug 2023

Updated: 1 year, 3 months ago

POST CATEGORY
  1. Linux
  2. System Admin
  3. Cloud
Suggested Posts:
CLOUD post image
Create IAM user policy for single S3 bucket access

Are you looking to grant specific access to an AWS S3 bucket for an IAM …

LINUX post image
Sync Cloud Storage Drives using Rclone

Rclone is an Open-source, multi-threaded, command line tool. It makes data synchronization much more …

LINUX post image
Create bootable USB drive from ISO image on Linux & Windows

This article is about creating a bootable USB drive from an ISO image. Below …

LINUX post image
Configure Django with Apache, MySQL and WSGi on Ubuntu

In this article, I'll be demonstrating how we can deploy Django website on an …

KNOWLEDGE post image
Read Binary Bits with these effective methods

You are most likely familiar with the concept of Binary Language. Yes, you guessed …

Sign up or Login to post comment.

Comments (0)