In this article, we’ll show you how to clone (copy/migrate) an installed Linux operating system to another (new) drive. The article shows the process of cloning Debian or Ubuntu, but it also applies to Red Hat/CentOS/Fedora Linux distros.
Install the parted tool using your package manager:
$ sudo apt install parted -y
Check the current partition table on the disk (MBR or GPT) with fdisk:
$ sudo fdisk /dev/sda -l
Disk label type: gpt
Shut down your computer (virtual machine) and connect a new disk (/dev/sdb).
Create a partition table on the new drive:
$ sudo parted /dev/sdb
If the source disk had a GPT (EFI) partition table, run the command:
> mklabel gpt
For MBR partition table (DOS BIOS):
> mklabel msdos
Exit parted:
> quit
Now clone the dev/sda disk to /dev/sdb using the dd command:
$ sudo dd if=/dev/sda of=/dev/sdb bs=1M conv=noerror,sync
Shut down the computer, unplug the old drive, and boot from the new one.
If the size of the new disk is larger than the size of the old one, you need to expand the root partition.
Install the growpart utility:
$ sudo apt install -y cloud-guest-utils
The growpart syntax:
growpart <device> <partition>
Extend partition 3 on /dev/sda:
$ sudo growpart /dev/sda 3
Now you need to expand the physical volume:
$ sudo pvresize /dev/sda3
Check the root path and file system type:
$ df -hT | grep mapper
/dev/mapper/debian-root xfs 10G 2.5G 7.5G 25% /
Extend the root logical volume:
$ sudo lvextend -r -l +100%FREE /dev/mapper/debian-root
Now you need to expand the file system (this is XFS in our example):
$ sudo xfs_growfs /
If you have EXT4, then expand it with:
$ sudo resize2fs /dev/mapper/debian-root