Replacing Hard Disk on Linux Server and Expanding Partitions

Not frequently but once in a while you would need to replace your “troubling” hard disk with a new shiny one. In order to keep all your mapping intact, you would like to use the same UUIDs and even the partition map. But as the new hard disk is generally a larger size, it makes sense to expand the main partition to the end of the drive. So here are some commands to do this job.

First make a copy of the existing partition table:

sfdisk -d /dev/sda > sda.dump

Now shutdown the computer, remove the old disk, and install the new disk. After booting, copy the partition table to the new disk.

sfdisk /dev/sda < sda.dump

Now open parted and expand the last partition through the end of the available space

parted /dev/sda
..print (to see the table)
..resizepart (use the end figure from the table to expand to the end)

Format the newly created partitions.

mkfs.ext4 /dev/sda1
mkfs.ext4 /dev/sda2

Everything should be alright now but if needed, you can also manually apply the UUIDs.

tune2fs /dev/sda1 -U xxxxe50b-89d4-xxxx-8d8e-e6c4d547xxxx
tune2fs /dev/sda2 -U xxxxae63-xxxx-4054-b55d-b52f751xxxxx