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 copy partition table from one disk to another or backup and restore partition table in Linux. The steps from this article to backup and restore partition table are performed on cent)S 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.

NOTE:
On GPT partitions you cannot perform such backup and restore of partition table.

	2 ways to backup and restore partition table in Linux (CentOS / RHEL 7)

 

Restore partition table using ‘sfdisk’ command

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
  • 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

 

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.

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
  • 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
NOTE:
The methods above assume that the hard drive has no bad blocks or any type of physical problems. The two methods only support MS-DOS partition tables, not GPT partition tables.

 

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

Leave a Reply

Your email address will not be published. Required fields are marked *