Copying partition table from one disk to another with older sfdisk under CentOS 7

Author:

Older version of sfdisk may still be used for msdos partition tables.
To copy the partition table from one disk to another using sfdisk a temporary file should be used to store the data for the partition table.

Here is an example of how to copy the msdos partition table from disk sda to disk sdb! Two simple commands

  1. Dump the source (sda) partition table to a temporary file.
  2. Redirect the standard input of the sfdisk utility with the above temporary file.

A copying partition table is really useful when recovering from a drive failure in a Linux software raid. Sometimes it is difficult or just easier to create the exact layout as the source mirror disk!

mdadm --add /dev/md1 /dev/sdb2
mdadm: /dev/sdb2 not large enough to join array

Errors such as the above are easily resolved with just two commands. The new versions of disk programs align the partitions, which may be a problem for a software RAID to join in a partition.

STEP 1) Dump the source partition table.

The source partition table is from /dev/sda:

[root@srv ~]# sfdisk -d /dev/sda > part_table_sda
sfdisk: Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.

There is a warning about not aligned partition, which may cause problems when creating from scratch, so copying the partition table is the best option in such cases.

Here is what the temporary file part_table_sda with the partition table information contains:

[root@srv ~]# cat part_table_sda 
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=     2048, size= 67045376, Id=fd, bootable
/dev/sda2 : start= 67110912, size=  1048576, Id=fd
/dev/sda3 : start= 68159488, size= 62883840, Id=fd
/dev/sda4 : start=131043328, size=845729792, Id= f
/dev/sda5 : start=131076096, size=845434880, Id=fd


This output is from an older version of sfdisk (sfdisk from util-linux 2.23.2). So no information for the partition table type, which means an additional step is required – the creation of a partition table. This version of sfdisk does not support GPT partition table!
Just create a partition, which will create a msdos partition table, too:

echo ',30,L' | sfdisk /dev/sdb
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 62260 cylinders, 255 heads, 63 sectors/track
Old situation:
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

Old situation:
sfdisk: No partitions found

New situation:
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1          0+     29      30-    240974+  83  Linux
/dev/sdb2          0       -       0          0    0  Empty
/dev/sdb3          0       -       0          0    0  Empty
/dev/sdb4          0       -       0          0    0  Empty
Warning: no primary partition is marked bootable (active)
This does not matter for LILO, but the DOS MBR will not boot this disk.
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)

Or the better way if parted is available:

[root@srv ~]# parted /dev/sdb --script 'mklabel msdos'
[root@srv ~]# parted /dev/sdb --script print
Model: ATA Micron_1100_MTFD (scsi)
Disk /dev/sdb: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

STEP 2) Import the temporary file with the partition table information.

There is an additional recommendation step to change the label and UUIDs of the disk and partitions in the temporary file if they are included (which is true only in the newer versions of sfdisk):

[root@srv47 ~]# sfdisk /dev/sdb < ./part_table_sda 
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 62260 cylinders, 255 heads, 63 sectors/track
Old situation:
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1          0+     29      30-    240974+  83  Linux
/dev/sdb2          0       -       0          0    0  Empty
/dev/sdb3          0       -       0          0    0  Empty
/dev/sdb4          0       -       0          0    0  Empty
New situation:
Units: sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *      2048  67047423   67045376  fd  Linux raid autodetect
/dev/sdb2      67110912  68159487    1048576  fd  Linux raid autodetect
/dev/sdb3      68159488 131043327   62883840  fd  Linux raid autodetect
/dev/sdb4     131043328 976773119  845729792   f  W95 Ext'd (LBA)
/dev/sdb5     131076096 976510975  845434880  fd  Linux raid autodetect
Warning: partition 1 does not end at a cylinder boundary
Warning: partition 2 does not start at a cylinder boundary
Warning: partition 2 does not end at a cylinder boundary
Warning: partition 3 does not start at a cylinder boundary
Warning: partition 3 does not end at a cylinder boundary
Warning: partition 4 does not start at a cylinder boundary
Warning: partition 4 does not end at a cylinder boundary
Warning: partition 5 does not start at a cylinder boundary
Warning: partition 5 does not end at a cylinder boundary
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)

None of the partitions are aligned and by copying the source partition table the layout is exactly replicated. It is important if the destination disk should be joined in an array group with the source disk!

Bonus – newer version of sfdisk with GTP support.

Just for comparison, here is newer sfdisk output (sfdisk from util-linux 2.32.1 of CentOS 8), which supports GPT partition table:

[root@srv ~]# sfdisk -d /dev/sda
label: gpt
label-id: 7D2CEBF0-CCA6-48B5-8164-AF3C77E4B40A
device: /dev/sda
unit: sectors
first-lba: 34
last-lba: 11721045134

/dev/sda1 : start=          34, size=     1046494, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5F8CFAB7-695E-4ECF-80D6-26AD0FC294D0, name="primary"
/dev/sda2 : start=     1046528, size=     1048576, type=A19D880F-05FC-4D3B-A006-743F0F84911E, uuid=7EDB84C2-F2A2-4284-8C99-6374945EF751, name="primary"
/dev/sda3 : start=     2095104, size=   102400000, type=A19D880F-05FC-4D3B-A006-743F0F84911E, uuid=7D91D6A0-253C-41C8-AA87-2BA34C78EA6B, name="primary"
/dev/sda4 : start=   104495104, size=    16777216, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=F2F99270-BB38-4EA6-81C6-ECF451CB5CE1, name="primary"
/dev/sda5 : start=   121272320, size= 11599771648, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=AE08636A-3BBE-4346-8EBE-ED2A177D7991, name="primary"

Leave a Reply

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