In my last article I gave you an overview on Public key Infrastructure (PKI) and shared the steps to generate a self signed certificate using openssl in Linux. Next let me show you the steps to clone partition table from one disk to another to repair or backup and restore partition table in Linux. The steps from this article to backup and restore partition table are performed on centOS 7 and can also be executed on RHEL 7 Linux node.
Before going for any sort of backup or restore procedure for partition table, you must be aware of different types of partition scheme i.e. GPT vs MBR.
Clone and Restore partition table using 'sfdisk' command
Backup Partition Table
Run the command below to backup the partition table on device /dev/sda
to /root/partition-sda.img
:
[root@node2 ~]# sfdisk -d /dev/sda > /root/partition-sda.img
[root@node2 ~]# ls -l /root/partition-sda.img -rw-r--r-- 1 root root 259 Apr 18 14:52 /root/partition-sda.img
As you can see the output is a ASCII type text file.
[root@node2 ~]# file /root/partition-sda.img /root/partition-sda.img: ASCII text
Content of this file
[root@node2 ~]# cat /root/partition-sda.img # partition table of /dev/sda unit: sectors /dev/sda1 : start= 2048, size= 1048576, Id=83, bootable /dev/sda2 : start= 1050624, size= 57688064, Id=8e /dev/sda3 : start= 0, size= 0, Id= 0 /dev/sda4 : start= 0, size= 0, Id= 0
Restore Partition Table
You should then copy /root/partition-sda.img
to some other storage, for example a portable USB disk.
If the partition is damaged and needs to be restored from backup, please connect your USB disk to the server and boot the server with installation media and enter rescue mode.
Do not mount the root partition at this time - select "Skip" when the system asks if you want to mount the root partition on /mnt/sysimage.
Create a temporary directory, for example /mnt/temp
and mount the filesystem of your USB device which contains your backup.
For example:
# mkdir /mnt/temp # mount /dev/sdb1 /mnt/temp/ # cd /mnt/temp/
where /dev/sdb1
is the file system of the USB disk. After that, run:
# sfdisk /dev/sda < /mnt/temp/partition-sda.img
To verify that the partition table has been restored, run:
# fdisk -l /dev/sda
Clone and Restore partition table using "dd" command
The MBR (Master Boot Record) occupies the first 446 bytes of the disk while the partition table occupies the next 64 bytes. We can use "dd
" to dump the range from 447 - 510 bytes in the first sector.
Backup Partition Table
For example, if the harddisk is /dev/sda
, then run the command below:
# dd if=/dev/sda of=/root/partition-sda.img bs=1 count=64 skip=446
Restore Partition Table
You should then copy /root/partition-sda.img
to some other storage, for example a portable USB disk.
If the partition is damaged and needs to be restored from backup, please connect your USB disk to the server and boot the server with installation media and enter rescue mode.
Do not mount the root partition at this time - select "Skip" when the system asks if you want to mount the root partition on /mnt/sysimage
.
Create a temporary directory, for example /mnt/temp
and mount the filesystem of your USB device which contains your backup.
For example:
# mkdir /mnt/temp # mount /dev/sdb1 /mnt/temp/ # cd /mnt/temp/
where /dev/sdb1
is the file system of the USB disk. After that, run:
# dd if=/mnt/temp/partition-sda.img of=/dev/sda bs=1 count=64 seek=446
To verify that the partition table has been restored, run:
# fdisk -l /dev/sda
Lastly I hope the steps from the article to backup and restore partition table using sfdisk
and dd
command on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
References:
Red Hat Knowledgebase
Related keywords: linux clone partition table, linux repair partition table, sfdisk
“On GPT partitions you cannot perform such backup and restore of partition table.”
My understanding is the gdisk does precisely this. And that sfdisk, the current version, also supports this. So why use fdisk?
Thank you for highlighting this, let me verify and may be I can either update this article or add another one for GPT partitions as well